src/compiler/CompileContext.h
author Asher Baker <asherkin@gmail.com>
Mon Jun 10 15:52:44 2013 +0100 (2013-06-10)
changeset 265 3a903b89a514
parent 263 ba85a47ee414
permissions -rw-r--r--
Fixed type checking for multidimensional arrays.
asherkin@263
     1
/*
asherkin@263
     2
 * Copyright (C) 2012 David Anderson
asherkin@263
     3
 *
asherkin@263
     4
 * This file is part of SourcePawn.
asherkin@263
     5
 *
asherkin@263
     6
 * SourcePawn is free software: you can redistribute it and/or modify it under
asherkin@263
     7
 * the terms of the GNU General Public License as published by the Free
asherkin@263
     8
 * Software Foundation, either version 3 of the License, or (at your option)
asherkin@263
     9
 * any later version.
asherkin@263
    10
 * 
asherkin@263
    11
 * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
asherkin@263
    12
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
asherkin@263
    13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
asherkin@263
    14
 *
asherkin@263
    15
 * You should have received a copy of the GNU General Public License along with
asherkin@263
    16
 * SourcePawn. If not, see http://www.gnu.org/licenses/.
asherkin@263
    17
 */
asherkin@263
    18
#ifndef _include_jitcraft_compile_context_h_
asherkin@263
    19
#define _include_jitcraft_compile_context_h_
asherkin@263
    20
asherkin@263
    21
#include <stdarg.h>
asherkin@263
    22
#include <string>
asherkin@263
    23
#include "../Messages.h"
asherkin@263
    24
#include "../PoolAllocator.h"
asherkin@263
    25
#include "../Handles.h"
asherkin@263
    26
#include "../Modules.h"
asherkin@263
    27
#include "../Packages.h"
asherkin@263
    28
asherkin@263
    29
