Added runtime support for default arguments.
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"
33 // A boolean type semantically represents 0 or 1. As an optimization, its
34 // internal storage type is the same as int32, and any non-zero value
35 // compares positively to "true".
38 // A character is an arbitrary 8-bit value. Characters are not supported
39 // as a normal storage class; they may only be used for arrays. When read
40 // from an array, they are immediately sign extended to 32 bits. Integers
41 // stored into an char array are truncated to 8 bits.
42 PrimitiveType_Char, // signed, 8-bit character
44 // Enumerations and untyped variables are stored as signed, 32-bit integers.
47 // Floating point values are 32-bit IEEE-754 floats. This is required ro
48 // SourcePawn 1 compatibility.
51 // This is a native-sized integer type (32-bit on 32-bit systems, 64-bit
52 // on 64-bit systems), and does not exist in language semantics.
58 class Type : public Object
61 // A primitive is a plain-old-data type.
64 // An array is a fixed-length vector of any other type.
67 // A reference type may only be specified on parameters, and
68 // references may only be computed to primitives or enums.
71 // A function type encapsulates a function signature.
74 // A native is a limited type of function used for system calls.
77 // Void is an internal type used to specify that a function returns no
81 // Enums are plain-old-data with int32 storage. They allow for extra
82 // type checking that would not be possible with integers.
85 // Structs are composite records that may contain primitives, arrays,
86 // functions, enums, and other structs.
89 // Unchecked is a magic type that has implicit, bitwise coercion to
90 // int32, float, bool, or an enum.
100 static const int UNKNOWN_SIZE = -1;
101 static const size_t MAX_UNSAFE_ARRAY_DEPTH = 5;
103 static Type *NewVoid(Zone *zone);
104 static Type *NewUnchecked(Zone *zone);
105 static Type *NewPrimitive(Zone *zone, PrimitiveType type);
106 static Type *NewArray(Zone *zone, Handle<Type> contained, int elements, bool isConst);
107 static Type *NewReference(Zone *zone, Handle<Type> contained, bool isConst);
108 static Type *NewFunction(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters,
109 Handle<FixedArray> defaults);
110 static Type *NewNative(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters,
111 Handle<FixedArray> defaults, bool isVariadic);
112 static Type *NewEnum(Zone *zone, Handle<String> name);
113 static Type *NewStruct(Zone *zone, Handle<String> name, Handle<FixedArray> fields, Handle<Map> newMap);
115 static bool Compare(Type *left, Type *right);
117 static Type *cast(Object *obj) {
118 assert(!obj || obj->is(MapKind_Type));
123 return kind() == PRIMITIVE;
126 return kind() == ARRAY;
129 return kind() == REFERENCE;
132 return kind() == FUNCTION || kind() == NATIVE;
135 return kind() == NATIVE;
138 return kind() == VOID;
141 return kind() == UNCHECKED;
144 return kind() == STRUCT;
146 bool isNativeVariadic() {
148 return variadicNative_;
150 PrimitiveType primitive() {
151 assert(isPrimitive());
155 return isPrimitive() || isEnum() || isUnchecked();
157 PrimitiveType pod() {
160 ? (primitive() == PrimitiveType_Bool)
161 ? PrimitiveType_Int32
163 : PrimitiveType_Int32;
166 assert(isArray() || isReference());
167 return Type::cast(contained_);
179 Type *temp = contained();
180 while (temp->isArray())
181 temp = temp->contained();
185 return isPrimitive() && primitive() == PrimitiveType_Float;
188 return isPrimitive() && primitive() == PrimitiveType_Int32;
190 bool isInt32OrEnum() {
191 return isInt32() || isEnum();
194 return isPrimitive() && primitive() == PrimitiveType_Bool;
198 contained()->isPrimitive() &&
199 contained()->primitive() == PrimitiveType_Char;
202 assert(isFunction());
205 FixedArray *parameters() {
206 assert(isFunction());
209 FixedArray *defaults() {
210 assert(isFunction());
213 Type *parameterAt(size_t i) {
214 return Type::cast(parameters()->at(i));
217 return !isPrimitive();
220 assert(kind() == PRIMITIVE || kind() == ARRAY || kind() == REFERENCE);
224 return kind() == ENUM;
228 assert(isEnum() || isStruct());
231 FixedArray *fields() {
235 Descriptor *lookupField(Handle<String> name, unsigned *index);
237 // Objects of this type can be created with this cached map.
239 assert(isStruct() || isArray());
242 void setNewMap(Map *map) {
248 static inline size_t offsetOfContained() {
249 return OFFSETOF(Type, contained_);
251 static inline size_t offsetOfReturnType() {
252 return OFFSETOF(Type, returnType_);
254 static inline size_t offsetOfParameters() {
255 return OFFSETOF(Type, parameters_);
257 static inline size_t offsetOfName() {
258 return OFFSETOF(Type, name_);
260 static inline size_t offsetOfFields() {
261 return OFFSETOF(Type, fields_);
263 static inline size_t offsetOfNewMap() {
264 return OFFSETOF(Type, newMap_);
266 static inline size_t offsetOfDefaults() {
267 return OFFSETOF(Type, defaults_);
269 // End GC Descriptor.
273 PrimitiveType primitive_;
275 // Primitives, arrays, and references.
282 // Arrays and references only.
283 Barriered<Type> contained_;
286 Barriered<Type> returnType_;
287 Barriered<FixedArray> parameters_;
288 Barriered<FixedArray> defaults_;
289 bool variadicNative_;
291 // Enums and structs only.
292 Barriered<String> name_;
295 Barriered<FixedArray> fields_;
296 Barriered<Map> newMap_;
308 static BoxedPrimitive Int(int i) {
310 b.type_ = PrimitiveType_Int32;
314 static BoxedPrimitive Bool(bool v) {
316 b.type_ = PrimitiveType_Bool;
321 PrimitiveType type() const {
325 assert(type() == PrimitiveType_Int32);
328 bool toBool() const {
329 assert(type() == PrimitiveType_Bool);
336 #endif // _include_jc_types_h_