src/compiler/Symbols.h
author David Anderson <dvander@alliedmods.net>
Sat Nov 17 19:07:49 2012 -0800 (2012-11-17)
changeset 196 9904633b1dfc
parent 195 c1ba166f3fc4
child 209 5cbec15fadfe
permissions -rw-r--r--
Fix Linux build problems.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of SourcePawn.
     6  *
     7  * SourcePawn is free software: you can redistribute it and/or modify it under
     8  * the terms of the GNU General Public License as published by the Free
     9  * Software Foundation, either version 3 of the License, or (at your option)
    10  * any later version.
    11  * 
    12  * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
    13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along with
    17  * SourcePawn. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #ifndef _include_sp2_symbol_h_
    20 #define _include_sp2_symbol_h_
    21 
    22 #include "../PoolAllocator.h"
    23 #include "../Handles.h"
    24 #include "../Opcodes.h"
    25 #include "../Types.h"
    26 #include "../Strings.h"
    27 
    28 namespace ke {
    29 
    30 class String;
    31 
    32 #define SYMBOL_KINDS(_) \
    33     /* Any kind of variable or argument produces a VariableSymbol. */ \
    34     _(Variable)         \
    35     /* A function declaration produces a FunctionSymbol. */ \
    36     _(Function)         \
    37     /* A named constant produces a ConstantSymbol. */ \
    38     _(Constant)         \
    39     /* A named type (class struct, typedef, etc) produces a TypeSymbol. */ \
    40     _(Type)             \
    41     /* // A module import produces a ModuleSymbol. */   \
    42     _(Module)
    43 
    44 #define _(name)     class name##Symbol;
    45 SYMBOL_KINDS(_)
    46 #undef _
    47 
    48 class Scope;
    49 
    50 // A symbol represents the declaration of a named entity.
    51 class Symbol : public PoolObject
    52 {
    53   public:
    54     enum Kind {
    55 #       define _(name) k##name,
    56         SYMBOL_KINDS(_)
    57 #       undef _
    58         kTotalSymbolKinds
    59     };
    60 
    61   public:
    62     Symbol(Scope *scope, Handle<String> name, const SourcePosition &pos)
    63       : scope_(scope),
    64         name_(name),
    65         pos_(pos)
    66     {
    67     }
    68 
    69     virtual Kind kind() const = 0;
    70 
    71     Handle<String> name() const {
    72         return name_;
    73     }
    74     const SourcePosition &pos() const {
    75         return pos_;
    76     }
    77     Handle<Type> type() const {
    78         return type_;
    79     }
    80     Scope *scope() const {
    81         return scope_;
    82     }
    83 
    84   public:
    85 #define _(name)                                             \
    86     bool is##name() const {                                 \
    87         return (kind() == k##name);                         \
    88     }                                                       \
    89     name##Symbol *as##name() {                              \
    90         if (is##name())                                     \
    91             return to##name();                              \
    92         return NULL;                                        \
    93     }                                                       \
    94     name##Symbol *to##name() {                              \
    95         assert(is##name());                                 \
    96         return reinterpret_cast<name##Symbol *>(this);      \
    97     }
    98     SYMBOL_KINDS(_)
    99 #undef _
   100 
   101   private:
   102     Scope *scope_;
   103     ScopedRoot<String> name_;
   104     SourcePosition pos_;
   105 
   106   protected:
   107     ScopedRoot<Type> type_;
   108 };
   109 
   110 class VariableSymbol : public Symbol
   111 {
   112   public:
   113     enum Storage {
   114         Unknown,
   115         Local,
   116         Heap
   117     };
   118 
   119   public:
   120     VariableSymbol(Scope *scope, Handle<String> name, const SourcePosition &pos)
   121       : Symbol(scope, name, pos),
   122         storage_(Unknown)
   123     {
   124     }
   125 
   126     VariableSymbol(Scope *scope, Handle<String> name, const SourcePosition &pos, Handle<Type> type)
   127       : Symbol(scope, name, pos),
   128         storage_(Unknown)
   129     {
   130         type_ = type;
   131     }
   132 
   133     Kind kind() const {
   134         return kVariable;
   135     }
   136     void setType(Type *type) {
   137         type_ = type;
   138     }
   139     void allocate(Storage storage, unsigned slot) {
   140         storage_ = storage;
   141         slot_ = slot;
   142     }
   143     Storage storage() const {
   144         return storage_;
   145     }
   146     unsigned slot() const {
   147         assert(storage() != Unknown);
   148         return slot_;
   149     }
   150 
   151   private:
   152     Storage storage_;
   153     unsigned slot_;
   154 };
   155 
   156 class TypeSymbol : public Symbol
   157 {
   158   public:
   159     TypeSymbol(Scope *scope, Handle<String> name, Handle<Type> type)
   160       : Symbol(scope, name, SourcePosition())
   161     {
   162         type_ = type;
   163     }
   164 
   165     Kind kind() const {
   166         return kType;
   167     }
   168 };
   169 
   170 class FunctionSymbol : public Symbol
   171 {
   172   public:
   173     FunctionSymbol(Scope *scope, Handle<String> name, const SourcePosition &pos, Handle<FunctionType> type)
   174       : Symbol(scope, name, pos)
   175     {
   176         type_ = type;
   177     }
   178 
   179     Kind kind() const {
   180         return kFunction;
   181     }
   182     FunctionType *type() const {
   183         return FunctionType::cast(type_);
   184     }
   185 };
   186 
   187 }
   188 
   189 #endif // _include_sp2_symbol_h_