Initial support for booleans. Coercion does not work yet.
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_Bool, // 0 or 1
33 PrimitiveType_Char, // signed, 8-bit character
34 PrimitiveType_Int32, // signed, 32-bit integer
35 PrimitiveType_Float, // 32-bit IEEE-754 float
36 PrimitiveType_Native, // native-sized word
40 class Type : public Object
57 static const int UNKNOWN_SIZE = -1;
58 static const size_t MAX_UNSAFE_ARRAY_DEPTH = 5;
60 static Type *NewVoid(Zone *zone);
61 static Type *NewPrimitive(Zone *zone, Handle<String> tag);
62 static Type *NewPrimitive(Zone *zone, PrimitiveType type);
63 static Type *NewArray(Zone *zone, Handle<Type> contained, int elements, bool isConst);
64 static Type *NewReference(Zone *zone, Handle<Type> contained, bool isConst);
65 static Type *NewFunction(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters);
66 static Type *NewNative(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters, bool isVariadic);
68 static bool Compare(Type *left, Type *right);
70 static Type *cast(Object *obj) {
71 assert(!obj || obj->is(MapKind_Type));
76 return kind() == PRIMITIVE;
79 return kind() == ARRAY;
82 return kind() == REFERENCE;
85 return kind() == FUNCTION || kind() == NATIVE;
88 return kind() == NATIVE;
91 return kind() == VOID;
93 bool isNativeVariadic() {
95 return variadicNative_;
97 PrimitiveType primitive() {
98 assert(isPrimitive());
102 assert(isArray() || isReference());
103 return Type::cast(contained_);
114 assert(isPrimitive());
119 Type *temp = contained();
120 while (temp->isArray())
121 temp = temp->contained();
125 return isPrimitive() && primitive() == PrimitiveType_Float;
128 return isPrimitive() && primitive() == PrimitiveType_Int32;
132 contained()->isPrimitive() &&
133 contained()->primitive() == PrimitiveType_Char;
136 assert(isFunction());
139 FixedArray *parameters() {
140 assert(isFunction());
143 Type *parameterAt(size_t i) {
144 return Type::cast(parameters()->at(i));
147 return !isPrimitive();
150 assert(kind() == PRIMITIVE || kind() == ARRAY || kind() == REFERENCE);
155 static inline size_t offsetOfContained() {
156 return OFFSETOF(Type, contained_);
158 static inline size_t offsetOfReturnType() {
159 return OFFSETOF(Type, returnType_);
161 static inline size_t offsetOfParameters() {
162 return OFFSETOF(Type, parameters_);
164 static inline size_t offsetOfTag() {
165 return OFFSETOF(Type, tag_);
167 // End GC Descriptor.
171 PrimitiveType primitive_;
172 Barriered<String> tag_;
174 // Primitives, arrays, and references.
181 // Arrays and references only.
182 Barriered<Type> contained_;
185 Barriered<Type> returnType_;
186 Barriered<FixedArray> parameters_;
187 bool variadicNative_;
198 static BoxedPrimitive Int(int i) {
200 b.type_ = PrimitiveType_Int32;
205 PrimitiveType type() const {
209 assert(type() == PrimitiveType_Int32);
216 #endif // _include_jc_types_h_