src/compiler/CompileContext.cpp
author David Anderson <dvander@alliedmods.net>
Sat Dec 29 00:55:38 2012 -0800 (2012-12-29)
changeset 250 0998a43cc155
parent 237 c77540a61905
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 #include <stdio.h>
    19 #include "../Zone.h"
    20 #include "../Interpreter.h"
    21 #include "Scanner.h"
    22 #include "CompileContext.h"
    23 #include "Parser.h"
    24 #include "NameBinding.h"
    25 #include "SemanticAnalysis.h"
    26 #include "../Spaces-inl.h"
    27 #include "../Heap-inl.h"
    28 
    29 using namespace ke;
    30 
    31 CompileContext::CompileContext(Zone *zone, const char *file)
    32   : zone_(zone),
    33     file_(file),
    34     active_(NULL)
    35 {
    36 }
    37 
    38 CompileContext::~CompileContext()
    39 {
    40     for (size_t i = 0; i < units_.length(); i++)
    41         delete units_[i];
    42 }
    43 
    44 bool
    45 CompileContext::addTranslationUnit(TranslationUnit *unit)
    46 {
    47     if (!units_.append(unit))
    48         return false;
    49 
    50     if (!unparsed_.append(unit))
    51         return false;
    52 
    53     return true;
    54 }
    55 
    56 char *
    57 CompileContext::loadSource(const char *file, size_t *lengthp)
    58 {
    59     char *bytes = NULL;
    60     size_t length;
    61 
    62     FILE *fp = fopen(file, "rb");
    63     if (!fp) {
    64         reportError(SourcePosition(), Message_CantOpenFile);
    65         return NULL;
    66     }
    67 
    68     if (fseek(fp, 0, SEEK_END)) {
    69         reportError(SourcePosition(), Message_ErrorSeekingFile);
    70         fclose(fp);
    71         return NULL;
    72     }
    73     long size = ftell(fp);
    74     if (size < 0) {
    75         reportError(SourcePosition(), Message_ErrorSeekingFile);
    76         fclose(fp);
    77         return NULL;
    78     }
    79     rewind(fp);
    80     length = size_t(size);
    81     bytes = new char[length + 1];
    82     if (fread(bytes, 1, length, fp) != length) {
    83         reportError(SourcePosition(), Message_ErrorReadingFile);
    84         fclose(fp);
    85         delete [] bytes;
    86         return NULL;
    87     }
    88     fclose(fp);
    89 
    90     *lengthp = length;
    91     return bytes;
    92 }
    93 
    94 bool
    95 CompileContext::parse(TranslationUnit *unit)
    96 {
    97     AutoEnterCompileContext enter(*this, unit);
    98 
    99     assert(!unit->tree_);
   100 
   101     size_t length;
   102     char *source = loadSource(unit->path_, &length);
   103     if (!source)
   104         return false;
   105 
   106     Parser parser(ZONE(), *this, source, length);
   107     if ((unit->tree_ = parser.parse()) == NULL)
   108         return false;
   109 
   110     return true;
   111 }
   112 
   113 void
   114 CompileContext::evaluateImports(TranslationUnit *unit)
   115 {
   116     if (units_.length() > 1)
   117         return;
   118 
   119 #if 0
   120     // For now we hardcode this...
   121     TranslationUnit *u2 = new (ZONE()->pool()) TranslationUnit();
   122     u2->path_ = "C:\\Users\\dvander\\jitcraft\\jitcraft-2\\src\\Debug\\wizard.sp";
   123 
   124     ImportStatement *stmt = unit->tree()->imports()->at(0)->toImportStatement();
   125     stmt->setImportSource(u2);
   126 
   127     if (!u2->dependents_.append(unit))
   128         return false;
   129 
   130     if (!addTranslationUnit(u2))
   131         return false;
   132 #endif
   133 }
   134 
   135 bool
   136 CompileContext::propagateFailure()
   137 {
   138     Vector<TranslationUnit *> worklist;
   139     for (size_t i = 0; i < units_.length(); i++) {
   140         if (units_[i]->failed() && !worklist.append(units_[i]))
   141             return false;
   142     }
   143 
   144     while (!worklist.empty()) {
   145         TranslationUnit *unit = worklist.popCopy();
   146         for (size_t i = 0; i < unit->dependents().length(); i++) {
   147             TranslationUnit *other = unit->dependents().at(i);
   148             if (other->failed())
   149                 continue;
   150 
   151             other->setDependencyFailed();
   152             if (!worklist.append(other))
   153                 return false;
   154         }
   155     }
   156 
   157     return true;
   158 }
   159 
   160 bool
   161 CompileContext::compile()
   162 {
   163     // Phase 1. Parse all modules we're importing. This process may load new
   164     // modules off disk, and add them to the parse queue.
   165     while (!unparsed_.empty()) {
   166         TranslationUnit *unit = unparsed_.popCopy();
   167 
   168         if (!parse(unit))
   169             continue;
   170 
   171         evaluateImports(unit);
   172     }
   173 
   174     // Phase 2. Create scopes and symbol tables for every lexical region with
   175     // a declaration. This phase excludes declarations created via imports.
   176     for (size_t i = 0; i < units_.length(); i++) {
   177         TranslationUnit *unit = units_[i];
   178         if (unit->failed())
   179             continue;
   180 
   181         PopulateNamesAndTypes(zone_, *this, unit);
   182     }
   183 
   184     // Phase 3. Complete imports and bind all names.
   185     for (size_t i = 0; i < units_.length(); i++) {
   186         TranslationUnit *unit = units_[i];
   187         if (unit->failed())
   188             continue;
   189 
   190         BindNamesAndTypes(zone_, *this, unit);
   191     }
   192 
   193     // Phase 4. Assign types to every public name in the module.
   194     for (size_t i = 0; i < units_.length(); i++) {
   195         TranslationUnit *unit = units_[i];
   196         if (unit->failed())
   197             continue;
   198 
   199     }
   200 
   201     // Intermediate phase. Decide which modules we should not perform
   202     // semantic analysis on.
   203     if (!propagateFailure())
   204         return false;
   205 
   206     // Phase 4. Now we can finally perform SemA+bytecode compilation.
   207     for (size_t i = 0; i < units_.length(); i++) {
   208         TranslationUnit *unit = units_[i];
   209         if (unit->failed())
   210             continue;
   211 
   212         AutoEnterCompileContext enter(*this, unit);
   213         SemanticAnalysis sema(zone_, *this, unit);
   214         sema.analyze();
   215     }
   216 
   217     // Phase 5. Run modules.
   218     for (size_t i = 0; i < units_.length(); i++) {
   219         TranslationUnit *unit = units_[i];
   220         if (unit->failed())
   221             break;
   222 
   223         InvokeArgs args;
   224         if (!zone_->stack().pushModuleFrame(unit->module_, args))
   225             assert(false);
   226 
   227         ke::LocalSlot slot;
   228         if (!Interpret(zone_, NULL, &slot))
   229             assert(false);
   230     }
   231 
   232     if (Ke::IMessageReporter *reporter = zone_->errorReporter()) {
   233         for (size_t i = 0; i < units_.length(); i++) {
   234             TranslationUnit *unit = units_[i];
   235 
   236             // We don't explicitly report errors for dependency failures or OOM
   237             // (OOM is reported separately (:TODO:) and dep failures are redundant.
   238             if (!unit->errors()->length())
   239                 continue;
   240 
   241             reporter->beginCompilationErrors(unit->path_);
   242             for (size_t j = 0; j < unit->errors()->length(); j++) {
   243                 const CompileError &error = unit->errors()->at(j);
   244                 reporter->reportCompilationError(error.message, error.line, error.col);
   245             }
   246             reporter->endCompilationErrors();
   247         }
   248     }
   249 
   250     return !units_[0]->failed();
   251 }
   252 
   253 void
   254 CompileContext::enter(TranslationUnit *unit)
   255 {
   256     assert(!active_);
   257     active_ = unit;
   258 }
   259 
   260 void
   261 CompileContext::leave()
   262 {
   263     assert(active_);
   264     active_ = NULL;
   265 }
   266 
   267 void
   268 CompileContext::reportErrorVa(const SourcePosition &pos, Message msg, va_list ap)
   269 {
   270     const char *report = BuildErrorMessage(Messages[msg].format, ap);
   271     if (!report) {
   272         active_->setOutOfMemory();
   273         return;
   274     }
   275 
   276     active_->appendError(report, pos.line, pos.col);
   277 }
   278 
   279 void
   280 CompileContext::reportError(const SourcePosition &pos, Message msg, ...)
   281 {
   282     va_list ap;
   283     va_start(ap, msg);
   284     reportErrorVa(pos, msg, ap);
   285     va_end(ap);
   286 }
   287 
   288 TranslationUnit::~TranslationUnit()
   289 {
   290     for (size_t i = 0; i < errors_.length(); i++)
   291         FreeErrorMessage(errors_[i].message);
   292 }