jitcraft/NameBinding.h
author David Anderson <dvander@alliedmods.net>
Tue Jul 24 23:53:34 2012 -0700 (2012-07-24)
changeset 86 32ad0523b309
parent 85 06a0015b6a5c
permissions -rw-r--r--
Renamed jitcraft/ to src/, fixed some /Wp64 warnings.
     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 ke {
    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 
    41   protected:
    42     SourcePosition pos_;
    43 
    44   public:
    45     enum Kind {
    46       VARIABLE,
    47       FUNCTION,
    48       ENUM,
    49       CONSTANT
    50     };
    51 
    52     BoundName(Scope *scope, Handle<String> name, const SourcePosition &pos);
    53 
    54     virtual Kind kind() const = 0;
    55 
    56     String *name() const {
    57         return name_;
    58     }
    59     Scope *scope() const {
    60         return scope_;
    61     }
    62     Variable *toVariable() {
    63         assert(kind() == VARIABLE);
    64         return reinterpret_cast<Variable *>(this);
    65     }
    66     NamedFunction *toNamedFunction() {
    67         assert(kind() == FUNCTION);
    68         return reinterpret_cast<NamedFunction *>(this);
    69     }
    70     NamedConstant *toNamedConstant() {
    71         assert(kind() == CONSTANT);
    72         return reinterpret_cast<NamedConstant *>(this);
    73     }
    74     Type *type() const {
    75         return type_;
    76     }
    77     void setType(Handle<Type> type) {
    78         type_ = type;
    79     }
    80     const SourcePosition &pos() {
    81         return pos_;
    82     }
    83 };
    84 
    85 class NamedFunction : public BoundName
    86 {
    87     unsigned slot_;
    88     TokenKind token_;
    89     ScopedRoot<Type> forwardType_;
    90     bool isVariadicNative_;
    91 
    92   public:
    93     NamedFunction(Scope *scope, Handle<String> name, const SourcePosition &pos, TokenKind token);
    94 
    95     Kind kind() const {
    96         return FUNCTION;
    97     }
    98     void allocate(unsigned slot) {
    99         slot_ = slot;
   100     }
   101     bool isForward() const {
   102         return token_ == TOK_FORWARD;
   103     }
   104     bool isPublic() const {
   105         return token_ == TOK_PUBLIC;
   106     }
   107     bool isNative() const {
   108         return token_ == TOK_NATIVE;
   109     }
   110     void setForwardType(Handle<Type> type) {
   111         forwardType_ = type;
   112     }
   113     Type *forwardType() {
   114         return forwardType_;
   115     }
   116     void setImplemented(const SourcePosition &at) {
   117         token_ = TOK_PUBLIC;
   118         pos_ = at;
   119     }
   120     void markNativeVariadic() {
   121         assert(token_ == TOK_NATIVE);
   122         isVariadicNative_ = true;
   123     }
   124     bool isNativeVariadic() {
   125         assert(token_ == TOK_NATIVE);
   126         return isVariadicNative_;
   127     }
   128     unsigned slot() const {
   129         return slot_;
   130     }
   131 };
   132 
   133 class NamedEnum : public BoundName
   134 {
   135   public:
   136     NamedEnum(Scope *scope, Handle<String> name, const SourcePosition &pos)
   137       : BoundName(scope, name, pos)
   138     {
   139     }
   140 
   141     Kind kind() const {
   142         return ENUM;
   143     }
   144 };
   145 
   146 class NamedConstant : public BoundName
   147 {
   148     BoxedPrimitive value_;
   149     Type *type_;
   150 
   151   public:
   152     NamedConstant(Scope *scope, Handle<String> name, const SourcePosition &pos)
   153       : BoundName(scope, name, pos),
   154         type_(NULL)
   155     {
   156     }
   157 
   158     Kind kind() const {
   159         return CONSTANT;
   160     }
   161     void set(Type *type, const BoxedPrimitive &value) {
   162         value_ = value;
   163         type_ = type;
   164     }
   165     Type *type() const {
   166         return type_;
   167     }
   168     BoxedPrimitive value() const {
   169         return value_;
   170     }
   171 };
   172 
   173 class Variable : public BoundName
   174 {
   175   public:
   176     enum Location {
   177         UNALLOCATED,
   178         STACK,
   179         PARAMETER,
   180         ENVIRONMENT
   181     };
   182 
   183     Variable(Scope *scope, Handle<String> name, const SourcePosition &pos);
   184 
   185     Kind kind() const {
   186         return VARIABLE;
   187     }
   188     bool unallocated() const {
   189         return location() == UNALLOCATED;
   190     }
   191     void allocate(Location location, unsigned slot) {
   192         location_ = location;
   193         slot_ = slot;
   194     }
   195     Location location() const {
   196         return location_;
   197     }
   198     unsigned slot() const {
   199         assert(!unallocated());
   200         return slot_;
   201     }
   202     bool needsHeapAllocation();
   203 
   204   private:
   205     Location location_;
   206     unsigned slot_;
   207 };
   208 
   209 }
   210 
   211 #endif // _include_name_binding_h_