jitcraft/Scopes.h
author David Anderson <dvander@alliedmods.net>
Tue Jul 24 23:53:34 2012 -0700 (2012-07-24)
changeset 86 32ad0523b309
parent 85 06a0015b6a5c
permissions -rw-r--r--
Renamed jitcraft/ to src/, fixed some /Wp64 warnings.
     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 "AST.h"
    23 #include "NameBinding.h"
    24 #include "Structures.h"
    25 
    26 namespace ke {
    27 
    28 class CompileContext;
    29 class EnvironmentMap;
    30 
    31 class Scope : public PoolObject
    32 {
    33     PoolAllocator &pool_;
    34     PoolList<BoundName *> names_;
    35     PoolList<Scope *> children_;
    36     PoolList<Variable *> parameters_;
    37     unsigned locals_;
    38     unsigned maxContainedLocals_;
    39     unsigned numFunctions_;
    40     unsigned numNatives_;
    41     PoolList<Variable *> envVars_;
    42     Variable *lifoSlot_;
    43     
    44     bool empty() const {
    45         return names_.length() == 0;
    46     }
    47     void unlink();
    48 
    49   public:
    50     enum Kind {
    51         GLOBAL,
    52         FUNCTION,
    53         BLOCK
    54     };
    55 
    56   public:
    57     Scope(PoolAllocator &pool, Kind kind, Scope *enclosing);
    58     bool initialize();
    59 
    60     Kind kind() const {
    61         return kind_;
    62     }
    63     Scope *enclosing() const {
    64         return enclosing_;
    65     }
    66     unsigned locals() const {
    67         return locals_;
    68     }
    69     unsigned maxContainedLocals() const {
    70         return maxContainedLocals_;
    71     }
    72     unsigned numFunctions() const {
    73         return numFunctions_;
    74     }
    75     unsigned numNatives() const {
    76         return numNatives_;
    77     }
    78     unsigned numArguments() const {
    79         return parameters_.length();
    80     }
    81     unsigned numEnvironmentVars() const {
    82         return envVars_.length();
    83     }
    84     Variable *getArgument(size_t index) {
    85         return parameters_[index];
    86     }
    87     Variable *lifoSlot() {
    88         return lifoSlot_;
    89     }
    90     void setUsesLifoHeap();
    91 
    92     Scope *unlinkIfEmpty();
    93     BoundName *localLookup(Handle<String> name);
    94     BoundName *lookup(Handle<String> name);
    95 
    96     NamedFunction *declareFunction(Handle<String> name, const SourcePosition &pos, TokenKind token);
    97     Variable *declareVariable(Handle<String> name, const SourcePosition &pos);
    98     Variable *declareArgument(Handle<String> name, const SourcePosition &pos);
    99     NamedEnum *declareEnum(Handle<String> name, const SourcePosition &pos);
   100     NamedConstant *declareConstant(Handle<String> name, const SourcePosition &pos);
   101 
   102     EnvironmentMap *createEnvironmentMap(Zone *zone);
   103 
   104     void allocate(Variable *var);
   105     void allocate();
   106     void allocateGlobals();
   107 
   108   public:
   109     static bool Bind(CompileContext &cc, ParseTree *tree);
   110     static void Allocate(ParseTree *tree);
   111 
   112   private:
   113     Kind kind_;
   114     Scope *enclosing_;
   115 };
   116 
   117 }
   118 
   119 #endif // _include_jitcraft_scopes_h_