Re-add enum support.
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"
25 #include "compiler/Token.h"
34 #define MAX_ARRAY_DEPTH 5
38 // A boolean type semantically represents 0 or 1. As an optimization, its
39 // internal storage type is the same as int32, and any non-zero value
40 // compares positively to "true".
43 // A character is an arbitrary 8-bit value. Characters are not supported
44 // as a normal storage class; they may only be used for arrays. When read
45 // from an array, they are immediately sign extended to 32 bits. Integers
46 // stored into an char array are truncated to 8 bits.
47 PrimitiveType_Char, // signed, 8-bit character
49 // Enumerations and untyped variables are stored as signed, 32-bit integers.
52 // Floating point values are 32-bit IEEE-754 floats. This is required ro
53 // SourcePawn 1 compatibility.
56 // This is a native-sized integer type (32-bit on 32-bit systems, 64-bit
57 // on 64-bit systems), and does not exist in language semantics.
63 class Type : public Object
67 // A primitive is a plain-old-data type.
70 // An array is a fixed-length vector of any other type.
74 // A reference type may only be specified on parameters, and
75 // references may only be computed to primitives or enums.
78 // A function type encapsulates a function signature.
81 // Void is an internal type used to specify that a function returns no
85 // Enums are plain-old-data with int32 storage. They allow for extra
86 // type checking that would not be possible with integers.
89 // Structs are composite records that may contain primitives, arrays,
90 // functions, enums, and other structs.
93 // Unchecked is a magic type that has implicit, bitwise coercion to
94 // int32, float, bool, or an enum.
104 static Type *NewVoid(Zone *zone);
105 static Type *NewUnchecked(Zone *zone);
106 static Type *NewPrimitive(Zone *zone, PrimitiveType type);
108 static bool Compare(Type *left, Type *right);
110 static Type *cast(Object *obj) {
112 obj->is(MapKind_Type) ||
113 obj->is(MapKind_FunctionType) ||
114 obj->is(MapKind_StructType) ||
115 obj->is(MapKind_ArrayType) ||
116 obj->is(MapKind_ReferenceType) ||
117 obj->is(MapKind_EnumType));
122 return kind() == PRIMITIVE;
125 return kind() == ARRAY || kind() == EXTERNAL_ARRAY;
128 return kind() == REFERENCE;
131 return kind() == FUNCTION;
134 return kind() == VOID;
137 return kind() == UNCHECKED;
140 return kind() == STRUCT;
142 PrimitiveType primitive() {
143 assert(isPrimitive());
147 return isPrimitive() || isEnum() || isUnchecked();
149 PrimitiveType pod() {
152 ? (primitive() == PrimitiveType_Bool)
153 ? PrimitiveType_Int32
155 : PrimitiveType_Int32;
158 return isPrimitive() && primitive() == PrimitiveType_Char;
161 return isPrimitive() && primitive() == PrimitiveType_Float;
164 return isPrimitive() && primitive() == PrimitiveType_Int32;
166 bool isInt32OrEnum() {
167 return isInt32() || isEnum();
170 return isPrimitive() && primitive() == PrimitiveType_Bool;
172 bool isExternalArray() {
173 return kind() == EXTERNAL_ARRAY;
176 return !isPrimitive();
179 assert(isPrimitive() ||
185 return kind() == ENUM;
190 PrimitiveType primitive_;
192 // Primitives, arrays, and references.
196 class EnumType : public Type
199 static EnumType *New(Zone *Zone, Handle<String> name);
206 static inline size_t offsetOfName() {
207 return OFFSETOF(EnumType, name_);
209 // End GC Descriptor.
211 static EnumType *cast(Object *obj) {
212 assert(!obj || obj->is(MapKind_EnumType));
213 return (EnumType *)obj;
217 Barriered<String> name_;
220 class ReferenceType : public Type
223 static ReferenceType *New(Zone *zone, Handle<Type> contained);
230 static inline size_t offsetOfContained() {
231 return OFFSETOF(ReferenceType, contained_);
233 // End GC Descriptor.
235 static ReferenceType *cast(Object *obj) {
236 assert(!obj || obj->is(MapKind_ReferenceType));
237 return (ReferenceType *)obj;
241 Barriered<Type> contained_;
244 class ArrayType : public Type
247 static const int DYNAMIC_ARRAY_SIZE = -1;
249 static ArrayType *New(Zone *zone, Handle<Type> contained, int elements);
250 static ArrayType *NewExternal(Zone *zone, Handle<Type> contained);
254 Type *temp = contained();
255 while (temp->isArray())
256 temp = ArrayType::cast(temp)->contained();
263 bool isFixedLength() {
265 return elements_ > 0;
268 return contained()->isPrimitive() &&
269 contained()->primitive() == PrimitiveType_Char;
271 unsigned fixedLength() {
272 assert(isFixedLength());
279 bool hasInlineElements() {
282 // We don't allocate traceable elements inline, since there's no
283 // point to the optimization; we're going to be allocating lots of
284 // objects anyway (in theory).
285 return isFixedLength() && !contained()->isTraceable();
288 // Arrays of this type can be created with this cached map.
292 void setNewMap(ArrayMap *map) {
298 static inline size_t offsetOfContained() {
299 return OFFSETOF(ArrayType, contained_);
301 static inline size_t offsetOfNewMap() {
302 return OFFSETOF(ArrayType, newMap_);
304 // End GC Descriptor.
306 static ArrayType *cast(Object *obj) {
307 assert(!obj || obj->is(MapKind_ArrayType));
308 return (ArrayType *)obj;
314 Barriered<Type> contained_;
315 Barriered<ArrayMap> newMap_;
318 class StructType : public Type
321 static StructType *New(Zone *zone, Handle<String> name, Handle<FixedArray> fields);
323 FixedArray *fields() {
327 Descriptor *lookupField(Handle<String> name, unsigned *index = NULL);
329 // Objects of this type can be created with this cached map.
330 StructMap *newMap() {
333 void setNewMap(StructMap *map) {
343 static inline size_t offsetOfFields() {
344 return OFFSETOF(StructType, fields_);
346 static inline size_t offsetOfNewMap() {
347 return OFFSETOF(StructType, newMap_);
349 static inline size_t offsetOfName() {
350 return OFFSETOF(StructType, name_);
352 // End GC Descriptor.
354 static StructType *cast(Object *obj) {
355 assert(!obj || obj->is(MapKind_StructType));
356 return (StructType *)obj;
361 Barriered<FixedArray> fields_;
362 Barriered<StructMap> newMap_;
363 Barriered<String> name_;
366 class FunctionType : public Type
369 static FunctionType *New(Zone *zone, Handle<String> name, TokenKind kind);
371 Type *parameterAt(size_t i) {
372 return Type::cast(parameters()->at(i));
375 assert(isFunction());
378 FixedArray *parameters() {
379 assert(isFunction());
382 FixedArray *defaults() {
383 assert(isFunction());
387 return token_ == TOK_NATIVE;
390 return token_ == TOK_FORWARD;
392 bool isNativeVariadic() {
394 return variadicNative_;
396 void setReturnType(Handle<Type> returnType) {
397 assert(!returnType_);
398 returnType_ = returnType;
400 void setParameterTypes(Handle<FixedArray> types) {
401 assert(!parameters_);
412 static inline size_t offsetOfReturnType() {
413 return OFFSETOF(FunctionType, returnType_);
415 static inline size_t offsetOfParameters() {
416 return OFFSETOF(FunctionType, parameters_);
418 static inline size_t offsetOfDefaults() {
419 return OFFSETOF(FunctionType, defaults_);
421 static inline size_t offsetOfName() {
422 return OFFSETOF(FunctionType, name_);
424 // End GC Descriptor.
426 static FunctionType *cast(Object *obj) {
427 assert(!obj || obj->is(MapKind_FunctionType));
428 return (FunctionType *)obj;
433 Barriered<Type> returnType_;
434 Barriered<FixedArray> parameters_;
435 Barriered<FixedArray> defaults_;
436 Barriered<String> name_;
438 bool variadicNative_;
450 static BoxedPrimitive Int(int i) {
452 b.type_ = PrimitiveType_Int32;
456 static BoxedPrimitive Float(float f) {
458 b.type_ = PrimitiveType_Float;
463 PrimitiveType type() const {
467 assert(type() == PrimitiveType_Int32);
470 float toFloat() const {
471 assert(type() == PrimitiveType_Float);
476 const char *GetTypeName(Type *type);
480 #endif // _include_jc_types_h_