src/compiler/CompileContext.h
author David Anderson <dvander@alliedmods.net>
Sat Dec 22 23:18:57 2012 -0800 (2012-12-22)
changeset 233 bbb498ccb4ac
parent 196 9904633b1dfc
child 250 0998a43cc155
permissions -rw-r--r--
Re-add enum support.
     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_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 
    28 namespace ke {
    29 
    30 class Zone;
    31 class ParseTree;
    32 class String;
    33 class ImportScope;
    34 class GlobalScope;
    35 struct SourcePosition;
    36 struct TranslationUnit;
    37 class ImportStatement;
    38 
    39 struct TranslationUnit : public PoolObject
    40 {
    41     TranslationUnit()
    42       : tree_(NULL),
    43         failed_(false),
    44         importScope_(NULL),
    45         globalScope_(NULL)
    46     {
    47     }
    48 
    49     ParseTree *tree_;
    50     const char *path_;
    51     bool failed_;
    52     PoolList<TranslationUnit *> dependents_;
    53     ImportScope *importScope_;
    54     GlobalScope *globalScope_;
    55     ScopedRoot<Module> module_;
    56 
    57     ParseTree *tree() const {
    58         return tree_;
    59     }
    60     PoolList<TranslationUnit *> &dependents() {
    61         return dependents_;
    62     }
    63     bool failed() const {
    64         return failed_;
    65     }
    66     void setImportScope(ImportScope *scope) {
    67         assert(!importScope_);
    68         importScope_ = scope;
    69     }
    70     void setGlobalScope(GlobalScope *scope) {
    71         assert(!globalScope_);
    72         globalScope_ = scope;
    73     }
    74     ImportScope *importScope() const {
    75         return importScope_;
    76     }
    77     GlobalScope *globalScope() const {
    78         return globalScope_;
    79     }
    80 };
    81 
    82 class CompileContext
    83 {
    84     Zone *zone_;
    85     unsigned failures_;
    86     const char *file_;
    87     Vector<TranslationUnit *> units_;
    88     Vector<TranslationUnit *> unparsed_;
    89 
    90   private:
    91     char *loadSource(const char *file, size_t *length);
    92     bool parse(TranslationUnit *unit);
    93     bool evaluateImports(TranslationUnit *unit);
    94     bool propagateFailure();
    95 
    96   public:
    97     CompileContext(Zone *zone, const char *file);
    98     ~CompileContext();
    99 
   100     bool addTranslationUnit(TranslationUnit *unit);
   101     bool compile();
   102 
   103     void reportErrorVa(const SourcePosition &pos, Message msg, va_list ap);
   104     void reportError(const SourcePosition &pos, Message msg, ...);
   105 
   106     unsigned failures() const {
   107         return failures_;
   108     }
   109 };
   110 
   111 }
   112 
   113 #endif // _include_jitcraft_compile_context_h_