src/compiler/SemanticAnalysis.h
author David Anderson <dvander@alliedmods.net>
Sat Nov 17 23:12:50 2012 -0800 (2012-11-17)
changeset 210 2f9997088e9b
parent 208 7c5be05296e3
child 212 443d24cfdf2d
permissions -rw-r--r--
Added missing file.
     1 /*
     2  * Copyright (C) 2012 David Anderson
     3  *
     4  * This file is part of SourcePawn.
     5  *
     6  * SourcePawn 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  * SourcePawn 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  * SourcePawn. If not, see http://www.gnu.org/licenses/.
    17  */
    18 #ifndef _include_sp2_sema_h_
    19 #define _include_sp2_sema_h_
    20 
    21 #include "AST.h"
    22 #include "HIR.h"
    23 #include "Scopes.h"
    24 #include "BytecodeEmitter.h"
    25 
    26 namespace ke {
    27 
    28 // Semantic analysis occurs after all names and types have been bound. It
    29 // performs the important task of validating the semantics and type safety
    30 // of a parse tree.
    31 // 
    32 // The semantic analysis phase also produces bytecode. This occurs on a
    33 // statement-by-statement basis. The result of analyzing an expression is a
    34 // tree of operations which is then flattened.
    35 class SemanticAnalysis : public AstVisitor
    36 {
    37   public:
    38     SemanticAnalysis(Zone *zone, CompileContext &cc, TranslationUnit *unit);
    39 
    40     void analyze();
    41 
    42   private:
    43     SemanticAnalysis(SemanticAnalysis *parent, Handle<FunctionType> fun);
    44     Function *compile(FunctionStatement *stmt);
    45 
    46   public:
    47     void visitFunctionStatement(FunctionStatement *node);
    48     void visitBlockStatement(BlockStatement *node);
    49     void visitExpressionStatement(ExpressionStatement *node);
    50     void visitReturnStatement(ReturnStatement *node);
    51 
    52     void visitCallExpression(CallExpression *node);
    53     void visitNameProxy(NameProxy *name);
    54     void visitIntegerLiteral(IntegerLiteral *node);
    55     void visitFloatLiteral(FloatLiteral *node);
    56     void visitVariableDeclaration(VariableDeclaration *node);
    57     void visitForStatement(ForStatement *node);
    58     void visitWhileStatement(WhileStatement *node);
    59     void visitAssignment(Assignment *node);
    60     void visitBinaryExpression(BinaryExpression *node);
    61     void visitBreakStatement(BreakStatement *node);
    62     void visitContinueStatement(ContinueStatement *node);
    63     void visitIfStatement(IfStatement *node);
    64 #if 0
    65     void visitFunctionTypeStatement(FunctionTypeStatement *node);
    66     void visitImportStatement(ImportStatement *node);
    67     void visitExpressionStatement(ExpressionStatement *node);
    68     void visitEnumStatement(EnumStatement *node);
    69     void visitStructureStatement(StructureStatement *node);
    70     void visitSwitchStatement(SwitchStatement *node);
    71 
    72     void visitBinaryExpression(BinaryExpression *node);
    73     void visitCallExpression(CallExpression *node);
    74     void visitIndexExpression(IndexExpression *node);
    75     void visitStringLiteral(StringLiteral *node);
    76     void visitIncDecExpression(IncDecExpression *node);
    77     void visitUnaryExpression(UnaryExpression *node);
    78     void visitTernaryExpression(TernaryExpression *node);
    79     void visitBooleanLiteral(BooleanLiteral *node);
    80     void visitFieldExpression(FieldExpression *node);
    81     void visitArrayLiteral(ArrayLiteral *node);
    82 #endif
    83 
    84   private:
    85     bool checkArgumentCount(Handle<FunctionType> type, unsigned actual);
    86 
    87   private:
    88     enum CoercionKind {
    89         Coerce_Arg,
    90         Coerce_Return,
    91         Coerce_Assign
    92     };
    93 
    94     void visitForTest(Expression *expr,
    95                       BytecodeEmitter::Label *trueBranch,
    96                       BytecodeEmitter::Label *falseBranch,
    97                       BytecodeEmitter::Label *fallthrough);
    98     void set(VariableSymbol *sym);
    99     HIR *eval(Expression *expr);
   100     HIR *binary(TokenKind token, HIR *left, HIR *right);
   101     HIR *coerce(HIR *from, Handle<Type> to, CoercionKind kind);
   102     Type *coercionType(TokenKind token, HIR *left, HIR *right);
   103     HLValue *lvalue(Expression *expr);
   104     void discharge(HIR *hir);
   105 
   106   private:
   107     // Used to target break/catch statements.
   108     class LoopScope
   109     {
   110         Scope *scope_;
   111         BytecodeEmitter::Label *bk_;
   112         BytecodeEmitter::Label *ct_;
   113         LoopScope **prevp_;
   114         LoopScope *prev_;
   115 
   116       public:
   117         LoopScope(Scope *scope, BytecodeEmitter::Label *bk, BytecodeEmitter::Label *ct,
   118                   LoopScope **prevp)
   119           : scope_(scope),
   120             bk_(bk),
   121             ct_(ct),
   122             prevp_(prevp),
   123             prev_(*prevp)
   124         {
   125             *prevp_ = this;
   126         }
   127 
   128         ~LoopScope()
   129         {
   130             *prevp_ = prev_;
   131         }
   132 
   133         BytecodeEmitter::Label *break_() {
   134             return bk_;
   135         }
   136         BytecodeEmitter::Label *continue_() {
   137             return ct_;
   138         }
   139         Scope *scope() {
   140             return scope_;
   141         }
   142     };
   143     
   144   private:
   145     Zone *zone_;
   146     PoolAllocator &pool_;
   147     CompileContext &cc_;
   148     TranslationUnit *unit_;
   149     HIR *hir_;
   150     Scope *scope_;
   151     BytecodeEmitter emitter_;
   152     ScopedRoot<Module> module_;
   153     ScopedRoot<FunctionType> fun_;
   154     LoopScope *loop_;
   155 };
   156 
   157 } // namespace ke
   158 
   159 #endif // _include_sp2_sema_h_