Implemented const for references and arrays.
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
54 static const int UNKNOWN_SIZE = -1;
55 static const size_t MAX_UNSAFE_ARRAY_DEPTH = 5;
57 static Type *NewPrimitive(Zone *zone, Handle<String> tag);
58 static Type *NewPrimitive(Zone *zone, PrimitiveType type);
59 static Type *NewArray(Zone *zone, Handle<Type> contained, int elements, bool isConst);
60 static Type *NewReference(Zone *zone, Handle<Type> contained, bool isConst);
61 static Type *NewFunction(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters);
63 static bool Compare(Type *left, Type *right);
65 static Type *cast(Object *obj) {
66 assert(!obj || obj->is(MapKind_Type));
71 return kind() == PRIMITIVE;
74 return kind() == ARRAY;
77 return kind() == REFERENCE;
80 return kind() == FUNCTION;
82 PrimitiveType primitive() {
83 assert(isPrimitive());
87 assert(isArray() || isReference());
88 return Type::cast(contained_);
99 assert(isPrimitive());
104 Type *temp = contained();
105 while (temp->isArray())
106 temp = temp->contained();
110 return isPrimitive() && primitive() == PrimitiveType_Float;
113 return isPrimitive() && primitive() == PrimitiveType_Int32;
117 contained()->isPrimitive() &&
118 contained()->primitive() == PrimitiveType_Char;
121 assert(isFunction());
124 FixedArray *parameters() {
125 assert(isFunction());
128 Type *parameterAt(size_t i) {
129 return Type::cast(parameters()->at(i));
132 return !isPrimitive();
135 assert(kind() == PRIMITIVE || kind() == ARRAY || kind() == REFERENCE);
140 static inline size_t offsetOfContained() {
141 return offsetof(Type, contained_);
143 static inline size_t offsetOfReturnType() {
144 return offsetof(Type, returnType_);
146 static inline size_t offsetOfParameters() {
147 return offsetof(Type, parameters_);
149 static inline size_t offsetOfTag() {
150 return offsetof(Type, tag_);
152 // End GC Descriptor.
156 PrimitiveType primitive_;
157 Barriered<String> tag_;
159 // Primitives, arrays, and references.
166 // Arrays and references only.
167 Barriered<Type> contained_;
170 Barriered<Type> returnType_;
171 Barriered<FixedArray> parameters_;
182 static BoxedPrimitive Int(int i) {
184 b.type_ = PrimitiveType_Int32;
189 PrimitiveType type() const {
193 assert(type() == PrimitiveType_Int32);
200 #endif // _include_jc_types_h_