src/compiler/SemanticAnalysis.h
author David Anderson <dvander@alliedmods.net>
Sun Jan 06 13:52:21 2013 -0800 (2013-01-06)
changeset 256 3c184218462d
parent 251 ca99a81745fe
child 263 ba85a47ee414
permissions -rw-r--r--
Initial implementation of module imports.
[email protected]
     1
/*
[email protected]
     2
 * Copyright (C) 2012 David Anderson
[email protected]
     3
 *
[email protected]
     4
 * This file is part of SourcePawn.
[email protected]
     5
 *
[email protected]
     6
 * SourcePawn is free software: you can redistribute it and/or modify it under
[email protected]
     7
 * the terms of the GNU General Public License as published by the Free
[email protected]
     8
 * Software Foundation, either version 3 of the License, or (at your option)
[email protected]
     9
 * any later version.
[email protected]
    10
 * 
[email protected]
    11
 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
[email protected]
    12
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
[email protected]
    13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
[email protected]
    14
 *
[email protected]
    15
 * You should have received a copy of the GNU General Public License along with
[email protected]
    16
 * SourcePawn. If not, see http://www.gnu.org/licenses/.
[email protected]
    17
 */
[email protected]
    18
#ifndef _include_sp2_sema_h_
[email protected]
    19
#define _include_sp2_sema_h_
[email protected]
    20
[email protected]
    21
#include "AST.h"
[email protected]
    22
#include "HIR.h"
[email protected]
    23
#include "Scopes.h"
[email protected]
    24
#include "BytecodeEmitter.h"
[email protected]
    25
[email protected]
    26
