Allow structs to contain arrays.
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;
37 type->returnType_ = NULL;
38 type->parameters_ = NULL;
39 type->defaults_ = NULL;
46 Type::NewUnchecked(Zone *zone)
48 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
52 type->kind_ = UNCHECKED;
53 type->contained_ = NULL;
54 type->returnType_ = NULL;
55 type->parameters_ = NULL;
56 type->defaults_ = NULL;
63 Type::NewPrimitive(Zone *zone, PrimitiveType prim)
65 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
69 type->kind_ = PRIMITIVE;
71 type->contained_ = NULL;
72 type->returnType_ = NULL;
73 type->parameters_ = NULL;
74 type->defaults_ = NULL;
78 type->primitive_ = prim;
83 Type::NewArray(Zone *zone, Handle<Type> contained, int elements, bool isConst)
85 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
91 type->contained_ = contained;
92 type->returnType_ = NULL;
93 type->parameters_ = NULL;
94 type->defaults_ = NULL;
97 type->elements_ = elements;
98 if (!contained->isArray())
101 type->levels_ = contained->levels() + 1;
102 type->const_ = isConst;
107 Type::NewExternalArray(Zone *zone, Handle<Type> contained)
109 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
113 assert(contained->isPrimitive());
115 type->kind_ = EXTERNAL_ARRAY;
117 type->contained_ = contained;
118 type->returnType_ = NULL;
119 type->parameters_ = NULL;
120 type->defaults_ = NULL;
121 type->fields_ = NULL;
122 type->newMap_ = NULL;
123 type->elements_ = DYNAMIC_ARRAY_SIZE;
125 type->const_ = false;
130 Type::NewReference(Zone *zone, Handle<Type> contained, bool isConst)
132 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
136 type->kind_ = REFERENCE;
138 type->contained_ = contained;
139 type->returnType_ = NULL;
140 type->parameters_ = NULL;
141 type->defaults_ = NULL;
142 type->fields_ = NULL;
143 type->newMap_ = NULL;
144 type->const_ = isConst;
149 Type::NewFunction(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters,
150 Handle<FixedArray> defaults)
152 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
156 type->kind_ = FUNCTION;
158 type->contained_ = NULL;
159 type->fields_ = NULL;
160 type->newMap_ = NULL;
161 type->returnType_ = returnType;
162 type->parameters_ = parameters;
163 type->defaults_ = defaults;
168 Type::NewEnum(Zone *zone, Handle<String> name)
170 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
176 type->contained_ = NULL;
177 type->returnType_ = NULL;
178 type->parameters_ = NULL;
179 type->defaults_ = NULL;
180 type->fields_ = NULL;
181 type->newMap_ = NULL;
186 Type::NewStruct(Zone *zone, Handle<String> name, Handle<FixedArray> fields, Handle<Map> newMap)
188 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
192 type->kind_ = STRUCT;
194 type->contained_ = NULL;
195 type->returnType_ = NULL;
196 type->parameters_ = NULL;
197 type->defaults_ = NULL;
198 type->newMap_ = newMap;
199 type->fields_ = fields;
204 Type::NewNative(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters,
205 Handle<FixedArray> defaults, bool variadic)
207 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
211 type->kind_ = NATIVE;
213 type->contained_ = NULL;
214 type->fields_ = NULL;
215 type->newMap_ = NULL;
216 type->returnType_ = returnType;
217 type->parameters_ = parameters;
218 type->defaults_ = defaults;
219 type->variadicNative_ = variadic;
224 Type::Compare(Type *left, Type *right)
229 if (left->kind() != right->kind())
232 switch (left->kind()) {
233 case Type::PRIMITIVE:
234 return left->primitive() == right->primitive();
238 if (left->levels() != right->levels())
240 if (left->elements_ != right->elements_)
242 return Compare(left->contained(), right->contained());
247 if (!Compare(left->returnType(), right->returnType()))
249 if (left->parameters()->length() != right->parameters()->length())
251 if (left->isNative() != right->isNative())
253 if (left->isNative() && (left->isNativeVariadic() != right->isNativeVariadic()))
255 for (unsigned i = 0; i < left->parameters()->length(); i++) {
256 Type *leftparam = left->parameterAt(i);
257 Type *rightparam = right->parameterAt(i);
258 if (!Compare(leftparam, rightparam))
271 assert(left->kind() == Type::REFERENCE);
272 return Compare(left->contained(), right->contained());
277 Type::lookupField(Handle<String> name, unsigned *index)
281 for (unsigned i = 0; i < fields_->length(); i++) {
282 Descriptor *desc = Descriptor::cast(fields_->at(i));
283 if (desc->name() == name) {