jitcraft/SemanticAnalysis.h
author David Anderson <dvander@alliedmods.net>
Wed Jul 04 19:39:00 2012 -0400 (2012-07-04)
changeset 47 050939c6181e
parent 40 ce787d61fb71
child 59 b7d61ddb3c8d
permissions -rw-r--r--
Implemented gcmaps and stack frame tracing.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of JITCraft.
     6  *
     7  * JITCraft 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  * Foobar 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  * JITCraft. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #ifndef _include_semantic_analysis_h_
    20 #define _include_semantic_analysis_h_
    21 
    22 #include <assert.h>
    23 #include "AST.h"
    24 
    25 namespace jc {
    26 
    27 class SemanticAnalysis : public AstVisitor
    28 {
    29     Zone *zone_;
    30     CompileContext &cc_;
    31     ParseTree *tree_;
    32     unsigned loopDepth_;
    33     Scope *scope_;
    34     ScopedRoot<Type> function_;
    35 
    36   private:
    37     bool canTest(AstNode *node, Handle<Type> type);
    38     void unaryTypeError(AstNode *node, TokenKind token, Handle<Type> type);
    39     void binaryTypeError(AstNode *node, TokenKind token, Handle<Type> left, Handle<Type> right);
    40     Type *typeCheckBinary(AstNode *node, TokenKind token, Handle<Type> left, Handle<Type> right);
    41 
    42     // These check if one type is compatible with another type, for various
    43     // purposes, like:
    44     //      return (expr) matches function return type
    45     //      call(a, b)    matches expected arg type
    46     //      a = b         matches declared type
    47     //
    48     bool typeCheckArrayAssignment(AstNode *node, Handle<Type> given, Handle<Type> actual, bool forAssign);
    49     bool typeCheckAssignment(AstNode *node, Handle<Type> given, Handle<Type> actual, bool forAssign);
    50 
    51     bool typeCheckParameter(AstNode *node, Handle<Type> given, Handle<Type> actual);
    52 
    53     Type *visitForLoad(Expression *expression);
    54     Type *visitForRef(Expression *expression);
    55     bool preanalyze(FunctionStatement *node);
    56     Type *visitForLValue(Expression *expression);
    57     Type *visitAndApplyType(Expression *expression);
    58 
    59   public:
    60     SemanticAnalysis(Zone *zone, CompileContext &cc, ParseTree *tree);
    61     bool analyze();
    62 
    63   public:
    64     void visit(NameProxy *name);
    65     void visit(Assignment *node);
    66     void visit(BinaryExpression *node);
    67     void visit(ReturnStatement *node);
    68     void visit(ForStatement *node);
    69     void visit(BlockStatement *node);
    70     void visit(VariableDeclaration *node);
    71     void visit(IntegerLiteral *node);
    72     void visit(ExpressionStatement *node);
    73     void visit(FunctionStatement *node);
    74     void visit(CallExpression *node);
    75     void visit(IfStatement *node);
    76     void visit(IndexExpression *node);
    77     void visit(EnumStatement *node);
    78     void visit(FloatLiteral *node);
    79     void visit(WhileStatement *node);
    80     void visit(BreakStatement *node);
    81     void visit(ContinueStatement *node);
    82     void visit(StringLiteral *node);
    83     void visit(IncDecExpression *node);
    84 };
    85 
    86 } // namespace jc
    87 
    88 #endif // _include_semantic_analysis_h_