src/compiler/Scopes.h
author David Anderson <dvander@alliedmods.net>
Sun Jan 06 13:52:21 2013 -0800 (2013-01-06)
changeset 256 3c184218462d
parent 195 c1ba166f3fc4
child 258 241d082d6d89
permissions -rw-r--r--
Initial implementation of module imports.
[email protected]
     1
/*
[email protected]
     2
 * Copyright (C) 2012 David Anderson
dvander[email protected]
     3
 *
[email protected]
     4
 * This file is part of JITCraft.
[email protected]
     5
 *
[email protected]
     6
 * JITCraft is free software: you can redistribute it and/or modify it under
[email protected]
     7
 * the terms of the GNU General Public License as published by the Free
[email protected]
     8
 * Software Foundation, either version 3 of the License, or (at your option)
[email protected]
     9
 * any later version.
[email protected]
    10
 * 
[email protected]
    11
 * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
[email protected]
    12
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
[email protected]
    13
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
[email protected]
    14
 *
[email protected]
    15
 * You should have received a copy of the GNU General Public License along with
[email protected]
    16
 * JITCraft. If not, see http://www.gnu.org/licenses/.
[email protected]
    17
 */
[email protected]
    18
#ifndef _include_jitcraft_scopes_h_
[email protected]
    19
#define _include_jitcraft_scopes_h_
[email protected]
    20
[email protected]
    21
#include "../PoolAllocator.h"
[email protected]
    22
#include "../Structures.h"
[email protected]
    23
#include "AST.h"
[email protected]
    24
#include "NameBinding.h"
[email protected]
    25
[email protected]
    26
namespace ke {
[email protected]
    27
[email protected]
    28
class CompileContext;
[email protected]
    29
class EnvironmentMap;
[email protected]
    30
class Struct;
[email protected]
    31
[email protected]
    32
class Scope : public PoolObject
[email protected]
    33
{
[email protected]
    34
  protected:
[email protected]
    35
    PoolAllocator &pool_;
[email protected]
    36
    PoolList<Symbol *> names_;
[email protected]
    37
    PoolList<Scope *> children_;
[email protected]
    38
    
[email protected]
    39
    bool empty() const {
[email protected]
    40
        return names_.length() == 0;
[email protected]
    41
    }
[email protected]
    42
    void unlink();
[email protected]
    43
[email protected]
    44
  public:
[email protected]
    45
    enum Kind {
[email protected]
    46
        IMPORT,
[email protected]
    47
        GLOBAL,
[email protected]
    48
        FUNCTION,
[email protected]
    49
        BLOCK
[email protected]
    50
    };
[email protected]
    51
[email protected]
    52
  public:
[email protected]
    53
    Scope(PoolAllocator &pool, Kind kind, Scope *enclosing);
[email protected]
    54
    bool initialize();
[email protected]
    55
[email protected]
    56
    Kind kind() const {
[email protected]
    57
        return kind_;
[email protected]
    58
    }
[email protected]
    59
    Scope *enclosing() const {
[email protected]
    60
        return enclosing_;
[email protected]
    61
    }
[email protected]
    62
[email protected]
    63
    bool addSymbol(Symbol *symbol);
[email protected]
    64
[email protected]
    65
    Scope *unlinkIfEmpty();
[email protected]
    66
    Symbol *localLookup(Handle<String> name);
[email protected]
    67
    Symbol *lookup(Handle<String> name);
[email protected]
    68
[email protected]
    69
    EnvironmentMap *createEnvironmentMap(Zone *zone);
[email protected]
    70
[email protected]
    71
    void setParent(Scope *parent) {
[email protected]
    72
        assert(!enclosing_ || enclosing_ == parent);
[email protected]
    73
        enclosing_ = parent;
[email protected]
    74
    }
[email protected]
    75
[email protected]
    76
  private:
[email protected]
    77
    Kind kind_;
[email protected]
    78
    Scope *enclosing_;
[email protected]
    79
};
[email protected]
    80
[email protected]
    81
class ArgumentScope : public Scope
[email protected]
    82
{
[email protected]
    83
  public:
[email protected]
    84
    static ArgumentScope *New(PoolAllocator &pool);
[email protected]
    85
[email protected]
    86
  private:
[email protected]
    87
    ArgumentScope(PoolAllocator &pool);
[email protected]
    88
};
[email protected]
    89
[email protected]
    90
class LocalScope : public Scope
[email protected]
    91
{
[email protected]
    92
  public:
[email protected]
    93
    static LocalScope *New(PoolAllocator &pool);
[email protected]
    94
[email protected]
    95
  private:
[email protected]
    96
    LocalScope(PoolAllocator &pool);
[email protected]
    97
};
[email protected]
    98
[email protected]
    99
class GlobalScope : public Scope
[email protected]
   100
{
[email protected]
   101
  public:
[email protected]
   102
    static GlobalScope *New(PoolAllocator &pool, ImportScope *parent);
[email protected]
   103
[email protected]
   104
  private:
[email protected]
   105
    GlobalScope(PoolAllocator &pool, ImportScope *parent);
[email protected]
   106
};
[email protected]
   107
[email protected]
   108
class ImportScope : public Scope
[email protected]
   109
{
[email protected]
   110
  public:
[email protected]
   111
    static ImportScope *New(PoolAllocator &pool);
[email protected]
   112
[email protected]
   113
  private:
[email protected]
   114
    ImportScope(PoolAllocator &pool);
[email protected]
   115
};
[email protected]
   116
[email protected]
   117
}
[email protected]
   118
[email protected]
   119
#endif // _include_jitcraft_scopes_h_