namespace ke {
[email protected]
    27
[email protected]
    28
// Semantic analysis occurs after all names and types have been bound. It
[email protected]
    29
// performs the important task of validating the semantics and type safety
[email protected]
    30
// of a parse tree.
[email protected]
    31
// 
[email protected]
    32
// The semantic analysis phase also produces bytecode. This occurs on a
[email protected]
    33
// statement-by-statement basis. The result of analyzing an expression is a
[email protected]
    34
// tree of operations which is then flattened.
[email protected]
    35
class SemanticAnalysis : public AstVisitor
[email protected]
    36
{
[email protected]
    37
  public:
[email protected]
    38
    SemanticAnalysis(Zone *zone, CompileContext &cc, TranslationUnit *unit);
[email protected]
    39
[email protected]
    40
    void analyze();
[email protected]
    41
[email protected]
    42
  private:
[email protected]
    43
    SemanticAnalysis(SemanticAnalysis *parent, Handle<FunctionType> fun);
[email protected]
    44
    Function *compile(FunctionStatement *stmt);
[email protected]
    45
[email protected]
    46
  public:
[email protected]
    47
    void visitFunctionStatement(FunctionStatement *node);
[email protected]
    48
    void visitBlockStatement(BlockStatement *node);
[email protected]
    49
    void visitExpressionStatement(ExpressionStatement *node);
[email protected]
    50
    void visitReturnStatement(ReturnStatement *node);
[email protected]
    51
    void visitEnumStatement(EnumStatement *node);
[email protected]
    52
[email protected]
    53
    void visitCallExpression(CallExpression *node);
[email protected]
    54
    void visitNameProxy(NameProxy *name);
[email protected]
    55
    void visitIntegerLiteral(IntegerLiteral *node);
[email protected]
    56
    void visitFloatLiteral(FloatLiteral *node);
[email protected]
    57
    void visitVariableDeclaration(VariableDeclaration *node);
[email protected]
    58
    void visitForStatement(ForStatement *node);
[email protected]
    59
    void visitWhileStatement(WhileStatement *node);
[email protected]
    60
    void visitAssignment(Assignment *node);
[email protected]
    61
    void visitBinaryExpression(BinaryExpression *node);
[email protected]
    62
    void visitBreakStatement(BreakStatement *node);
[email protected]
    63
    void visitContinueStatement(ContinueStatement *node);
[email protected]208
    64
    void visitIfStatement(IfStatement *node);
[email protected]
    65
    void visitIndexExpression(IndexExpression *node);
[email protected]
    66
    void visitIncDecExpression(IncDecExpression *node);
[email protected]
    67
    void visitStringLiteral(StringLiteral *node);
[email protected]
    68
    void visitUnaryExpression(UnaryExpression *node);
[email protected]
    69
    void visitBooleanLiteral(BooleanLiteral *node);
[email protected]
    70
    void visitFieldExpression(FieldExpression *node);
[email protected]
    71
#if 0
[email protected]
    72
    void visitFunctionTypeStatement(FunctionTypeStatement *node);
[email protected]
    73
    void visitImportStatement(ImportStatement *node);
[email protected]
    74
    void visitStructureStatement(StructureStatement *node);
[email protected]
    75
    void visitSwitchStatement(SwitchStatement *node);
[email protected]
    76
[email protected]
    77
    void visitTernaryExpression(TernaryExpression *node);
[email protected]
    78
    void visitArrayLiteral(ArrayLiteral *node);
[email protected]
    79
#endif
[email protected]
    80
[email protected]
    81
  private:
[email protected]
    82
    bool checkArgumentCount(Handle<FunctionType> type, unsigned actual);
[email protected]
    83
    void import(HIR *hir, FieldExpression *node);
[email protected]
    84
[email protected]
    85
  private:
[email protected]
    86
    enum CoercionKind {
[email protected]
    87
        Coerce_Arg,
[email protected]
    88
        Coerce_Return,
[email protected]
    89
        Coerce_Decl,
[email protected]
    90
        Coerce_Assign
[email protected]
    91
    };
[email protected]
    92
[email protected]
    93
    // For a given control flow path, whether it returns.
[email protected]
    94
    enum ReturnStatus
[email protected]
    95
    {
[email protected]
    96
        Returns_Never,
[email protected]
    97
        Returns_Sometimes,
[email protected]
    98
        Returns_Always
[email protected]
    99
    };
[email protected]
   100
[email protected]
   101
    // For a given control flow path, whether it terminates,
[email protected]
   102
    // and if it terminates, whether it returns.
[email protected]
   103
    class ControlStatus
[email protected]
   104
    {
[email protected]
   105
        bool terminates_;
[email protected]
   106
        ReturnStatus returns_;
[email protected]
   107
[email protected]
   108
      public:
[email protected]
   109
        ControlStatus()
[email protected]
   110
        {
[email protected]
   111
        }
[email protected]
   112
        ControlStatus(bool terminates, ReturnStatus returns)
[email protected]
   113
          : terminates_(terminates),
[email protected]
   114
            returns_(returns)
[email protected]
   115
        {
[email protected]
   116
        }
[email protected]
   117
[email protected]
   118
        bool terminates() const {
[email protected]
   119
            return terminates_;
[email protected]
   120
        }
[email protected]
   121
        ReturnStatus returns() const {
[email protected]
   122
            return returns_;
[email protected]
   123
        }
[email protected]
   124
    };
[email protected]
   125
[email protected]
   126
    class ControlScope
[email protected]
   127
    {
[email protected]
   128
        ReturnStatus returns_;
[email protected]
   129
        ControlScope **prevp_;
[email protected]
   130
        ControlScope *prev_;
[email protected]
   131
        bool terminates_;
[email protected]
   132
[email protected]
   133
      public:
[email protected]
   134
        ControlScope(ControlScope **prevp)
[email protected]
   135
          : returns_(Returns_Never),
[email protected]
   136
            prevp_(prevp),
[email protected]
   137
            prev_(*prevp),
[email protected]
   138
            terminates_(false)
[email protected]
   139
        {
[email protected]
   140
            *prevp_ = this;
[email protected]
   141
        }
[email protected]
   142
        ~ControlScope() {
[email protected]
   143
            *prevp_ = prev_;
[email protected]
   144
        }
[email protected]
   145
        ControlStatus status() {
[email protected]
   146
            return ControlStatus(terminates_, returns_);
[email protected]
   147
        }
[email protected]
   148
[email protected]
   149
        void returns(ReturnStatus status) {
[email protected]
   150
            // We allow multiple terminates, but only the first one takes
[email protected]
   151
            // effect. For example, |break; continue;| is legal, but the
[email protected]
   152
            // continue is unreachable.
[email protected]
   153
            if (terminates_)
[email protected]
   154
                return;
[email protected]
   155
            if (status > returns_)
[email protected]
   156
                returns_ = status;
[email protected]
   157
        }
[email protected]
   158
        void terminate(ReturnStatus status) {
[email protected]
   159
            if (terminates_)
[email protected]
   160
                return;
[email protected]
   161
            terminates_ = true;
[email protected]
   162
            returns_ = status;
[email protected]
   163
        }
[email protected]
   164
        void merge(const ControlStatus &left, const ControlStatus &right);
[email protected]
   165
        void inherit(const ControlStatus &other);
[email protected]
   166
    };
[email protected]
   167
[email protected]
   168
[email protected]
   169
    void requireLifoStack();
[email protected]
   170
    ControlStatus visitForControl(Statement *statement);
[email protected]
   171
    ControlStatus visitLoopBody(Statement *stmt);
[email protected]
   172
    void visitForTest(Expression *expr,
[email protected]
   173
                      BytecodeEmitter::Label *trueBranch,
[email protected]
   174
                      BytecodeEmitter::Label *falseBranch,
[email protected]
   175
                      BytecodeEmitter::Label *fallthrough);
[email protected]
   176
    void set(VariableSymbol *sym);
[email protected]
   177
    HIR *eval(Expression *expr);
[email protected]
   178
    HIR *binary(AstNode *node, TokenKind token, HIR *left, HIR *right);
[email protected]
   179
    HIR *coerce(HIR *from, Handle<Type> to, CoercionKind kind);
[email protected]
   180
    Type *coercionType(TokenKind token, HIR *left, HIR *right);
[email protected]
   181
    HLValue *lvalue(Expression *expr);
[email protected]
   182
    void discharge(HIR *hir);
[email protected]
   183
    HIR *typeFailure(const SourcePosition &pos, Handle<Type> from, Handle<Type> to);
[email protected]
   184
[email protected]
   185
    HIR *typeCheckArray(const SourcePosition &pos,
[email protected]
   186
                        HIR *from,
[email protected]
   187
                        Handle<ArrayType> argActual,
[email protected]
   188
                        CoercionKind kind);
[email protected]
   189
[email protected]
   190
  private:
[email protected]
   191
    // Used to target break/catch statements.
[email protected]
   192
    class LoopScope
[email protected]
   193
    {
[email protected]
   194
        Scope *scope_;
[email protected]
   195
        BytecodeEmitter::Label *bk_;
[email protected]
   196
        BytecodeEmitter::Label *ct_;
[email protected]
   197
        LoopScope **prevp_;
[email protected]
   198
        LoopScope *prev_;
[email protected]
   199
        bool usesLifoStack_;
[email protected]
   200
        bool breaks_;
[email protected]
   201
[email protected]
   202
      public:
[email protected]
   203
        LoopScope(Scope *scope, BytecodeEmitter::Label *bk, BytecodeEmitter::Label *ct,
[email protected]
   204
                  LoopScope **prevp)
[email protected]
   205
          : scope_(scope),
[email protected]
   206
            bk_(bk),
[email protected]
   207
            ct_(ct),
[email protected]
   208
            prevp_(prevp),
[email protected]
   209
            prev_(*prevp),
[email protected]
   210
            usesLifoStack_(false),
[email protected]
   211
            breaks_(false)
[email protected]
   212
        {
[email protected]
   213
            *prevp_ = this;
[email protected]
   214
        }
[email protected]
   215
[email protected]
   216
        ~LoopScope()
[email protected]
   217
        {
[email protected]
   218
            *prevp_ = prev_;
[email protected]
   219
        }
[email protected]
   220
[email protected]
   221
        BytecodeEmitter::Label *break_() {
[email protected]
   222
            return bk_;
[email protected]
   223
        }
[email protected]
   224
        BytecodeEmitter::Label *continue_() {
[email protected]
   225
            return ct_;
[email protected]
   226
        }
[email protected]
   227
        Scope *scope() {
[email protected]
   228
            return scope_;
[email protected]
   229
        }
[email protected]
   230
        void setUsesLifoStack() {
[email protected]
   231
            usesLifoStack_ = true;
[email protected]
   232
        }
[email protected]
   233
        bool usesLifoStack() const {
[email protected]
   234
            return usesLifoStack_;
[email protected]
   235
        }
[email protected]
   236
        void setBreaks() {
[email protected]
   237
            breaks_ = true;
[email protected]
   238
        }
[email protected]
   239
        bool breaks() const {
[email protected]
   240
            return breaks_;
[email protected]
   241
        }
[email protected]
   242
    };
[email protected]
   243
    
[email protected]
   244
  private:
[email protected]
   245
    Zone *zone_;
[email protected]
   246
    PoolAllocator &pool_;
[email protected]
   247
    CompileContext &cc_;
[email protected]
   248
    TranslationUnit *unit_;
[email protected]
   249
    HIR *hir_;
[email protected]
   250
    Scope *scope_;
[email protected]
   251
    BytecodeEmitter emitter_;
[email protected]
   252
    ScopedRoot<FunctionType> fun_;
[email protected]
   253
    LoopScope *loop_;
[email protected]
   254
    ControlScope *control_;
[email protected]
   255
};
[email protected]
   256
[email protected]
   257
} // namespace ke
[email protected]
   258
[email protected]
   259
#endif // _include_sp2_sema_h_