jitcraft/NameBinding.h
author David Anderson <dvander@alliedmods.net>
Mon Jun 25 02:57:28 2012 -0700 (2012-06-25)
changeset 0 c455ebb9a5dc
child 1 27f12e2fc369
permissions -rw-r--r--
Initial import of prototype.
     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 "StringPool.h"
    23 
    24 namespace jc {
    25 
    26 class Variable;
    27 class NamedFunction;
    28 class Scope;
    29 class Type;
    30 
    31 class BoundName : public PoolObject
    32 {
    33     Name *name_;
    34     Scope *scope_;
    35     Type *type_;
    36 
    37   public:
    38     enum Kind {
    39       VARIABLE,
    40       FUNCTION
    41     };
    42 
    43     BoundName(Scope *scope, Name *name);
    44 
    45     virtual Kind kind() const = 0;
    46 
    47     Name *name() const {
    48         return name_;
    49     }
    50     Scope  *scope() const {
    51         return scope_;
    52     }
    53     Variable *toVariable() {
    54         assert(kind() == VARIABLE);
    55         return reinterpret_cast<Variable *>(this);
    56     }
    57     NamedFunction *toNamedFunction() {
    58         assert(kind() == FUNCTION);
    59         return reinterpret_cast<NamedFunction *>(this);
    60     }
    61     Type *type() const {
    62         return type_;
    63     }
    64     void setType(Type *type) {
    65         type_ = type;
    66     }
    67 };
    68 
    69 class NamedFunction : public BoundName
    70 {
    71     unsigned slot_;
    72 
    73   public:
    74     NamedFunction(Scope *scope, Name *name);
    75 
    76     Kind kind() const {
    77         return FUNCTION;
    78     }
    79     void allocate(unsigned slot) {
    80         slot_ = slot;
    81     }
    82     unsigned slot() const {
    83         return slot_;
    84     }
    85 };
    86 
    87 class Variable : public BoundName
    88 {
    89   public:
    90     enum Location {
    91         UNALLOCATED,
    92         STACK,
    93         PARAMETER
    94     };
    95 
    96     Variable(Scope *scope, Name *name);
    97 
    98     Kind kind() const {
    99         return VARIABLE;
   100     }
   101     bool unallocated() const {
   102         return location() == UNALLOCATED;
   103     }
   104     void allocate(Location location, unsigned slot) {
   105         location_ = location;
   106         slot_ = slot;
   107     }
   108     Location location() const {
   109         return location_;
   110     }
   111     unsigned slot() const {
   112         assert(!unallocated());
   113         return slot_;
   114     }
   115 
   116   private:
   117     Location location_;
   118     unsigned slot_;
   119 };
   120 
   121 }
   122 
   123 #endif // _include_name_binding_h_