jitcraft/SemanticAnalysis.h
author David Anderson <dvander@alliedmods.net>
Tue Jul 03 00:21:37 2012 -0700 (2012-07-03)
changeset 29 c0e27b4f0672
parent 25 23e8b3d8e4e6
child 30 8c8bf75114f7
permissions -rw-r--r--
Added RootScopes for AST roots.
     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_semantic_analysis_h_
    19 #define _include_semantic_analysis_h_
    20 
    21 #include <assert.h>
    22 #include "AST.h"
    23 
    24 namespace jc {
    25 
    26 class SemanticAnalysis : public AstVisitor
    27 {
    28     CompileContext &cc_;
    29     ParseTree *tree_;
    30     unsigned loopDepth_;
    31     Scope *scope_;
    32     Type *function_;
    33 
    34   private:
    35     bool canTest(AstNode *node, Type *type);
    36     void unaryTypeError(AstNode *node, TokenKind token, Type *type);
    37     void binaryTypeError(AstNode *node, TokenKind token, Type *left, Type *right);
    38     Type *typeCheckBinary(AstNode *node, TokenKind token, Type *left, Type *right);
    39 
    40     // These check if one type is compatible with another type, for various
    41     // purposes, like:
    42     //      return (expr) matches function return type
    43     //      call(a, b)    matches expected arg type
    44     //      a = b         matches declared type
    45     //
    46     bool typeCheckArrayAssignment(AstNode *node, Type *given, Type *actual, bool forAssign);
    47     bool typeCheckAssignment(AstNode *node, Type *given, Type *actual, bool forAssign);
    48 
    49     bool typeCheckParameter(AstNode *node, Type *given, Type *actual);
    50 
    51     Type *visitForLoad(Expression *expression);
    52     Type *visitForRef(Expression *expression);
    53     bool preanalyze(FunctionStatement *node);
    54     Type *visitForLValue(Expression *expression);
    55     Type *visitAndApplyType(Expression *expression);
    56 
    57   public:
    58     SemanticAnalysis(CompileContext &cc, ParseTree *tree);
    59     bool analyze();
    60 
    61   public:
    62     void visit(NameProxy *name);
    63     void visit(Assignment *node);
    64     void visit(BinaryExpression *node);
    65     void visit(ReturnStatement *node);
    66     void visit(ForStatement *node);
    67     void visit(BlockStatement *node);
    68     void visit(VariableDeclaration *node);
    69     void visit(IntegerLiteral *node);
    70     void visit(ExpressionStatement *node);
    71     void visit(FunctionStatement *node);
    72     void visit(CallExpression *node);
    73     void visit(IfStatement *node);
    74     void visit(IndexExpression *node);
    75     void visit(EnumStatement *node);
    76     void visit(FloatLiteral *node);
    77     void visit(WhileStatement *node);
    78     void visit(BreakStatement *node);
    79     void visit(ContinueStatement *node);
    80     void visit(StringLiteral *node);
    81     void visit(IncDecExpression *node);
    82 };
    83 
    84 } // namespace jc
    85 
    86 #endif // _include_semantic_analysis_h_