src/compiler/Scopes.h
changeset 195 c1ba166f3fc4
parent 145 e9d804821c48
child 256 3c184218462d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/compiler/Scopes.h	Sat Nov 17 18:41:01 2012 -0800
     1.3 @@ -0,0 +1,126 @@
     1.4 +/*
     1.5 + * Copyright (C) 2012 David Anderson
     1.6 + *
     1.7 + * This file is part of JITCraft.
     1.8 + *
     1.9 + * JITCraft 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 + * Foobar 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 + * JITCraft. If not, see http://www.gnu.org/licenses/.
    1.20 + */
    1.21 +#ifndef _include_jitcraft_scopes_h_
    1.22 +#define _include_jitcraft_scopes_h_
    1.23 +
    1.24 +#include "../PoolAllocator.h"
    1.25 +#include "../Structures.h"
    1.26 +#include "AST.h"
    1.27 +#include "NameBinding.h"
    1.28 +
    1.29 +namespace ke {
    1.30 +
    1.31 +class CompileContext;
    1.32 +class EnvironmentMap;
    1.33 +class Struct;
    1.34 +
    1.35 +class Scope : public PoolObject
    1.36 +{
    1.37 +  protected:
    1.38 +    PoolAllocator &pool_;
    1.39 +    PoolList<Symbol *> names_;
    1.40 +    PoolList<Scope *> children_;
    1.41 +    
    1.42 +    bool empty() const {
    1.43 +        return names_.length() == 0;
    1.44 +    }
    1.45 +    void unlink();
    1.46 +
    1.47 +  public:
    1.48 +    enum Kind {
    1.49 +        IMPORT,
    1.50 +        GLOBAL,
    1.51 +        FUNCTION,
    1.52 +        BLOCK
    1.53 +    };
    1.54 +
    1.55 +  public:
    1.56 +    Scope(PoolAllocator &pool, Kind kind, Scope *enclosing);
    1.57 +    bool initialize();
    1.58 +
    1.59 +    Kind kind() const {
    1.60 +        return kind_;
    1.61 +    }
    1.62 +    Scope *enclosing() const {
    1.63 +        return enclosing_;
    1.64 +    }
    1.65 +
    1.66 +    bool addSymbol(Symbol *symbol);
    1.67 +
    1.68 +    Scope *unlinkIfEmpty();
    1.69 +    Symbol *localLookup(Handle<String> name);
    1.70 +    Symbol *lookup(Handle<String> name);
    1.71 +
    1.72 +    EnvironmentMap *createEnvironmentMap(Zone *zone);
    1.73 +
    1.74 +    void setParent(Scope *parent) {
    1.75 +        assert(!enclosing_ || enclosing_ == parent);
    1.76 +        enclosing_ = parent;
    1.77 +    }
    1.78 +
    1.79 +  private:
    1.80 +    Kind kind_;
    1.81 +    Scope *enclosing_;
    1.82 +};
    1.83 +
    1.84 +class ArgumentScope : public Scope
    1.85 +{
    1.86 +  public:
    1.87 +    static ArgumentScope *New(PoolAllocator &pool);
    1.88 +
    1.89 +  private:
    1.90 +    ArgumentScope(PoolAllocator &pool);
    1.91 +};
    1.92 +
    1.93 +class LocalScope : public Scope
    1.94 +{
    1.95 +  public:
    1.96 +    static LocalScope *New(PoolAllocator &pool);
    1.97 +
    1.98 +  private:
    1.99 +    LocalScope(PoolAllocator &pool);
   1.100 +};
   1.101 +
   1.102 +class GlobalScope : public Scope
   1.103 +{
   1.104 +  public:
   1.105 +    static GlobalScope *New(PoolAllocator &pool, ImportScope *parent);
   1.106 +
   1.107 +    bool addPublic(Symbol *symbol);
   1.108 +    Symbol *findPublic(Handle<String> name);
   1.109 +    Struct *build(Zone *zone);
   1.110 +
   1.111 +  private:
   1.112 +    GlobalScope(PoolAllocator &pool, ImportScope *parent);
   1.113 +
   1.114 +  private:
   1.115 +    PoolList<Symbol *> publics_;
   1.116 +};
   1.117 +
   1.118 +class ImportScope : public Scope
   1.119 +{
   1.120 +  public:
   1.121 +    static ImportScope *New(PoolAllocator &pool);
   1.122 +
   1.123 +  private:
   1.124 +    ImportScope(PoolAllocator &pool);
   1.125 +};
   1.126 +
   1.127 +}
   1.128 +
   1.129 +#endif // _include_jitcraft_scopes_h_