src/compiler/AST.h
changeset 195 c1ba166f3fc4
parent 191 9d17f29fe16a
child 196 9904633b1dfc
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/compiler/AST.h	Sat Nov 17 18:41:01 2012 -0800
     1.3 @@ -0,0 +1,1155 @@
     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_sourcepawn_ast_h_
    1.23 +#define _include_sourcepawn_ast_h_
    1.24 +
    1.25 +#include "../PoolAllocator.h"
    1.26 +#include "../Vector.h"
    1.27 +#include "Token.h"
    1.28 +#include "NameBinding.h"
    1.29 +#include "Symbols.h"
    1.30 +#include "../QualType.h"
    1.31 +
    1.32 +namespace ke {
    1.33 +
    1.34 +class Scope;
    1.35 +
    1.36 +#define ASTKINDS(_)             \
    1.37 +    _(VariableDeclaration)      \
    1.38 +    _(ForStatement)             \
    1.39 +    _(ReturnStatement)          \
    1.40 +    _(IntegerLiteral)           \
    1.41 +    _(FloatLiteral)             \
    1.42 +    _(StringLiteral)            \
    1.43 +    _(BinaryExpression)         \
    1.44 +    _(BlockStatement)           \
    1.45 +    _(Assignment)               \
    1.46 +    _(NameProxy)                \
    1.47 +    _(ExpressionStatement)      \
    1.48 +    _(FunctionStatement)        \
    1.49 +    _(CallExpression)           \
    1.50 +    _(IfStatement)              \
    1.51 +    _(IndexExpression)          \
    1.52 +    _(FieldExpression)          \
    1.53 +    _(EnumStatement)            \
    1.54 +    _(WhileStatement)           \
    1.55 +    _(BreakStatement)           \
    1.56 +    _(ContinueStatement)        \
    1.57 +    _(FunctionTypeStatement)    \
    1.58 +    _(IncDecExpression)         \
    1.59 +    _(UnaryExpression)          \
    1.60 +    _(TernaryExpression)        \
    1.61 +    _(BooleanLiteral)           \
    1.62 +    _(StructureStatement)       \
    1.63 +    _(SwitchStatement)          \
    1.64 +    _(ArrayLiteral)             \
    1.65 +	_(ImportStatement)
    1.66 +
    1.67 +// Forward declarations.
    1.68 +#define _(name) class name;
    1.69 +ASTKINDS(_)
    1.70 +#undef _
    1.71 +
    1.72 +class AstVisitor;
    1.73 +
    1.74 +// Interface for AST nodes.
    1.75 +class AstNode : public PoolObject
    1.76 +{
    1.77 +    SourcePosition pos_;
    1.78 +
    1.79 +  public:
    1.80 +    enum Kind {
    1.81 +#       define _(name) k##name,
    1.82 +        ASTKINDS(_)
    1.83 +#       undef _
    1.84 +        AstKind_Invalid
    1.85 +    };
    1.86 +
    1.87 +    AstNode(const SourcePosition &pos)
    1.88 +      : pos_(pos)
    1.89 +    {
    1.90 +    }
    1.91 +    
    1.92 +    virtual Kind kind() const = 0;
    1.93 +
    1.94 +#define _(name)     bool is##name() { return kind() == k##name; }                   \
    1.95 +                    name *to##name() { assert(is##name()); return (name *)this; }   \
    1.96 +                    name *as##name() { if (!is##name()) return NULL; return to##name(); }
    1.97 +    ASTKINDS(_)
    1.98 +#undef _
    1.99 +
   1.100 +    virtual void accept(AstVisitor *visitor) = 0;
   1.101 +
   1.102 +    const SourcePosition &pos() const {
   1.103 +        return pos_;
   1.104 +    }
   1.105 +};
   1.106 +
   1.107 +#define DECLARE_NODE(type)                      \
   1.108 +    Kind kind() const {                         \
   1.109 +        return k##type;                         \
   1.110 +    }                                           \
   1.111 +    void accept(AstVisitor *visitor) {          \
   1.112 +        visitor->visit##type(this);             \
   1.113 +    }
   1.114 +
   1.115 +class AstVisitor
   1.116 +{
   1.117 +  public:
   1.118 +#define _(name) virtual void visit##name(name *node) { assert(false); }
   1.119 +    ASTKINDS(_)
   1.120 +#undef _
   1.121 +};
   1.122 +
   1.123 +class Statement : public AstNode
   1.124 +{
   1.125 +  public:
   1.126 +    Statement(const SourcePosition &pos)
   1.127 +      : AstNode(pos)
   1.128 +    {
   1.129 +    }
   1.130 +};
   1.131 +
   1.132 +class Expression : public AstNode
   1.133 +{
   1.134 +    ScopedRoot<Type> type_;
   1.135 +
   1.136 +  public:
   1.137 +    Expression(const SourcePosition &pos)
   1.138 +      : AstNode(pos),
   1.139 +        type_(NULL)
   1.140 +    {
   1.141 +    }
   1.142 +
   1.143 +    void setType(Type *type) {
   1.144 +        type_ = type;
   1.145 +    }
   1.146 +    Type *type() const {
   1.147 +        return type_;
   1.148 +    }
   1.149 +};
   1.150 +
   1.151 +typedef PoolList<Statement *> StatementList;
   1.152 +typedef PoolList<Expression *> ExpressionList;
   1.153 +
   1.154 +class VariableDeclaration : public Statement
   1.155 +{
   1.156 +    TypeQualifiers qual_;
   1.157 +    ScopedRoot<String> name_;
   1.158 +    Expression *initialization_;
   1.159 +    Expression *type_;
   1.160 +    VariableSymbol *sym_;
   1.161 +
   1.162 +  public:
   1.163 +    VariableDeclaration(const SourcePosition &pos, TypeQualifiers qual, Handle<String> name,
   1.164 +                        Expression *type, Expression *initialization)
   1.165 +      : Statement(pos),
   1.166 +        qual_(qual),
   1.167 +        name_(name),
   1.168 +        initialization_(initialization),
   1.169 +        type_(type),
   1.170 +        sym_(NULL)
   1.171 +    {
   1.172 +    }
   1.173 +
   1.174 +    DECLARE_NODE(VariableDeclaration);
   1.175 +
   1.176 +    Expression *initialization() const {
   1.177 +        return initialization_;
   1.178 +    }
   1.179 +    Handle<String> name() {
   1.180 +        return name_;
   1.181 +    }
   1.182 +    Expression *declType() const {
   1.183 +        return type_;
   1.184 +    }
   1.185 +    void setSymbol(VariableSymbol *sym) {
   1.186 +        assert(!sym_);
   1.187 +        sym_ = sym;
   1.188 +    }
   1.189 +    VariableSymbol *sym() const {
   1.190 +        return sym_;
   1.191 +    }
   1.192 +    Expression *type() const {
   1.193 +        return type_;
   1.194 +    }
   1.195 +};
   1.196 +
   1.197 +class Parameter : public PoolObject
   1.198 +{
   1.199 +    ScopedRoot<String> name_;
   1.200 +    Expression *type_;
   1.201 +    bool reference_;
   1.202 +    bool const_;
   1.203 +    Expression *initializer_;
   1.204 +    VariableSymbol *sym_;
   1.205 +    SourcePosition pos_;
   1.206 +
   1.207 +  public:
   1.208 +    Parameter(Handle<String> name, Expression *type, bool reference, bool isConst,
   1.209 +              Expression *initializer, const SourcePosition &pos)
   1.210 +      : name_(name),
   1.211 +        type_(type),
   1.212 +        reference_(reference),
   1.213 +        const_(isConst),
   1.214 +        initializer_(initializer),
   1.215 +        sym_(NULL),
   1.216 +        pos_(pos)
   1.217 +    {
   1.218 +    }
   1.219 +
   1.220 +    Expression *type() const {
   1.221 +        return type_;
   1.222 +    }
   1.223 +    Handle<String> name() const {
   1.224 +        return name_;
   1.225 +    }
   1.226 +    bool reference() const {
   1.227 +        return reference_;
   1.228 +    }
   1.229 +    bool isConst() const {
   1.230 +        return const_;
   1.231 +    }
   1.232 +    Expression *initializer() const {
   1.233 +        return initializer_;
   1.234 +    }
   1.235 +    void setSymbol(VariableSymbol *sym) {
   1.236 +        assert(!sym_);
   1.237 +        sym_ = sym;
   1.238 +    }
   1.239 +    VariableSymbol *sym() const {
   1.240 +        return sym_;
   1.241 +    }
   1.242 +    const SourcePosition &pos() const {
   1.243 +        return pos_;
   1.244 +    }
   1.245 +};
   1.246 +
   1.247 +class NameProxy : public Expression
   1.248 +{
   1.249 +    ScopedRoot<String> name_;
   1.250 +    Symbol *binding_;
   1.251 +
   1.252 +  public:
   1.253 +    NameProxy(const SourcePosition &pos, Handle<String> name)
   1.254 +      : Expression(pos),
   1.255 +        name_(name),
   1.256 +        binding_(NULL)
   1.257 +    {
   1.258 +    }
   1.259 +
   1.260 +    DECLARE_NODE(NameProxy);
   1.261 +    
   1.262 +    Handle<String> name() const {
   1.263 +        return name_;
   1.264 +    }
   1.265 +    Symbol *sym() const {
   1.266 +        return binding_;
   1.267 +    }
   1.268 +    void bind(Symbol *sym) {
   1.269 +        binding_ = sym;
   1.270 +    }
   1.271 +};
   1.272 +
   1.273 +class BooleanLiteral : public Expression
   1.274 +{
   1.275 +    TokenKind token_;
   1.276 +
   1.277 +  public:
   1.278 +    BooleanLiteral(const SourcePosition &pos, TokenKind token)
   1.279 +      : Expression(pos),
   1.280 +        token_(token)
   1.281 +    {
   1.282 +    }
   1.283 +
   1.284 +    DECLARE_NODE(BooleanLiteral);
   1.285 +    TokenKind token() const {
   1.286 +        return token_;
   1.287 +    }
   1.288 +};
   1.289 +
   1.290 +class IntegerLiteral : public Expression
   1.291 +{
   1.292 +    int value_;
   1.293 +
   1.294 +  public:
   1.295 +    IntegerLiteral(const SourcePosition &pos, int value)
   1.296 +      : Expression(pos),
   1.297 +        value_(value)
   1.298 +    {
   1.299 +    }
   1.300 +
   1.301 +    DECLARE_NODE(IntegerLiteral);
   1.302 +
   1.303 +    int value() const {
   1.304 +        return value_;
   1.305 +    }
   1.306 +};
   1.307 +
   1.308 +class FloatLiteral : public Expression
   1.309 +{
   1.310 +    double value_;
   1.311 +
   1.312 +  public:
   1.313 +    FloatLiteral(const SourcePosition &pos, double value)
   1.314 +      : Expression(pos),
   1.315 +        value_(value)
   1.316 +    {
   1.317 +    }
   1.318 +
   1.319 +    DECLARE_NODE(FloatLiteral);
   1.320 +
   1.321 +    double value() const {
   1.322 +        return value_;
   1.323 +    }
   1.324 +};
   1.325 +
   1.326 +class StringLiteral : public Expression
   1.327 +{
   1.328 +    ScopedRoot<ByteArray> string_;
   1.329 +
   1.330 +  public:
   1.331 +    StringLiteral(const SourcePosition &pos, ByteArray *array)
   1.332 +      : Expression(pos),
   1.333 +        string_(array)
   1.334 +    {
   1.335 +    }
   1.336 +
   1.337 +    DECLARE_NODE(StringLiteral);
   1.338 +
   1.339 +    ByteArray *string() const {
   1.340 +        return string_;
   1.341 +    }
   1.342 +};
   1.343 +
   1.344 +class ArrayLiteral : public Expression
   1.345 +{
   1.346 +    TokenKind token_;
   1.347 +    ExpressionList *expressions_;
   1.348 +
   1.349 +  public:
   1.350 +    ArrayLiteral(const SourcePosition &pos, TokenKind token, ExpressionList *expressions)
   1.351 +      : Expression(pos),
   1.352 +        token_(token),
   1.353 +        expressions_(expressions)
   1.354 +    {
   1.355 +    }
   1.356 +
   1.357 +    DECLARE_NODE(ArrayLiteral);
   1.358 +
   1.359 +    TokenKind token() const {
   1.360 +        return token_;
   1.361 +    }
   1.362 +    ExpressionList *expressions() const {
   1.363 +        return expressions_;
   1.364 +    }
   1.365 +    bool isFixed() const {
   1.366 +        return token_ == TOK_LBRACE;
   1.367 +    }
   1.368 +};
   1.369 +
   1.370 +class UnaryExpression : public Expression
   1.371 +{
   1.372 +    Expression *expression_;
   1.373 +    TokenKind token_;
   1.374 +    NameProxy *tag_;
   1.375 +
   1.376 +  public:
   1.377 +    UnaryExpression(const SourcePosition &pos, TokenKind token, Expression *expr)
   1.378 +      : Expression(pos),
   1.379 +        expression_(expr),
   1.380 +        token_(token),
   1.381 +        tag_(NULL)
   1.382 +    {
   1.383 +    }
   1.384 +
   1.385 +    UnaryExpression(const SourcePosition &pos, TokenKind token, Expression *expr,
   1.386 +                    NameProxy *tag)
   1.387 +      : Expression(pos),
   1.388 +        expression_(expr),
   1.389 +        token_(token),
   1.390 +        tag_(tag)
   1.391 +    {
   1.392 +    }
   1.393 +
   1.394 +    DECLARE_NODE(UnaryExpression);
   1.395 +
   1.396 +    Expression *expression() const {
   1.397 +        return expression_;
   1.398 +    }
   1.399 +    TokenKind token() const {
   1.400 +        return token_;
   1.401 +    }
   1.402 +    NameProxy *tag() const {
   1.403 +        return tag_;
   1.404 +    }
   1.405 +};
   1.406 +
   1.407 +class BinaryExpression : public Expression
   1.408 +{
   1.409 +    Expression *left_;
   1.410 +    Expression *right_;
   1.411 +    TokenKind token_;
   1.412 +
   1.413 +  public:
   1.414 +    BinaryExpression(const SourcePosition &pos, TokenKind token, Expression *left, Expression *right)
   1.415 +      : Expression(pos),
   1.416 +        left_(left),
   1.417 +        right_(right),
   1.418 +        token_(token)
   1.419 +    {
   1.420 +    }
   1.421 +
   1.422 +    DECLARE_NODE(BinaryExpression);
   1.423 +
   1.424 +    Expression *left() const {
   1.425 +        return left_;
   1.426 +    }
   1.427 +    Expression *right() const {
   1.428 +        return right_;
   1.429 +    }
   1.430 +    TokenKind token() const {
   1.431 +        return token_;
   1.432 +    }
   1.433 +};
   1.434 +
   1.435 +class TernaryExpression : public Expression
   1.436 +{
   1.437 +    Expression *condition_;
   1.438 +    Expression *left_;
   1.439 +    Expression *right_;
   1.440 +
   1.441 +  public:
   1.442 +    TernaryExpression(const SourcePosition &pos, Expression *condition, Expression *left, Expression *right)
   1.443 +      : Expression(pos),
   1.444 +        condition_(condition),
   1.445 +        left_(left),
   1.446 +        right_(right)
   1.447 +    {
   1.448 +    }
   1.449 +
   1.450 +    DECLARE_NODE(TernaryExpression);
   1.451 +
   1.452 +    Expression *condition() const {
   1.453 +        return condition_;
   1.454 +    }
   1.455 +    Expression *left() const {
   1.456 +        return left_;
   1.457 +    }
   1.458 +    Expression *right() const {
   1.459 +        return right_;
   1.460 +    }
   1.461 +};
   1.462 +
   1.463 +class IndexExpression : public Expression
   1.464 +{
   1.465 +    Expression *left_;
   1.466 +    Expression *right_;
   1.467 +
   1.468 +  public:
   1.469 +    IndexExpression(const SourcePosition &pos, Expression *left, Expression *right)
   1.470 +      : Expression(pos),
   1.471 +        left_(left),
   1.472 +        right_(right)
   1.473 +    {
   1.474 +    }
   1.475 +
   1.476 +    DECLARE_NODE(IndexExpression);
   1.477 +
   1.478 +    Expression *left() const {
   1.479 +        return left_;
   1.480 +    }
   1.481 +    Expression *right() const {
   1.482 +        return right_;
   1.483 +    }
   1.484 +};
   1.485 +
   1.486 +class FieldExpression : public Expression
   1.487 +{
   1.488 +    Expression *left_;
   1.489 +    ScopedRoot<String> field_;
   1.490 +    unsigned fieldIndex_;
   1.491 +
   1.492 +  public:
   1.493 +    FieldExpression(const SourcePosition &pos, Expression *left, Handle<String> field)
   1.494 +      : Expression(pos),
   1.495 +        left_(left),
   1.496 +        field_(field),
   1.497 +        fieldIndex_(unsigned(-1))
   1.498 +    {
   1.499 +    }
   1.500 +
   1.501 +    DECLARE_NODE(FieldExpression);
   1.502 +
   1.503 +    Expression *left() const {
   1.504 +        return left_;
   1.505 +    }
   1.506 +    String *field() const {
   1.507 +        return field_;
   1.508 +    }
   1.509 +    void setFieldIndex(unsigned index) {
   1.510 +        assert(fieldIndex_ == unsigned(-1));
   1.511 +        fieldIndex_ = index;
   1.512 +    }
   1.513 +    unsigned fieldIndex() const {
   1.514 +        assert(fieldIndex_ != unsigned(-1));
   1.515 +        return fieldIndex_;
   1.516 +    }
   1.517 +};
   1.518 +
   1.519 +class CallExpression : public Expression
   1.520 +{
   1.521 +    Expression *callee_;
   1.522 +    ExpressionList *arguments_;
   1.523 +
   1.524 +  public:
   1.525 +    CallExpression(const SourcePosition &pos, Expression *callee, ExpressionList *arguments)
   1.526 +      : Expression(pos),
   1.527 +        callee_(callee),
   1.528 +        arguments_(arguments)
   1.529 +    {
   1.530 +    }
   1.531 +
   1.532 +    DECLARE_NODE(CallExpression);
   1.533 +
   1.534 +    Expression *callee() const {
   1.535 +        return callee_;
   1.536 +    }
   1.537 +    ExpressionList *arguments() const {
   1.538 +        return arguments_;
   1.539 +    }
   1.540 +};
   1.541 +
   1.542 +class ForStatement : public Statement
   1.543 +{
   1.544 +    Statement *initialization_;
   1.545 +    Expression *condition_;
   1.546 +    Statement *update_;
   1.547 +    Statement *body_;
   1.548 +    Scope *scope_;
   1.549 +
   1.550 +  public:
   1.551 +    ForStatement(const SourcePosition &pos, Statement *initialization,
   1.552 +                 Expression *condition, Statement *update, Statement *body)
   1.553 +      : Statement(pos),
   1.554 +        initialization_(initialization),
   1.555 +        condition_(condition),
   1.556 +        update_(update),
   1.557 +        body_(body),
   1.558 +        scope_(NULL)
   1.559 +    {
   1.560 +    }
   1.561 +
   1.562 +    DECLARE_NODE(ForStatement);
   1.563 +
   1.564 +    Statement *initialization() const {
   1.565 +        return initialization_;
   1.566 +    }
   1.567 +    Expression *condition() const {
   1.568 +        return condition_;
   1.569 +    }
   1.570 +    Statement *update() const {
   1.571 +        return update_;
   1.572 +    }
   1.573 +    Statement *body() const {
   1.574 +        return body_;
   1.575 +    }
   1.576 +    Scope *scope() const {
   1.577 +        return scope_;
   1.578 +    }
   1.579 +    void setScope(Scope *scope) {
   1.580 +        scope_ = scope;
   1.581 +    }
   1.582 +};
   1.583 +
   1.584 +class WhileStatement : public Statement
   1.585 +{
   1.586 +    TokenKind token_;
   1.587 +    Expression *condition_;
   1.588 +    Statement *body_;
   1.589 +
   1.590 +  public:
   1.591 +    WhileStatement(const SourcePosition &pos, TokenKind kind, Expression *condition, Statement *body)
   1.592 +      : Statement(pos),
   1.593 +        token_(kind),
   1.594 +        condition_(condition),
   1.595 +        body_(body)
   1.596 +    {
   1.597 +    }
   1.598 +    
   1.599 +    DECLARE_NODE(WhileStatement);
   1.600 +
   1.601 +    TokenKind token() const {
   1.602 +        return token_;
   1.603 +    }
   1.604 +    Expression *condition() const {
   1.605 +        return condition_;
   1.606 +    }
   1.607 +    Statement *body() const {
   1.608 +        return body_;
   1.609 +    }
   1.610 +};
   1.611 +
   1.612 +class ReturnStatement : public Statement
   1.613 +{
   1.614 +    Expression *expression_;
   1.615 +
   1.616 +  public:
   1.617 +    ReturnStatement(const SourcePosition &pos, Expression *expression)
   1.618 +      : Statement(pos),
   1.619 +        expression_(expression)
   1.620 +    {
   1.621 +    }
   1.622 +
   1.623 +    DECLARE_NODE(ReturnStatement);
   1.624 +
   1.625 +    Expression *expression() const {
   1.626 +        return expression_;
   1.627 +    }
   1.628 +};
   1.629 +
   1.630 +class BreakStatement : public Statement
   1.631 +{
   1.632 +  public:
   1.633 +    BreakStatement(const SourcePosition &pos)
   1.634 +      : Statement(pos)
   1.635 +    {
   1.636 +    }
   1.637 +
   1.638 +    DECLARE_NODE(BreakStatement);
   1.639 +};
   1.640 +
   1.641 +class ContinueStatement : public Statement
   1.642 +{
   1.643 +  public:
   1.644 +    ContinueStatement(const SourcePosition &pos)
   1.645 +      : Statement(pos)
   1.646 +    {
   1.647 +    }
   1.648 +
   1.649 +    DECLARE_NODE(ContinueStatement);
   1.650 +};
   1.651 +
   1.652 +class Assignment : public Expression
   1.653 +{
   1.654 +    TokenKind token_;
   1.655 +    Expression *lvalue_;
   1.656 +    Expression *expression_;
   1.657 +
   1.658 +  public:
   1.659 +    Assignment(const SourcePosition &pos, TokenKind token, Expression *left, Expression *right)
   1.660 +      : Expression(pos),
   1.661 +        token_(token),
   1.662 +        lvalue_(left),
   1.663 +        expression_(right)
   1.664 +    {
   1.665 +    }
   1.666 +
   1.667 +    DECLARE_NODE(Assignment);
   1.668 +
   1.669 +    TokenKind token() const {
   1.670 +        return token_;
   1.671 +    }
   1.672 +    Expression *lvalue() const {
   1.673 +        return lvalue_;
   1.674 +    }
   1.675 +    Expression *expression() const {
   1.676 +        return expression_;
   1.677 +    }
   1.678 +};
   1.679 +
   1.680 +class ExpressionStatement : public Statement
   1.681 +{
   1.682 +    Expression *expression_;
   1.683 +
   1.684 +  public:
   1.685 +    ExpressionStatement(Expression *expression)
   1.686 +      : Statement(expression->pos()),
   1.687 +        expression_(expression)
   1.688 +    {
   1.689 +    }
   1.690 +
   1.691 +    DECLARE_NODE(ExpressionStatement);
   1.692 +
   1.693 +    Expression *expression() const {
   1.694 +        return expression_;
   1.695 +    }
   1.696 +};
   1.697 +
   1.698 +class BlockStatement : public Statement
   1.699 +{
   1.700 +    StatementList *statements_;
   1.701 +    Scope *scope_;
   1.702 +
   1.703 +  public:
   1.704 +    BlockStatement(const SourcePosition &pos, StatementList *statements)
   1.705 +      : Statement(pos),
   1.706 +        statements_(statements),
   1.707 +        scope_(NULL)
   1.708 +    {
   1.709 +    }
   1.710 +
   1.711 +    DECLARE_NODE(BlockStatement);
   1.712 +
   1.713 +    StatementList *statements() const {
   1.714 +        return statements_;
   1.715 +    }
   1.716 +    Scope *scope() const {
   1.717 +        return scope_;
   1.718 +    }
   1.719 +    void setScope(Scope *scope) {
   1.720 +        scope_ = scope;
   1.721 +    }
   1.722 +};
   1.723 +
   1.724 +typedef PoolList<Parameter *> ParameterList;
   1.725 +
   1.726 +class FunctionSignature
   1.727 +{
   1.728 +  public:
   1.729 +    FunctionSignature(Expression *returnType, ParameterList *parameters, bool variadic)
   1.730 +      : returnType_(returnType),
   1.731 +        parameters_(parameters),
   1.732 +        variadic_(variadic)
   1.733 +    {
   1.734 +    }
   1.735 +
   1.736 +    Expression *returnType() const {
   1.737 +        return returnType_;
   1.738 +    }
   1.739 +    ParameterList *parameters() const {
   1.740 +        return parameters_;
   1.741 +    }
   1.742 +    bool variadic() const {
   1.743 +        return variadic_;
   1.744 +    }
   1.745 +
   1.746 +  private:
   1.747 +    Expression *returnType_;
   1.748 +    ParameterList *parameters_;
   1.749 +    bool variadic_;
   1.750 +};
   1.751 +
   1.752 +class FunctionTypeStatement : public Statement
   1.753 +{
   1.754 +  public:
   1.755 +    FunctionTypeStatement(const SourcePosition &pos, Handle<String> name, const FunctionSignature &signature)
   1.756 +      : Statement(pos),
   1.757 +        name_(name),
   1.758 +        signature_(signature)
   1.759 +    {
   1.760 +    }
   1.761 +
   1.762 +    DECLARE_NODE(FunctionTypeStatement);
   1.763 +
   1.764 +    Handle<String> name() const {
   1.765 +        return name_;
   1.766 +    }
   1.767 +    const FunctionSignature &signature() const {
   1.768 +        return signature_;
   1.769 +    }
   1.770 +
   1.771 +  private:
   1.772 +    ScopedRoot<String> name_;
   1.773 +    FunctionSignature signature_;
   1.774 +    
   1.775 +};
   1.776 +
   1.777 +class FunctionStatement : public Statement
   1.778 +{
   1.779 +    ScopedRoot<String> name_;
   1.780 +    TokenKind kind_;
   1.781 +    Statement *body_;
   1.782 +    FunctionSignature signature_;
   1.783 +    bool explicitReturn_;
   1.784 +    FunctionSymbol *sym_;
   1.785 +    Scope *argScope_;
   1.786 +    Scope *varScope_;
   1.787 +
   1.788 +  public:
   1.789 +    FunctionStatement(const SourcePosition &pos, Handle<String> name, TokenKind kind,
   1.790 +                      Statement *body, const FunctionSignature &signature, bool explicitReturn)
   1.791 +      : Statement(pos),
   1.792 +        name_(name),
   1.793 +        kind_(kind),
   1.794 +        body_(body),
   1.795 +        signature_(signature),
   1.796 +        explicitReturn_(explicitReturn),
   1.797 +        sym_(NULL),
   1.798 +        argScope_(NULL),
   1.799 +        varScope_(NULL)
   1.800 +    {
   1.801 +    }
   1.802 +
   1.803 +    DECLARE_NODE(FunctionStatement);
   1.804 +
   1.805 +    Handle<String> name() const {
   1.806 +        return name_;
   1.807 +    }
   1.808 +    Statement *body() const {
   1.809 +        return body_;
   1.810 +    }
   1.811 +    const FunctionSignature &signature() {
   1.812 +        return signature_;
   1.813 +    }
   1.814 +    bool hasExplicitReturn() const {
   1.815 +        return explicitReturn_;
   1.816 +    }
   1.817 +    TokenKind token() const {
   1.818 +        return kind_;
   1.819 +    }
   1.820 +    void setSymbol(FunctionSymbol *sym) {
   1.821 +        assert(!sym_);
   1.822 +        sym_ = sym;
   1.823 +    }
   1.824 +    FunctionSymbol *sym() const {
   1.825 +        return sym_;
   1.826 +    }
   1.827 +    void setScopes(Scope *argScope, Scope *varScope) {
   1.828 +        argScope_ = argScope;
   1.829 +        varScope_ = varScope;
   1.830 +    }
   1.831 +    Scope *argScope() const {
   1.832 +        return argScope_;
   1.833 +    }
   1.834 +    Scope *varScope() const {
   1.835 +        return varScope_;
   1.836 +    }
   1.837 +};
   1.838 +
   1.839 +class IfStatement : public Statement
   1.840 +{
   1.841 +    Expression *condition_;
   1.842 +    Statement *ifTrue_;
   1.843 +    Statement *ifFalse_;
   1.844 +
   1.845 +  public:
   1.846 +    IfStatement(const SourcePosition &pos, Expression *condition, Statement *ifTrue)
   1.847 +      : Statement(pos),
   1.848 +        condition_(condition),
   1.849 +        ifTrue_(ifTrue),
   1.850 +        ifFalse_(NULL)
   1.851 +    {
   1.852 +    }
   1.853 +
   1.854 +    DECLARE_NODE(IfStatement);
   1.855 +
   1.856 +    Expression *condition() const {
   1.857 +        return condition_;
   1.858 +    }
   1.859 +    Statement *ifTrue() const {
   1.860 +        return ifTrue_;
   1.861 +    }
   1.862 +    Statement *ifFalse() const {
   1.863 +        return ifFalse_;
   1.864 +    }
   1.865 +    void setIfFalse(Statement *ifFalse) {
   1.866 +        ifFalse_ = ifFalse;
   1.867 +    }
   1.868 +};
   1.869 +
   1.870 +class EnumStatement : public Statement
   1.871 +{
   1.872 +    ScopedRoot<String> name_;
   1.873 +    TypeSymbol *sym_;
   1.874 +
   1.875 +  public:
   1.876 +    struct Entry {
   1.877 +        ScopedRoot<String> name;
   1.878 +        Expression *expr;
   1.879 +
   1.880 +        Entry(Handle<String> name, Expression *expr)
   1.881 +          : name(name),
   1.882 +            expr(expr)
   1.883 +        {
   1.884 +        }
   1.885 +    };
   1.886 +
   1.887 +    typedef PoolList<Entry> EntryList;
   1.888 +
   1.889 +  public:
   1.890 +    EnumStatement(const SourcePosition &pos, Handle<String> name, EntryList *entries)
   1.891 +      : Statement(pos),
   1.892 +        name_(name),
   1.893 +        entries_(entries),
   1.894 +        sym_(NULL)
   1.895 +    {
   1.896 +    }
   1.897 +
   1.898 +    DECLARE_NODE(EnumStatement);
   1.899 +
   1.900 +    String *name() const {
   1.901 +        return name_;
   1.902 +    }
   1.903 +    EntryList *entries() const {
   1.904 +        return entries_;
   1.905 +    }
   1.906 +    void setSymbol(TypeSymbol *sym) {
   1.907 +        assert(!sym_);
   1.908 +        sym_ = sym;
   1.909 +    }
   1.910 +    TypeSymbol *sym() const {
   1.911 +        return sym_;
   1.912 +    }
   1.913 +
   1.914 +  private:
   1.915 +    EntryList *entries_;
   1.916 +};
   1.917 +
   1.918 +class IncDecExpression : public Expression
   1.919 +{
   1.920 +    TokenKind token_;
   1.921 +    Expression *expression_;
   1.922 +    bool postfix_;
   1.923 +
   1.924 +  public:
   1.925 +    IncDecExpression(const SourcePosition &pos, TokenKind token, Expression *expression, bool postfix)
   1.926 +      : Expression(pos),
   1.927 +        token_(token),
   1.928 +        expression_(expression),
   1.929 +        postfix_(postfix)
   1.930 +    {
   1.931 +    }
   1.932 +
   1.933 +    DECLARE_NODE(IncDecExpression);
   1.934 +
   1.935 +    TokenKind token() const {
   1.936 +        return token_;
   1.937 +    }
   1.938 +    Expression *expression() const {
   1.939 +        return expression_;
   1.940 +    }
   1.941 +    bool postfix() const {
   1.942 +        return postfix_;
   1.943 +    }
   1.944 +};
   1.945 +
   1.946 +class Case : public PoolObject
   1.947 +{
   1.948 +  public:
   1.949 +    Case(Expression *expression, ExpressionList *others, Statement *statement)
   1.950 +      : expression_(expression),
   1.951 +        others_(others),
   1.952 +        statement_(statement)
   1.953 +    {
   1.954 +    }
   1.955 +
   1.956 +    Expression *expression() const {
   1.957 +        return expression_;
   1.958 +    }
   1.959 +    PoolList<Expression *> *others() const {
   1.960 +        return others_;
   1.961 +    }
   1.962 +    Statement *statement() const {
   1.963 +        return statement_;
   1.964 +    }
   1.965 +
   1.966 +  private:
   1.967 +    Expression *expression_;
   1.968 +    PoolList <Expression *> *others_;
   1.969 +    Statement *statement_;
   1.970 +};
   1.971 +
   1.972 +struct CaseValue
   1.973 +{
   1.974 +    BoxedPrimitive box;
   1.975 +    size_t statement;
   1.976 +
   1.977 +    CaseValue(size_t statement)
   1.978 +      : statement(statement)
   1.979 +    {
   1.980 +    }
   1.981 +};
   1.982 +
   1.983 +typedef PoolList<CaseValue> CaseValueList;
   1.984 +
   1.985 +class SwitchStatement : public Statement
   1.986 +{
   1.987 +  public:
   1.988 +    SwitchStatement(const SourcePosition &pos, Expression *expression, PoolList<Case *> *cases,
   1.989 +                    Statement *def)
   1.990 +      : Statement(pos),
   1.991 +        expression_(expression),
   1.992 +        cases_(cases),
   1.993 +        default_(def),
   1.994 +        table_(NULL)
   1.995 +    {
   1.996 +    }
   1.997 +
   1.998 +    DECLARE_NODE(SwitchStatement);
   1.999 +
  1.1000 +    Expression *expression() const {
  1.1001 +        return expression_;
  1.1002 +    }
  1.1003 +    PoolList<Case *> *cases() const {
  1.1004 +        return cases_;
  1.1005 +    }
  1.1006 +    Statement *defaultCase() const {
  1.1007 +        return default_;
  1.1008 +    }
  1.1009 +    void setCaseValueList(CaseValueList *list) {
  1.1010 +        table_ = list;
  1.1011 +    }
  1.1012 +    CaseValueList *caseValueList() const {
  1.1013 +        return table_;
  1.1014 +    }
  1.1015 +
  1.1016 +  private:
  1.1017 +    Expression *expression_;
  1.1018 +    PoolList<Case *> *cases_;
  1.1019 +    Statement *default_;
  1.1020 +    CaseValueList *table_;
  1.1021 +};
  1.1022 +
  1.1023 +class StructureStatement : public Statement
  1.1024 +{
  1.1025 +  public:
  1.1026 +    class Field : public PoolObject
  1.1027 +    {
  1.1028 +        SourcePosition pos_;
  1.1029 +        Expression *type_;
  1.1030 +        ScopedRoot<String> name_;
  1.1031 +
  1.1032 +      public:
  1.1033 +        Field(const SourcePosition &pos,
  1.1034 +              Expression *type,
  1.1035 +              Handle<String> name)
  1.1036 +          : pos_(pos),
  1.1037 +            type_(type),
  1.1038 +            name_(name)
  1.1039 +        {
  1.1040 +        }
  1.1041 +
  1.1042 +        const SourcePosition &pos() const {
  1.1043 +            return pos_;
  1.1044 +        }
  1.1045 +        Handle<String> name() const {
  1.1046 +            return name_;
  1.1047 +        }
  1.1048 +        Expression *type() const {
  1.1049 +            return type_;
  1.1050 +        }
  1.1051 +    };
  1.1052 +
  1.1053 +    typedef PoolList<StructureStatement::Field *> FieldList;
  1.1054 +
  1.1055 +  public:
  1.1056 +    StructureStatement(const SourcePosition &pos, Handle<String> name, FieldList *fields)
  1.1057 +      : Statement(pos),
  1.1058 +        name_(name),
  1.1059 +        fields_(fields),
  1.1060 +        sym_(NULL)
  1.1061 +    {
  1.1062 +    }
  1.1063 +
  1.1064 +    DECLARE_NODE(StructureStatement);
  1.1065 +
  1.1066 +    FieldList *fields() const {
  1.1067 +        return fields_;
  1.1068 +    }
  1.1069 +    Handle<String> name() const {
  1.1070 +        return name_;
  1.1071 +    }
  1.1072 +    void setSymbol(TypeSymbol *sym) {
  1.1073 +        assert(!sym_);
  1.1074 +        sym_ = sym;
  1.1075 +    }
  1.1076 +    TypeSymbol *sym() const {
  1.1077 +        return sym_;
  1.1078 +    }
  1.1079 +
  1.1080 +  private:
  1.1081 +    ScopedRoot<String> name_;
  1.1082 +    FieldList *fields_;
  1.1083 +    TypeSymbol *sym_;
  1.1084 +};
  1.1085 +
  1.1086 +typedef PoolList<ScopedRoot<String> *> NameList;
  1.1087 +
  1.1088 +class ImportStatement : public Statement
  1.1089 +{
  1.1090 +  public:
  1.1091 +    ImportStatement(const SourcePosition &pos, NameList *names, TokenKind suffix)
  1.1092 +      : Statement(pos),
  1.1093 +        names_(names),
  1.1094 +        suffix_(suffix),
  1.1095 +        source_(NULL)
  1.1096 +    {
  1.1097 +    }
  1.1098 +
  1.1099 +    DECLARE_NODE(ImportStatement);
  1.1100 +
  1.1101 +    NameList *path() const {
  1.1102 +        return names_;
  1.1103 +    }
  1.1104 +    void setImportSource(TranslationUnit *unit) {
  1.1105 +        source_ = unit;
  1.1106 +    }
  1.1107 +    TranslationUnit *source() const {
  1.1108 +        return source_;
  1.1109 +    }
  1.1110 +    TokenKind suffix() const {
  1.1111 +        return suffix_;
  1.1112 +    }
  1.1113 +    Handle<String> name() const {
  1.1114 +        assert(suffix() != TOK_STAR);
  1.1115 +        return *names_->at(names_->length() - 1);
  1.1116 +    }
  1.1117 +
  1.1118 +  private:
  1.1119 +    NameList *names_;
  1.1120 +    TokenKind suffix_;
  1.1121 +    TranslationUnit *source_;
  1.1122 +};
  1.1123 +
  1.1124 +typedef StructureStatement::FieldList FieldList;
  1.1125 +typedef PoolList<NameProxy *> NameProxyList;
  1.1126 +
  1.1127 +#undef DECLARE_NODE
  1.1128 +
  1.1129 +class ParseTree : public PoolObject
  1.1130 +{
  1.1131 +    StatementList *statements_;
  1.1132 +    NameProxyList *unbound_;
  1.1133 +    PoolList<ImportStatement *> *imports_;
  1.1134 +
  1.1135 +  public:
  1.1136 +    ParseTree(StatementList *statements, NameProxyList *unbound, PoolList<ImportStatement *> *imports)
  1.1137 +      : statements_(statements),
  1.1138 +        unbound_(unbound),
  1.1139 +        imports_(imports)
  1.1140 +    {
  1.1141 +    }
  1.1142 +
  1.1143 +    void dump(FILE *fp);
  1.1144 +
  1.1145 +    StatementList *statements() const {
  1.1146 +        return statements_;
  1.1147 +    }
  1.1148 +    NameProxyList *unbound() const {
  1.1149 +        return unbound_;
  1.1150 +    }
  1.1151 +    PoolList<ImportStatement *> *imports() const {
  1.1152 +        return imports_;
  1.1153 +    }
  1.1154 +};
  1.1155 +
  1.1156 +}
  1.1157 +
  1.1158 +#endif // _include_sourcepawn_ast_h_