Fix OS X build.
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)));
36 type->contained_ = NULL;
41 Type::NewUnchecked(Zone *zone)
43 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
47 type->kind_ = UNCHECKED;
48 type->contained_ = NULL;
53 Type::NewPrimitive(Zone *zone, PrimitiveType prim)
55 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
59 type->kind_ = PRIMITIVE;
61 type->contained_ = NULL;
62 type->primitive_ = prim;
67 Type::NewArray(Zone *zone, Handle<Type> contained, int elements, bool isConst)
69 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
75 type->contained_ = contained;
76 type->elements_ = elements;
77 if (!contained->isArray())
80 type->levels_ = contained->levels() + 1;
81 type->const_ = isConst;
86 Type::NewExternalArray(Zone *zone, Handle<Type> contained)
88 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
92 assert(contained->isPrimitive());
94 type->kind_ = EXTERNAL_ARRAY;
96 type->contained_ = contained;
97 type->elements_ = DYNAMIC_ARRAY_SIZE;
104 Type::NewReference(Zone *zone, Handle<Type> contained, bool isConst)
106 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
110 type->kind_ = REFERENCE;
112 type->contained_ = contained;
113 type->const_ = isConst;
118 Type::NewEnum(Zone *zone, Handle<String> name)
120 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
126 type->contained_ = NULL;
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 if (left->levels() != right->levels())
169 if (left->elements_ != right->elements_)
171 return Compare(left->contained(), right->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(left->contained(), right->contained());
209 StructType::New(Zone *zone, Handle<String> name, Handle<FixedArray> fields)
211 Local<StructType> type(zone, StructType::cast(zone->allocate(MapKind_StructType, sizeof(StructType), Heap::Tenure_Old)));
215 type->kind_ = STRUCT;
217 type->contained_ = NULL;
218 type->newMap_ = NULL;
219 type->fields_ = fields;
221 // Fill in field offsets.
222 StructureBufferBuilder builder;
223 Local<Type> fieldType(zone);
224 for (size_t i = 0; i < fields->length(); i++) {
225 Descriptor *desc = Descriptor::cast(fields->at(i));
226 fieldType = desc->type();
229 if (!builder.add(fieldType, &offset))
231 desc->setOffset(offset);
234 Local<StructMap> map(zone, StructMap::New(zone, builder, type));
244 StructType::lookupField(Handle<String> name, unsigned *index)
246 for (unsigned i = 0; i < fields_->length(); i++) {
247 Descriptor *desc = Descriptor::cast(fields_->at(i));
248 if (desc->name() == name) {
259 FunctionType::New(Zone *zone, TokenKind token)
261 Local<FunctionType> type(zone, FunctionType::cast(zone->allocate(MapKind_FunctionType, sizeof(FunctionType), Heap::Tenure_Old)));
265 type->kind_ = FUNCTION;
267 type->contained_ = NULL;
268 type->parameters_ = NULL;
269 type->defaults_ = NULL;
270 type->returnType_ = NULL;
271 type->token_ = token;
275 // :TODO: this leaks an interior pointer.
277 ke::GetTypeName(Type *type)
281 if (type->isFunction())
285 if (type->isEnum() || type->isStruct())
286 return type->name()->chars();
287 if (type->isReference())
288 type = type->contained();
289 switch (type->primitive()) {
290 case PrimitiveType_Bool:
292 case PrimitiveType_Char:
294 case PrimitiveType_Int32:
296 case PrimitiveType_Float: