jitcraft/Scopes.h
author David Anderson <dvander@alliedmods.net>
Sun Jul 01 16:36:39 2012 -0700 (2012-07-01)
changeset 22 765ab1b672a3
parent 12 75e385f68b7e
child 25 23e8b3d8e4e6
permissions -rw-r--r--
Added support for fused math-assign ops (+=, etc).
     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     Variable *lifoSlot_;
    38     
    39     bool empty() const {
    40         return names_.size() == 0;
    41     }
    42     void unlink();
    43 
    44   public:
    45     enum Kind {
    46         GLOBAL,
    47         FUNCTION,
    48         BLOCK
    49     };
    50 
    51   public:
    52     Scope(PoolAllocator &pool, Kind kind, Scope *enclosing);
    53     bool initialize();
    54 
    55     Kind kind() const {
    56         return kind_;
    57     }
    58     Scope *enclosing() const {
    59         return enclosing_;
    60     }
    61     unsigned locals() const {
    62         return locals_;
    63     }
    64     unsigned maxContainedLocals() const {
    65         return maxContainedLocals_;
    66     }
    67     unsigned numFunctions() const {
    68         return numFunctions_;
    69     }
    70     unsigned numArguments() const {
    71         return parameters_.size();
    72     }
    73     Variable *getArgument(size_t index) {
    74         return parameters_[index];
    75     }
    76     Variable *lifoSlot() {
    77         return lifoSlot_;
    78     }
    79     void setUsesLifoHeap();
    80 
    81     Scope *unlinkIfEmpty();
    82     BoundName *localLookup(Name *name);
    83     BoundName *lookup(Name *name);
    84     NamedFunction *declareFunction(Name *name);
    85     Variable *declareVariable(Name *name);
    86     Variable *declareArgument(Name *name);
    87     NamedEnum *declareEnum(Name *name);
    88     NamedConstant *declareConstant(Name *name);
    89     void allocate(Variable *var);
    90     void allocate();
    91     void allocateGlobals();
    92 
    93   public:
    94     static bool Bind(ParseTree *tree);
    95     static void Allocate(ParseTree *tree);
    96 
    97   private:
    98     Kind kind_;
    99     Scope *enclosing_;
   100 };
   101 
   102 }
   103 
   104 #endif // _include_jitcraft_scopes_h_