src/compiler/Symbols.h
author David Anderson <dvander@alliedmods.net>
Sat Nov 17 23:12:50 2012 -0800 (2012-11-17)
changeset 210 2f9997088e9b
parent 209 5cbec15fadfe
child 220 5df4a112967d
permissions -rw-r--r--
Added missing file.
     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         assert(!type_);
   138         type_ = type;
   139     }
   140     void allocate(Storage storage, unsigned slot) {
   141         storage_ = storage;
   142         slot_ = slot;
   143     }
   144     Storage storage() const {
   145         return storage_;
   146     }
   147     unsigned slot() const {
   148         assert(storage() != Unknown);
   149         return slot_;
   150     }
   151 
   152   private:
   153     Storage storage_;
   154     unsigned slot_;
   155 };
   156 
   157 class TypeSymbol : public Symbol
   158 {
   159   public:
   160     TypeSymbol(Scope *scope, Handle<String> name, Handle<Type> type)
   161       : Symbol(scope, name, SourcePosition())
   162     {
   163         type_ = type;
   164     }
   165 
   166     Kind kind() const {
   167         return kType;
   168     }
   169 };
   170 
   171 class FunctionSymbol : public Symbol
   172 {
   173   public:
   174     FunctionSymbol(Scope *scope, Handle<String> name, const SourcePosition &pos, Handle<FunctionType> type)
   175       : Symbol(scope, name, pos)
   176     {
   177         type_ = type;
   178     }
   179 
   180     Kind kind() const {
   181         return kFunction;
   182     }
   183     FunctionType *type() const {
   184         return FunctionType::cast(type_);
   185     }
   186 };
   187 
   188 }
   189 
   190 #endif // _include_sp2_symbol_h_