Basic support for parsing and creating types from structure syntax.
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/.
28 Type::NewVoid(Zone *zone)
30 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
35 type->contained_ = NULL;
36 type->returnType_ = NULL;
37 type->parameters_ = NULL;
43 Type::NewPrimitive(Zone *zone, PrimitiveType prim)
45 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
49 type->kind_ = PRIMITIVE;
51 type->contained_ = NULL;
52 type->returnType_ = NULL;
53 type->parameters_ = NULL;
55 type->primitive_ = prim;
60 Type::NewArray(Zone *zone, Handle<Type> contained, int elements, bool isConst)
62 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
68 type->contained_ = contained;
69 type->returnType_ = NULL;
70 type->parameters_ = NULL;
72 type->elements_ = elements;
73 if (!contained->isArray())
76 type->levels_ = contained->levels() + 1;
77 type->const_ = isConst;
82 Type::NewReference(Zone *zone, Handle<Type> contained, bool isConst)
84 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
88 type->kind_ = REFERENCE;
90 type->contained_ = contained;
91 type->returnType_ = NULL;
92 type->parameters_ = NULL;
94 type->const_ = isConst;
99 Type::NewFunction(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters)
101 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
105 type->kind_ = FUNCTION;
107 type->contained_ = NULL;
108 type->fields_ = NULL;
109 type->returnType_ = returnType;
110 type->parameters_ = parameters;
115 Type::NewEnum(Zone *zone, Handle<String> name)
117 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
123 type->contained_ = NULL;
124 type->returnType_ = NULL;
125 type->parameters_ = NULL;
126 type->fields_ = NULL;
131 Type::NewStruct(Zone *zone, Handle<String> name, Handle<FixedArray> fields)
133 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
137 type->kind_ = STRUCT;
139 type->contained_ = NULL;
140 type->returnType_ = NULL;
141 type->parameters_ = NULL;
142 type->fields_ = fields;
147 Type::NewNative(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters, bool variadic)
149 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
153 type->kind_ = NATIVE;
155 type->contained_ = NULL;
156 type->fields_ = NULL;
157 type->returnType_ = returnType;
158 type->parameters_ = parameters;
159 type->variadicNative_ = variadic;
164 Type::Compare(Type *left, Type *right)
169 if (left->kind() != right->kind())
172 switch (left->kind()) {
173 case Type::PRIMITIVE:
174 return left->primitive() == right->primitive();
178 if (left->levels() != right->levels())
180 if (left->elements() != right->elements())
182 return Compare(left->contained(), right->contained());
187 if (!Compare(left->returnType(), right->returnType()))
189 if (left->parameters()->length() != right->parameters()->length())
191 if (left->isNative() != right->isNative())
193 if (left->isNative() && (left->isNativeVariadic() != right->isNativeVariadic()))
195 for (unsigned i = 0; i < left->parameters()->length(); i++) {
196 Type *leftparam = left->parameterAt(i);
197 Type *rightparam = right->parameterAt(i);
198 if (!Compare(leftparam, rightparam))
211 assert(left->kind() == Type::REFERENCE);
212 return Compare(left->contained(), right->contained());