src/compiler/Symbols.h
changeset 195 c1ba166f3fc4
child 196 9904633b1dfc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/compiler/Symbols.h	Sat Nov 17 18:41:01 2012 -0800
     1.3 @@ -0,0 +1,188 @@
     1.4 +/* vim: set ts=4 sw=4 tw=99 et:
     1.5 + *
     1.6 + * Copyright (C) 2012 David Anderson
     1.7 + *
     1.8 + * This file is part of SourcePawn.
     1.9 + *
    1.10 + * SourcePawn is free software: you can redistribute it and/or modify it under
    1.11 + * the terms of the GNU General Public License as published by the Free
    1.12 + * Software Foundation, either version 3 of the License, or (at your option)
    1.13 + * any later version.
    1.14 + * 
    1.15 + * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
    1.16 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.17 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    1.18 + *
    1.19 + * You should have received a copy of the GNU General Public License along with
    1.20 + * SourcePawn. If not, see http://www.gnu.org/licenses/.
    1.21 + */
    1.22 +#ifndef _include_sp2_symbol_h_
    1.23 +#define _include_sp2_symbol_h_
    1.24 +
    1.25 +#include "../PoolAllocator.h"
    1.26 +#include "../Handles.h"
    1.27 +#include "../Opcodes.h"
    1.28 +#include "../Types.h"
    1.29 +
    1.30 +namespace ke {
    1.31 +
    1.32 +class String;
    1.33 +
    1.34 +#define SYMBOL_KINDS(_) \
    1.35 +    /* Any kind of variable or argument produces a VariableSymbol. */ \
    1.36 +    _(Variable)         \
    1.37 +    /* A function declaration produces a FunctionSymbol. */ \
    1.38 +    _(Function)         \
    1.39 +    /* A named constant produces a ConstantSymbol. */ \
    1.40 +    _(Constant)         \
    1.41 +    /* A named type (class struct, typedef, etc) produces a TypeSymbol. */ \
    1.42 +    _(Type)             \
    1.43 +    /* // A module import produces a ModuleSymbol. */   \
    1.44 +    _(Module)
    1.45 +
    1.46 +#define _(name)     class name##Symbol;
    1.47 +SYMBOL_KINDS(_)
    1.48 +#undef _
    1.49 +
    1.50 +class Scope;
    1.51 +
    1.52 +// A symbol represents the declaration of a named entity.
    1.53 +class Symbol : public PoolObject
    1.54 +{
    1.55 +  public:
    1.56 +    enum Kind {
    1.57 +#       define _(name) k##name,
    1.58 +        SYMBOL_KINDS(_)
    1.59 +#       undef _
    1.60 +        kTotalSymbolKinds
    1.61 +    };
    1.62 +
    1.63 +  public:
    1.64 +    Symbol(Scope *scope, Handle<String> name, const SourcePosition &pos)
    1.65 +      : scope_(scope),
    1.66 +        name_(name),
    1.67 +        pos_(pos)
    1.68 +    {
    1.69 +    }
    1.70 +
    1.71 +    virtual Kind kind() const = 0;
    1.72 +
    1.73 +    Handle<String> name() const {
    1.74 +        return name_;
    1.75 +    }
    1.76 +    const SourcePosition &pos() const {
    1.77 +        return pos_;
    1.78 +    }
    1.79 +    Handle<Type> type() const {
    1.80 +        return type_;
    1.81 +    }
    1.82 +    Scope *scope() const {
    1.83 +        return scope_;
    1.84 +    }
    1.85 +
    1.86 +  public:
    1.87 +#define _(name)                                             \
    1.88 +    bool is##name() const {                                 \
    1.89 +        return (kind() == k##name);                         \
    1.90 +    }                                                       \
    1.91 +    name##Symbol *as##name() {                              \
    1.92 +        if (is##name())                                     \
    1.93 +            return to##name();                              \
    1.94 +        return NULL;                                        \
    1.95 +    }                                                       \
    1.96 +    name##Symbol *to##name() {                              \
    1.97 +        assert(is##name());                                 \
    1.98 +        return reinterpret_cast<name##Symbol *>(this);      \
    1.99 +    }
   1.100 +    SYMBOL_KINDS(_)
   1.101 +#undef _
   1.102 +
   1.103 +  private:
   1.104 +    Scope *scope_;
   1.105 +    ScopedRoot<String> name_;
   1.106 +    SourcePosition pos_;
   1.107 +
   1.108 +  protected:
   1.109 +    ScopedRoot<Type> type_;
   1.110 +};
   1.111 +
   1.112 +class VariableSymbol : public Symbol
   1.113 +{
   1.114 +  public:
   1.115 +    enum Storage {
   1.116 +        Unknown,
   1.117 +        Local,
   1.118 +        Heap
   1.119 +    };
   1.120 +
   1.121 +  public:
   1.122 +    VariableSymbol(Scope *scope, Handle<String> name, const SourcePosition &pos)
   1.123 +      : Symbol(scope, name, pos),
   1.124 +        storage_(Unknown)
   1.125 +    {
   1.126 +    }
   1.127 +
   1.128 +    VariableSymbol(Scope *scope, Handle<String> name, const SourcePosition &pos, Handle<Type> type)
   1.129 +      : Symbol(scope, name, pos),
   1.130 +        storage_(Unknown)
   1.131 +    {
   1.132 +        type_ = type;
   1.133 +    }
   1.134 +
   1.135 +    Kind kind() const {
   1.136 +        return kVariable;
   1.137 +    }
   1.138 +    void setType(Type *type) {
   1.139 +        type_ = type;
   1.140 +    }
   1.141 +    void allocate(Storage storage, unsigned slot) {
   1.142 +        storage_ = storage;
   1.143 +        slot_ = slot;
   1.144 +    }
   1.145 +    Storage storage() const {
   1.146 +        return storage_;
   1.147 +    }
   1.148 +    unsigned slot() const {
   1.149 +        assert(storage() != Unknown);
   1.150 +        return slot_;
   1.151 +    }
   1.152 +
   1.153 +  private:
   1.154 +    Storage storage_;
   1.155 +    unsigned slot_;
   1.156 +};
   1.157 +
   1.158 +class TypeSymbol : public Symbol
   1.159 +{
   1.160 +  public:
   1.161 +    TypeSymbol(Scope *scope, Handle<String> name, Handle<Type> type)
   1.162 +      : Symbol(scope, name, SourcePosition())
   1.163 +    {
   1.164 +        type_ = type;
   1.165 +    }
   1.166 +
   1.167 +    Kind kind() const {
   1.168 +        return kType;
   1.169 +    }
   1.170 +};
   1.171 +
   1.172 +class FunctionSymbol : public Symbol
   1.173 +{
   1.174 +  public:
   1.175 +    FunctionSymbol(Scope *scope, Handle<String> name, const SourcePosition &pos, Handle<FunctionType> type)
   1.176 +      : Symbol(scope, name, pos)
   1.177 +    {
   1.178 +        type_ = type;
   1.179 +    }
   1.180 +
   1.181 +    Kind kind() const {
   1.182 +        return kFunction;
   1.183 +    }
   1.184 +    FunctionType *type() const {
   1.185 +        return FunctionType::cast(type_);
   1.186 +    }
   1.187 +};
   1.188 +
   1.189 +}
   1.190 +
   1.191 +#endif // _include_sp2_symbol_h_