Fix clang bustage.
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.
67 // Values of this type cannot be modified.
68 TypeQual_Const = (1 << 2)
71 class Type : public Object
75 // A primitive is a plain-old-data type.
78 // Void is an internal type used to specify that a function returns no
82 // Enums are plain-old-data with int32 storage. They allow for extra
83 // type checking that would not be possible with integers.
86 // Unchecked is a magic type that has implicit, bitwise coercion to
87 // int32, float, bool, or an enum.
90 // Contains type qualifiers, and a back pointer to the actual
94 // ------ Add traceable types below (STRING FIRST!) -------
96 // An immutable type that is a primitive in terms of language
97 // semantics, but gets a special type here to make internal
101 // An array is a fixed-length vector of any other type.
105 // A reference type may only be specified on parameters, and
106 // references may only be computed to primitives or enums.
109 // A function type encapsulates a function signature.
112 // Structs are composite records that may contain primitives, arrays,
113 // functions, enums, and other structs.
117 void init(Kind kind, Type *root = NULL);
125 static Type *NewVoid(Zone *zone);
126 static Type *NewUnchecked(Zone *zone);
127 static Type *NewPrimitive(Zone *zone, PrimitiveType type);
128 static Type *NewString(Zone *zone);
129 static Type *NewQualified(Zone *zone, TypeQualifiers qual, Handle<Type> type);
131 static Type *cast(Object *obj) {
133 obj->is(MapKind_Type) ||
134 obj->is(MapKind_FunctionType) ||
135 obj->is(MapKind_StructType) ||
136 obj->is(MapKind_ArrayType) ||
137 obj->is(MapKind_ReferenceType) ||
138 obj->is(MapKind_EnumType));
143 return kind() == PRIMITIVE;
146 return kind() == ARRAY || kind() == EXTERNAL_ARRAY;
149 return kind() == REFERENCE;
152 return kind() == FUNCTION;
155 return kind() == VOID;
158 return kind() == UNCHECKED;
161 return kind() == STRUCT;
163 PrimitiveType primitive() {
164 assert(isPrimitive());
165 return root_->primitive_;
168 return (isPrimitive() && !isString()) || isEnum() || isUnchecked();
170 PrimitiveType pod() {
173 ? (primitive() == PrimitiveType_Bool)
174 ? PrimitiveType_Int32
176 : PrimitiveType_Int32;
179 return isPrimitive() && primitive() == PrimitiveType_Char;
182 return isPrimitive() && primitive() == PrimitiveType_Float;
185 return isPrimitive() && primitive() == PrimitiveType_Int32;
187 bool isInt32OrEnum() {
188 return isInt32() || isEnum();
191 return isPrimitive() && primitive() == PrimitiveType_Bool;
194 return kind() == STRING;
196 bool isExternalArray() {
197 return kind() == EXTERNAL_ARRAY;
200 return kind() >= STRING;
203 return kind() == ENUM;
205 bool hasTypeQualifiers() {
206 return kind_ == QUALTYPE;
209 TypeQualifiers quals() {
210 if (!hasTypeQualifiers())
211 return TypeQual_None;
214 Type *unqualified() {
219 static inline size_t offsetOfRoot() {
220 return OFFSETOF(Type, root_);
222 // End GC Descriptor.
226 PrimitiveType primitive_;
227 TypeQualifiers quals_;
229 // For unqualified types, this points to |this|. For qualified types, it
230 // points to the unqualified original type.
231 Barriered<Type> root_;
234 class EnumType : public Type
237 static EnumType *New(Zone *Zone, Handle<String> name);
244 static inline size_t offsetOfName() {
245 return OFFSETOF(EnumType, name_);
247 // End GC Descriptor.
249 static EnumType *cast(Object *obj) {
250 assert(!obj || obj->is(MapKind_EnumType));
251 return (EnumType *)obj;
255 Barriered<String> name_;
258 class ReferenceType : public Type
261 static ReferenceType *New(Zone *zone, Handle<Type> contained);
268 static inline size_t offsetOfContained() {
269 return OFFSETOF(ReferenceType, contained_);
271 // End GC Descriptor.
273 static ReferenceType *cast(Type *obj) {
276 obj = obj->unqualified();
277 assert(obj->is(MapKind_ReferenceType));
278 return (ReferenceType *)obj;
282 Barriered<Type> contained_;
285 class ArrayType : public Type
288 static const int DYNAMIC_ARRAY_SIZE = -1;
290 static ArrayType *New(Zone *zone, Handle<Type> contained, int elements);
291 static ArrayType *NewExternal(Zone *zone, Handle<Type> contained);
294 Type *temp = contained();
295 while (temp->isArray())
296 temp = ArrayType::cast(temp)->contained();
303 bool isFixedLength() {
304 return elements_ > 0;
307 return contained()->isPrimitive() &&
308 contained()->primitive() == PrimitiveType_Char;
310 unsigned fixedLength() {
311 assert(isFixedLength());
317 bool hasInlineElements() {
318 // We don't allocate traceable elements inline, since there's no
319 // point to the optimization; we're going to be allocating lots of
320 // objects anyway (in theory).
321 return isFixedLength() && !contained()->isTraceable();
324 // Arrays of this type can be created with this cached map.
328 void setNewMap(ArrayMap *map) {
334 static inline size_t offsetOfContained() {
335 return OFFSETOF(ArrayType, contained_);
337 static inline size_t offsetOfNewMap() {
338 return OFFSETOF(ArrayType, newMap_);
340 // End GC Descriptor.
342 static ArrayType *cast(Type *obj) {
345 obj = obj->unqualified();
346 assert(obj->is(MapKind_ArrayType));
347 return (ArrayType *)obj;
353 Barriered<Type> contained_;
354 Barriered<ArrayMap> newMap_;
357 class StructType : public Type
360 static StructType *New(Zone *zone, Handle<String> name, Handle<FixedArray> fields);
362 FixedArray *fields() {
366 Descriptor *lookupField(Handle<String> name, unsigned *index = NULL);
368 // Objects of this type can be created with this cached map.
369 StructMap *newMap() {
372 void setNewMap(StructMap *map) {
382 static inline size_t offsetOfFields() {
383 return OFFSETOF(StructType, fields_);
385 static inline size_t offsetOfNewMap() {
386 return OFFSETOF(StructType, newMap_);
388 static inline size_t offsetOfName() {
389 return OFFSETOF(StructType, name_);
391 // End GC Descriptor.
393 static StructType *cast(Type *obj) {
396 obj = obj->unqualified();
397 assert(obj->is(MapKind_StructType));
398 return (StructType *)obj;
403 Barriered<FixedArray> fields_;
404 Barriered<StructMap> newMap_;
405 Barriered<String> name_;
408 class FunctionType : public Type
411 static FunctionType *New(Zone *zone, Handle<String> name, TokenKind kind);
413 Type *parameterAt(size_t i) {
414 return Type::cast(parameters()->at(i));
417 assert(isFunction());
420 FixedArray *parameters() {
421 assert(isFunction());
424 FixedArray *defaults() {
425 assert(isFunction());
429 return token_ == TOK_NATIVE;
432 return token_ == TOK_FORWARD;
434 bool isNativeVariadic() {
436 return variadicNative_;
438 void setReturnType(Handle<Type> returnType) {
439 assert(!returnType_);
440 returnType_ = returnType;
442 void setParameterTypes(Handle<FixedArray> types) {
443 assert(!parameters_);
454 static inline size_t offsetOfReturnType() {
455 return OFFSETOF(FunctionType, returnType_);
457 static inline size_t offsetOfParameters() {
458 return OFFSETOF(FunctionType, parameters_);
460 static inline size_t offsetOfDefaults() {
461 return OFFSETOF(FunctionType, defaults_);
463 static inline size_t offsetOfName() {
464 return OFFSETOF(FunctionType, name_);
466 // End GC Descriptor.
468 static FunctionType *cast(Type *obj) {
471 obj = obj->unqualified();
472 assert(obj->is(MapKind_FunctionType));
473 return (FunctionType *)obj;
478 Barriered<Type> returnType_;
479 Barriered<FixedArray> parameters_;
480 Barriered<FixedArray> defaults_;
481 Barriered<String> name_;
483 bool variadicNative_;
495 static BoxedPrimitive Int(int i) {
497 b.type_ = PrimitiveType_Int32;
501 static BoxedPrimitive Float(float f) {
503 b.type_ = PrimitiveType_Float;
508 PrimitiveType type() const {
512 assert(type() == PrimitiveType_Int32);
515 float toFloat() const {
516 assert(type() == PrimitiveType_Float);
521 const char *GetTypeName(Type *type, char *buffer, size_t maxlen);
525 #endif // _include_jc_types_h_