jitcraft/Scopes.h
author David Anderson <dvander@alliedmods.net>
Tue Jul 10 02:11:13 2012 -0700 (2012-07-10)
changeset 68 b40b0e18a918
parent 63 dd57743d220e
child 84 eaa43f765cbd
permissions -rw-r--r--
Added support for ISourcePawnEngine natives.
     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 
    25 namespace ke {
    26 
    27 class Scope : public PoolObject
    28 {
    29     PoolAllocator &pool_;
    30     PoolList<BoundName *> names_;
    31     PoolList<Scope *> children_;
    32     PoolList<Variable *> parameters_;
    33     unsigned locals_;
    34     unsigned maxContainedLocals_;
    35     unsigned numFunctions_;
    36     unsigned numNatives_;
    37     Variable *lifoSlot_;
    38     
    39     bool empty() const {
    40         return names_.length() == 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 numNatives() const {
    71         return numNatives_;
    72     }
    73     unsigned numArguments() const {
    74         return parameters_.length();
    75     }
    76     Variable *getArgument(size_t index) {
    77         return parameters_[index];
    78     }
    79     Variable *lifoSlot() {
    80         return lifoSlot_;
    81     }
    82     void setUsesLifoHeap();
    83 
    84     Scope *unlinkIfEmpty();
    85     BoundName *localLookup(Handle<String> name);
    86     BoundName *lookup(Handle<String> name);
    87 
    88     NamedFunction *declareFunction(Handle<String> name, const SourcePosition &pos, TokenKind token);
    89     Variable *declareVariable(Handle<String> name, const SourcePosition &pos);
    90     Variable *declareArgument(Handle<String> name, const SourcePosition &pos);
    91     NamedEnum *declareEnum(Handle<String> name, const SourcePosition &pos);
    92     NamedConstant *declareConstant(Handle<String> name, const SourcePosition &pos);
    93 
    94     void allocate(Variable *var);
    95     void allocate();
    96     void allocateGlobals();
    97 
    98   public:
    99     static bool Bind(ParseTree *tree);
   100     static void Allocate(ParseTree *tree);
   101 
   102   private:
   103     Kind kind_;
   104     Scope *enclosing_;
   105 };
   106 
   107 }
   108 
   109 #endif // _include_jitcraft_scopes_h_