Re-introduce definite return analysis.
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 ReferenceType::New(Zone *zone, Handle<Type> contained)
108 Local<ReferenceType> type(zone, ReferenceType::cast(zone->allocate(MapKind_ReferenceType, sizeof(ReferenceType), Heap::Tenure_Old)));
112 type->kind_ = REFERENCE;
114 type->const_ = false;
115 type->contained_ = contained;
120 Type::NewEnum(Zone *zone, Handle<String> name)
122 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
133 Type::NewNative(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters,
134 Handle<FixedArray> defaults, bool variadic)
136 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
140 type->kind_ = NATIVE;
142 type->contained_ = NULL;
143 type->fields_ = NULL;
144 type->newMap_ = NULL;
145 type->returnType_ = returnType;
146 type->parameters_ = parameters;
147 type->defaults_ = defaults;
148 type->variadicNative_ = variadic;
154 Type::Compare(Type *left, Type *right)
159 if (left->kind() != right->kind())
162 switch (left->kind()) {
163 case Type::PRIMITIVE:
164 return left->primitive() == right->primitive();
168 ArrayType *aleft = ArrayType::cast(left);
169 ArrayType *aright = ArrayType::cast(right);
170 if (aleft->levels() != aright->levels())
172 if (aleft->isFixedLength() != aright->isFixedLength())
174 if (aleft->isFixedLength() && aleft->fixedLength() != aright->fixedLength())
176 return Compare(aleft->contained(), aright->contained());
181 FunctionType *fleft = FunctionType::cast(left);
182 FunctionType *fright = FunctionType::cast(right);
183 if (!Compare(fleft->returnType(), fright->returnType()))
185 if (fleft->parameters()->length() != fright->parameters()->length())
187 if (fleft->isNative() != fright->isNative())
189 if (fleft->isNative() && (fleft->isNativeVariadic() != fright->isNativeVariadic()))
191 for (unsigned i = 0; i < fleft->parameters()->length(); i++) {
192 Type *leftparam = fleft->parameterAt(i);
193 Type *rightparam = fright->parameterAt(i);
194 if (!Compare(leftparam, rightparam))
207 assert(left->kind() == Type::REFERENCE);
208 return Compare(ReferenceType::cast(left)->contained(), ReferenceType::cast(right)->contained());
213 StructType::New(Zone *zone, Handle<String> name, Handle<FixedArray> fields)
215 Local<StructType> type(zone, StructType::cast(zone->allocate(MapKind_StructType, sizeof(StructType), Heap::Tenure_Old)));
219 type->kind_ = STRUCT;
221 type->newMap_ = NULL;
222 type->fields_ = fields;
224 // Fill in field offsets.
225 StructureBufferBuilder builder;
226 Local<Type> fieldType(zone);
227 for (size_t i = 0; i < fields->length(); i++) {
228 Descriptor *desc = Descriptor::cast(fields->at(i));
229 fieldType = desc->type();
232 if (!builder.add(fieldType, &offset))
234 desc->setOffset(offset);
237 Local<StructMap> map(zone, StructMap::New(zone, builder, type));
247 StructType::lookupField(Handle<String> name, unsigned *index)
249 for (unsigned i = 0; i < fields_->length(); i++) {
250 Descriptor *desc = Descriptor::cast(fields_->at(i));
251 if (desc->name() == name) {
262 FunctionType::New(Zone *zone, TokenKind token)
264 Local<FunctionType> type(zone, FunctionType::cast(zone->allocate(MapKind_FunctionType, sizeof(FunctionType), Heap::Tenure_Old)));
268 type->kind_ = FUNCTION;
270 type->parameters_ = NULL;
271 type->defaults_ = NULL;
272 type->returnType_ = NULL;
273 type->token_ = token;
277 // :TODO: this leaks an interior pointer.
279 ke::GetTypeName(Type *type)
283 if (type->isFunction())
287 if (type->isEnum() || type->isStruct())
288 return type->name()->chars();
289 if (type->isReference())
290 type = ReferenceType::cast(type)->contained();
291 switch (type->primitive()) {
292 case PrimitiveType_Bool:
294 case PrimitiveType_Char:
296 case PrimitiveType_Int32:
298 case PrimitiveType_Float: