Added clang support. A lot of the motion here is introducing a new offsetof because clang's seems broken?
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/.
27 Type::NewVoid(Zone *zone)
29 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;
42 Type::NewPrimitive(Zone *zone, Handle<String> tag)
44 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
48 type->kind_ = PRIMITIVE;
50 type->contained_ = NULL;
51 type->returnType_ = NULL;
52 type->parameters_ = NULL;
54 if (tag && strcmp(tag->chars(), "Float") == 0)
55 type->primitive_ = PrimitiveType_Float;
56 else if (tag && strcmp(tag->chars(), "String") == 0)
57 type->primitive_ = PrimitiveType_Char;
59 type->primitive_ = PrimitiveType_Int32;
64 Type::NewPrimitive(Zone *zone, PrimitiveType prim)
66 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
70 type->kind_ = PRIMITIVE;
72 type->contained_ = NULL;
73 type->returnType_ = NULL;
74 type->parameters_ = NULL;
75 type->primitive_ = prim;
80 Type::NewArray(Zone *zone, Handle<Type> contained, int elements, bool isConst)
82 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
88 type->contained_ = contained;
89 type->returnType_ = NULL;
90 type->parameters_ = NULL;
91 type->elements_ = elements;
92 if (contained->isPrimitive())
95 type->levels_ = contained->levels() + 1;
96 type->const_ = isConst;
101 Type::NewReference(Zone *zone, Handle<Type> contained, bool isConst)
103 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
107 type->kind_ = REFERENCE;
109 type->contained_ = contained;
110 type->returnType_ = NULL;
111 type->parameters_ = NULL;
112 type->const_ = isConst;
117 Type::NewFunction(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters)
119 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
123 type->kind_ = FUNCTION;
124 type->contained_ = NULL;
125 type->returnType_ = returnType;
126 type->parameters_ = parameters;
131 Type::NewNative(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters, bool variadic)
133 Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
137 type->kind_ = NATIVE;
138 type->contained_ = NULL;
139 type->returnType_ = returnType;
140 type->parameters_ = parameters;
141 type->variadicNative_ = variadic;
146 Type::Compare(Type *left, Type *right)
148 if (left->kind() != right->kind())
151 switch (left->kind()) {
152 case Type::PRIMITIVE:
153 return (left->primitive() == right->primitive() &&
154 left->tag() == right->tag());
158 if (left->levels() != right->levels())
160 if (left->elements() != right->elements())
162 return Compare(left->contained(), right->contained());
167 if (!Compare(left->returnType(), right->returnType()))
169 if (left->parameters()->length() != right->parameters()->length())
171 if (left->isNative() != right->isNative())
173 if (left->isNative() && (left->isNativeVariadic() != right->isNativeVariadic()))
175 for (unsigned i = 0; i < left->parameters()->length(); i++) {
176 Type *leftparam = left->parameterAt(i);
177 Type *rightparam = right->parameterAt(i);
178 if (!Compare(leftparam, rightparam))
185 assert(left->kind() == Type::REFERENCE);
186 return Compare(left->contained(), right->contained());