Get string literals working again.
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of SourcePawn.
7 * SourcePawn is free software: you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License as published by the Free
9 * Software Foundation, either version 3 of the License, or (at your option)
12 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
13 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along with
17 * SourcePawn. If not, see http://www.gnu.org/licenses/.
23 #include "Structures.h"
29 Type::NewVoid(Zone *zone)
31 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
40 Type::NewUnchecked(Zone *zone)
42 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
46 type->kind_ = UNCHECKED;
51 Type::NewPrimitive(Zone *zone, PrimitiveType prim)
53 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
57 type->kind_ = PRIMITIVE;
58 type->primitive_ = prim;
63 ArrayType::New(Zone *zone, Handle<Type> contained, int elements)
65 if (contained->isArray() &&
66 ArrayType::cast(contained)->levels() == MAX_ARRAY_DEPTH)
68 zone->reportError(Message_MaximumArrayDepth);
72 Local<ArrayType> type(zone, ArrayType::cast(zone->allocate(MapKind_ArrayType, sizeof(ArrayType), Heap::Tenure_Old)));
77 type->contained_ = contained;
78 type->elements_ = elements;
79 if (!contained->isArray())
82 type->levels_ = ArrayType::cast(contained)->levels() + 1;
87 ArrayType::NewExternal(Zone *zone, Handle<Type> contained)
89 Local<ArrayType> type(zone, ArrayType::cast(zone->allocate(MapKind_ArrayType, sizeof(ArrayType), Heap::Tenure_Old)));
93 assert(contained->isPrimitive());
95 type->kind_ = EXTERNAL_ARRAY;
96 type->contained_ = contained;
97 type->elements_ = DYNAMIC_ARRAY_SIZE;
103 ReferenceType::New(Zone *zone, Handle<Type> contained)
105 Local<ReferenceType> type(zone, ReferenceType::cast(zone->allocate(MapKind_ReferenceType, sizeof(ReferenceType), Heap::Tenure_Old)));
109 type->kind_ = REFERENCE;
110 type->const_ = false;
111 type->contained_ = contained;
116 EnumType::New(Zone *zone, Handle<String> name)
118 Local<EnumType> type(zone, EnumType::cast(zone->allocate(MapKind_EnumType, sizeof(EnumType), Heap::Tenure_Old)));
129 Type::NewNative(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters,
130 Handle<FixedArray> defaults, bool variadic)
132 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
136 type->kind_ = NATIVE;
137 type->contained_ = NULL;
138 type->fields_ = NULL;
139 type->newMap_ = NULL;
140 type->returnType_ = returnType;
141 type->parameters_ = parameters;
142 type->defaults_ = defaults;
143 type->variadicNative_ = variadic;
149 Type::Compare(Type *left, Type *right)
154 if (left->kind() != right->kind())
157 switch (left->kind()) {
158 case Type::PRIMITIVE:
159 return left->primitive() == right->primitive();
163 ArrayType *aleft = ArrayType::cast(left);
164 ArrayType *aright = ArrayType::cast(right);
165 if (aleft->levels() != aright->levels())
167 if (aleft->isFixedLength() != aright->isFixedLength())
169 if (aleft->isFixedLength() && aleft->fixedLength() != aright->fixedLength())
171 return Compare(aleft->contained(), aright->contained());
176 FunctionType *fleft = FunctionType::cast(left);
177 FunctionType *fright = FunctionType::cast(right);
178 if (!Compare(fleft->returnType(), fright->returnType()))
180 if (fleft->parameters()->length() != fright->parameters()->length())
182 if (fleft->isNative() != fright->isNative())
184 if (fleft->isNative() && (fleft->isNativeVariadic() != fright->isNativeVariadic()))
186 for (unsigned i = 0; i < fleft->parameters()->length(); i++) {
187 Type *leftparam = fleft->parameterAt(i);
188 Type *rightparam = fright->parameterAt(i);
189 if (!Compare(leftparam, rightparam))
202 assert(left->kind() == Type::REFERENCE);
203 return Compare(ReferenceType::cast(left)->contained(), ReferenceType::cast(right)->contained());
208 StructType::New(Zone *zone, Handle<String> name, Handle<FixedArray> fields)
210 Local<StructType> type(zone, StructType::cast(zone->allocate(MapKind_StructType, sizeof(StructType), Heap::Tenure_Old)));
214 type->kind_ = STRUCT;
215 type->newMap_ = NULL;
216 type->fields_ = fields;
219 // Fill in field offsets.
220 StructureBufferBuilder builder;
221 Local<Type> fieldType(zone);
222 for (size_t i = 0; i < fields->length(); i++) {
223 Descriptor *desc = Descriptor::cast(fields->at(i));
224 fieldType = desc->type();
227 if (!builder.add(fieldType, &offset))
229 desc->setOffset(offset);
232 Local<StructMap> map(zone, StructMap::New(zone, builder, type));
242 StructType::lookupField(Handle<String> name, unsigned *index)
244 for (unsigned i = 0; i < fields_->length(); i++) {
245 Descriptor *desc = Descriptor::cast(fields_->at(i));
246 if (desc->name() == name) {
257 FunctionType::New(Zone *zone, Handle<String> name, TokenKind token)
259 Local<FunctionType> type(zone, FunctionType::cast(zone->allocate(MapKind_FunctionType, sizeof(FunctionType), Heap::Tenure_Old)));
263 type->kind_ = FUNCTION;
264 type->parameters_ = NULL;
265 type->defaults_ = NULL;
266 type->returnType_ = NULL;
268 type->token_ = token;
272 // :TODO: this leaks an interior pointer.
274 ke::GetTypeName(Type *type)
278 if (type->isFunction())
283 return EnumType::cast(type)->name()->chars();
284 if (type->isStruct())
285 return StructType::cast(type)->name()->chars();
286 if (type->isReference())
287 type = ReferenceType::cast(type)->contained();
288 switch (type->primitive()) {
289 case PrimitiveType_Bool:
291 case PrimitiveType_Char:
293 case PrimitiveType_Int32:
295 case PrimitiveType_Float: