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