diff -r b029d63565a2 -r c1ba166f3fc4 src/Types.h --- a/src/Types.h Mon Sep 17 00:10:01 2012 -0700 +++ b/src/Types.h Sat Nov 17 18:41:01 2012 -0800 @@ -22,11 +22,13 @@ #include "Heap.h" #include "Barriers.h" #include "FixedArray.h" +#include "compiler/Token.h" namespace ke { class String; class Descriptor; +class StructMap; #define MAX_ARRAY_DEPTH 5 @@ -59,6 +61,7 @@ class Type : public Object { + protected: enum Kind { // A primitive is a plain-old-data type. PRIMITIVE, @@ -74,9 +77,6 @@ // A function type encapsulates a function signature. FUNCTION, - // A native is a limited type of function used for system calls. - NATIVE, - // Void is an internal type used to specify that a function returns no // value. VOID, @@ -108,17 +108,15 @@ static Type *NewArray(Zone *zone, Handle contained, int elements, bool isConst); static Type *NewExternalArray(Zone *zone, Handle contained); static Type *NewReference(Zone *zone, Handle contained, bool isConst); - static Type *NewFunction(Zone *zone, Handle returnType, Handle parameters, - Handle defaults); - static Type *NewNative(Zone *zone, Handle returnType, Handle parameters, - Handle defaults, bool isVariadic); static Type *NewEnum(Zone *zone, Handle name); - static Type *NewStruct(Zone *zone, Handle name, Handle fields, Handle newMap); static bool Compare(Type *left, Type *right); static Type *cast(Object *obj) { - assert(!obj || obj->is(MapKind_Type)); + assert(!obj || + obj->is(MapKind_Type) || + obj->is(MapKind_FunctionType) || + obj->is(MapKind_StructType)); return (Type *)obj; } @@ -132,10 +130,7 @@ return kind() == REFERENCE; } bool isFunction() { - return kind() == FUNCTION || kind() == NATIVE; - } - bool isNative() { - return kind() == NATIVE; + return kind() == FUNCTION; } bool isVoid() { return kind() == VOID; @@ -146,10 +141,6 @@ bool isStruct() { return kind() == STRUCT; } - bool isNativeVariadic() { - assert(isNative()); - return variadicNative_; - } PrimitiveType primitive() { assert(isPrimitive()); return primitive_; @@ -216,21 +207,6 @@ bool isExternalArray() { return kind() == EXTERNAL_ARRAY; } - Type *returnType() { - assert(isFunction()); - return returnType_; - } - FixedArray *parameters() { - assert(isFunction()); - return parameters_; - } - FixedArray *defaults() { - assert(isFunction()); - return defaults_; - } - Type *parameterAt(size_t i) { - return Type::cast(parameters()->at(i)); - } bool isTraceable() { return !isPrimitive(); } @@ -248,47 +224,25 @@ assert(isEnum() || isStruct()); return name_; } - FixedArray *fields() { - assert(isStruct()); - return fields_; + + Map *newMap() { + assert(false); + return NULL; } - Descriptor *lookupField(Handle name, unsigned *index); - - // Objects of this type can be created with this cached map. - Map *newMap() { - assert(isStruct() || isArray()); - return newMap_; - } - void setNewMap(Map *map) { - assert(!newMap()); - newMap_ = map; + void setNewMap(void *p) { + assert(false); } // GC Descriptor. static inline size_t offsetOfContained() { return OFFSETOF(Type, contained_); } - static inline size_t offsetOfReturnType() { - return OFFSETOF(Type, returnType_); - } - static inline size_t offsetOfParameters() { - return OFFSETOF(Type, parameters_); - } static inline size_t offsetOfName() { return OFFSETOF(Type, name_); } - static inline size_t offsetOfFields() { - return OFFSETOF(Type, fields_); - } - static inline size_t offsetOfNewMap() { - return OFFSETOF(Type, newMap_); - } - static inline size_t offsetOfDefaults() { - return OFFSETOF(Type, defaults_); - } // End GC Descriptor. - private: + protected: Kind kind_; PrimitiveType primitive_; @@ -302,18 +256,116 @@ // Arrays and references only. Barriered contained_; + // Enums and structs only. + Barriered name_; +}; + +class StructType : public Type +{ + public: + static StructType *New(Zone *zone, Handle name, Handle fields); + + FixedArray *fields() { + assert(isStruct()); + return fields_; + } + Descriptor *lookupField(Handle name, unsigned *index = NULL); + + // Objects of this type can be created with this cached map. + StructMap *newMap() { + return newMap_; + } + void setNewMap(StructMap *map) { + assert(!newMap()); + newMap_ = map; + } + + // GC Descriptor. + static inline size_t offsetOfFields() { + return OFFSETOF(StructType, fields_); + } + static inline size_t offsetOfNewMap() { + return OFFSETOF(StructType, newMap_); + } + // End GC Descriptor. + + static StructType *cast(Object *obj) { + assert(!obj || obj->is(MapKind_StructType)); + return (StructType *)obj; + } + + private: + // Structs only. + Barriered fields_; + Barriered newMap_; +}; + +class FunctionType : public Type +{ + public: + static FunctionType *New(Zone *zone, TokenKind kind); + + Type *parameterAt(size_t i) { + return Type::cast(parameters()->at(i)); + } + Type *returnType() { + assert(isFunction()); + return returnType_; + } + FixedArray *parameters() { + assert(isFunction()); + return parameters_; + } + FixedArray *defaults() { + assert(isFunction()); + return defaults_; + } + bool isNative() { + return token_ == TOK_NATIVE; + } + bool isForward() { + return token_ == TOK_FORWARD; + } + bool isNativeVariadic() { + assert(isNative()); + return variadicNative_; + } + void setReturnType(Handle returnType) { + assert(!returnType_); + returnType_ = returnType; + } + void setParameterTypes(Handle types) { + assert(!parameters_); + parameters_ = types; + } + TokenKind token() { + return token_; + } + + // GC Descriptor. + static inline size_t offsetOfReturnType() { + return OFFSETOF(FunctionType, returnType_); + } + static inline size_t offsetOfParameters() { + return OFFSETOF(FunctionType, parameters_); + } + static inline size_t offsetOfDefaults() { + return OFFSETOF(FunctionType, defaults_); + } + // End GC Descriptor. + + static FunctionType *cast(Object *obj) { + assert(!obj || obj->is(MapKind_FunctionType)); + return (FunctionType *)obj; + } + + private: // Functions only. Barriered returnType_; Barriered parameters_; Barriered defaults_; + TokenKind token_; bool variadicNative_; - - // Enums and structs only. - Barriered name_; - - // Structs only. - Barriered fields_; - Barriered newMap_; }; class BoxedPrimitive @@ -351,6 +403,8 @@ } }; +const char *GetTypeName(Type *type); + } #endif // _include_jc_types_h_