src/compiler/CompileContext.h
author Asher Baker <asherkin@gmail.com>
Mon Jun 10 00:46:29 2013 +0100 (2013-06-10)
changeset 262 c4b1341297e5
parent 261 219b49bf4a03
child 263 ba85a47ee414
permissions -rw-r--r--
Fix warnings when building with clang.
     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_jitcraft_compile_context_h_
    19 #define _include_jitcraft_compile_context_h_
    20 
    21 #include <stdarg.h>
    22 #include <string>
    23 #include "../Messages.h"
    24 #include "../PoolAllocator.h"
    25 #include "../Handles.h"
    26 #include "../Modules.h"
    27 #include "../Packages.h"
    28 
    29 namespace ke {
    30 
    31 class Zone;
    32 class ParseTree;
    33 class String;
    34 class ImportScope;
    35 class GlobalScope;
    36 struct SourcePosition;
    37 struct TranslationUnit;
    38 class ImportStatement;
    39 
    40 struct CompileError
    41 {
    42     CompileError()
    43       : message(NULL)
    44     {
    45     }
    46     CompileError(unsigned line, unsigned col, const char *message)
    47       : line(line),
    48         col(col),
    49         message(message)
    50     {
    51     }
    52 
    53     unsigned line;
    54     unsigned col;
    55     const char *message;
    56 };
    57 
    58 struct TranslationUnit : public PoolObject
    59 {
    60     TranslationUnit(Handle<Module> module)
    61       : tree_(NULL),
    62         importScope_(NULL),
    63         globalScope_(NULL),
    64         module_(module),
    65         enoughMemory_(true),
    66         dependencyFailed_(false)
    67     {
    68     }
    69     
    70     void destroy();
    71 
    72     ParseTree *tree_;
    73     PoolList<TranslationUnit *> dependents_;
    74     ImportScope *importScope_;
    75     GlobalScope *globalScope_;
    76     ScopedRoot<Module> module_;
    77 
    78     ParseTree *tree() const {
    79         return tree_;
    80     }
    81     PoolList<TranslationUnit *> &dependents() {
    82         return dependents_;
    83     }
    84     bool failed() const {
    85         return errors_.length() ||
    86                !enoughMemory_ ||
    87                dependencyFailed_;
    88     }
    89     void setImportScope(ImportScope *scope) {
    90         assert(!importScope_);
    91         importScope_ = scope;
    92     }
    93     void setGlobalScope(GlobalScope *scope) {
    94         assert(!globalScope_);
    95         globalScope_ = scope;
    96     }
    97     ImportScope *importScope() const {
    98         return importScope_;
    99     }
   100     GlobalScope *globalScope() const {
   101         return globalScope_;
   102     }
   103     Handle<Module> module() {
   104         return module_;
   105     }
   106 
   107     void setDependencyFailed() {
   108         dependencyFailed_ = true;
   109     }
   110     void setOutOfMemory() {
   111         enoughMemory_ = false;
   112     }
   113     void appendError(const char *report, unsigned line, unsigned col) {
   114         enoughMemory_ |= errors_.append(CompileError(line, col, report));
   115     }
   116     const Vector<CompileError> *errors() {
   117         return &errors_;
   118     }
   119 
   120   private:
   121     bool enoughMemory_;
   122     bool dependencyFailed_;
   123     Vector<CompileError> errors_;
   124 };
   125 
   126 class CompileContext
   127 {
   128     Zone *zone_;
   129     Vector<TranslationUnit *> units_;
   130     Vector<TranslationUnit *> unparsed_;
   131     TranslationUnit *active_;
   132 
   133   private:
   134     char *loadSource(Handle<Module> module, size_t *length);
   135     bool parse(TranslationUnit *unit);
   136     bool evaluateImports(TranslationUnit *unit);
   137     bool propagateFailure();
   138     bool add(Handle<Module> module);
   139 
   140   public:
   141     CompileContext(Zone *zone);
   142     ~CompileContext();
   143 
   144     bool initialize(Handle<Package> package, const char *name);
   145     bool compile();
   146     void enter(TranslationUnit *unit);
   147     void leave();
   148 
   149     void reportErrorVa(const SourcePosition &pos, Message msg, va_list ap);
   150     void reportError(const SourcePosition &pos, Message msg, ...);
   151 };
   152 
   153 class AutoEnterCompileContext
   154 {
   155     CompileContext &cc_;
   156 
   157   public:
   158     AutoEnterCompileContext(CompileContext &cc, TranslationUnit *unit)
   159       : cc_(cc)
   160     {
   161       cc_.enter(unit);
   162     }
   163     ~AutoEnterCompileContext() {
   164       cc_.leave();
   165     }
   166 };
   167 
   168 } // namespace ke
   169 
   170 #endif // _include_jitcraft_compile_context_h_