Implemented const for references and arrays.
authorDavid Anderson <dvander@alliedmods.net>
Mon Jul 09 22:25:52 2012 -0700 (2012-07-09)
changeset 67b765a6383849
parent 66 cbba1997dc71
child 68 b40b0e18a918
Implemented const for references and arrays.
jitcraft/AST.h
jitcraft/Messages.tbl
jitcraft/Parser.cpp
jitcraft/Scanner.cpp
jitcraft/SemanticAnalysis.cpp
jitcraft/Token.h
jitcraft/TypeManager.cpp
jitcraft/TypeManager.h
jitcraft/Types.cpp
jitcraft/Types.h
jitcraft/Zone.cpp
     1.1 --- a/jitcraft/AST.h	Mon Jul 09 21:40:20 2012 -0700
     1.2 +++ b/jitcraft/AST.h	Mon Jul 09 22:25:52 2012 -0700
     1.3 @@ -198,13 +198,15 @@
     1.4      Variable *variable_;
     1.5      ScopedRoot<String> tag_;
     1.6      bool reference_;
     1.7 +    bool const_;
     1.8      ExpressionList *dimensions_;
     1.9  
    1.10    public:
    1.11 -    Parameter(Variable *var, Handle<String> tag, bool reference, ExpressionList *dimensions)
    1.12 +    Parameter(Variable *var, Handle<String> tag, bool reference, bool isConst, ExpressionList *dimensions)
    1.13        : variable_(var),
    1.14          tag_(tag),
    1.15          reference_(reference),
    1.16 +        const_(isConst),
    1.17          dimensions_(dimensions)
    1.18      {
    1.19      }
    1.20 @@ -221,6 +223,9 @@
    1.21      ExpressionList *dimensions() const {
    1.22          return dimensions_;
    1.23      }
    1.24 +    bool isConst() const {
    1.25 +        return const_;
    1.26 +    }
    1.27  };
    1.28  
    1.29  class NameProxy : public Expression
     2.1 --- a/jitcraft/Messages.tbl	Mon Jul 09 21:40:20 2012 -0700
     2.2 +++ b/jitcraft/Messages.tbl	Mon Jul 09 22:25:52 2012 -0700
     2.3 @@ -45,4 +45,5 @@
     2.4  MSG(ForwardNotImplemented,          SyntaxError,            "forward '%s' not implemented")
     2.5  MSG(ForwardDeclareMismatches,       SyntaxError,            "declaration does not match original forward signature")
     2.6  MSG(ArrayCantBeReference,           SyntaxError,            "a parameter cannot be both an array and a reference")
     2.7 -MSG(ArraySizeMustBeConstant,        SyntaxError,            "array size must be a constant integer")
     2.8 \ No newline at end of file
     2.9 +MSG(ArraySizeMustBeConstant,        SyntaxError,            "array size must be a constant integer")
    2.10 +MSG(ReferenceIsConstant,            SyntaxError,            "cannot assign to a constant lvalue")
    2.11 \ No newline at end of file
     3.1 --- a/jitcraft/Parser.cpp	Mon Jul 09 21:40:20 2012 -0700
     3.2 +++ b/jitcraft/Parser.cpp	Mon Jul 09 22:25:52 2012 -0700
     3.3 @@ -894,6 +894,7 @@
     3.4          return NULL;
     3.5      if (!match(TOK_RPAREN)) {
     3.6          while (true) {
     3.7 +            bool isConst = match(TOK_CONST);
     3.8              bool ref = match(TOK_AMPERSAND);
     3.9  
    3.10              SourcePosition begin = scanner_.begin();
    3.11 @@ -931,7 +932,7 @@
    3.12                      return NULL;
    3.13              }
    3.14  
    3.15 -            Parameter *parameter = new (pool_) Parameter(arg, tag, ref, postDimensions);
    3.16 +            Parameter *parameter = new (pool_) Parameter(arg, tag, ref, isConst, postDimensions);
    3.17              if (!parameters->append(parameter))
    3.18                  return NULL;
    3.19  
     4.1 --- a/jitcraft/Scanner.cpp	Mon Jul 09 21:40:20 2012 -0700
     4.2 +++ b/jitcraft/Scanner.cpp	Mon Jul 09 22:25:52 2012 -0700
     4.3 @@ -369,6 +369,8 @@
     4.4          return TOK_FORWARD;
     4.5      if (literal_ == "public")
     4.6          return TOK_PUBLIC;
     4.7 +    if (literal_ == "const")
     4.8 +        return TOK_CONST;
     4.9      return (c == ':') ? TOK_LABEL : TOK_NAME;
    4.10  }
    4.11  
     5.1 --- a/jitcraft/SemanticAnalysis.cpp	Mon Jul 09 21:40:20 2012 -0700
     5.2 +++ b/jitcraft/SemanticAnalysis.cpp	Mon Jul 09 22:25:52 2012 -0700
     5.3 @@ -236,6 +236,11 @@
     5.4          if (!type)
     5.5              return NULL;
     5.6  
     5.7 +        if (type->isConst()) {
     5.8 +            cc_.reportError(expression->pos(), Message_ReferenceIsConstant);
     5.9 +            return NULL;
    5.10 +        }
    5.11 +
    5.12          return type->contained();
    5.13      }
    5.14  
    5.15 @@ -254,8 +259,13 @@
    5.16      if (!var->type())
    5.17          return NULL;
    5.18  
    5.19 -    if  (var->type()->isReference())
    5.20 +    if  (var->type()->isReference()) {
    5.21 +        if (var->type()->isConst()) {
    5.22 +            cc_.reportError(expression->pos(), Message_ReferenceIsConstant);
    5.23 +            return NULL;
    5.24 +        }
    5.25          return var->type()->contained();
    5.26 +    }
    5.27      return var->type();
    5.28  }
    5.29  
    5.30 @@ -479,7 +489,7 @@
    5.31                  size = init->elements();
    5.32              }
    5.33  
    5.34 -            type = zone_->types()->unsafeArray(type, size);
    5.35 +            type = zone_->types()->unsafeArray(type, size, false);
    5.36              if (!type)
    5.37                  return;
    5.38  
    5.39 @@ -547,7 +557,7 @@
    5.40                  return NULL;
    5.41              }
    5.42  
    5.43 -            type = zone_->types()->getReference(type);
    5.44 +            type = zone_->types()->getReference(type, parameter->isConst());
    5.45              if (!type)
    5.46                  return false;
    5.47          } else if (parameter->dimensions()) {
    5.48 @@ -570,7 +580,7 @@
    5.49                      size = box.toInt();
    5.50                  }
    5.51  
    5.52 -                type = Type::NewArray(zone_, type, size, false);
    5.53 +                type = zone_->types()->unsafeArray(type, size, parameter->isConst());
    5.54                  if (!type)
    5.55                      return false;
    5.56              }
     6.1 --- a/jitcraft/Token.h	Mon Jul 09 21:40:20 2012 -0700
     6.2 +++ b/jitcraft/Token.h	Mon Jul 09 22:25:52 2012 -0700
     6.3 @@ -56,6 +56,7 @@
     6.4      _(CONTINUE,           "<continue>")                 \
     6.5      _(FORWARD,            "<forward>")                  \
     6.6      _(PUBLIC,             "<public>")                   \
     6.7 +    _(CONST,              "<const>")                    \
     6.8      _(PLUS,               "+")                          \
     6.9      _(MINUS,              "-")                          \
    6.10      _(STAR,               "*")                          \
     7.1 --- a/jitcraft/TypeManager.cpp	Mon Jul 09 21:40:20 2012 -0700
     7.2 +++ b/jitcraft/TypeManager.cpp	Mon Jul 09 22:25:52 2012 -0700
     7.3 @@ -54,7 +54,7 @@
     7.4      Local<Type> type(zone_);
     7.5      for (unsigned i = 0; i < PrimitiveTypes_Total; i++) {
     7.6          type = primitiveTypes_[i];
     7.7 -        if ((referenceTypes_[i] = Type::NewReference(zone_, type)) == NULL)
     7.8 +        if ((referenceTypes_[i] = Type::NewReference(zone_, type, false)) == NULL)
     7.9              return false;
    7.10      }
    7.11  
    7.12 @@ -74,20 +74,20 @@
    7.13  }
    7.14  
    7.15  Type *
    7.16 -TypeManager::unsafeArray(Handle<Type> contained, int elements)
    7.17 +TypeManager::unsafeArray(Handle<Type> contained, int elements, bool isConst)
    7.18  {
    7.19      // :TODO: cache this.
    7.20 -    return Type::NewArray(zone_, contained, elements, false);
    7.21 +    return Type::NewArray(zone_, contained, elements, isConst);
    7.22  }
    7.23  
    7.24  Type *
    7.25 -TypeManager::getReference(Handle<Type> type)
    7.26 +TypeManager::getReference(Handle<Type> type, bool isConst)
    7.27  {
    7.28 -    if (type->isFloat() || (type->isInt32() && !type->tag()))
    7.29 +    if (!isConst && (type->isFloat() || (type->isInt32() && !type->tag())))
    7.30          return referenceTypes_[type->primitive()];
    7.31  
    7.32      // :TODO: cache this.
    7.33 -    return Type::NewReference(zone_, type);
    7.34 +    return Type::NewReference(zone_, type, isConst);
    7.35  }
    7.36  
    7.37  Type *
     8.1 --- a/jitcraft/TypeManager.h	Mon Jul 09 21:40:20 2012 -0700
     8.2 +++ b/jitcraft/TypeManager.h	Mon Jul 09 22:25:52 2012 -0700
     8.3 @@ -44,8 +44,8 @@
     8.4          return primitiveTypes_[type];
     8.5      }
     8.6  
     8.7 -    Type *getReference(Handle<Type> type);
     8.8 -    Type *unsafeArray(Handle<Type> contained, int elements);
     8.9 +    Type *getReference(Handle<Type> type, bool isConst);
    8.10 +    Type *unsafeArray(Handle<Type> contained, int elements, bool isConst);
    8.11      Type *newFunction(Handle<Type> returnType, Handle<FixedArray> args);
    8.12  
    8.13      void accept(VirtualObjectVisitor *visitor);
     9.1 --- a/jitcraft/Types.cpp	Mon Jul 09 21:40:20 2012 -0700
     9.2 +++ b/jitcraft/Types.cpp	Mon Jul 09 22:25:52 2012 -0700
     9.3 @@ -82,7 +82,7 @@
     9.4  }
     9.5  
     9.6  Type *
     9.7 -Type::NewReference(Zone *zone, Handle<Type> contained)
     9.8 +Type::NewReference(Zone *zone, Handle<Type> contained, bool isConst)
     9.9  {
    9.10      Local<Type> type(zone, Type::cast(zone->allocate(MapKind_Type, sizeof(Type), Heap::Tenure_Old)));
    9.11      if (!type)
    9.12 @@ -93,6 +93,7 @@
    9.13      type->contained_ = contained;
    9.14      type->returnType_ = NULL;
    9.15      type->parameters_ = NULL;
    9.16 +    type->const_ = isConst;
    9.17      return type;
    9.18  }
    9.19  
    10.1 --- a/jitcraft/Types.h	Mon Jul 09 21:40:20 2012 -0700
    10.2 +++ b/jitcraft/Types.h	Mon Jul 09 22:25:52 2012 -0700
    10.3 @@ -57,7 +57,7 @@
    10.4      static Type *NewPrimitive(Zone *zone, Handle<String> tag);
    10.5      static Type *NewPrimitive(Zone *zone, PrimitiveType type);
    10.6      static Type *NewArray(Zone *zone, Handle<Type> contained, int elements, bool isConst);
    10.7 -    static Type *NewReference(Zone *zone, Handle<Type> contained);
    10.8 +    static Type *NewReference(Zone *zone, Handle<Type> contained, bool isConst);
    10.9      static Type *NewFunction(Zone *zone, Handle<Type> returnType, Handle<FixedArray> parameters);
   10.10  
   10.11      static bool Compare(Type *left, Type *right);
   10.12 @@ -117,10 +117,6 @@
   10.13                 contained()->isPrimitive() &&
   10.14                 contained()->primitive() == PrimitiveType_Char;
   10.15      }
   10.16 -    bool isConst() {
   10.17 -        assert(isArray());
   10.18 -        return const_;
   10.19 -    }
   10.20      Type *returnType() {
   10.21          assert(isFunction());
   10.22          return returnType_;
   10.23 @@ -135,6 +131,10 @@
   10.24      bool isTraceable() {
   10.25          return !isPrimitive();
   10.26      }
   10.27 +    bool isConst() {
   10.28 +        assert(kind() == PRIMITIVE || kind() == ARRAY || kind() == REFERENCE);
   10.29 +        return const_;
   10.30 +    }
   10.31  
   10.32      // GC Descriptor.
   10.33      static inline size_t offsetOfContained() {
   10.34 @@ -156,10 +156,12 @@
   10.35      PrimitiveType primitive_;
   10.36      Barriered<String> tag_;
   10.37  
   10.38 +    // Primitives, arrays, and references.
   10.39 +    bool const_;
   10.40 +
   10.41      // Arrays only.
   10.42      int32 elements_;
   10.43      unsigned levels_;
   10.44 -    bool const_;
   10.45  
   10.46      // Arrays and references only.
   10.47      Barriered<Type> contained_;
    11.1 --- a/jitcraft/Zone.cpp	Mon Jul 09 21:40:20 2012 -0700
    11.2 +++ b/jitcraft/Zone.cpp	Mon Jul 09 22:25:52 2012 -0700
    11.3 @@ -93,7 +93,7 @@
    11.4          if (!type)
    11.5              return false;
    11.6  
    11.7 -        type = types_->unsafeArray(type, Type::UNKNOWN_SIZE);
    11.8 +        type = types_->unsafeArray(type, Type::UNKNOWN_SIZE, false);
    11.9          if (!type)
   11.10              return false;
   11.11  
   11.12 @@ -106,7 +106,7 @@
   11.13          if (!type)
   11.14              return false;
   11.15  
   11.16 -        type = types_->unsafeArray(type, Type::UNKNOWN_SIZE);
   11.17 +        type = types_->unsafeArray(type, Type::UNKNOWN_SIZE, false);
   11.18          if (!type)
   11.19              return false;
   11.20