jitcraft/NameBinding.h
author David Anderson <dvander@alliedmods.net>
Sat Jul 07 20:28:29 2012 -0700 (2012-07-07)
changeset 54 8e22792a3921
parent 37 abb03d835b53
child 59 b7d61ddb3c8d
permissions -rw-r--r--
Convert Name and StringPool to a GC-safe symbol table.
     1 /*
     2  * Copyright (C) 2012 David Anderson
     3  *
     4  * This file is part of JITCraft.
     5  *
     6  * JITCraft is free software: you can redistribute it and/or modify it under
     7  * the terms of the GNU General Public License as published by the Free
     8  * Software Foundation, either version 3 of the License, or (at your option)
     9  * any later version.
    10  * 
    11  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along with
    16  * JITCraft. If not, see http://www.gnu.org/licenses/.
    17  */
    18 #ifndef _include_name_binding_h_
    19 #define _include_name_binding_h_
    20 
    21 #include "PoolAllocator.h"
    22 #include "Types.h"
    23 #include "Scanner.h"
    24 
    25 namespace jc {
    26 
    27 class Variable;
    28 class NamedFunction;
    29 class Scope;
    30 class Type;
    31 class NamedEnum;
    32 class NamedConstant;
    33 class String;
    34 
    35 class BoundName : public PoolObject
    36 {
    37     Scope *scope_;
    38     ScopedRoot<String> name_;
    39     ScopedRoot<Type> type_;
    40     SourcePosition pos_;
    41 
    42   public:
    43     enum Kind {
    44       VARIABLE,
    45       FUNCTION,
    46       ENUM,
    47       CONSTANT
    48     };
    49 
    50     BoundName(Scope *scope, Handle<String> name, const SourcePosition &pos);
    51 
    52     virtual Kind kind() const = 0;
    53 
    54     String *name() const {
    55         return name_;
    56     }
    57     Scope *scope() const {
    58         return scope_;
    59     }
    60     Variable *toVariable() {
    61         assert(kind() == VARIABLE);
    62         return reinterpret_cast<Variable *>(this);
    63     }
    64     NamedFunction *toNamedFunction() {
    65         assert(kind() == FUNCTION);
    66         return reinterpret_cast<NamedFunction *>(this);
    67     }
    68     NamedConstant *toNamedConstant() {
    69         assert(kind() == CONSTANT);
    70         return reinterpret_cast<NamedConstant *>(this);
    71     }
    72     Type *type() const {
    73         return type_;
    74     }
    75     void setType(Handle<Type> type) {
    76         type_ = type;
    77     }
    78     const SourcePosition &pos() {
    79         return pos_;
    80     }
    81 };
    82 
    83 class NamedFunction : public BoundName
    84 {
    85     unsigned slot_;
    86 
    87   public:
    88     NamedFunction(Scope *scope, Handle<String> name, const SourcePosition &pos);
    89 
    90     Kind kind() const {
    91         return FUNCTION;
    92     }
    93     void allocate(unsigned slot) {
    94         slot_ = slot;
    95     }
    96     unsigned slot() const {
    97         return slot_;
    98     }
    99 };
   100 
   101 class NamedEnum : public BoundName
   102 {
   103   public:
   104     NamedEnum(Scope *scope, Handle<String> name, const SourcePosition &pos)
   105       : BoundName(scope, name, pos)
   106     {
   107     }
   108 
   109     Kind kind() const {
   110         return ENUM;
   111     }
   112 };
   113 
   114 class NamedConstant : public BoundName
   115 {
   116     BoxedPrimitive value_;
   117     Type *type_;
   118 
   119   public:
   120     NamedConstant(Scope *scope, Handle<String> name, const SourcePosition &pos)
   121       : BoundName(scope, name, pos),
   122         type_(NULL)
   123     {
   124     }
   125 
   126     Kind kind() const {
   127         return CONSTANT;
   128     }
   129     void set(Type *type, const BoxedPrimitive &value) {
   130         value_ = value;
   131         type_ = type;
   132     }
   133     Type *type() const {
   134         return type_;
   135     }
   136     BoxedPrimitive value() const {
   137         return value_;
   138     }
   139 };
   140 
   141 class Variable : public BoundName
   142 {
   143   public:
   144     enum Location {
   145         UNALLOCATED,
   146         STACK,
   147         PARAMETER
   148     };
   149 
   150     Variable(Scope *scope, Handle<String> name, const SourcePosition &pos);
   151 
   152     Kind kind() const {
   153         return VARIABLE;
   154     }
   155     bool unallocated() const {
   156         return location() == UNALLOCATED;
   157     }
   158     void allocate(Location location, unsigned slot) {
   159         location_ = location;
   160         slot_ = slot;
   161     }
   162     Location location() const {
   163         return location_;
   164     }
   165     unsigned slot() const {
   166         assert(!unallocated());
   167         return slot_;
   168     }
   169 
   170   private:
   171     Location location_;
   172     unsigned slot_;
   173 };
   174 
   175 }
   176 
   177 #endif // _include_name_binding_h_