Added definite return analysis and void-return deduction.
2 * Copyright (C) 2012 David Anderson
4 * This file is part of JITCraft.
6 * JITCraft is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
11 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along with
16 * JITCraft. If not, see http://www.gnu.org/licenses/.
26 Type::NewVoid(Zone *zone)
28 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
34 type->contained_ = NULL;
35 type->returnType_ = NULL;
36 type->parameters_ = NULL;
41 Type::NewPrimitive(Zone *zone, Handle<String> tag)
43 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
47 type->kind_ = PRIMITIVE;
49 type->contained_ = NULL;
50 type->returnType_ = NULL;
51 type->parameters_ = NULL;
53 if (tag && strcmp(tag->chars(), "Float") == 0)
54 type->primitive_ = PrimitiveType_Float;
55 else if (tag && strcmp(tag->chars(), "String") == 0)
56 type->primitive_ = PrimitiveType_Char;
58 type->primitive_ = PrimitiveType_Int32;
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->primitive_ = prim;
79 Type::NewArray(Zone *zone, Handle<Type> contained, int elements, bool isConst)
81 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
87 type->contained_ = contained;
88 type->returnType_ = NULL;
89 type->parameters_ = NULL;
90 type->elements_ = elements;
91 if (contained->isPrimitive())
94 type->levels_ = contained->levels() + 1;
95 type->const_ = isConst;
100 Type::NewReference(Zone *zone, Handle<Type> contained, bool isConst)
102 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
106 type->kind_ = REFERENCE;
108 type->contained_ = contained;
109 type->returnType_ = NULL;
110 type->parameters_ = NULL;
111 type->const_ = isConst;
116 Type::NewFunction(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters)
118 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
122 type->kind_ = FUNCTION;
123 type->contained_ = NULL;
124 type->returnType_ = returnType;
125 type->parameters_ = parameters;
130 Type::NewNative(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters, bool variadic)
132 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
136 type->kind_ = NATIVE;
137 type->contained_ = NULL;
138 type->returnType_ = returnType;
139 type->parameters_ = parameters;
140 type->variadicNative_ = variadic;
145 Type::Compare(Type *left, Type *right)
147 if (left->kind() != right->kind())
150 switch (left->kind()) {
151 case Type::PRIMITIVE:
152 return (left->primitive() == right->primitive() &&
153 left->tag() == right->tag());
157 if (left->levels() != right->levels())
159 if (left->elements() != right->elements())
161 return Compare(left->contained(), right->contained());
166 if (!Compare(left->returnType(), right->returnType()))
168 if (left->parameters()->length() != right->parameters()->length())
170 if (left->isNative() != right->isNative())
172 if (left->isNative() && (left->isNativeVariadic() != right->isNativeVariadic()))
174 for (unsigned i = 0; i < left->parameters()->length(); i++) {
175 Type *leftparam = left->parameterAt(i);
176 Type *rightparam = right->parameterAt(i);
177 if (!Compare(leftparam, rightparam))
184 assert(left->kind() == Type::REFERENCE);
185 return Compare(left->contained(), right->contained());