jitcraft/Scopes.h
author David Anderson <dvander@alliedmods.net>
Mon Jun 25 02:57:28 2012 -0700 (2012-06-25)
changeset 0 c455ebb9a5dc
child 1 27f12e2fc369
permissions -rw-r--r--
Initial import of prototype.
     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 <vector>
    22 #include "PoolAllocator.h"
    23 #include "AST.h"
    24 #include "NameBinding.h"
    25 
    26 namespace jc {
    27 
    28 class Scope : public PoolObject
    29 {
    30     PoolAllocator &pool_;
    31     std::vector<BoundName *> names_;
    32     std::vector<Scope *> children_;
    33     std::vector<Variable *> parameters_;
    34     unsigned locals_;
    35     unsigned maxContainedLocals_;
    36     unsigned numFunctions_;
    37 
    38     bool empty() const {
    39         return names_.size() == 0;
    40     }
    41     void unlink();
    42 
    43   public:
    44     enum Kind {
    45         GLOBAL,
    46         FUNCTION,
    47         BLOCK
    48     };
    49 
    50   public:
    51     Scope(PoolAllocator &pool, Kind kind, Scope *enclosing);
    52     bool initialize();
    53 
    54     Kind kind() const {
    55         return kind_;
    56     }
    57     Scope *enclosing() const {
    58         return enclosing_;
    59     }
    60     unsigned locals() const {
    61         return locals_;
    62     }
    63     unsigned maxContainedLocals() const {
    64         return maxContainedLocals_;
    65     }
    66     unsigned numFunctions() const {
    67         return numFunctions_;
    68     }
    69     unsigned numArguments() const {
    70         return parameters_.size();
    71     }
    72     Variable *getArgument(size_t index) {
    73         return parameters_[index];
    74     }
    75 
    76     Scope *unlinkIfEmpty();
    77     BoundName *localLookup(Name *name);
    78     BoundName *lookup(Name *name);
    79     NamedFunction *declareFunction(Name *name);
    80     Variable *declareVariable(Name *name);
    81     Variable *declareArgument(Name *name);
    82     void allocate();
    83     void allocateGlobals();
    84 
    85   public:
    86     static bool BindAndAllocate(ParseTree *tree);
    87 
    88   private:
    89     Kind kind_;
    90     Scope *enclosing_;
    91 };
    92 
    93 }
    94 
    95 #endif // _include_jitcraft_scopes_h_