Parser support for public variables.
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::NewReference(Zone *zone, Handle<Type> contained, bool isConst)
109 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
113 type->kind_ = REFERENCE;
115 type->contained_ = contained;
116 type->returnType_ = NULL;
117 type->parameters_ = NULL;
118 type->defaults_ = NULL;
119 type->fields_ = NULL;
120 type->newMap_ = NULL;
121 type->const_ = isConst;
126 Type::NewFunction(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters,
127 Handle<FixedArray> defaults)
129 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
133 type->kind_ = FUNCTION;
135 type->contained_ = NULL;
136 type->fields_ = NULL;
137 type->newMap_ = NULL;
138 type->returnType_ = returnType;
139 type->parameters_ = parameters;
140 type->defaults_ = defaults;
145 Type::NewEnum(Zone *zone, Handle<String> name)
147 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
153 type->contained_ = NULL;
154 type->returnType_ = NULL;
155 type->parameters_ = NULL;
156 type->defaults_ = NULL;
157 type->fields_ = NULL;
158 type->newMap_ = NULL;
163 Type::NewStruct(Zone *zone, Handle<String> name, Handle<FixedArray> fields, Handle<Map> newMap)
165 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
169 type->kind_ = STRUCT;
171 type->contained_ = NULL;
172 type->returnType_ = NULL;
173 type->parameters_ = NULL;
174 type->defaults_ = NULL;
175 type->newMap_ = newMap;
176 type->fields_ = fields;
181 Type::NewNative(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters,
182 Handle<FixedArray> defaults, bool variadic)
184 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
188 type->kind_ = NATIVE;
190 type->contained_ = NULL;
191 type->fields_ = NULL;
192 type->newMap_ = NULL;
193 type->returnType_ = returnType;
194 type->parameters_ = parameters;
195 type->defaults_ = defaults;
196 type->variadicNative_ = variadic;
201 Type::Compare(Type *left, Type *right)
206 if (left->kind() != right->kind())
209 switch (left->kind()) {
210 case Type::PRIMITIVE:
211 return left->primitive() == right->primitive();
215 if (left->levels() != right->levels())
217 if (left->elements() != right->elements())
219 return Compare(left->contained(), right->contained());
224 if (!Compare(left->returnType(), right->returnType()))
226 if (left->parameters()->length() != right->parameters()->length())
228 if (left->isNative() != right->isNative())
230 if (left->isNative() && (left->isNativeVariadic() != right->isNativeVariadic()))
232 for (unsigned i = 0; i < left->parameters()->length(); i++) {
233 Type *leftparam = left->parameterAt(i);
234 Type *rightparam = right->parameterAt(i);
235 if (!Compare(leftparam, rightparam))
248 assert(left->kind() == Type::REFERENCE);
249 return Compare(left->contained(), right->contained());
254 Type::lookupField(Handle<String> name, unsigned *index)
258 for (unsigned i = 0; i < fields_->length(); i++) {
259 Descriptor *desc = Descriptor::cast(fields_->at(i));
260 if (desc->name() == name) {