src/compiler/CompileContext.h
changeset 263 ba85a47ee414
parent 262 c4b1341297e5
     1.1 --- a/src/compiler/CompileContext.h	Mon Jun 10 00:46:29 2013 +0100
     1.2 +++ b/src/compiler/CompileContext.h	Mon Jun 10 01:04:43 2013 +0100
     1.3 @@ -1,170 +1,170 @@
     1.4 -/*
     1.5 - * Copyright (C) 2012 David Anderson
     1.6 - *
     1.7 - * This file is part of SourcePawn.
     1.8 - *
     1.9 - * SourcePawn is free software: you can redistribute it and/or modify it under
    1.10 - * the terms of the GNU General Public License as published by the Free
    1.11 - * Software Foundation, either version 3 of the License, or (at your option)
    1.12 - * any later version.
    1.13 - * 
    1.14 - * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
    1.15 - * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.16 - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    1.17 - *
    1.18 - * You should have received a copy of the GNU General Public License along with
    1.19 - * SourcePawn. If not, see http://www.gnu.org/licenses/.
    1.20 - */
    1.21 -#ifndef _include_jitcraft_compile_context_h_
    1.22 -#define _include_jitcraft_compile_context_h_
    1.23 -
    1.24 -#include <stdarg.h>
    1.25 -#include <string>
    1.26 -#include "../Messages.h"
    1.27 -#include "../PoolAllocator.h"
    1.28 -#include "../Handles.h"
    1.29 -#include "../Modules.h"
    1.30 -#include "../Packages.h"
    1.31 -
    1.32 -namespace ke {
    1.33 -
    1.34 -class Zone;
    1.35 -class ParseTree;
    1.36 -class String;
    1.37 -class ImportScope;
    1.38 -class GlobalScope;
    1.39 -struct SourcePosition;
    1.40 -struct TranslationUnit;
    1.41 -class ImportStatement;
    1.42 -
    1.43 -struct CompileError
    1.44 -{
    1.45 -    CompileError()
    1.46 -      : message(NULL)
    1.47 -    {
    1.48 -    }
    1.49 -    CompileError(unsigned line, unsigned col, const char *message)
    1.50 -      : line(line),
    1.51 -        col(col),
    1.52 -        message(message)
    1.53 -    {
    1.54 -    }
    1.55 -
    1.56 -    unsigned line;
    1.57 -    unsigned col;
    1.58 -    const char *message;
    1.59 -};
    1.60 -
    1.61 -struct TranslationUnit : public PoolObject
    1.62 -{
    1.63 -    TranslationUnit(Handle<Module> module)
    1.64 -      : tree_(NULL),
    1.65 -        importScope_(NULL),
    1.66 -        globalScope_(NULL),
    1.67 -        module_(module),
    1.68 -        enoughMemory_(true),
    1.69 -        dependencyFailed_(false)
    1.70 -    {
    1.71 -    }
    1.72 -    
    1.73 -    void destroy();
    1.74 -
    1.75 -    ParseTree *tree_;
    1.76 -    PoolList<TranslationUnit *> dependents_;
    1.77 -    ImportScope *importScope_;
    1.78 -    GlobalScope *globalScope_;
    1.79 -    ScopedRoot<Module> module_;
    1.80 -
    1.81 -    ParseTree *tree() const {
    1.82 -        return tree_;
    1.83 -    }
    1.84 -    PoolList<TranslationUnit *> &dependents() {
    1.85 -        return dependents_;
    1.86 -    }
    1.87 -    bool failed() const {
    1.88 -        return errors_.length() ||
    1.89 -               !enoughMemory_ ||
    1.90 -               dependencyFailed_;
    1.91 -    }
    1.92 -    void setImportScope(ImportScope *scope) {
    1.93 -        assert(!importScope_);
    1.94 -        importScope_ = scope;
    1.95 -    }
    1.96 -    void setGlobalScope(GlobalScope *scope) {
    1.97 -        assert(!globalScope_);
    1.98 -        globalScope_ = scope;
    1.99 -    }
   1.100 -    ImportScope *importScope() const {
   1.101 -        return importScope_;
   1.102 -    }
   1.103 -    GlobalScope *globalScope() const {
   1.104 -        return globalScope_;
   1.105 -    }
   1.106 -    Handle<Module> module() {
   1.107 -        return module_;
   1.108 -    }
   1.109 -
   1.110 -    void setDependencyFailed() {
   1.111 -        dependencyFailed_ = true;
   1.112 -    }
   1.113 -    void setOutOfMemory() {
   1.114 -        enoughMemory_ = false;
   1.115 -    }
   1.116 -    void appendError(const char *report, unsigned line, unsigned col) {
   1.117 -        enoughMemory_ |= errors_.append(CompileError(line, col, report));
   1.118 -    }
   1.119 -    const Vector<CompileError> *errors() {
   1.120 -        return &errors_;
   1.121 -    }
   1.122 -
   1.123 -  private:
   1.124 -    bool enoughMemory_;
   1.125 -    bool dependencyFailed_;
   1.126 -    Vector<CompileError> errors_;
   1.127 -};
   1.128 -
   1.129 -class CompileContext
   1.130 -{
   1.131 -    Zone *zone_;
   1.132 -    Vector<TranslationUnit *> units_;
   1.133 -    Vector<TranslationUnit *> unparsed_;
   1.134 -    TranslationUnit *active_;
   1.135 -
   1.136 -  private:
   1.137 -    char *loadSource(Handle<Module> module, size_t *length);
   1.138 -    bool parse(TranslationUnit *unit);
   1.139 -    bool evaluateImports(TranslationUnit *unit);
   1.140 -    bool propagateFailure();
   1.141 -    bool add(Handle<Module> module);
   1.142 -
   1.143 -  public:
   1.144 -    CompileContext(Zone *zone);
   1.145 -    ~CompileContext();
   1.146 -
   1.147 -    bool initialize(Handle<Package> package, const char *name);
   1.148 -    bool compile();
   1.149 -    void enter(TranslationUnit *unit);
   1.150 -    void leave();
   1.151 -
   1.152 -    void reportErrorVa(const SourcePosition &pos, Message msg, va_list ap);
   1.153 -    void reportError(const SourcePosition &pos, Message msg, ...);
   1.154 -};
   1.155 -
   1.156 -class AutoEnterCompileContext
   1.157 -{
   1.158 -    CompileContext &cc_;
   1.159 -
   1.160 -  public:
   1.161 -    AutoEnterCompileContext(CompileContext &cc, TranslationUnit *unit)
   1.162 -      : cc_(cc)
   1.163 -    {
   1.164 -      cc_.enter(unit);
   1.165 -    }
   1.166 -    ~AutoEnterCompileContext() {
   1.167 -      cc_.leave();
   1.168 -    }
   1.169 -};
   1.170 -
   1.171 -} // namespace ke
   1.172 -
   1.173 -#endif // _include_jitcraft_compile_context_h_
   1.174 +/*
   1.175 + * Copyright (C) 2012 David Anderson
   1.176 + *
   1.177 + * This file is part of SourcePawn.
   1.178 + *
   1.179 + * SourcePawn is free software: you can redistribute it and/or modify it under
   1.180 + * the terms of the GNU General Public License as published by the Free
   1.181 + * Software Foundation, either version 3 of the License, or (at your option)
   1.182 + * any later version.
   1.183 + * 
   1.184 + * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
   1.185 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   1.186 + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
   1.187 + *
   1.188 + * You should have received a copy of the GNU General Public License along with
   1.189 + * SourcePawn. If not, see http://www.gnu.org/licenses/.
   1.190 + */
   1.191 +#ifndef _include_jitcraft_compile_context_h_
   1.192 +#define _include_jitcraft_compile_context_h_
   1.193 +
   1.194 +#include <stdarg.h>
   1.195 +#include <string>
   1.196 +#include "../Messages.h"
   1.197 +#include "../PoolAllocator.h"
   1.198 +#include "../Handles.h"
   1.199 +#include "../Modules.h"
   1.200 +#include "../Packages.h"
   1.201 +
   1.202 +namespace ke {
   1.203 +
   1.204 +class Zone;
   1.205 +class ParseTree;
   1.206 +class String;
   1.207 +class ImportScope;
   1.208 +class GlobalScope;
   1.209 +struct SourcePosition;
   1.210 +struct TranslationUnit;
   1.211 +class ImportStatement;
   1.212 +
   1.213 +struct CompileError
   1.214 +{
   1.215 +    CompileError()
   1.216 +      : message(NULL)
   1.217 +    {
   1.218 +    }
   1.219 +    CompileError(unsigned line, unsigned col, const char *message)
   1.220 +      : line(line),
   1.221 +        col(col),
   1.222 +        message(message)
   1.223 +    {
   1.224 +    }
   1.225 +
   1.226 +    unsigned line;
   1.227 +    unsigned col;
   1.228 +    const char *message;
   1.229 +};
   1.230 +
   1.231 +struct TranslationUnit : public PoolObject
   1.232 +{
   1.233 +    TranslationUnit(Handle<Module> module)
   1.234 +      : tree_(NULL),
   1.235 +        importScope_(NULL),
   1.236 +        globalScope_(NULL),
   1.237 +        module_(module),
   1.238 +        enoughMemory_(true),
   1.239 +        dependencyFailed_(false)
   1.240 +    {
   1.241 +    }
   1.242 +    
   1.243 +    void destroy();
   1.244 +
   1.245 +    ParseTree *tree_;
   1.246 +    PoolList<TranslationUnit *> dependents_;
   1.247 +    ImportScope *importScope_;
   1.248 +    GlobalScope *globalScope_;
   1.249 +    ScopedRoot<Module> module_;
   1.250 +
   1.251 +    ParseTree *tree() const {
   1.252 +        return tree_;
   1.253 +    }
   1.254 +    PoolList<TranslationUnit *> &dependents() {
   1.255 +        return dependents_;
   1.256 +    }
   1.257 +    bool failed() const {
   1.258 +        return errors_.length() ||
   1.259 +               !enoughMemory_ ||
   1.260 +               dependencyFailed_;
   1.261 +    }
   1.262 +    void setImportScope(ImportScope *scope) {
   1.263 +        assert(!importScope_);
   1.264 +        importScope_ = scope;
   1.265 +    }
   1.266 +    void setGlobalScope(GlobalScope *scope) {
   1.267 +        assert(!globalScope_);
   1.268 +        globalScope_ = scope;
   1.269 +    }
   1.270 +    ImportScope *importScope() const {
   1.271 +        return importScope_;
   1.272 +    }
   1.273 +    GlobalScope *globalScope() const {
   1.274 +        return globalScope_;
   1.275 +    }
   1.276 +    Handle<Module> module() {
   1.277 +        return module_;
   1.278 +    }
   1.279 +
   1.280 +    void setDependencyFailed() {
   1.281 +        dependencyFailed_ = true;
   1.282 +    }
   1.283 +    void setOutOfMemory() {
   1.284 +        enoughMemory_ = false;
   1.285 +    }
   1.286 +    void appendError(const char *report, unsigned line, unsigned col) {
   1.287 +        enoughMemory_ |= errors_.append(CompileError(line, col, report));
   1.288 +    }
   1.289 +    const Vector<CompileError> *errors() {
   1.290 +        return &errors_;
   1.291 +    }
   1.292 +
   1.293 +  private:
   1.294 +    bool enoughMemory_;
   1.295 +    bool dependencyFailed_;
   1.296 +    Vector<CompileError> errors_;
   1.297 +};
   1.298 +
   1.299 +class CompileContext
   1.300 +{
   1.301 +    Zone *zone_;
   1.302 +    Vector<TranslationUnit *> units_;
   1.303 +    Vector<TranslationUnit *> unparsed_;
   1.304 +    TranslationUnit *active_;
   1.305 +
   1.306 +  private:
   1.307 +    char *loadSource(Handle<Module> module, size_t *length);
   1.308 +    bool parse(TranslationUnit *unit);
   1.309 +    bool evaluateImports(TranslationUnit *unit);
   1.310 +    bool propagateFailure();
   1.311 +    bool add(Handle<Module> module);
   1.312 +
   1.313 +  public:
   1.314 +    CompileContext(Zone *zone);
   1.315 +    ~CompileContext();
   1.316 +
   1.317 +    bool initialize(Handle<Package> package, const char *name);
   1.318 +    bool compile();
   1.319 +    void enter(TranslationUnit *unit);
   1.320 +    void leave();
   1.321 +
   1.322 +    void reportErrorVa(const SourcePosition &pos, Message msg, va_list ap);
   1.323 +    void reportError(const SourcePosition &pos, Message msg, ...);
   1.324 +};
   1.325 +
   1.326 +class AutoEnterCompileContext
   1.327 +{
   1.328 +    CompileContext &cc_;
   1.329 +
   1.330 +  public:
   1.331 +    AutoEnterCompileContext(CompileContext &cc, TranslationUnit *unit)
   1.332 +      : cc_(cc)
   1.333 +    {
   1.334 +      cc_.enter(unit);
   1.335 +    }
   1.336 +    ~AutoEnterCompileContext() {
   1.337 +      cc_.leave();
   1.338 +    }
   1.339 +};
   1.340 +
   1.341 +} // namespace ke
   1.342 +
   1.343 +#endif // _include_jitcraft_compile_context_h_