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