namespace ke {
asherkin@263
    30
asherkin@263
    31
class Zone;
asherkin@263
    32
class ParseTree;
asherkin@263
    33
class String;
asherkin@263
    34
class ImportScope;
asherkin@263
    35
class GlobalScope;
asherkin@263
    36
struct SourcePosition;
asherkin@263
    37
struct TranslationUnit;
asherkin@263
    38
class ImportStatement;
asherkin@263
    39
asherkin@263
    40
struct CompileError
asherkin@263
    41
{
asherkin@263
    42
    CompileError()
asherkin@263
    43
      : message(NULL)
asherkin@263
    44
    {
asherkin@263
    45
    }
asherkin@263
    46
    CompileError(unsigned line, unsigned col, const char *message)
asherkin@263
    47
      : line(line),
asherkin@263
    48
        col(col),
asherkin@263
    49
        message(message)
asherkin@263
    50
    {
asherkin@263
    51
    }
asherkin@263
    52
asherkin@263
    53
    unsigned line;
asherkin@263
    54
    unsigned col;
asherkin@263
    55
    const char *message;
asherkin@263
    56
};
asherkin@263
    57
asherkin@263
    58
struct TranslationUnit : public PoolObject
asherkin@263
    59
{
asherkin@263
    60
    TranslationUnit(Handle<Module> module)
asherkin@263
    61
      : tree_(NULL),
asherkin@263
    62
        importScope_(NULL),
asherkin@263
    63
        globalScope_(NULL),
asherkin@263
    64
        module_(module),
asherkin@263
    65
        enoughMemory_(true),
asherkin@263
    66
        dependencyFailed_(false)
asherkin@263
    67
    {
asherkin@263
    68
    }
asherkin@263
    69
    
asherkin@263
    70
    void destroy();
asherkin@263
    71
asherkin@263
    72
    ParseTree *tree_;
asherkin@263
    73
    PoolList<TranslationUnit *> dependents_;
asherkin@263
    74
    ImportScope *importScope_;
asherkin@263
    75
    GlobalScope *globalScope_;
asherkin@263
    76
    ScopedRoot<Module> module_;
asherkin@263
    77
asherkin@263
    78
    ParseTree *tree() const {
asherkin@263
    79
        return tree_;
asherkin@263
    80
    }
asherkin@263
    81
    PoolList<TranslationUnit *> &dependents() {
asherkin@263
    82
        return dependents_;
asherkin@263
    83
    }
asherkin@263
    84
    bool failed() const {
asherkin@263
    85
        return errors_.length() ||
asherkin@263
    86
               !enoughMemory_ ||
asherkin@263
    87
               dependencyFailed_;
asherkin@263
    88
    }
asherkin@263
    89
    void setImportScope(ImportScope *scope) {
asherkin@263
    90
        assert(!importScope_);
asherkin@263
    91
        importScope_ = scope;
asherkin@263
    92
    }
asherkin@263
    93
    void setGlobalScope(GlobalScope *scope) {
asherkin@263
    94
        assert(!globalScope_);
asherkin@263
    95
        globalScope_ = scope;
asherkin@263
    96
    }
asherkin@263
    97
    ImportScope *importScope() const {
asherkin@263
    98
        return importScope_;
asherkin@263
    99
    }
asherkin@263
   100
    GlobalScope *globalScope() const {
asherkin@263
   101
        return globalScope_;
asherkin@263
   102
    }
asherkin@263
   103
    Handle<Module> module() {
asherkin@263
   104
        return module_;
asherkin@263
   105
    }
asherkin@263
   106
asherkin@263
   107
    void setDependencyFailed() {
asherkin@263
   108
        dependencyFailed_ = true;
asherkin@263
   109
    }
asherkin@263
   110
    void setOutOfMemory() {
asherkin@263
   111
        enoughMemory_ = false;
asherkin@263
   112
    }
asherkin@263
   113
    void appendError(const char *report, unsigned line, unsigned col) {
asherkin@263
   114
        enoughMemory_ |= errors_.append(CompileError(line, col, report));
asherkin@263
   115
    }
asherkin@263
   116
    const Vector<CompileError> *errors() {
asherkin@263
   117
        return &errors_;
asherkin@263
   118
    }
asherkin@263
   119
asherkin@263
   120
  private:
asherkin@263
   121
    bool enoughMemory_;
asherkin@263
   122
    bool dependencyFailed_;
asherkin@263
   123
    Vector<CompileError> errors_;
asherkin@263
   124
};
asherkin@263
   125
asherkin@263
   126
class CompileContext
asherkin@263
   127
{
asherkin@263
   128
    Zone *zone_;
asherkin@263
   129
    Vector<TranslationUnit *> units_;
asherkin@263
   130
    Vector<TranslationUnit *> unparsed_;
asherkin@263
   131
    TranslationUnit *active_;
asherkin@263
   132
asherkin@263
   133
  private:
asherkin@263
   134
    char *loadSource(Handle<Module> module, size_t *length);
asherkin@263
   135
    bool parse(TranslationUnit *unit);
asherkin@263
   136
    bool evaluateImports(TranslationUnit *unit);
asherkin@263
   137
    bool propagateFailure();
asherkin@263
   138
    bool add(Handle<Module> module);
asherkin@263
   139
asherkin@263
   140
  public:
asherkin@263
   141
    CompileContext(Zone *zone);
asherkin@263
   142
    ~CompileContext();
asherkin@263
   143
asherkin@263
   144
    bool initialize(Handle<Package> package, const char *name);
asherkin@263
   145
    bool compile();
asherkin@263
   146
    void enter(TranslationUnit *unit);
asherkin@263
   147
    void leave();
asherkin@263
   148
asherkin@263
   149
    void reportErrorVa(const SourcePosition &pos, Message msg, va_list ap);
asherkin@263
   150
    void reportError(const SourcePosition &pos, Message msg, ...);
asherkin@263
   151
};
asherkin@263
   152
asherkin@263
   153
class AutoEnterCompileContext
asherkin@263
   154
{
asherkin@263
   155
    CompileContext &cc_;
asherkin@263
   156
asherkin@263
   157
  public:
asherkin@263
   158
    AutoEnterCompileContext(CompileContext &cc, TranslationUnit *unit)
asherkin@263
   159
      : cc_(cc)
asherkin@263
   160
    {
asherkin@263
   161
      cc_.enter(unit);
asherkin@263
   162
    }
asherkin@263
   163
    ~AutoEnterCompileContext() {
asherkin@263
   164
      cc_.leave();
asherkin@263
   165
    }
asherkin@263
   166
};
asherkin@263
   167
asherkin@263
   168
} // namespace ke
asherkin@263
   169
asherkin@263
   170
#endif // _include_jitcraft_compile_context_h_