Added clang support. A lot of the motion here is introducing a new offsetof because clang's seems broken?
1 /* vim: set ts=4 sw=4 tw=99 et:
3 * Copyright (C) 2012 David Anderson
5 * This file is part of JITCraft.
7 * JITCraft 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 * Foobar 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 * JITCraft. If not, see http://www.gnu.org/licenses/.
19 #ifndef _include_jc_types_h_
20 #define _include_jc_types_h_
24 #include "FixedArray.h"
32 PrimitiveType_Char, // signed, 8-bit character
33 PrimitiveType_Int32, // signed, 32-bit integer
34 PrimitiveType_Float, // 32-bit IEEE-754 float
35 PrimitiveType_Native, // native-sized word
39 class Type : public Object
56 static const int UNKNOWN_SIZE = -1;
57 static const size_t MAX_UNSAFE_ARRAY_DEPTH = 5;
59 static Type *NewVoid(Zone *zone);
60 static Type *NewPrimitive(Zone *zone, Handle<String> tag);
61 static Type *NewPrimitive(Zone *zone, PrimitiveType type);
62 static Type *NewArray(Zone *zone, Handle<Type> contained, int elements, bool isConst);
63 static Type *NewReference(Zone *zone, Handle<Type> contained, bool isConst);
64 static Type *NewFunction(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters);
65 static Type *NewNative(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters, bool isVariadic);
67 static bool Compare(Type *left, Type *right);
69 static Type *cast(Object *obj) {
70 assert(!obj || obj->is(MapKind_Type));
75 return kind() == PRIMITIVE;
78 return kind() == ARRAY;
81 return kind() == REFERENCE;
84 return kind() == FUNCTION || kind() == NATIVE;
87 return kind() == NATIVE;
90 return kind() == VOID;
92 bool isNativeVariadic() {
94 return variadicNative_;
96 PrimitiveType primitive() {
97 assert(isPrimitive());
101 assert(isArray() || isReference());
102 return Type::cast(contained_);
113 assert(isPrimitive());
118 Type *temp = contained();
119 while (temp->isArray())
120 temp = temp->contained();
124 return isPrimitive() && primitive() == PrimitiveType_Float;
127 return isPrimitive() && primitive() == PrimitiveType_Int32;
131 contained()->isPrimitive() &&
132 contained()->primitive() == PrimitiveType_Char;
135 assert(isFunction());
138 FixedArray *parameters() {
139 assert(isFunction());
142 Type *parameterAt(size_t i) {
143 return Type::cast(parameters()->at(i));
146 return !isPrimitive();
149 assert(kind() == PRIMITIVE || kind() == ARRAY || kind() == REFERENCE);
154 static inline size_t offsetOfContained() {
155 return OFFSETOF(Type, contained_);
157 static inline size_t offsetOfReturnType() {
158 return OFFSETOF(Type, returnType_);
160 static inline size_t offsetOfParameters() {
161 return OFFSETOF(Type, parameters_);
163 static inline size_t offsetOfTag() {
164 return OFFSETOF(Type, tag_);
166 // End GC Descriptor.
170 PrimitiveType primitive_;
171 Barriered<String> tag_;
173 // Primitives, arrays, and references.
180 // Arrays and references only.
181 Barriered<Type> contained_;
184 Barriered<Type> returnType_;
185 Barriered<FixedArray> parameters_;
186 bool variadicNative_;
197 static BoxedPrimitive Int(int i) {
199 b.type_ = PrimitiveType_Int32;
204 PrimitiveType type() const {
208 assert(type() == PrimitiveType_Int32);
215 #endif // _include_jc_types_h_