src/compiler/Symbols.h
author David Anderson <dvander@alliedmods.net>
Sun Jan 06 13:52:21 2013 -0800 (2013-01-06)
changeset 256 3c184218462d
parent 233 bbb498ccb4ac
child 258 241d082d6d89
permissions -rw-r--r--
Initial implementation of module imports.
     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     /* An import name. */ \
    42     _(Import)
    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         Arg,
   117         Heap
   118     };
   119 
   120   public:
   121     VariableSymbol(Scope *scope, Handle<String> name, const SourcePosition &pos)
   122       : Symbol(scope, name, pos),
   123         storage_(Unknown)
   124     {
   125     }
   126 
   127     VariableSymbol(Scope *scope, Handle<String> name, const SourcePosition &pos, Handle<Type> type)
   128       : Symbol(scope, name, pos),
   129         storage_(Unknown)
   130     {
   131         type_ = type;
   132     }
   133 
   134     Kind kind() const {
   135         return kVariable;
   136     }
   137     void setType(Type *type) {
   138         assert(!type_);
   139         type_ = type;
   140     }
   141     void allocate(Storage storage, unsigned slot) {
   142         storage_ = storage;
   143         slot_ = slot;
   144     }
   145     Storage storage() const {
   146         return storage_;
   147     }
   148     unsigned slot() const {
   149         assert(storage() != Unknown);
   150         return slot_;
   151     }
   152 
   153   private:
   154     Storage storage_;
   155     unsigned slot_;
   156 };
   157 
   158 class TypeSymbol : public Symbol
   159 {
   160   public:
   161     TypeSymbol(Scope *scope, Handle<String> name, Handle<Type> type)
   162       : Symbol(scope, name, SourcePosition())
   163     {
   164         type_ = type;
   165     }
   166 
   167     Kind kind() const {
   168         return kType;
   169     }
   170 };
   171 
   172 class FunctionSymbol : public Symbol
   173 {
   174   public:
   175     FunctionSymbol(Scope *scope, Handle<String> name, const SourcePosition &pos, Handle<FunctionType> type)
   176       : Symbol(scope, name, pos)
   177     {
   178         type_ = type;
   179     }
   180 
   181     Kind kind() const {
   182         return kFunction;
   183     }
   184     FunctionType *type() const {
   185         return FunctionType::cast(type_);
   186     }
   187 };
   188 
   189 class ConstantSymbol : public Symbol
   190 {
   191   public:
   192     ConstantSymbol(Scope *scope, Handle<String> name, const SourcePosition &pos, Handle<Type> type,
   193                    const BoxedPrimitive &prim)
   194       : Symbol(scope, name, pos),
   195         value_(prim)
   196     {
   197         type_ = type;
   198     }
   199 
   200     Kind kind() const {
   201         return kConstant;
   202     }
   203     const BoxedPrimitive &value() const {
   204         return value_;
   205     }
   206 
   207   private:
   208     BoxedPrimitive value_;
   209 };
   210 
   211 class ImportSymbol : public Symbol
   212 {
   213   public:
   214     ImportSymbol(Scope *scope, Handle<String> name, const SourcePosition &pos, Handle<Type> type,
   215                  unsigned index)
   216       : Symbol(scope, name, pos),
   217         index_(index)
   218     {
   219         type_ = type;
   220     }
   221 
   222     Kind kind() const {
   223         return kImport;
   224     }
   225     unsigned index() const {
   226         return index_;
   227     }
   228 
   229   private:
   230       unsigned index_;
   231 };
   232 
   233 }
   234 
   235 #endif // _include_sp2_symbol_h_