src/Modules.h
author Asher Baker <asherkin@gmail.com>
Mon Jun 10 01:04:43 2013 +0100 (2013-06-10)
changeset 263 ba85a47ee414
parent 256 3c184218462d
permissions -rw-r--r--
Normalise line endings.
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_jc_module_h_
asherkin@263
    19
#define _include_jc_module_h_
asherkin@263
    20
asherkin@263
    21
#include "Heap.h"
asherkin@263
    22
#include "Code.h"
asherkin@263
    23
#include "Functions.h"
asherkin@263
    24
#include "FixedArray.h"
asherkin@263
    25
#include "Packages.h"
asherkin@263
    26
asherkin@263
    27
namespace ke {
asherkin@263
    28
asherkin@263
    29
class Environment;
asherkin@263
    30
class Struct;
asherkin@263
    31
class Package;
asherkin@263
    32
struct TranslationUnit;
asherkin@263
    33
asherkin@263
    34
class Module : public Importable
asherkin@263
    35
{
asherkin@263
    36
    Barriered<Code> code_;
asherkin@263
    37
    Barriered<Struct> globals_;
asherkin@263
    38
    void *nativeIdentity_;
asherkin@263
    39
    TranslationUnit *unit_;
asherkin@263
    40
    Barriered<FixedArray> imports_;
asherkin@263
    41
asherkin@263
    42
  public:
asherkin@263
    43
    static Module *New(Zone *zone, Handle<Package> package, Handle<String> path, Handle<String> name);
asherkin@263
    44
    
asherkin@263
    45
    Code *code() {
asherkin@263
    46
        return code_;
asherkin@263
    47
    }
asherkin@263
    48
    Struct *globals() {
asherkin@263
    49
        return globals_;
asherkin@263
    50
    }
asherkin@263
    51
asherkin@263
    52
    bool compiled() {
asherkin@263
    53
        return !!code_;
asherkin@263
    54
    }
asherkin@263
    55
asherkin@263
    56
    // Modules are pre-created, and may have their intermediate compilation
asherkin@263
    57
    // data attached while they are being compiled.
asherkin@263
    58
    bool compiling() {
asherkin@263
    59
        return !!unit_;
asherkin@263
    60
    }
asherkin@263
    61
    TranslationUnit *translationUnit() {
asherkin@263
    62
        return unit_;
asherkin@263
    63
    }
asherkin@263
    64
    void setTranslationUnit(TranslationUnit *unit) {
asherkin@263
    65
        unit_ = unit;
asherkin@263
    66
    }
asherkin@263
    67
asherkin@263
    68
    unsigned imports() {
asherkin@263
    69
        return imports_ ? imports_->length() / 2 : 0;
asherkin@263
    70
    }
asherkin@263
    71
    Importable *getImportAt(unsigned index) {
asherkin@263
    72
        return Importable::cast(imports_->at(index * 2));
asherkin@263
    73
    }
asherkin@263
    74
    FixedArray *importPathAt(unsigned index) {
asherkin@263
    75
        return FixedArray::cast(imports_->at(index * 2 + 1));
asherkin@263
    76
    }
asherkin@263
    77
    void setImports(Handle<FixedArray> imports) {
asherkin@263
    78
        imports_ = imports;
asherkin@263
    79
    }
asherkin@263
    80
    void setCode(Handle<Code> code) {
asherkin@263
    81
        code_ = code;
asherkin@263
    82
    }
asherkin@263
    83
    void setGlobals(Handle<Struct> globals) {
asherkin@263
    84
        globals_ = globals;
asherkin@263
    85
    }
asherkin@263
    86
asherkin@263
    87
    // Begin GC Descriptor.
asherkin@263
    88
    static inline size_t offsetOfCode() {
asherkin@263
    89
        return OFFSETOF(Module, code_);
asherkin@263
    90
    }
asherkin@263
    91
    static inline size_t offsetOfGlobals() {
asherkin@263
    92
        return OFFSETOF(Module, globals_);
asherkin@263
    93
    }
asherkin@263
    94
    static inline size_t offsetOfImports() {
asherkin@263
    95
        return OFFSETOF(Module, imports_);
asherkin@263
    96
    }
asherkin@263
    97
    // End GC Descriptor.
asherkin@263
    98
asherkin@263
    99
    static inline Module *cast(Object *obj) {
asherkin@263
   100
        assert(!obj || obj->is(MapKind_Module));
asherkin@263
   101
        return reinterpret_cast<Module *>(obj);
asherkin@263
   102
    }
asherkin@263
   103
};
asherkin@263
   104
asherkin@263
   105
}
asherkin@263
   106
asherkin@263
   107
#endif // _include_jc_module_h_