src/compiler/CompileContext.h
author David Anderson <dvander@alliedmods.net>
Tue Jan 01 16:28:34 2013 -0800 (2013-01-01)
changeset 252 1dee2330aa78
parent 251 ca99a81745fe
child 261 219b49bf4a03
permissions -rw-r--r--
Fix linux/mac support.
     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         oom_(false),
    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                oom_ ||
    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         oom_ = true;
   112     }
   113     void appendError(const char *report, unsigned line, unsigned col) {
   114         oom_ |= errors_.append(CompileError(line, col, report));
   115     }
   116     const Vector<CompileError> *errors() {
   117         return &errors_;
   118     }
   119 
   120   private:
   121     bool oom_;
   122     bool dependencyFailed_;
   123     Vector<CompileError> errors_;
   124 };
   125 
   126 class CompileContext
   127 {
   128     Zone *zone_;
   129     const char *file_;
   130     Vector<TranslationUnit *> units_;
   131     Vector<TranslationUnit *> unparsed_;
   132     TranslationUnit *active_;
   133 
   134   private:
   135     char *loadSource(Handle<Module> module, size_t *length);
   136     bool parse(TranslationUnit *unit);
   137     bool evaluateImports(TranslationUnit *unit);
   138     bool propagateFailure();
   139     bool add(Handle<Module> module);
   140 
   141   public:
   142     CompileContext(Zone *zone, const char *file);
   143     ~CompileContext();
   144 
   145     bool initialize(Handle<Package> package, const char *name);
   146     bool compile();
   147     void enter(TranslationUnit *unit);
   148     void leave();
   149 
   150     void reportErrorVa(const SourcePosition &pos, Message msg, va_list ap);
   151     void reportError(const SourcePosition &pos, Message msg, ...);
   152 };
   153 
   154 class AutoEnterCompileContext
   155 {
   156     CompileContext &cc_;
   157 
   158   public:
   159     AutoEnterCompileContext(CompileContext &cc, TranslationUnit *unit)
   160       : cc_(cc)
   161     {
   162       cc_.enter(unit);
   163     }
   164     ~AutoEnterCompileContext() {
   165       cc_.leave();
   166     }
   167 };
   168 
   169 } // namespace ke
   170 
   171 #endif // _include_jitcraft_compile_context_h_