src/Types.h
changeset 195 c1ba166f3fc4
parent 188 bab29bcec7fe
child 209 5cbec15fadfe
     1.1 --- a/src/Types.h	Mon Sep 17 00:10:01 2012 -0700
     1.2 +++ b/src/Types.h	Sat Nov 17 18:41:01 2012 -0800
     1.3 @@ -22,11 +22,13 @@
     1.4  #include "Heap.h"
     1.5  #include "Barriers.h"
     1.6  #include "FixedArray.h"
     1.7 +#include "compiler/Token.h"
     1.8  
     1.9  namespace ke {
    1.10  
    1.11  class String;
    1.12  class Descriptor;
    1.13 +class StructMap;
    1.14  
    1.15  #define MAX_ARRAY_DEPTH     5
    1.16  
    1.17 @@ -59,6 +61,7 @@
    1.18  
    1.19  class Type : public Object
    1.20  {
    1.21 +  protected:
    1.22      enum Kind {
    1.23          // A primitive is a plain-old-data type.
    1.24          PRIMITIVE,
    1.25 @@ -74,9 +77,6 @@
    1.26          // A function type encapsulates a function signature.
    1.27          FUNCTION,
    1.28  
    1.29 -        // A native is a limited type of function used for system calls.
    1.30 -        NATIVE,
    1.31 -
    1.32          // Void is an internal type used to specify that a function returns no
    1.33          // value.
    1.34          VOID,
    1.35 @@ -108,17 +108,15 @@
    1.36      static Type *NewArray(Zone *zone, Handle<Type> contained, int elements, bool isConst);
    1.37      static Type *NewExternalArray(Zone *zone, Handle<Type> contained);
    1.38      static Type *NewReference(Zone *zone, Handle<Type> contained, bool isConst);
    1.39 -    static Type *NewFunction(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters,
    1.40 -                             Handle<FixedArray> defaults);
    1.41 -    static Type *NewNative(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters,
    1.42 -                           Handle<FixedArray> defaults, bool isVariadic);
    1.43      static Type *NewEnum(Zone *zone, Handle<String> name);
    1.44 -    static Type *NewStruct(Zone *zone, Handle<String> name, Handle<FixedArray> fields, Handle<Map> newMap);
    1.45  
    1.46      static bool Compare(Type *left, Type *right);
    1.47  
    1.48      static Type *cast(Object *obj) {
    1.49 -        assert(!obj || obj->is(MapKind_Type));
    1.50 +        assert(!obj ||
    1.51 +               obj->is(MapKind_Type) ||
    1.52 +               obj->is(MapKind_FunctionType) ||
    1.53 +               obj->is(MapKind_StructType));
    1.54          return (Type *)obj;
    1.55      }
    1.56  
    1.57 @@ -132,10 +130,7 @@
    1.58          return kind() == REFERENCE;
    1.59      }
    1.60      bool isFunction() {
    1.61 -        return kind() == FUNCTION || kind() == NATIVE;
    1.62 -    }
    1.63 -    bool isNative() {
    1.64 -        return kind() == NATIVE;
    1.65 +        return kind() == FUNCTION;
    1.66      }
    1.67      bool isVoid() {
    1.68          return kind() == VOID;
    1.69 @@ -146,10 +141,6 @@
    1.70      bool isStruct() {
    1.71          return kind() == STRUCT;
    1.72      }
    1.73 -    bool isNativeVariadic() {
    1.74 -        assert(isNative());
    1.75 -        return variadicNative_;
    1.76 -    }
    1.77      PrimitiveType primitive() {
    1.78          assert(isPrimitive());
    1.79          return primitive_;
    1.80 @@ -216,21 +207,6 @@
    1.81      bool isExternalArray() {
    1.82          return kind() == EXTERNAL_ARRAY;
    1.83      }
    1.84 -    Type *returnType() {
    1.85 -        assert(isFunction());
    1.86 -        return returnType_;
    1.87 -    }
    1.88 -    FixedArray *parameters() {
    1.89 -        assert(isFunction());
    1.90 -        return parameters_;
    1.91 -    }
    1.92 -    FixedArray *defaults() {
    1.93 -        assert(isFunction());
    1.94 -        return defaults_;
    1.95 -    }
    1.96 -    Type *parameterAt(size_t i) {
    1.97 -        return Type::cast(parameters()->at(i));
    1.98 -    }
    1.99      bool isTraceable() {
   1.100          return !isPrimitive();
   1.101      }
   1.102 @@ -248,47 +224,25 @@
   1.103          assert(isEnum() || isStruct());
   1.104          return name_;
   1.105      }
   1.106 -    FixedArray *fields() {
   1.107 -        assert(isStruct());
   1.108 -        return fields_;
   1.109 +
   1.110 +    Map *newMap() {
   1.111 +        assert(false);
   1.112 +        return NULL;
   1.113      }
   1.114 -    Descriptor *lookupField(Handle<String> name, unsigned *index);
   1.115 -
   1.116 -    // Objects of this type can be created with this cached map.
   1.117 -    Map *newMap() {
   1.118 -        assert(isStruct() || isArray());
   1.119 -        return newMap_;
   1.120 -    }
   1.121 -    void setNewMap(Map *map) {
   1.122 -        assert(!newMap());
   1.123 -        newMap_ = map;
   1.124 +    void setNewMap(void *p) {
   1.125 +        assert(false);
   1.126      }
   1.127  
   1.128      // GC Descriptor.
   1.129      static inline size_t offsetOfContained() {
   1.130          return OFFSETOF(Type, contained_);
   1.131      }
   1.132 -    static inline size_t offsetOfReturnType() {
   1.133 -        return OFFSETOF(Type, returnType_);
   1.134 -    }
   1.135 -    static inline size_t offsetOfParameters() {
   1.136 -        return OFFSETOF(Type, parameters_);
   1.137 -    }
   1.138      static inline size_t offsetOfName() {
   1.139          return OFFSETOF(Type, name_);
   1.140      }
   1.141 -    static inline size_t offsetOfFields() {
   1.142 -        return OFFSETOF(Type, fields_);
   1.143 -    }
   1.144 -    static inline size_t offsetOfNewMap() {
   1.145 -        return OFFSETOF(Type, newMap_);
   1.146 -    }
   1.147 -    static inline size_t offsetOfDefaults() {
   1.148 -        return OFFSETOF(Type, defaults_);
   1.149 -    }
   1.150      // End GC Descriptor.
   1.151  
   1.152 -  private:
   1.153 +  protected:
   1.154      Kind kind_;
   1.155      PrimitiveType primitive_;
   1.156  
   1.157 @@ -302,18 +256,116 @@
   1.158      // Arrays and references only.
   1.159      Barriered<Type> contained_;
   1.160  
   1.161 +    // Enums and structs only.
   1.162 +    Barriered<String> name_;
   1.163 +};
   1.164 +
   1.165 +class StructType : public Type
   1.166 +{
   1.167 +  public:
   1.168 +    static StructType *New(Zone *zone, Handle<String> name, Handle<FixedArray> fields);
   1.169 +
   1.170 +    FixedArray *fields() {
   1.171 +        assert(isStruct());
   1.172 +        return fields_;
   1.173 +    }
   1.174 +    Descriptor *lookupField(Handle<String> name, unsigned *index = NULL);
   1.175 +
   1.176 +    // Objects of this type can be created with this cached map.
   1.177 +    StructMap *newMap() {
   1.178 +        return newMap_;
   1.179 +    }
   1.180 +    void setNewMap(StructMap *map) {
   1.181 +        assert(!newMap());
   1.182 +        newMap_ = map;
   1.183 +    }
   1.184 +
   1.185 +    // GC Descriptor.
   1.186 +    static inline size_t offsetOfFields() {
   1.187 +        return OFFSETOF(StructType, fields_);
   1.188 +    }
   1.189 +    static inline size_t offsetOfNewMap() {
   1.190 +        return OFFSETOF(StructType, newMap_);
   1.191 +    }
   1.192 +    // End GC Descriptor.
   1.193 +
   1.194 +    static StructType *cast(Object *obj) {
   1.195 +        assert(!obj || obj->is(MapKind_StructType));
   1.196 +        return (StructType *)obj;
   1.197 +    }
   1.198 +
   1.199 +  private:
   1.200 +    // Structs only.
   1.201 +    Barriered<FixedArray> fields_;
   1.202 +    Barriered<StructMap> newMap_;
   1.203 +};
   1.204 +
   1.205 +class FunctionType : public Type
   1.206 +{
   1.207 +  public:
   1.208 +    static FunctionType *New(Zone *zone, TokenKind kind);
   1.209 +
   1.210 +    Type *parameterAt(size_t i) {
   1.211 +        return Type::cast(parameters()->at(i));
   1.212 +    }
   1.213 +    Type *returnType() {
   1.214 +        assert(isFunction());
   1.215 +        return returnType_;
   1.216 +    }
   1.217 +    FixedArray *parameters() {
   1.218 +        assert(isFunction());
   1.219 +        return parameters_;
   1.220 +    }
   1.221 +    FixedArray *defaults() {
   1.222 +        assert(isFunction());
   1.223 +        return defaults_;
   1.224 +    }
   1.225 +    bool isNative() {
   1.226 +        return token_ == TOK_NATIVE;
   1.227 +    }
   1.228 +    bool isForward() {
   1.229 +        return token_ == TOK_FORWARD;
   1.230 +    }
   1.231 +    bool isNativeVariadic() {
   1.232 +        assert(isNative());
   1.233 +        return variadicNative_;
   1.234 +    }
   1.235 +    void setReturnType(Handle<Type> returnType) {
   1.236 +        assert(!returnType_);
   1.237 +        returnType_ = returnType;
   1.238 +    }
   1.239 +    void setParameterTypes(Handle<FixedArray> types) {
   1.240 +        assert(!parameters_);
   1.241 +        parameters_ = types;
   1.242 +    }
   1.243 +    TokenKind token() {
   1.244 +        return token_;
   1.245 +    }
   1.246 +
   1.247 +    // GC Descriptor.
   1.248 +    static inline size_t offsetOfReturnType() {
   1.249 +        return OFFSETOF(FunctionType, returnType_);
   1.250 +    }
   1.251 +    static inline size_t offsetOfParameters() {
   1.252 +        return OFFSETOF(FunctionType, parameters_);
   1.253 +    }
   1.254 +    static inline size_t offsetOfDefaults() {
   1.255 +        return OFFSETOF(FunctionType, defaults_);
   1.256 +    }
   1.257 +    // End GC Descriptor.
   1.258 +
   1.259 +    static FunctionType *cast(Object *obj) {
   1.260 +        assert(!obj || obj->is(MapKind_FunctionType));
   1.261 +        return (FunctionType *)obj;
   1.262 +    }
   1.263 +
   1.264 +  private:
   1.265      // Functions only.
   1.266      Barriered<Type> returnType_;
   1.267      Barriered<FixedArray> parameters_;
   1.268      Barriered<FixedArray> defaults_;
   1.269 +    TokenKind token_;
   1.270      bool variadicNative_;
   1.271 -
   1.272 -    // Enums and structs only.
   1.273 -    Barriered<String> name_;
   1.274 -
   1.275 -    // Structs only.
   1.276 -    Barriered<FixedArray> fields_;
   1.277 -    Barriered<Map> newMap_;
   1.278  };
   1.279  
   1.280  class BoxedPrimitive
   1.281 @@ -351,6 +403,8 @@
   1.282      }
   1.283  };
   1.284  
   1.285 +const char *GetTypeName(Type *type);
   1.286 +
   1.287  }
   1.288  
   1.289  #endif // _include_jc_types_h_