Added array support back.
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 if (contained->isArray() &&
67 ArrayType::cast(contained)->levels() == MAX_ARRAY_DEPTH)
69 zone->reportError(Message_MaximumArrayDepth);
73 Local<ArrayType> type(zone, ArrayType::cast(zone->allocate(MapKind_ArrayType, sizeof(ArrayType), Heap::Tenure_Old)));
79 type->contained_ = contained;
80 type->elements_ = elements;
81 if (!contained->isArray())
84 type->levels_ = ArrayType::cast(contained)->levels() + 1;
89 ArrayType::NewExternal(Zone *zone, Handle<Type> contained)
91 Local<ArrayType> type(zone, ArrayType::cast(zone->allocate(MapKind_ArrayType, sizeof(ArrayType), Heap::Tenure_Old)));
95 assert(contained->isPrimitive());
97 type->kind_ = EXTERNAL_ARRAY;
99 type->contained_ = contained;
100 type->elements_ = DYNAMIC_ARRAY_SIZE;
106 Type::NewReference(Zone *zone, Handle<Type> contained, bool isConst)
108 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
112 type->kind_ = REFERENCE;
114 type->const_ = isConst;
119 Type::NewEnum(Zone *zone, Handle<String> name)
121 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
132 Type::NewNative(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters,
133 Handle<FixedArray> defaults, bool variadic)
135 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
139 type->kind_ = NATIVE;
141 type->contained_ = NULL;
142 type->fields_ = NULL;
143 type->newMap_ = NULL;
144 type->returnType_ = returnType;
145 type->parameters_ = parameters;
146 type->defaults_ = defaults;
147 type->variadicNative_ = variadic;
153 Type::Compare(Type *left, Type *right)
158 if (left->kind() != right->kind())
161 switch (left->kind()) {
162 case Type::PRIMITIVE:
163 return left->primitive() == right->primitive();
167 ArrayType *aleft = ArrayType::cast(left);
168 ArrayType *aright = ArrayType::cast(right);
169 if (aleft->levels() != aright->levels())
171 if (aleft->isFixedLength() != aright->isFixedLength())
173 if (aleft->isFixedLength() && aleft->fixedLength() != aright->fixedLength())
175 return Compare(aleft->contained(), aright->contained());
180 FunctionType *fleft = FunctionType::cast(left);
181 FunctionType *fright = FunctionType::cast(right);
182 if (!Compare(fleft->returnType(), fright->returnType()))
184 if (fleft->parameters()->length() != fright->parameters()->length())
186 if (fleft->isNative() != fright->isNative())
188 if (fleft->isNative() && (fleft->isNativeVariadic() != fright->isNativeVariadic()))
190 for (unsigned i = 0; i < fleft->parameters()->length(); i++) {
191 Type *leftparam = fleft->parameterAt(i);
192 Type *rightparam = fright->parameterAt(i);
193 if (!Compare(leftparam, rightparam))
206 assert(left->kind() == Type::REFERENCE);
207 return Compare(left->contained_ref(), right->contained_ref());
212 StructType::New(Zone *zone, Handle<String> name, Handle<FixedArray> fields)
214 Local<StructType> type(zone, StructType::cast(zone->allocate(MapKind_StructType, sizeof(StructType), Heap::Tenure_Old)));
218 type->kind_ = STRUCT;
220 type->newMap_ = NULL;
221 type->fields_ = fields;
223 // Fill in field offsets.
224 StructureBufferBuilder builder;
225 Local<Type> fieldType(zone);
226 for (size_t i = 0; i < fields->length(); i++) {
227 Descriptor *desc = Descriptor::cast(fields->at(i));
228 fieldType = desc->type();
231 if (!builder.add(fieldType, &offset))
233 desc->setOffset(offset);
236 Local<StructMap> map(zone, StructMap::New(zone, builder, type));
246 StructType::lookupField(Handle<String> name, unsigned *index)
248 for (unsigned i = 0; i < fields_->length(); i++) {
249 Descriptor *desc = Descriptor::cast(fields_->at(i));
250 if (desc->name() == name) {
261 FunctionType::New(Zone *zone, TokenKind token)
263 Local<FunctionType> type(zone, FunctionType::cast(zone->allocate(MapKind_FunctionType, sizeof(FunctionType), Heap::Tenure_Old)));
267 type->kind_ = FUNCTION;
269 type->parameters_ = NULL;
270 type->defaults_ = NULL;
271 type->returnType_ = NULL;
272 type->token_ = token;
276 // :TODO: this leaks an interior pointer.
278 ke::GetTypeName(Type *type)
282 if (type->isFunction())
286 if (type->isEnum() || type->isStruct())
287 return type->name()->chars();
288 if (type->isReference())
289 type = type->contained_ref();
290 switch (type->primitive()) {
291 case PrimitiveType_Bool:
293 case PrimitiveType_Char:
295 case PrimitiveType_Int32:
297 case PrimitiveType_Float: