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.
     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 #ifndef _include_jitcraft_scopes_h_
    19 #define _include_jitcraft_scopes_h_
    20 
    21 #include "../PoolAllocator.h"
    22 #include "../Structures.h"
    23 #include "AST.h"
    24 #include "NameBinding.h"
    25 
    26 namespace ke {
    27 
    28 class CompileContext;
    29 class EnvironmentMap;
    30 class Struct;
    31 
    32 class Scope : public PoolObject
    33 {
    34   protected:
    35     PoolAllocator &pool_;
    36     PoolList<Symbol *> names_;
    37     PoolList<Scope *> children_;
    38     
    39     bool empty() const {
    40         return names_.length() == 0;
    41     }
    42     void unlink();
    43 
    44   public:
    45     enum Kind {
    46         IMPORT,
    47         GLOBAL,
    48         FUNCTION,
    49         BLOCK
    50     };
    51 
    52   public:
    53     Scope(PoolAllocator &pool, Kind kind, Scope *enclosing);
    54     bool initialize();
    55 
    56     Kind kind() const {
    57         return kind_;
    58     }
    59     Scope *enclosing() const {
    60         return enclosing_;
    61     }
    62 
    63     bool addSymbol(Symbol *symbol);
    64 
    65     Scope *unlinkIfEmpty();
    66     Symbol *localLookup(Handle<String> name);
    67     Symbol *lookup(Handle<String> name);
    68 
    69     EnvironmentMap *createEnvironmentMap(Zone *zone);
    70 
    71     void setParent(Scope *parent) {
    72         assert(!enclosing_ || enclosing_ == parent);
    73         enclosing_ = parent;
    74     }
    75 
    76   private:
    77     Kind kind_;
    78     Scope *enclosing_;
    79 };
    80 
    81 class ArgumentScope : public Scope
    82 {
    83   public:
    84     static ArgumentScope *New(PoolAllocator &pool);
    85 
    86   private:
    87     ArgumentScope(PoolAllocator &pool);
    88 };
    89 
    90 class LocalScope : public Scope
    91 {
    92   public:
    93     static LocalScope *New(PoolAllocator &pool);
    94 
    95   private:
    96     LocalScope(PoolAllocator &pool);
    97 };
    98 
    99 class GlobalScope : public Scope
   100 {
   101   public:
   102     static GlobalScope *New(PoolAllocator &pool, ImportScope *parent);
   103 
   104   private:
   105     GlobalScope(PoolAllocator &pool, ImportScope *parent);
   106 };
   107 
   108 class ImportScope : public Scope
   109 {
   110   public:
   111     static ImportScope *New(PoolAllocator &pool);
   112 
   113   private:
   114     ImportScope(PoolAllocator &pool);
   115 };
   116 
   117 }
   118 
   119 #endif // _include_jitcraft_scopes_h_