src/AST.h
author David Anderson <dvander@alliedmods.net>
Sun Jul 29 19:37:47 2012 -0700 (2012-07-29)
changeset 135 72912cf0dd18
parent 133 7797936ba34c
child 136 7b32fc1bf153
permissions -rw-r--r--
Support TOK_CONST as a declarator in the parser.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of SourcePawn.
     6  *
     7  * SourcePawn is free software: you can redistribute it and/or modify it under
     8  * the terms of the GNU General Public License as published by the Free
     9  * Software Foundation, either version 3 of the License, or (at your option)
    10  * any later version.
    11  * 
    12  * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
    13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along with
    17  * SourcePawn. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #ifndef _include_jitcraft_ast_h_
    20 #define _include_jitcraft_ast_h_
    21 
    22 #include "PoolAllocator.h"
    23 #include "Token.h"
    24 #include "NameBinding.h"
    25 #include "Vector.h"
    26 
    27 namespace ke {
    28 
    29 #define ASTKINDS(_)             \
    30     _(VariableDeclaration)      \
    31     _(ForStatement)             \
    32     _(ReturnStatement)          \
    33     _(IntegerLiteral)           \
    34     _(FloatLiteral)             \
    35     _(StringLiteral)            \
    36     _(BinaryExpression)         \
    37     _(BlockStatement)           \
    38     _(Assignment)               \
    39     _(NameProxy)                \
    40     _(ExpressionStatement)      \
    41     _(FunctionStatement)        \
    42     _(CallExpression)           \
    43     _(IfStatement)              \
    44     _(IndexExpression)          \
    45     _(EnumStatement)            \
    46     _(WhileStatement)           \
    47     _(BreakStatement)           \
    48     _(ContinueStatement)        \
    49     _(IncDecExpression)         \
    50     _(UnaryExpression)          \
    51     _(TernaryExpression)
    52 
    53 // Forward declarations.
    54 #define _(name) class name;
    55 ASTKINDS(_)
    56 #undef _
    57 
    58 class AstVisitor;
    59 
    60 // Interface for AST nodes.
    61 class AstNode : public PoolObject
    62 {
    63     SourcePosition pos_;
    64 
    65   public:
    66     enum Kind {
    67 #       define _(name) k##name,
    68         ASTKINDS(_)
    69 #       undef _
    70         AstKind_Invalid
    71     };
    72 
    73     AstNode(const SourcePosition &pos)
    74       : pos_(pos)
    75     {
    76     }
    77     
    78     virtual Kind kind() const = 0;
    79 
    80 #define _(name)     bool is##name() { return kind() == k##name; }                   \
    81                     name *to##name() { assert(is##name()); return (name *)this; }
    82     ASTKINDS(_)
    83 #undef _
    84 
    85     virtual void accept(AstVisitor *visitor) = 0;
    86 
    87     const SourcePosition &pos() const {
    88         return pos_;
    89     }
    90 };
    91 
    92 class AstVisitor
    93 {
    94   public:
    95 #define _(name) virtual void visit(name *node) = 0;
    96     ASTKINDS(_)
    97 #undef _
    98 };
    99 
   100 class NameLocation
   101 {
   102   public:
   103     enum Kind {
   104         LOCAL
   105     };
   106 
   107     static NameLocation Local(VariableDeclaration *decl) {
   108         NameLocation loc;
   109         loc.kind_ = LOCAL;
   110         loc.u_.decl = decl;
   111         return loc;
   112     }
   113 
   114     Kind kind() const {
   115         return kind_;
   116     }
   117 
   118     VariableDeclaration *local() const {
   119         assert(kind() == LOCAL);
   120         return u_.decl;
   121     }
   122 
   123   private:
   124     Kind kind_;
   125     union {
   126         VariableDeclaration *decl;
   127     } u_;
   128 };
   129 
   130 class Statement : public AstNode
   131 {
   132   public:
   133     Statement(const SourcePosition &pos)
   134       : AstNode(pos)
   135     {
   136     }
   137 };
   138 
   139 class Expression : public AstNode
   140 {
   141     ScopedRoot<Type> type_;
   142 
   143   public:
   144     Expression(const SourcePosition &pos)
   145       : AstNode(pos),
   146         type_(NULL)
   147     {
   148     }
   149 
   150     void setType(Type *type) {
   151         type_ = type;
   152     }
   153     Type *type() const {
   154         return type_;
   155     }
   156 };
   157 
   158 typedef PoolList<Statement *> StatementList;
   159 typedef PoolList<Expression *> ExpressionList;
   160 
   161 class VariableDeclaration : public Statement
   162 {
   163     Variable *variable_;
   164     Expression *initialization_;
   165     ExpressionList *postDimensions_;
   166     ScopedRoot<String> tag_;
   167     TokenKind declarator_;
   168 
   169   public:
   170     VariableDeclaration(const SourcePosition &pos, TokenKind declarator, Variable *var,
   171                         Handle<String> tag, ExpressionList *postDimensions,
   172                         Expression *initialization)
   173       : Statement(pos),
   174         variable_(var),
   175         initialization_(initialization),
   176         postDimensions_(postDimensions),
   177         tag_(tag)
   178     {
   179     }
   180 
   181     String *tag() const {
   182         return tag_;
   183     }
   184     Kind kind() const {
   185         return kVariableDeclaration;
   186     }
   187     Variable *variable() const {
   188         return variable_;
   189     }
   190     Expression *initialization() const {
   191         return initialization_;
   192     }
   193     ExpressionList *postDimensions() const {
   194         return postDimensions_;
   195     }
   196     void accept(AstVisitor *visitor) {
   197         visitor->visit(this);
   198     }
   199     TokenKind declarator() const {
   200         return declarator_;
   201     }
   202 };
   203 
   204 class Parameter : public PoolObject
   205 {
   206     Variable *variable_;
   207     ScopedRoot<String> tag_;
   208     bool reference_;
   209     bool const_;
   210     ExpressionList *dimensions_;
   211     Expression *initializer_;
   212 
   213   public:
   214     Parameter(Variable *var, Handle<String> tag, bool reference, bool isConst,
   215               ExpressionList *dimensions, Expression *initializer)
   216       : variable_(var),
   217         tag_(tag),
   218         reference_(reference),
   219         const_(isConst),
   220         dimensions_(dimensions),
   221         initializer_(initializer)
   222     {
   223     }
   224 
   225     String *tag() const {
   226         return tag_;
   227     }
   228     Variable *variable() const {
   229         return variable_;
   230     }
   231     bool reference() const {
   232         return reference_;
   233     }
   234     ExpressionList *dimensions() const {
   235         return dimensions_;
   236     }
   237     bool isConst() const {
   238         return const_;
   239     }
   240     Expression *initializer() const {
   241         return initializer_;
   242     }
   243 };
   244 
   245 class NameProxy : public Expression
   246 {
   247     ScopedRoot<String> name_;
   248     BoundName *binding_;
   249 
   250   public:
   251     NameProxy(const SourcePosition &pos, Handle<String> name, BoundName *binding)
   252       : Expression(pos),
   253         name_(name),
   254         binding_(binding)
   255     {
   256     }
   257 
   258     Kind kind() const {
   259         return kNameProxy;
   260     }
   261     String *name() const {
   262         return name_;
   263     }
   264     void accept(AstVisitor *visitor) {
   265         visitor->visit(this);
   266     }
   267     BoundName *binding() const {
   268         return binding_;
   269     }
   270     void bind(BoundName *name) {
   271         assert(!binding());
   272         binding_ = name;
   273     }
   274 };
   275 
   276 class IntegerLiteral : public Expression
   277 {
   278     int value_;
   279 
   280   public:
   281     IntegerLiteral(const SourcePosition &pos, int value)
   282       : Expression(pos),
   283         value_(value)
   284     {
   285     }
   286 
   287     Kind kind() const {
   288         return kIntegerLiteral;
   289     }
   290     int value() const {
   291         return value_;
   292     }
   293     void accept(AstVisitor *visitor) {
   294         visitor->visit(this);
   295     }
   296 };
   297 
   298 class FloatLiteral : public Expression
   299 {
   300     double value_;
   301 
   302   public:
   303     FloatLiteral(const SourcePosition &pos, double value)
   304       : Expression(pos),
   305         value_(value)
   306     {
   307     }
   308 
   309     Kind kind() const {
   310         return kFloatLiteral;
   311     }
   312     double value() const {
   313         return value_;
   314     }
   315     void accept(AstVisitor *visitor) {
   316         visitor->visit(this);
   317     }
   318 };
   319 
   320 class StringLiteral : public Expression
   321 {
   322     ScopedRoot<ByteArray> string_;
   323 
   324   public:
   325     StringLiteral(const SourcePosition &pos, ByteArray *array)
   326       : Expression(pos),
   327         string_(array)
   328     {
   329     }
   330 
   331     Kind kind() const {
   332         return kStringLiteral;
   333     }
   334     ByteArray *string() const {
   335         return string_;
   336     }
   337     void accept(AstVisitor *visitor) {
   338         visitor->visit(this);
   339     }
   340 };
   341 
   342 class UnaryExpression : public Expression
   343 {
   344     Expression *expression_;
   345     TokenKind token_;
   346 
   347   public:
   348     UnaryExpression(const SourcePosition &pos, TokenKind token, Expression *expr)
   349       : Expression(pos),
   350         expression_(expr),
   351         token_(token)
   352     {
   353     }
   354 
   355     Kind kind() const {
   356         return kUnaryExpression;
   357     }
   358     Expression *expression() const {
   359         return expression_;
   360     }
   361     TokenKind token() const {
   362         return token_;
   363     }
   364     void accept(AstVisitor *visitor) {
   365         visitor->visit(this);
   366     }
   367 };
   368 
   369 class BinaryExpression : public Expression
   370 {
   371     Expression *left_;
   372     Expression *right_;
   373     TokenKind token_;
   374 
   375   public:
   376     BinaryExpression(const SourcePosition &pos, TokenKind token, Expression *left, Expression *right)
   377       : Expression(pos),
   378         left_(left),
   379         right_(right),
   380         token_(token)
   381     {
   382     }
   383 
   384     Kind kind() const {
   385         return kBinaryExpression;
   386     }
   387     Expression *left() const {
   388         return left_;
   389     }
   390     Expression *right() const {
   391         return right_;
   392     }
   393     TokenKind token() const {
   394         return token_;
   395     }
   396     void accept(AstVisitor *visitor) {
   397         visitor->visit(this);
   398     }
   399 };
   400 
   401 class TernaryExpression : public Expression
   402 {
   403     Expression *condition_;
   404     Expression *left_;
   405     Expression *right_;
   406 
   407   public:
   408     TernaryExpression(const SourcePosition &pos, Expression *condition, Expression *left, Expression *right)
   409       : Expression(pos),
   410         condition_(condition),
   411         left_(left),
   412         right_(right)
   413     {
   414     }
   415 
   416     Kind kind() const {
   417         return kTernaryExpression;
   418     }
   419     Expression *condition() const {
   420         return condition_;
   421     }
   422     Expression *left() const {
   423         return left_;
   424     }
   425     Expression *right() const {
   426         return right_;
   427     }
   428     void accept(AstVisitor *visitor) {
   429         visitor->visit(this);
   430     }
   431 };
   432 
   433 class IndexExpression : public Expression
   434 {
   435     Expression *left_;
   436     Expression *right_;
   437 
   438   public:
   439     IndexExpression(const SourcePosition &pos, Expression *left, Expression *right)
   440       : Expression(pos),
   441         left_(left),
   442         right_(right)
   443     {
   444     }
   445 
   446     Kind kind() const {
   447         return kIndexExpression;
   448     }
   449     Expression *left() const {
   450         return left_;
   451     }
   452     Expression *right() const {
   453         return right_;
   454     }
   455     void accept(AstVisitor *visitor) {
   456         visitor->visit(this);
   457     }
   458 };
   459 
   460 class CallExpression : public Expression
   461 {
   462     Expression *callee_;
   463     ExpressionList *arguments_;
   464 
   465   public:
   466     CallExpression(const SourcePosition &pos, Expression *callee, ExpressionList *arguments)
   467       : Expression(pos),
   468         callee_(callee),
   469         arguments_(arguments)
   470     {
   471     }
   472 
   473     Kind kind() const {
   474         return kCallExpression;
   475     }
   476     Expression *callee() const {
   477         return callee_;
   478     }
   479     ExpressionList *arguments() const {
   480         return arguments_;
   481     }
   482     void accept(AstVisitor *visitor) {
   483         visitor->visit(this);
   484     }
   485 };
   486 
   487 class ForStatement : public Statement
   488 {
   489     Scope *scope_;
   490     Statement *initialization_;
   491     Expression *condition_;
   492     Statement *update_;
   493     Statement *body_;
   494 
   495   public:
   496     ForStatement(const SourcePosition &pos, Scope *scope, Statement *initialization,
   497                  Expression *condition, Statement *update, Statement *body)
   498       : Statement(pos),
   499         scope_(scope),
   500         initialization_(initialization),
   501         condition_(condition),
   502         update_(update),
   503         body_(body)
   504     {
   505     }
   506 
   507     Kind kind() const {
   508         return kForStatement;
   509     }
   510     Statement *initialization() const {
   511         return initialization_;
   512     }
   513     Scope *scope() const {
   514         return scope_;
   515     }
   516     Expression *condition() const {
   517         return condition_;
   518     }
   519     Statement *update() const {
   520         return update_;
   521     }
   522     Statement *body() const {
   523         return body_;
   524     }
   525     void accept(AstVisitor *visitor) {
   526         visitor->visit(this);
   527     }
   528 };
   529 
   530 class WhileStatement : public Statement
   531 {
   532     TokenKind token_;
   533     Expression *condition_;
   534     Statement *body_;
   535 
   536   public:
   537     WhileStatement(const SourcePosition &pos, TokenKind kind, Expression *condition, Statement *body)
   538       : Statement(pos),
   539         token_(kind),
   540         condition_(condition),
   541         body_(body)
   542     {
   543     }
   544     
   545     Kind kind() const {
   546         return kWhileStatement;
   547     }
   548     TokenKind token() const {
   549         return token_;
   550     }
   551     Expression *condition() const {
   552         return condition_;
   553     }
   554     Statement *body() const {
   555         return body_;
   556     }
   557     void accept(AstVisitor *visitor) {
   558         visitor->visit(this);
   559     }
   560 };
   561 
   562 class ReturnStatement : public Statement
   563 {
   564     Expression *expression_;
   565 
   566   public:
   567     ReturnStatement(const SourcePosition &pos, Expression *expression)
   568       : Statement(pos),
   569         expression_(expression)
   570     {
   571     }
   572 
   573     Kind kind() const {
   574         return kReturnStatement;
   575     }
   576     Expression *expression() const {
   577         return expression_;
   578     }
   579     void accept(AstVisitor *visitor) {
   580         visitor->visit(this);
   581     }
   582 };
   583 
   584 class BreakStatement : public Statement
   585 {
   586   public:
   587     BreakStatement(const SourcePosition &pos)
   588       : Statement(pos)
   589     {
   590     }
   591 
   592     Kind kind() const {
   593         return kBreakStatement;
   594     }
   595     void accept(AstVisitor *visitor) {
   596         visitor->visit(this);
   597     }
   598 };
   599 
   600 class ContinueStatement : public Statement
   601 {
   602   public:
   603     ContinueStatement(const SourcePosition &pos)
   604       : Statement(pos)
   605     {
   606     }
   607 
   608     Kind kind() const {
   609         return kContinueStatement;
   610     }
   611     void accept(AstVisitor *visitor) {
   612         visitor->visit(this);
   613     }
   614 };
   615 
   616 class Assignment : public Expression
   617 {
   618     TokenKind token_;
   619     Expression *lvalue_;
   620     Expression *expression_;
   621 
   622   public:
   623     Assignment(const SourcePosition &pos, TokenKind token, Expression *left, Expression *right)
   624       : Expression(pos),
   625         token_(token),
   626         lvalue_(left),
   627         expression_(right)
   628     {
   629     }
   630 
   631     Kind kind() const {
   632         return kAssignment;
   633     }
   634     TokenKind token() const {
   635         return token_;
   636     }
   637     Expression *lvalue() const {
   638         return lvalue_;
   639     }
   640     Expression *expression() const {
   641         return expression_;
   642     }
   643     void accept(AstVisitor *visitor) {
   644         visitor->visit(this);
   645     }
   646 };
   647 
   648 class ExpressionStatement : public Statement
   649 {
   650     Expression *expression_;
   651 
   652   public:
   653     ExpressionStatement(Expression *expression)
   654       : Statement(expression->pos()),
   655         expression_(expression)
   656     {
   657     }
   658 
   659     Kind kind() const {
   660         return kExpressionStatement;
   661     }
   662     Expression *expression() const {
   663         return expression_;
   664     }
   665     void accept(AstVisitor *visitor) {
   666         visitor->visit(this);
   667     }
   668 };
   669 
   670 class BlockStatement : public Statement
   671 {
   672     Scope *scope_;
   673     StatementList *statements_;
   674 
   675   public:
   676     BlockStatement(const SourcePosition &pos, Scope *scope, StatementList *statements)
   677       : Statement(pos),
   678         scope_(scope),
   679         statements_(statements)
   680     {
   681     }
   682 
   683     Kind kind() const {
   684         return kBlockStatement;
   685     }
   686     Scope *scope() const {
   687         return scope_;
   688     }
   689     StatementList *statements() const {
   690         return statements_;
   691     }
   692     void accept(AstVisitor *visitor) {
   693         visitor->visit(this);
   694     }
   695 };
   696 
   697 typedef PoolList<Parameter *> ParameterList;
   698 
   699 class FunctionStatement : public Statement
   700 {
   701     Scope *scope_;
   702     NamedFunction *fun_;
   703     Statement *body_;
   704     ParameterList *parameters_;
   705     bool explicitReturn_;
   706     ScopedRoot<String> tag_;
   707 
   708   public:
   709     FunctionStatement(const SourcePosition &pos, Scope *scope, NamedFunction *fun, Statement *body,
   710                       ParameterList *parameters, Handle<String> returnTag, bool explicitReturn)
   711       : Statement(pos),
   712         scope_(scope),
   713         fun_(fun),
   714         body_(body),
   715         parameters_(parameters),
   716         explicitReturn_(explicitReturn)
   717     {
   718     }
   719 
   720     Kind kind() const {
   721         return kFunctionStatement;
   722     }
   723     Scope *scope() const {
   724         return scope_;
   725     }
   726     NamedFunction *fun() const {
   727         return fun_;
   728     }
   729     Statement *body() const {
   730         return body_;
   731     }
   732     ParameterList *parameters() const {
   733         return parameters_;
   734     }
   735     void accept(AstVisitor *visitor) {
   736         visitor->visit(this);
   737     }
   738     bool hasExplicitReturn() const {
   739         return explicitReturn_;
   740     }
   741     String *returnTag() const {
   742         return tag_;
   743     }
   744 };
   745 
   746 class IfStatement : public Statement
   747 {
   748     Expression *condition_;
   749     Statement *ifTrue_;
   750     Statement *ifFalse_;
   751 
   752   public:
   753     IfStatement(const SourcePosition &pos, Expression *condition, Statement *ifTrue)
   754       : Statement(pos),
   755         condition_(condition),
   756         ifTrue_(ifTrue),
   757         ifFalse_(NULL)
   758     {
   759     }
   760 
   761     Kind kind() const {
   762         return kIfStatement;
   763     }
   764     Expression *condition() const {
   765         return condition_;
   766     }
   767     Statement *ifTrue() const {
   768         return ifTrue_;
   769     }
   770     Statement *ifFalse() const {
   771         return ifFalse_;
   772     }
   773     void setIfFalse(Statement *ifFalse) {
   774         ifFalse_ = ifFalse;
   775     }
   776     void accept(AstVisitor *visitor) {
   777         visitor->visit(this);
   778     }
   779 };
   780 
   781 class EnumStatement : public Statement
   782 {
   783     ScopedRoot<String> name_;
   784 
   785   public:
   786     struct Entry {
   787         NamedConstant *name;
   788         Expression *expr;
   789 
   790         Entry(NamedConstant *name, Expression *expr)
   791           : name(name),
   792             expr(expr)
   793         {
   794         }
   795     };
   796 
   797     typedef PoolList<Entry> EntryList;
   798 
   799   public:
   800     EnumStatement(const SourcePosition &pos, Handle<String> name, EntryList *entries)
   801       : Statement(pos),
   802         name_(name),
   803         entries_(entries)
   804     {
   805     }
   806 
   807     Kind kind() const {
   808         return kEnumStatement;
   809     }
   810     String *name() const {
   811         return name_;
   812     }
   813     EntryList *entries() const {
   814         return entries_;
   815     }
   816     void accept(AstVisitor *visitor) {
   817         visitor->visit(this);
   818     }
   819 
   820   private:
   821     EntryList *entries_;
   822 };
   823 
   824 class IncDecExpression : public Expression
   825 {
   826     TokenKind token_;
   827     Expression *expression_;
   828     bool postfix_;
   829 
   830   public:
   831     IncDecExpression(const SourcePosition &pos, TokenKind token, Expression *expression, bool postfix)
   832       : Expression(pos),
   833         token_(token),
   834         expression_(expression),
   835         postfix_(postfix)
   836     {
   837     }
   838 
   839     Kind kind() const {
   840         return kIncDecExpression;
   841     }
   842     TokenKind token() const {
   843         return token_;
   844     }
   845     Expression *expression() const {
   846         return expression_;
   847     }
   848     bool postfix() const {
   849         return postfix_;
   850     }
   851     void accept(AstVisitor *visitor) {
   852         visitor->visit(this);
   853     }
   854 };
   855 
   856 class Scope;
   857 
   858 typedef PoolList<NameProxy *> NameProxyList;
   859 
   860 class ParseTree : public PoolObject
   861 {
   862     StatementList *statements_;
   863     Scope *scope_;
   864     NameProxyList *unbound_;
   865 
   866   public:
   867     ParseTree(Scope *scope, StatementList *statements, NameProxyList *unbound)
   868       : statements_(statements),
   869         scope_(scope),
   870         unbound_(unbound)
   871     {
   872     }
   873 
   874     void dump(FILE *fp);
   875 
   876     StatementList *statements() const {
   877         return statements_;
   878     }
   879     Scope *scope() const {
   880         return scope_;
   881     }
   882     NameProxyList *unbound() const {
   883         return unbound_;
   884     }
   885 };
   886 
   887 }
   888 
   889 #endif // _include_jitcraft_ast_h_