src/compiler/AST.h
author David Anderson <dvander@alliedmods.net>
Sun Jan 06 13:52:21 2013 -0800 (2013-01-06)
changeset 256 3c184218462d
parent 251 ca99a81745fe
child 258 241d082d6d89
permissions -rw-r--r--
Initial implementation of module imports.
[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_sourcepawn_ast_h_
[email protected]
    20
#define _include_sourcepawn_ast_h_
[email protected]
    21
[email protected]
    22
#include "../PoolAllocator.h"
[email protected]
    23
#include "../Vector.h"
[email protected]
    24
#include "Token.h"
[email protected]
    25
#include "NameBinding.h"
[email protected]
    26
#include "Symbols.h"
[email protected]
    27
[email protected]
    28
namespace ke {
[email protected]
    29
[email protected]
    30
class Scope;
[email protected]
    31
class Importable;
[email protected]
    32
struct PathComponent;
[email protected]
    33
[email protected]
    34
#define ASTKINDS(_)             \
[email protected]
    35
    _(VariableDeclaration)      \
[email protected]
    36
    _(ForStatement)             \
[email protected]
    37
    _(ReturnStatement)          \
[email protected]
    38
    _(IntegerLiteral)           \
[email protected]
    39
    _(FloatLiteral)             \
[email protected]
    40
    _(StringLiteral)            \
[email protected]
    41
    _(BinaryExpression)         \
[email protected]
    42
    _(BlockStatement)           \
[email protected]
    43
    _(Assignment)               \
[email protected]
    44
    _(NameProxy)                \
[email protected]
    45
    _(ExpressionStatement)      \
[email protected]
    46
    _(FunctionStatement)        \
[email protected]
    47
    _(CallExpression)           \
[email protected]
    48
    _(IfStatement)              \
[email protected]
    49
    _(IndexExpression)          \
[email protected]
    50
    _(FieldExpression)          \
[email protected]
    51
    _(EnumStatement)            \
[email protected]
    52
    _(WhileStatement)           \
[email protected]
    53
    _(BreakStatement)           \
[email protected]
    54
    _(ContinueStatement)        \
[email protected]
    55
    _(FunctionTypeStatement)    \
[email protected]
    56
    _(IncDecExpression)         \
[email protected]
    57
    _(UnaryExpression)          \
[email protected]
    58
    _(TernaryExpression)        \
[email protected]
    59
    _(BooleanLiteral)           \
[email protected]
    60
    _(StructureStatement)       \
[email protected]
    61
    _(SwitchStatement)          \
[email protected]
    62
    _(ArrayLiteral)             \
[email protected]
    63
    _(ImportStatement)
[email protected]
    64
[email protected]
    65
// Forward declarations.
[email protected]
    66
#define _(name) class name;
[email protected]
    67
ASTKINDS(_)
[email protected]
    68
#undef _
[email protected]
    69
[email protected]
    70
class AstVisitor;
[email protected]
    71
[email protected]
    72
// Interface for AST nodes.
[email protected]
    73
class AstNode : public PoolObject
[email protected]
    74
{
[email protected]
    75
    SourcePosition pos_;
[email protected]
    76
[email protected]
    77
  public:
[email protected]
    78
    enum Kind {
[email protected]
    79
#       define _(name) k##name,
[email protected]
    80
        ASTKINDS(_)
[email protected]
    81
#       undef _
[email protected]
    82
        AstKind_Invalid
[email protected]
    83
    };
[email protected]
    84
[email protected]
    85
    AstNode(const SourcePosition &pos)
[email protected]
    86
      : pos_(pos)
[email protected]
    87
    {
[email protected]
    88
    }
[email protected]
    89
    
[email protected]
    90
    virtual Kind kind() const = 0;
[email protected]
    91
[email protected]
    92
#define _(name)     bool is##name() { return kind() == k##name; }                   \
[email protected]
    93
                    name *to##name() { assert(is##name()); return (name *)this; }   \
[email protected]
    94
                    name *as##name() { if (!is##name()) return NULL; return to##name(); }
[email protected]
    95
    ASTKINDS(_)
[email protected]
    96
#undef _
[email protected]
    97
[email protected]
    98
    virtual void accept(AstVisitor *visitor) = 0;
[email protected]
    99
[email protected]
   100
    const SourcePosition &pos() const {
[email protected]
   101
        return pos_;
[email protected]
   102
    }
[email protected]
   103
};
[email protected]
   104
[email protected]
   105
#define DECLARE_NODE(type)                      \
[email protected]
   106
    Kind kind() const {                         \
[email protected]
   107
        return k##type;                         \
[email protected]
   108
    }                                           \
[email protected]
   109
    void accept(AstVisitor *visitor) {          \
[email protected]
   110
        visitor->visit##type(this);             \
[email protected]
   111
    }
[email protected]
   112
[email protected]
   113
class AstVisitor
[email protected]
   114
{
[email protected]
   115
  public:
[email protected]
   116
#define _(name) virtual void visit##name(name *node) { assert(false); }
[email protected]
   117
    ASTKINDS(_)
[email protected]
   118
#undef _
[email protected]
   119
};
[email protected]
   120
[email protected]
   121
class Statement : public AstNode
[email protected]
   122
{
[email protected]
   123
  public:
[email protected]
   124
    Statement(const SourcePosition &pos)
[email protected]
   125
      : AstNode(pos)
[email protected]
   126
    {
[email protected]
   127
    }
[email protected]
   128
};
[email protected]
   129
[email protected]
   130
class Expression : public AstNode
[email protected]
   131
{
[email protected]
   132
  public:
[email protected]
   133
    Expression(const SourcePosition &pos)
[email protected]
   134
      : AstNode(pos)
[email protected]
   135
    {
[email protected]
   136
    }
[email protected]
   137
};
[email protected]
   138
[email protected]
   139
typedef PoolList<Statement *> StatementList;
[email protected]
   140
typedef PoolList<Expression *> ExpressionList;
[email protected]
   141
[email protected]
   142
class VariableDeclaration : public Statement
[email protected]
   143
{
[email protected]
   144
    TypeQualifiers qual_;
[email protected]
   145
    ScopedRoot<String> name_;
[email protected]
   146
    Expression *initialization_;
[email protected]
   147
    ExpressionList *dims_;
[email protected]
   148
    Expression *type_;
[email protected]
   149
    VariableSymbol *sym_;
[email protected]
   150
[email protected]
   151
  public:
[email protected]
   152
    VariableDeclaration(const SourcePosition &pos, TypeQualifiers qual, Handle<String> name,
dvand[email protected]
   153
                        Expression *type, ExpressionList *dims, Expression *initialization)
[email protected]
   154
      : Statement(pos),
[email protected]
   155
        qual_(qual),
[email protected]
   156
        name_(name),
[email protected]
   157
        initialization_(initialization),
[email protected]
   158
        dims_(dims),
[email protected]
   159
        type_(type),
[email protected]
   160
        sym_(NULL)
[email protected]
   161
    {
[email protected]
   162
    }
[email protected]
   163
[email protected]
   164
    DECLARE_NODE(VariableDeclaration);
[email protected]
   165
[email protected]
   166
    Expression *initialization() const {
[email protected]
   167
        return initialization_;
[email protected]
   168
    }
[email protected]
   169
    Handle<String> name() {
[email protected]
   170
        return name_;
[email protected]
   171
    }
[email protected]
   172
    Expression *declType() const {
[email protected]
   173
        return type_;
[email protected]
   174
    }
[email protected]
   175
    void setSymbol(VariableSymbol *sym) {
[email protected]
   176
        assert(!sym_);
[email protected]
   177
        sym_ = sym;
[email protected]
   178
    }
[email protected]
   179
    VariableSymbol *sym() const {
[email protected]
   180
        return sym_;
[email protected]
   181
    }
[email protected]
   182
    Expression *type() const {
[email protected]
   183
        return type_;
[email protected]
   184
    }
[email protected]
   185
    ExpressionList *dims() const {
[email protected]
   186
        return dims_;
[email protected]
   187
    }
[email protected]
   188
    TypeQualifiers quals() const {
[email protected]
   189
        return qual_;
[email protected]
   190
    }
[email protected]
   191
};
[email protected]
   192
[email protected]
   193
class Parameter : public PoolObject
[email protected]
   194
{
[email protected]
   195
    ScopedRoot<String> name_;
[email protected]
   196
    Expression *type_;
[email protected]
   197
    bool reference_;
[email protected]
   198
    Expression *initializer_;
[email protected]
   199
    VariableSymbol *sym_;
[email protected]
   200
    SourcePosition pos_;
[email protected]
   201
    ExpressionList *dims_;
[email protected]
   202
    TypeQualifiers quals_;
[email protected]
   203
[email protected]
   204
  public:
[email protected]
   205
    Parameter(Handle<String> name, Expression *type, bool reference, TypeQualifiers quals,
[email protected]
   206
              ExpressionList *dims, Expression *initializer, const SourcePosition &pos)
[email protected]
   207
      : name_(name),
[email protected]
   208
        type_(type),
[email protected]
   209
        reference_(reference),
[email protected]
   210
        initializer_(initializer),
[email protected]
   211
        sym_(NULL),
[email protected]
   212
        pos_(pos),
[email protected]
   213
        dims_(dims),
[email protected]
   214
        quals_(quals)
[email protected]
   215
    {
[email protected]
   216
    }
[email protected]
   217
[email protected]
   218
    Expression *type() const {
[email protected]
   219
        return type_;
[email protected]
   220
    }
[email protected]
   221
    Handle<String> name() const {
[email protected]
   222
        return name_;
[email protected]
   223
    }
[email protected]
   224
    bool reference() const {
[email protected]
   225
        return reference_;
[email protected]
   226
    }
[email protected]
   227
    TypeQualifiers quals() const {
[email protected]
   228
        return quals_;
[email protected]
   229
    }
[email protected]
   230
    ExpressionList *dims() const {
[email protected]
   231
        return dims_;
[email protected]
   232
    }
[email protected]
   233
    Expression *initializer() const {
[email protected]
   234
        return initializer_;
[email protected]
   235
    }
[email protected]
   236
    void setSymbol(VariableSymbol *sym) {
[email protected]
   237
        assert(!sym_);
[email protected]
   238
        sym_ = sym;
[email protected]
   239
    }
[email protected]
   240
    VariableSymbol *sym() const {
[email protected]
   241
        return sym_;
[email protected]
   242
    }
[email protected]
   243
    const SourcePosition &pos() const {
[email protected]
   244
        return pos_;
[email protected]
   245
    }
[email protected]
   246
};
[email protected]
   247
[email protected]
   248
class NameProxy : public Expression
[email protected]
   249
{
[email protected]
   250
    ScopedRoot<String> name_;
[email protected]
   251
    Symbol *binding_;
[email protected]
   252
[email protected]
   253
  public:
[email protected]
   254
    NameProxy(const SourcePosition &pos, Handle<String> name)
[email protected]
   255
      : Expression(pos),
[email protected]
   256
        name_(name),
[email protected]
   257
        binding_(NULL)
[email protected]
   258
    {
[email protected]
   259
    }
[email protected]
   260
[email protected]
   261
    DECLARE_NODE(NameProxy);
[email protected]
   262
    
[email protected]
   263
    Handle<String> name() const {
[email protected]
   264
        return name_;
[email protected]
   265
    }
[email protected]
   266
    Symbol *sym() const {
[email protected]
   267
        return binding_;
[email protected]
   268
    }
[email protected]
   269
    void bind(Symbol *sym) {
[email protected]
   270
        binding_ = sym;
[email protected]
   271
    }
[email protected]
   272
};
[email protected]
   273
[email protected]
   274
class BooleanLiteral : public Expression
[email protected]
   275
{
[email protected]
   276
    TokenKind token_;
[email protected]
   277
[email protected]
   278
  public:
[email protected]
   279
    BooleanLiteral(const SourcePosition &pos, TokenKind token)
[email protected]
   280
      : Expression(pos),
[email protected]
   281
        token_(token)
[email protected]
   282
    {
[email protected]
   283
    }
[email protected]
   284
[email protected]
   285
    DECLARE_NODE(BooleanLiteral);
[email protected]
   286
    TokenKind token() const {
[email protected]
   287
        return token_;
[email protected]
   288
    }
[email protected]
   289
};
[email protected]
   290
[email protected]
   291
class IntegerLiteral : public Expression
[email protected]
   292
{
[email protected]
   293
    int value_;
[email protected]
   294
[email protected]
   295
  public:
[email protected]
   296
    IntegerLiteral(const SourcePosition &pos, int value)
[email protected]
   297
      : Expression(pos),
[email protected]
   298
        value_(value)
[email protected]
   299
    {
[email protected]
   300
    }
[email protected]
   301
[email protected]
   302
    DECLARE_NODE(IntegerLiteral);
[email protected]
   303
[email protected]
   304
    int value() const {
[email protected]
   305
        return value_;
[email protected]
   306
    }
[email protected]
   307
};
[email protected]
   308
[email protected]
   309
class FloatLiteral : public Expression
[email protected]
   310
{
[email protected]
   311
    double value_;
[email protected]
   312
[email protected]
   313
  public:
[email protected]
   314
    FloatLiteral(const SourcePosition &pos, double value)
[email protected]
   315
      : Expression(pos),
[email protected]
   316
        value_(value)
[email protected]
   317
    {
[email protected]
   318
    }
[email protected]
   319
[email protected]
   320
    DECLARE_NODE(FloatLiteral);
[email protected]
   321
[email protected]
   322
    double value() const {
[email protected]
   323
        return value_;
[email protected]
   324
    }
[email protected]
   325
};
[email protected]
   326
[email protected]
   327
class StringLiteral : public Expression
[email protected]
   328
{
[email protected]
   329
    ScopedRoot<String> string_;
[email protected]
   330
[email protected]
   331
  public:
[email protected]
   332
    StringLiteral(const SourcePosition &pos, String *string)
[email protected]
   333
      : Expression(pos),
[email protected]
   334
        string_(string)
[email protected]
   335
    {
[email protected]
   336
    }
[email protected]
   337
[email protected]
   338
    DECLARE_NODE(StringLiteral);
[email protected]
   339
[email protected]
   340
    String *string() const {
[email protected]
   341
        return string_;
[email protected]
   342
    }
[email protected]
   343
};
[email protected]
   344
[email protected]
   345
class ArrayLiteral : public Expression
[email protected]
   346
{
[email protected]
   347
    TokenKind token_;
[email protected]
   348
    ExpressionList *expressions_;
[email protected]
   349
[email protected]
   350
  public:
[email protected]
   351
    ArrayLiteral(const SourcePosition &pos, TokenKind token, ExpressionList *expressions)
[email protected]
   352
      : Expression(pos),
[email protected]
   353
        token_(token),
[email protected]
   354
        expressions_(expressions)
[email protected]
   355
    {
[email protected]
   356
    }
[email protected]
   357
[email protected]
   358
    DECLARE_NODE(ArrayLiteral);
[email protected]
   359
[email protected]
   360
    TokenKind token() const {
[email protected]
   361
        return token_;
[email protected]
   362
    }
[email protected]
   363
    ExpressionList *expressions() const {
[email protected]
   364
        return expressions_;
[email protected]
   365
    }
[email protected]
   366
    bool isFixed() const {
[email protected]
   367
        return token_ == TOK_LBRACE;
[email protected]
   368
    }
[email protected]
   369
};
[email protected]
   370
[email protected]
   371
class UnaryExpression : public Expression
[email protected]
   372
{
[email protected]
   373
    Expression *expression_;
[email protected]
   374
    TokenKind token_;
[email protected]
   375
    NameProxy *tag_;
[email protected]
   376
[email protected]
   377
  public:
[email protected]
   378
    UnaryExpression(const SourcePosition &pos, TokenKind token, Expression *expr)
[email protected]
   379
      : Expression(pos),
[email protected]
   380
        expression_(expr),
[email protected]
   381
        token_(token),
[email protected]
   382
        tag_(NULL)
[email protected]
   383
    {
[email protected]
   384
    }
[email protected]
   385
[email protected]
   386
    UnaryExpression(const SourcePosition &pos, TokenKind token, Expression *expr,
[email protected]
   387
                    NameProxy *tag)
[email protected]
   388
      : Expression(pos),
[email protected]
   389
        expression_(expr),
[email protected]
   390
        token_(token),
[email protected]
   391
        tag_(tag)
[email protected]
   392
    {
[email protected]
   393
    }
[email protected]
   394
[email protected]
   395
    DECLARE_NODE(UnaryExpression);
[email protected]
   396
[email protected]
   397
    Expression *expression() const {
[email protected]
   398
        return expression_;
[email protected]
   399
    }
[email protected]
   400
    TokenKind token() const {
[email protected]
   401
        return token_;
[email protected]
   402
    }
[email protected]
   403
    NameProxy *tag() const {
[email protected]
   404
        return tag_;
[email protected]
   405
    }
[email protected]
   406
};
[email protected]
   407
[email protected]
   408
class BinaryExpression : public Expression
[email protected]
   409
{
[email protected]
   410
    Expression *left_;
[email protected]
   411
    Expression *right_;
[email protected]
   412
    TokenKind token_;
[email protected]
   413
[email protected]
   414
  public:
[email protected]
   415
    BinaryExpression(const SourcePosition &pos, TokenKind token, Expression *left, Expression *right)
[email protected]
   416
      : Expression(pos),
[email protected]
   417
        left_(left),
[email protected]
   418
        right_(right),
[email protected]
   419
        token_(token)
[email protected]
   420
    {
[email protected]
   421
    }
[email protected]
   422
[email protected]
   423
    DECLARE_NODE(BinaryExpression);
[email protected]
   424
[email protected]
   425
    Expression *left() const {
[email protected]
   426
        return left_;
[email protected]
   427
    }
[email protected]
   428
    Expression *right() const {
[email protected]
   429
        return right_;
[email protected]
   430
    }
[email protected]
   431
    TokenKind token() const {
[email protected]
   432
        return token_;
[email protected]
   433
    }
[email protected]
   434
};
[email protected]
   435
[email protected]
   436
class TernaryExpression : public Expression
[email protected]
   437
{
[email protected]
   438
    Expression *condition_;
[email protected]
   439
    Expression *left_;
[email protected]
   440
    Expression *right_;
[email protected]
   441
[email protected]
   442
  public:
[email protected]
   443
    TernaryExpression(const SourcePosition &pos, Expression *condition, Expression *left, Expression *right)
[email protected]
   444
      : Expression(pos),
[email protected]
   445
        condition_(condition),
[email protected]
   446
        left_(left),
[email protected]
   447
        right_(right)
[email protected]
   448
    {
[email protected]
   449
    }
[email protected]
   450
[email protected]
   451
    DECLARE_NODE(TernaryExpression);
[email protected]
   452
[email protected]
   453
    Expression *condition() const {
[email protected]
   454
        return condition_;
[email protected]
   455
    }
[email protected]
   456
    Expression *left() const {
[email protected]
   457
        return left_;
[email protected]
   458
    }
[email protected]
   459
    Expression *right() const {
[email protected]
   460
        return right_;
[email protected]
   461
    }
[email protected]
   462
};
[email protected]
   463
[email protected]
   464
class IndexExpression : public Expression
[email protected]
   465
{
[email protected]
   466
    Expression *left_;
[email protected]
   467
    Expression *right_;
[email protected]
   468
[email protected]
   469
  public:
[email protected]
   470
    IndexExpression(const SourcePosition &pos, Expression *left, Expression *right)
[email protected]
   471
      : Expression(pos),
[email protected]
   472
        left_(left),
[email protected]
   473
        right_(right)
[email protected]
   474
    {
[email protected]
   475
    }
[email protected]
   476
[email protected]
   477
    DECLARE_NODE(IndexExpression);
[email protected]
   478
[email protected]
   479
    Expression *left() const {
[email protected]
   480
        return left_;
[email protected]
   481
    }
[email protected]
   482
    Expression *right() const {
[email protected]
   483
        return right_;
[email protected]
   484
    }
[email protected]
   485
};
[email protected]
   486
[email protected]
   487
class FieldExpression : public Expression
[email protected]
   488
{
[email protected]
   489
    Expression *left_;
[email protected]
   490
    ScopedRoot<String> field_;
[email protected]
   491
[email protected]
   492
  public:
[email protected]
   493
    FieldExpression(const SourcePosition &pos, Expression *left, Handle<String> field)
[email protected]
   494
      : Expression(pos),
[email protected]
   495
        left_(left),
[email protected]
   496
        field_(field)
[email protected]
   497
    {
[email protected]
   498
    }
[email protected]
   499
[email protected]
   500
    DECLARE_NODE(FieldExpression);
[email protected]
   501
[email protected]
   502
    Expression *left() const {
[email protected]
   503
        return left_;
[email protected]
   504
    }
[email protected]
   505
    Handle<String> field() const {
[email protected]
   506
        return field_;
[email protected]
   507
    }
[email protected]
   508
};
[email protected]
   509
[email protected]
   510
class CallExpression : public Expression
[email protected]
   511
{
[email protected]
   512
    Expression *callee_;
[email protected]
   513
    ExpressionList *arguments_;
[email protected]
   514
[email protected]
   515
  public:
[email protected]
   516
    CallExpression(const SourcePosition &pos, Expression *callee, ExpressionList *arguments)
[email protected]
   517
      : Expression(pos),
[email protected]
   518
        callee_(callee),
[email protected]
   519
        arguments_(arguments)
[email protected]
   520
    {
[email protected]
   521
    }
[email protected]
   522
[email protected]
   523
    DECLARE_NODE(CallExpression);
[email protected]
   524
[email protected]
   525
    Expression *callee() const {
[email protected]
   526
        return callee_;
[email protected]
   527
    }
[email protected]
   528
    ExpressionList *arguments() const {
[email protected]
   529
        return arguments_;
[email protected]
   530
    }
[email protected]
   531
};
[email protected]
   532
[email protected]
   533
class ForStatement : public Statement
[email protected]
   534
{
[email protected]
   535
    Statement *initialization_;
[email protected]
   536
    Expression *condition_;
[email protected]
   537
    Statement *update_;
[email protected]
   538
    Statement *body_;
[email protected]
   539
    Scope *scope_;
[email protected]
   540
[email protected]
   541
  public:
[email protected]
   542
    ForStatement(const SourcePosition &pos, Statement *initialization,
[email protected]
   543
                 Expression *condition, Statement *update, Statement *body)
[email protected]
   544
      : Statement(pos),
[email protected]
   545
        initialization_(initialization),
[email protected]
   546
        condition_(condition),
[email protected]
   547
        update_(update),
[email protected]
   548
        body_(body),
[email protected]
   549
        scope_(NULL)
[email protected]
   550
    {
[email protected]
   551
    }
[email protected]
   552
[email protected]
   553
    DECLARE_NODE(ForStatement);
[email protected]
   554
[email protected]
   555
    Statement *initialization() const {
[email protected]
   556
        return initialization_;
[email protected]
   557
    }
[email protected]
   558
    Expression *condition() const {
[email protected]
   559
        return condition_;
[email protected]
   560
    }
[email protected]
   561
    Statement *update() const {
[email protected]
   562
        return update_;
[email protected]
   563
    }
[email protected]
   564
    Statement *body() const {
[email protected]
   565
        return body_;
[email protected]
   566
    }
[email protected]
   567
    Scope *scope() const {
[email protected]
   568
        return scope_;
[email protected]
   569
    }
[email protected]
   570
    void setScope(Scope *scope) {
[email protected]
   571
        scope_ = scope;
[email protected]
   572
    }
[email protected]
   573
};
[email protected]
   574
[email protected]
   575
class WhileStatement : public Statement
[email protected]
   576
{
[email protected]
   577
    TokenKind token_;
[email protected]
   578
    Expression *condition_;
[email protected]
   579
    Statement *body_;
dvande[email protected]
   580
[email protected]
   581
  public:
[email protected]
   582
    WhileStatement(const SourcePosition &pos, TokenKind kind, Expression *condition, Statement *body)
[email protected]
   583
      : Statement(pos),
[email protected]
   584
        token_(kind),
[email protected]
   585
        condition_(condition),
[email protected]
   586
        body_(body)
[email protected]
   587
    {
[email protected]
   588
    }
[email protected]
   589
    
[email protected]
   590
    DECLARE_NODE(WhileStatement);
[email protected]
   591
[email protected]
   592
    TokenKind token() const {
[email protected]
   593
        return token_;
[email protected]
   594
    }
[email protected]
   595
    Expression *condition() const {
[email protected]
   596
        return condition_;
[email protected]
   597
    }
[email protected]
   598
    Statement *body() const {
[email protected]
   599
        return body_;
[email protected]
   600
    }
[email protected]
   601
};
[email protected]
   602
[email protected]
   603
class ReturnStatement : public Statement
[email protected]
   604
{
[email protected]
   605
    Expression *expression_;
[email protected]
   606
[email protected]
   607
  public:
[email protected]
   608
    ReturnStatement(const SourcePosition &pos, Expression *expression)
[email protected]
   609
      : Statement(pos),
[email protected]
   610
        expression_(expression)
[email protected]
   611
    {
[email protected]
   612
    }
[email protected]
   613
[email protected]
   614
    DECLARE_NODE(ReturnStatement);
[email protected]
   615
[email protected]
   616
    Expression *expression() const {
[email protected]
   617
        return expression_;
[email protected]
   618
    }
[email protected]
   619
};
[email protected]
   620
[email protected]
   621
class BreakStatement : public Statement
[email protected]
   622
{
[email protected]
   623
  public:
[email protected]
   624
    BreakStatement(const SourcePosition &pos)
[email protected]
   625
      : Statement(pos)
[email protected]
   626
    {
[email protected]
   627
    }
[email protected]
   628
[email protected]
   629
    DECLARE_NODE(BreakStatement);
[email protected]
   630
};
[email protected]
   631
[email protected]
   632
class ContinueStatement : public Statement
[email protected]
   633
{
[email protected]
   634
  public:
[email protected]
   635
    ContinueStatement(const SourcePosition &pos)
[email protected]
   636
      : Statement(pos)
[email protected]
   637
    {
[email protected]
   638
    }
[email protected]
   639
[email protected]
   640
    DECLARE_NODE(ContinueStatement);
[email protected]
   641
};
[email protected]
   642
[email protected]
   643
class Assignment : public Expression
[email protected]
   644
{
[email protected]
   645
    TokenKind token_;
[email protected]
   646
    Expression *lvalue_;
[email protected]
   647
    Expression *expression_;
[email protected]
   648
[email protected]
   649
  public:
[email protected]
   650
    Assignment(const SourcePosition &pos, TokenKind token, Expression *left, Expression *right)
[email protected]
   651
      : Expression(pos),
[email protected]
   652
        token_(token),
[email protected]
   653
        lvalue_(left),
[email protected]
   654
        expression_(right)
[email protected]
   655
    {
[email protected]
   656
    }
[email protected]
   657
[email protected]
   658
    DECLARE_NODE(Assignment);
[email protected]
   659
[email protected]
   660
    TokenKind token() const {
[email protected]
   661
        return token_;
[email protected]
   662
    }
[email protected]
   663
    Expression *lvalue() const {
[email protected]
   664
        return lvalue_;
[email protected]
   665
    }
[email protected]
   666
    Expression *expression() const {
[email protected]
   667
        return expression_;
[email protected]
   668
    }
[email protected]
   669
};
[email protected]
   670
[email protected]
   671
class ExpressionStatement : public Statement
[email protected]
   672
{
[email protected]
   673
    Expression *expression_;
[email protected]
   674
[email protected]
   675
  public:
[email protected]
   676
    ExpressionStatement(Expression *expression)
[email protected]
   677
      : Statement(expression->pos()),
[email protected]
   678
        expression_(expression)
[email protected]
   679
    {
[email protected]
   680
    }
[email protected]
   681
[email protected]
   682
    DECLARE_NODE(ExpressionStatement);
[email protected]
   683
[email protected]
   684
    Expression *expression() const {
[email protected]
   685
        return expression_;
[email protected]
   686
    }
[email protected]
   687
};
[email protected]
   688
[email protected]
   689
class BlockStatement : public Statement
[email protected]
   690
{
[email protected]
   691
    StatementList *statements_;
[email protected]
   692
    Scope *scope_;
[email protected]
   693
    TokenKind type_;
[email protected]
   694
[email protected]
   695
  public:
[email protected]
   696
    BlockStatement(const SourcePosition &pos, StatementList *statements, TokenKind kind)
[email protected]
   697
      : Statement(pos),
[email protected]
   698
        statements_(statements),
[email protected]
   699
        scope_(NULL),
[email protected]
   700
        type_(kind)
[email protected]
   701
    {
[email protected]
   702
    }
[email protected]
   703
[email protected]
   704
    DECLARE_NODE(BlockStatement);
[email protected]
   705
[email protected]
   706
    StatementList *statements() const {
[email protected]
   707
        return statements_;
[email protected]
   708
    }
[email protected]
   709
    Scope *scope() const {
[email protected]
   710
        return scope_;
[email protected]
   711
    }
[email protected]
   712
    void setScope(Scope *scope) {
[email protected]
   713
        scope_ = scope;
[email protected]
   714
    }
[email protected]
   715
    TokenKind type() const {
[email protected]
   716
        return type_;
[email protected]
   717
    }
[email protected]
   718
};
[email protected]
   719
[email protected]
   720
typedef PoolList<Parameter *> ParameterList;
[email protected]
   721
[email protected]
   722
class FunctionSignature
[email protected]
   723
{
[email protected]
   724
  public:
[email protected]
   725
    FunctionSignature(Expression *returnType, ParameterList *parameters, bool variadic)
[email protected]
   726
      : returnType_(returnType),
[email protected]
   727
        parameters_(parameters),
[email protected]
   728
        variadic_(variadic)
[email protected]
   729
    {
[email protected]
   730
    }
[email protected]
   731
[email protected]
   732
    Expression *returnType() const {
[email protected]
   733
        return returnType_;
[email protected]
   734
    }
[email protected]
   735
    ParameterList *parameters() const {
[email protected]
   736
        return parameters_;
[email protected]
   737
    }
[email protected]
   738
    bool variadic() const {
[email protected]
   739
        return variadic_;
[email protected]
   740
    }
[email protected]
   741
[email protected]
   742
  private:
[email protected]
   743
    Expression *returnType_;
[email protected]
   744
    ParameterList *parameters_;
[email protected]
   745
    bool variadic_;
[email protected]
   746
};
[email protected]
   747
[email protected]
   748
class FunctionTypeStatement : public Statement
[email protected]
   749
{
[email protected]
   750
  public:
[email protected]
   751
    FunctionTypeStatement(const SourcePosition &pos, Handle<String> name, const FunctionSignature &signature)
[email protected]
   752
      : Statement(pos),
[email protected]
   753
        name_(name),
[email protected]
   754
        signature_(signature)
[email protected]
   755
    {
[email protected]
   756
    }
[email protected]
   757
[email protected]
   758
    DECLARE_NODE(FunctionTypeStatement);
[email protected]
   759
[email protected]
   760
    Handle<String> name() const {
[email protected]
   761
        return name_;
[email protected]
   762
    }
[email protected]
   763
    const FunctionSignature &signature() const {
[email protected]
   764
        return signature_;
[email protected]
   765
    }
[email protected]
   766
[email protected]
   767
  private:
[email protected]
   768
    ScopedRoot<String> name_;
[email protected]
   769
    FunctionSignature signature_;
[email protected]
   770
    
[email protected]
   771
};
[email protected]
   772
[email protected]
   773
class FunctionStatement : public Statement
[email protected]
   774
{
[email protected]
   775
    ScopedRoot<String> name_;
[email protected]
   776
    TokenKind kind_;
[email protected]
   777
    Statement *body_;
[email protected]
   778
    FunctionSignature signature_;
[email protected]
   779
    bool explicitReturn_;
[email protected]
   780
    FunctionSymbol *sym_;
[email protected]
   781
    Scope *argScope_;
[email protected]
   782
    Scope *varScope_;
[email protected]
   783
[email protected]
   784
  public:
[email protected]
   785
    FunctionStatement(const SourcePosition &pos, Handle<String> name, TokenKind kind,
[email protected]
   786
                      Statement *body, const FunctionSignature &signature, bool explicitReturn)
[email protected]
   787
      : Statement(pos),
[email protected]
   788
        name_(name),
[email protected]
   789
        kind_(kind),
[email protected]
   790
        body_(body),
[email protected]
   791
        signature_(signature),
[email protected]
   792
        explicitReturn_(explicitReturn),
[email protected]
   793
        sym_(NULL),
[email protected]
   794
        argScope_(NULL),
[email protected]
   795
        varScope_(NULL)
[email protected]
   796
    {
[email protected]
   797
    }
[email protected]
   798
[email protected]
   799
    DECLARE_NODE(FunctionStatement);
[email protected]
   800
[email protected]
   801
    Handle<String> name() const {
[email protected]
   802
        return name_;
[email protected]
   803
    }
[email protected]
   804
    Statement *body() const {
[email protected]
   805
        return body_;
[email protected]
   806
    }
[email protected]
   807
    const FunctionSignature &signature() {
[email protected]
   808
        return signature_;
[email protected]
   809
    }
[email protected]
   810
    bool hasExplicitReturn() const {
[email protected]
   811
        return explicitReturn_;
[email protected]
   812
    }
[email protected]
   813
    TokenKind token() const {
[email protected]
   814
        return kind_;
[email protected]
   815
    }
[email protected]
   816
    void setSymbol(FunctionSymbol *sym) {
[email protected]
   817
        assert(!sym_);
[email protected]
   818
        sym_ = sym;
[email protected]
   819
    }
[email protected]
   820
    FunctionSymbol *sym() const {
[email protected]
   821
        return sym_;
[email protected]
   822
    }
[email protected]
   823
    void setScopes(Scope *argScope, Scope *varScope) {
[email protected]
   824
        argScope_ = argScope;
[email protected]
   825
        varScope_ = varScope;
[email protected]
   826
    }
[email protected]
   827
    Scope *argScope() const {
[email protected]
   828
        return argScope_;
[email protected]
   829
    }
dva[email protected]
   830
    Scope *varScope() const {
[email protected]
   831
        return varScope_;
[email protected]
   832
    }
[email protected]
   833
};
[email protected]
   834
[email protected]
   835
class IfStatement : public Statement
[email protected]
   836
{
[email protected]
   837
    Expression *condition_;
[email protected]
   838
    Statement *ifTrue_;
[email protected]
   839
    Statement *ifFalse_;
[email protected]
   840
[email protected]
   841
  public:
[email protected]
   842
    IfStatement(const SourcePosition &pos, Expression *condition, Statement *ifTrue)
[email protected]
   843
      : Statement(pos),
[email protected]
   844
        condition_(condition),
[email protected]
   845
        ifTrue_(ifTrue),
[email protected]
   846
        ifFalse_(NULL)
[email protected]
   847
    {
[email protected]
   848
    }
[email protected]
   849
[email protected]
   850
    DECLARE_NODE(IfStatement);
[email protected]
   851
[email protected]
   852
    Expression *condition() const {
[email protected]
   853
        return condition_;
[email protected]
   854
    }
[email protected]
   855
    Statement *ifTrue() const {
[email protected]
   856
        return ifTrue_;
[email protected]
   857
    }
[email protected]
   858
    Statement *ifFalse() const {
[email protected]
   859
        return ifFalse_;
[email protected]
   860
    }
[email protected]
   861
    void setIfFalse(Statement *ifFalse) {
[email protected]
   862
        ifFalse_ = ifFalse;
[email protected]
   863
    }
[email protected]
   864
};
[email protected]
   865
[email protected]
   866
class EnumStatement : public Statement
[email protected]
   867
{
[email protected]
   868
    ScopedRoot<String> name_;
[email protected]
   869
    TypeSymbol *sym_;
[email protected]
   870
[email protected]
   871
  public:
[email protected]
   872
    struct Entry {
[email protected]
   873
        ScopedRoot<String> name;
[email protected]
   874
        Expression *expr;
[email protected]
   875
        SourcePosition pos;
[email protected]
   876
[email protected]
   877
        Entry(Handle<String> name, Expression *expr, const SourcePosition &pos)
[email protected]
   878
          : name(name),
[email protected]
   879
            expr(expr),
[email protected]
   880
            pos(pos)
[email protected]
   881
        {
[email protected]
   882
        }
[email protected]
   883
    };
[email protected]
   884
[email protected]
   885
    typedef PoolList<Entry> EntryList;
[email protected]
   886
[email protected]
   887
  public:
[email protected]
   888
    EnumStatement(const SourcePosition &pos, Handle<String> name, EntryList *entries)
[email protected]
   889
      : Statement(pos),
[email protected]
   890
        name_(name),
[email protected]
   891
        sym_(NULL),
[email protected]
   892
        entries_(entries)
[email protected]
   893
    {
[email protected]
   894
    }
[email protected]
   895
[email protected]
   896
    DECLARE_NODE(EnumStatement);
[email protected]
   897
[email protected]
   898
    String *name() const {
[email protected]
   899
        return name_;
[email protected]
   900
    }
[email protected]
   901
    EntryList *entries() const {
[email protected]
   902
        return entries_;
[email protected]
   903
    }
[email protected]
   904
    void setSymbol(TypeSymbol *sym) {
[email protected]
   905
        assert(!sym_);
[email protected]
   906
        sym_ = sym;
[email protected]
   907
    }
[email protected]
   908
    TypeSymbol *sym() const {
[email protected]
   909
        return sym_;
[email protected]
   910
    }
[email protected]
   911
[email protected]
   912
  private:
[email protected]
   913
    EntryList *entries_;
[email protected]
   914
};
[email protected]
   915
[email protected]
   916
class IncDecExpression : public Expression
[email protected]
   917
{
[email protected]
   918
    TokenKind token_;
[email protected]
   919
    Expression *expression_;
[email protected]
   920
    bool postfix_;
[email protected]
   921
[email protected]
   922
  public:
[email protected]
   923
    IncDecExpression(const SourcePosition &pos, TokenKind token, Expression *expression, bool postfix)
[email protected]
   924
      : Expression(pos),
[email protected]
   925
        token_(token),
[email protected]
   926
        expression_(expression),
[email protected]
   927
        postfix_(postfix)
[email protected]
   928
    {
[email protected]
   929
    }
[email protected]
   930
[email protected]
   931
    DECLARE_NODE(IncDecExpression);
[email protected]
   932
[email protected]
   933
    TokenKind token() const {
[email protected]
   934
        return token_;
[email protected]
   935
    }
[email protected]
   936
    Expression *expression() const {
[email protected]
   937
        return expression_;
[email protected]
   938
    }
[email protected]
   939
    bool postfix() const {
[email protected]
   940
        return postfix_;
[email protected]
   941
    }
[email protected]
   942
};
[email protected]
   943
[email protected]
   944
class Case : public PoolObject
[email protected]
   945
{
[email protected]
   946
  public:
[email protected]
   947
    Case(Expression *expression, ExpressionList *others, Statement *statement)
[email protected]
   948
      : expression_(expression),
[email protected]
   949
        others_(others),
[email protected]
   950
        statement_(statement)
[email protected]
   951
    {
[email protected]
   952
    }
[email protected]
   953
[email protected]
   954
    Expression *expression() const {
[email protected]
   955
        return expression_;
[email protected]
   956
    }
[email protected]
   957
    PoolList<Expression *> *others() const {
[email protected]
   958
        return others_;
[email protected]
   959
    }
[email protected]
   960
    Statement *statement() const {
[email protected]
   961
        return statement_;
[email protected]
   962
    }
[email protected]
   963
[email protected]
   964
  private:
[email protected]
   965
    Expression *expression_;
[email protected]
   966
    PoolList <Expression *> *others_;
[email protected]
   967
    Statement *statement_;
[email protected]
   968
};
[email protected]
   969
[email protected]
   970
struct CaseValue
[email protected]
   971
{
[email protected]
   972
    BoxedPrimitive box;
[email protected]
   973
    size_t statement;
[email protected]
   974
[email protected]
   975
    CaseValue(size_t statement)
[email protected]
   976
      : statement(statement)
[email protected]
   977
    {
[email protected]
   978
    }
[email protected]
   979
};
[email protected]
   980
[email protected]
   981
typedef PoolList<CaseValue> CaseValueList;
[email protected]
   982
[email protected]
   983
class SwitchStatement : public Statement
[email protected]
   984
{
[email protected]
   985
  public:
[email protected]
   986
    SwitchStatement(const SourcePosition &pos, Expression *expression, PoolList<Case *> *cases,
[email protected]
   987
                    Statement *def)
[email protected]
   988
      : Statement(pos),
[email protected]
   989
        expression_(expression),
[email protected]
   990
        cases_(cases),
[email protected]
   991
        default_(def),
[email protected]
   992
        table_(NULL)
[email protected]
   993
    {
[email protected]
   994
    }
[email protected]
   995
[email protected]
   996
    DECLARE_NODE(SwitchStatement);
[email protected]
   997
[email protected]
   998
    Expression *expression() const {
[email protected]
   999
        return expression_;
[email protected]
  1000
    }
[email protected]
  1001
    PoolList<Case *> *cases() const {
[email protected]
  1002
        return cases_;
[email protected]
  1003
    }
[email protected]
  1004
    Statement *defaultCase() const {
[email protected]
  1005
        return default_;
[email protected]
  1006
    }
[email protected]
  1007
    void setCaseValueList(CaseValueList *list) {
[email protected]
  1008
        table_ = list;
[email protected]
  1009
    }
[email protected]
  1010
    CaseValueList *caseValueList() const {
[email protected]
  1011
        return table_;
[email protected]
  1012
    }
[email protected]
  1013
[email protected]
  1014
  private:
[email protected]
  1015
    Expression *expression_;
[email protected]
  1016
    PoolList<Case *> *cases_;
[email protected]
  1017
    Statement *default_;
[email protected]
  1018
    CaseValueList *table_;
[email protected]
  1019
};
[email protected]
  1020
[email protected]
  1021
class StructureStatement : public Statement
[email protected]
  1022
{
[email protected]
  1023
  public:
[email protected]
  1024
    class Field : public PoolObject
[email protected]
  1025
    {
[email protected]
  1026
        SourcePosition pos_;
[email protected]
  1027
        Expression *type_;
[email protected]
  1028
        ScopedRoot<String> name_;
[email protected]
  1029
[email protected]
  1030
      public:
[email protected]
  1031
        Field(const SourcePosition &pos,
[email protected]
  1032
              Expression *type,
[email protected]
  1033
              Handle<String> name)
[email protected]
  1034
          : pos_(pos),
[email protected]
  1035
            type_(type),
[email protected]
  1036
            name_(name)
[email protected]
  1037
        {
[email protected]
  1038
        }
[email protected]
  1039
[email protected]
  1040
        const SourcePosition &pos() const {
[email protected]
  1041
            return pos_;
[email protected]
  1042
        }
[email protected]
  1043
        Handle<String> name() const {
[email protected]
  1044
            return name_;
[email protected]
  1045
        }
[email protected]
  1046
        Expression *type() const {
[email protected]
  1047
            return type_;
[email protected]
  1048
        }
[email protected]
  1049
    };
[email protected]
  1050
[email protected]
  1051
    typedef PoolList<StructureStatement::Field *> FieldList;
[email protected]
  1052
[email protected]
  1053
  public:
[email protected]
  1054
    StructureStatement(const SourcePosition &pos, Handle<String> name, FieldList *fields)
[email protected]
  1055
      : Statement(pos),
[email protected]
  1056
        name_(name),
[email protected]
  1057
        fields_(fields),
[email protected]
  1058
        sym_(NULL)
[email protected]
  1059
    {
[email protected]
  1060
    }
[email protected]
  1061
[email protected]
  1062
    DECLARE_NODE(StructureStatement);
[email protected]
  1063
[email protected]
  1064
    FieldList *fields() const {
[email protected]
  1065
        return fields_;
[email protected]
  1066
    }
[email protected]
  1067
    Handle<String> name() const {
[email protected]
  1068
        return name_;
[email protected]
  1069
    }
[email protected]
  1070
    void setSymbol(TypeSymbol *sym) {
[email protected]
  1071
        assert(!sym_);
[email protected]
  1072
        sym_ = sym;
[email protected]
  1073
    }
[email protected]
  1074
    TypeSymbol *sym() const {
[email protected]
  1075
        return sym_;
[email protected]
  1076
    }
[email protected]
  1077
[email protected]
  1078
  private:
[email protected]
  1079
    ScopedRoot<String> name_;
[email protected]
  1080
    FieldList *fields_;
[email protected]
  1081
    TypeSymbol *sym_;
[email protected]
  1082
};
[email protected]
  1083
[email protected]
  1084
typedef PoolList<ScopedRoot<String> *> NameList;
[email protected]
  1085
[email protected]
  1086
class ImportStatement : public Statement
[email protected]
  1087
{
[email protected]
  1088
  public:
[email protected]
  1089
    ImportStatement(const SourcePosition &pos, PathComponent *path)
[email protected]
  1090
      : Statement(pos),
[email protected]
  1091
        path_(path)
[email protected]
  1092
    {
[email protected]
  1093
    }
[email protected]
  1094
[email protected]
  1095
    DECLARE_NODE(ImportStatement);
[email protected]
  1096
[email protected]
  1097
    PathComponent *path() const {
[email protected]
  1098
        return path_;
[email protected]
  1099
    }
[email protected]
  1100
    void setSource(Importable *source) {
[email protected]
  1101
        source_ = source;
[email protected]
  1102
    }
[email protected]
  1103
    Handle<Importable> source() {
[email protected]
  1104
        return source_;
[email protected]
  1105
    }
[email protected]
  1106
    unsigned importIndex() {
[email protected]
  1107
        return importIndex_;
[email protected]
  1108
    }
[email protected]
  1109
    void setImportIndex(unsigned index) {
[email protected]
  1110
        importIndex_ = index;
[email protected]
  1111
    }
[email protected]
  1112
[email protected]
  1113
  private:
[email protected]
  1114
    PathComponent *path_;
[email protected]
  1115
    ScopedRoot<Importable> source_;
[email protected]
  1116
    unsigned importIndex_;
[email protected]
  1117
};
[email protected]
  1118
[email protected]
  1119
typedef StructureStatement::FieldList FieldList;
[email protected]
  1120
typedef PoolList<NameProxy *> NameProxyList;
[email protected]
  1121
[email protected]
  1122
#undef DECLARE_NODE
[email protected]
  1123
[email protected]
  1124
class ParseTree : public PoolObject
[email protected]
  1125
{
[email protected]
  1126
    StatementList *statements_;
[email protected]
  1127
    NameProxyList *unbound_;
[email protected]
  1128
    PoolList<ImportStatement *> *imports_;
[email protected]
  1129
[email protected]
  1130
  public:
[email protected]
  1131
    ParseTree(StatementList *statements, NameProxyList *unbound, PoolList<ImportStatement *> *imports)
[email protected]
  1132
      : statements_(statements),
[email protected]
  1133
        unbound_(unbound),
[email protected]
  1134
        imports_(imports)
[email protected]
  1135
    {
[email protected]
  1136
    }
[email protected]
  1137
[email protected]
  1138
    void dump(FILE *fp);
[email protected]
  1139
[email protected]
  1140
    StatementList *statements() const {
[email protected]
  1141
        return statements_;
[email protected]
  1142
    }
[email protected]
  1143
    NameProxyList *unbound() const {
[email protected]
  1144
        return unbound_;
[email protected]
  1145
    }
[email protected]
  1146
    PoolList<ImportStatement *> *imports() const {
[email protected]
  1147
        return imports_;
[email protected]
  1148
    }
[email protected]
  1149
};
[email protected]
  1150
[email protected]
  1151
}
[email protected]
  1152
[email protected]
  1153
#endif // _include_sourcepawn_ast_h_