Move PrimitiveType_String to a dedicated string type.
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::NewString(Zone *zone)
42 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
51 Type::NewUnchecked(Zone *zone)
53 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
57 type->kind_ = UNCHECKED;
62 Type::NewPrimitive(Zone *zone, PrimitiveType prim)
64 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
68 type->kind_ = PRIMITIVE;
69 type->primitive_ = prim;
74 ArrayType::New(Zone *zone, Handle<Type> contained, int elements)
76 if (contained->isArray() &&
77 ArrayType::cast(contained)->levels() == MAX_ARRAY_DEPTH)
79 zone->reportError(Message_MaximumArrayDepth);
83 Local<ArrayType> type(zone, ArrayType::cast(zone->allocate(MapKind_ArrayType, sizeof(ArrayType), Heap::Tenure_Old)));
88 type->contained_ = contained;
89 type->elements_ = elements;
90 if (!contained->isArray())
93 type->levels_ = ArrayType::cast(contained)->levels() + 1;
98 ArrayType::NewExternal(Zone *zone, Handle<Type> contained)
100 Local<ArrayType> type(zone, ArrayType::cast(zone->allocate(MapKind_ArrayType, sizeof(ArrayType), Heap::Tenure_Old)));
104 assert(contained->isPrimitive());
106 type->kind_ = EXTERNAL_ARRAY;
107 type->contained_ = contained;
108 type->elements_ = DYNAMIC_ARRAY_SIZE;
114 ReferenceType::New(Zone *zone, Handle<Type> contained)
116 Local<ReferenceType> type(zone, ReferenceType::cast(zone->allocate(MapKind_ReferenceType, sizeof(ReferenceType), Heap::Tenure_Old)));
120 type->kind_ = REFERENCE;
121 type->const_ = false;
122 type->contained_ = contained;
127 EnumType::New(Zone *zone, Handle<String> name)
129 Local<EnumType> type(zone, EnumType::cast(zone->allocate(MapKind_EnumType, sizeof(EnumType), Heap::Tenure_Old)));
140 Type::NewNative(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters,
141 Handle<FixedArray> defaults, bool variadic)
143 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
147 type->kind_ = NATIVE;
148 type->contained_ = NULL;
149 type->fields_ = NULL;
150 type->newMap_ = NULL;
151 type->returnType_ = returnType;
152 type->parameters_ = parameters;
153 type->defaults_ = defaults;
154 type->variadicNative_ = variadic;
160 Type::Compare(Type *left, Type *right)
165 if (left->kind() != right->kind())
168 switch (left->kind()) {
169 case Type::PRIMITIVE:
170 return left->primitive() == right->primitive();
174 ArrayType *aleft = ArrayType::cast(left);
175 ArrayType *aright = ArrayType::cast(right);
176 if (aleft->levels() != aright->levels())
178 if (aleft->isFixedLength() != aright->isFixedLength())
180 if (aleft->isFixedLength() && aleft->fixedLength() != aright->fixedLength())
182 return Compare(aleft->contained(), aright->contained());
187 FunctionType *fleft = FunctionType::cast(left);
188 FunctionType *fright = FunctionType::cast(right);
189 if (!Compare(fleft->returnType(), fright->returnType()))
191 if (fleft->parameters()->length() != fright->parameters()->length())
193 if (fleft->isNative() != fright->isNative())
195 if (fleft->isNative() && (fleft->isNativeVariadic() != fright->isNativeVariadic()))
197 for (unsigned i = 0; i < fleft->parameters()->length(); i++) {
198 Type *leftparam = fleft->parameterAt(i);
199 Type *rightparam = fright->parameterAt(i);
200 if (!Compare(leftparam, rightparam))
213 assert(left->kind() == Type::REFERENCE);
214 return Compare(ReferenceType::cast(left)->contained(), ReferenceType::cast(right)->contained());
219 StructType::New(Zone *zone, Handle<String> name, Handle<FixedArray> fields)
221 Local<StructType> type(zone, StructType::cast(zone->allocate(MapKind_StructType, sizeof(StructType), Heap::Tenure_Old)));
225 type->kind_ = STRUCT;
226 type->newMap_ = NULL;
227 type->fields_ = fields;
230 // Fill in field offsets.
231 StructureBufferBuilder builder;
232 Local<Type> fieldType(zone);
233 for (size_t i = 0; i < fields->length(); i++) {
234 Descriptor *desc = Descriptor::cast(fields->at(i));
235 fieldType = desc->type();
238 if (!builder.add(fieldType, &offset))
240 desc->setOffset(offset);
243 Local<StructMap> map(zone, StructMap::New(zone, builder, type));
253 StructType::lookupField(Handle<String> name, unsigned *index)
255 for (unsigned i = 0; i < fields_->length(); i++) {
256 Descriptor *desc = Descriptor::cast(fields_->at(i));
257 if (desc->name() == name) {
268 FunctionType::New(Zone *zone, Handle<String> name, TokenKind token)
270 Local<FunctionType> type(zone, FunctionType::cast(zone->allocate(MapKind_FunctionType, sizeof(FunctionType), Heap::Tenure_Old)));
274 type->kind_ = FUNCTION;
275 type->parameters_ = NULL;
276 type->defaults_ = NULL;
277 type->returnType_ = NULL;
279 type->token_ = token;
283 // :TODO: this leaks an interior pointer.
285 ke::GetTypeName(Type *type)
289 if (type->isFunction())
294 return EnumType::cast(type)->name()->chars();
295 if (type->isStruct())
296 return StructType::cast(type)->name()->chars();
297 if (type->isReference())
298 type = ReferenceType::cast(type)->contained();
299 switch (type->primitive()) {
300 case PrimitiveType_Bool:
302 case PrimitiveType_Char:
304 case PrimitiveType_Int32:
306 case PrimitiveType_Float: