src/compiler/CompileContext.h
author David Anderson <dvander@alliedmods.net>
Sat Dec 29 00:55:38 2012 -0800 (2012-12-29)
changeset 250 0998a43cc155
parent 196 9904633b1dfc
child 251 ca99a81745fe
permissions -rw-r--r--
Collect error messages per-translation unit.
     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 CompileError
    40 {
    41     CompileError()
    42       : message(NULL)
    43     {
    44     }
    45     CompileError(unsigned line, unsigned col, const char *message)
    46       : line(line),
    47         col(col),
    48         message(message)
    49     {
    50     }
    51 
    52     unsigned line;
    53     unsigned col;
    54     const char *message;
    55 };
    56 
    57 struct TranslationUnit
    58 {
    59     TranslationUnit()
    60       : tree_(NULL),
    61         importScope_(NULL),
    62         globalScope_(NULL),
    63         oom_(false),
    64         dependencyFailed_(false)
    65     {
    66     }
    67     ~TranslationUnit();
    68 
    69     ParseTree *tree_;
    70     const char *path_;
    71     PoolList<TranslationUnit *> dependents_;
    72     ImportScope *importScope_;
    73     GlobalScope *globalScope_;
    74     ScopedRoot<Module> module_;
    75 
    76     ParseTree *tree() const {
    77         return tree_;
    78     }
    79     PoolList<TranslationUnit *> &dependents() {
    80         return dependents_;
    81     }
    82     bool failed() const {
    83         return errors_.length() ||
    84                oom_ ||
    85                dependencyFailed_;
    86     }
    87     void setImportScope(ImportScope *scope) {
    88         assert(!importScope_);
    89         importScope_ = scope;
    90     }
    91     void setGlobalScope(GlobalScope *scope) {
    92         assert(!globalScope_);
    93         globalScope_ = scope;
    94     }
    95     ImportScope *importScope() const {
    96         return importScope_;
    97     }
    98     GlobalScope *globalScope() const {
    99         return globalScope_;
   100     }
   101 
   102     void setDependencyFailed() {
   103         dependencyFailed_ = true;
   104     }
   105     void setOutOfMemory() {
   106         oom_ = true;
   107     }
   108     void appendError(const char *report, unsigned line, unsigned col) {
   109         oom_ |= errors_.append(CompileError(line, col, report));
   110     }
   111     const Vector<CompileError> *errors() {
   112         return &errors_;
   113     }
   114 
   115   private:
   116     bool oom_;
   117     bool dependencyFailed_;
   118     Vector<CompileError> errors_;
   119 };
   120 
   121 class CompileContext
   122 {
   123     Zone *zone_;
   124     const char *file_;
   125     Vector<TranslationUnit *> units_;
   126     Vector<TranslationUnit *> unparsed_;
   127     TranslationUnit *active_;
   128 
   129   private:
   130     char *loadSource(const char *file, size_t *length);
   131     bool parse(TranslationUnit *unit);
   132     void evaluateImports(TranslationUnit *unit);
   133     bool propagateFailure();
   134 
   135   public:
   136     CompileContext(Zone *zone, const char *file);
   137     ~CompileContext();
   138 
   139     bool addTranslationUnit(TranslationUnit *unit);
   140     bool compile();
   141     void enter(TranslationUnit *unit);
   142     void leave();
   143 
   144     void reportErrorVa(const SourcePosition &pos, Message msg, va_list ap);
   145     void reportError(const SourcePosition &pos, Message msg, ...);
   146 };
   147 
   148 class AutoEnterCompileContext
   149 {
   150     CompileContext &cc_;
   151 
   152   public:
   153     AutoEnterCompileContext(CompileContext &cc, TranslationUnit *unit)
   154       : cc_(cc)
   155     {
   156       cc_.enter(unit);
   157     }
   158     ~AutoEnterCompileContext() {
   159       cc_.leave();
   160     }
   161 };
   162 
   163 } // namespace ke
   164 
   165 #endif // _include_jitcraft_compile_context_h_