Split ArrayType from 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::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;
59 type->primitive_ = prim;
64 ArrayType::New(Zone *zone, Handle<Type> contained, int elements)
66 Local<ArrayType> type(zone, ArrayType::cast(zone->allocate(MapKind_ArrayType, sizeof(ArrayType), Heap::Tenure_Old)));
72 type->contained_ = contained;
73 type->elements_ = elements;
74 if (!contained->isArray())
77 type->levels_ = ArrayType::cast(contained)->levels() + 1;
82 ArrayType::NewExternal(Zone *zone, Handle<Type> contained)
84 Local<ArrayType> type(zone, ArrayType::cast(zone->allocate(MapKind_ArrayType, sizeof(ArrayType), Heap::Tenure_Old)));
88 assert(contained->isPrimitive());
90 type->kind_ = EXTERNAL_ARRAY;
92 type->contained_ = contained;
93 type->elements_ = DYNAMIC_ARRAY_SIZE;
99 Type::NewReference(Zone *zone, Handle<Type> contained, bool isConst)
101 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
105 type->kind_ = REFERENCE;
107 type->const_ = isConst;
112 Type::NewEnum(Zone *zone, Handle<String> name)
114 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
125 Type::NewNative(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters,
126 Handle<FixedArray> defaults, bool variadic)
128 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
132 type->kind_ = NATIVE;
134 type->contained_ = NULL;
135 type->fields_ = NULL;
136 type->newMap_ = NULL;
137 type->returnType_ = returnType;
138 type->parameters_ = parameters;
139 type->defaults_ = defaults;
140 type->variadicNative_ = variadic;
146 Type::Compare(Type *left, Type *right)
151 if (left->kind() != right->kind())
154 switch (left->kind()) {
155 case Type::PRIMITIVE:
156 return left->primitive() == right->primitive();
160 ArrayType *aleft = ArrayType::cast(left);
161 ArrayType *aright = ArrayType::cast(right);
162 if (aleft->levels() != aright->levels())
164 if (aleft->isFixedLength() != aright->isFixedLength())
166 if (aleft->isFixedLength() && aleft->fixedLength() != aright->fixedLength())
168 return Compare(aleft->contained(), aright->contained());
173 FunctionType *fleft = FunctionType::cast(left);
174 FunctionType *fright = FunctionType::cast(right);
175 if (!Compare(fleft->returnType(), fright->returnType()))
177 if (fleft->parameters()->length() != fright->parameters()->length())
179 if (fleft->isNative() != fright->isNative())
181 if (fleft->isNative() && (fleft->isNativeVariadic() != fright->isNativeVariadic()))
183 for (unsigned i = 0; i < fleft->parameters()->length(); i++) {
184 Type *leftparam = fleft->parameterAt(i);
185 Type *rightparam = fright->parameterAt(i);
186 if (!Compare(leftparam, rightparam))
199 assert(left->kind() == Type::REFERENCE);
200 return Compare(left->contained_ref(), right->contained_ref());
206 StructType::New(Zone *zone, Handle<String> name, Handle<FixedArray> fields)
208 Local<StructType> type(zone, StructType::cast(zone->allocate(MapKind_StructType, sizeof(StructType), Heap::Tenure_Old)));
212 type->kind_ = STRUCT;
214 type->newMap_ = NULL;
215 type->fields_ = fields;
217 // Fill in field offsets.
218 StructureBufferBuilder builder;
219 Local<Type> fieldType(zone);
220 for (size_t i = 0; i < fields->length(); i++) {
221 Descriptor *desc = Descriptor::cast(fields->at(i));
222 fieldType = desc->type();
225 if (!builder.add(fieldType, &offset))
227 desc->setOffset(offset);
230 Local<StructMap> map(zone, StructMap::New(zone, builder, type));
240 StructType::lookupField(Handle<String> name, unsigned *index)
242 for (unsigned i = 0; i < fields_->length(); i++) {
243 Descriptor *desc = Descriptor::cast(fields_->at(i));
244 if (desc->name() == name) {
255 FunctionType::New(Zone *zone, TokenKind token)
257 Local<FunctionType> type(zone, FunctionType::cast(zone->allocate(MapKind_FunctionType, sizeof(FunctionType), Heap::Tenure_Old)));
261 type->kind_ = FUNCTION;
263 type->parameters_ = NULL;
264 type->defaults_ = NULL;
265 type->returnType_ = NULL;
266 type->token_ = token;
270 // :TODO: this leaks an interior pointer.
272 ke::GetTypeName(Type *type)
276 if (type->isFunction())
280 if (type->isEnum() || type->isStruct())
281 return type->name()->chars();
282 if (type->isReference())
283 type = type->contained_ref();
284 switch (type->primitive()) {
285 case PrimitiveType_Bool:
287 case PrimitiveType_Char:
289 case PrimitiveType_Int32:
291 case PrimitiveType_Float: