1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/jitcraft/Scopes.h Mon Jun 25 02:57:28 2012 -0700
1.3 @@ -0,0 +1,95 @@
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 <vector>
1.25 +#include "PoolAllocator.h"
1.26 +#include "AST.h"
1.27 +#include "NameBinding.h"
1.28 +
1.29 +namespace jc {
1.30 +
1.31 +class Scope : public PoolObject
1.32 +{
1.33 + PoolAllocator &pool_;
1.34 + std::vector<BoundName *> names_;
1.35 + std::vector<Scope *> children_;
1.36 + std::vector<Variable *> parameters_;
1.37 + unsigned locals_;
1.38 + unsigned maxContainedLocals_;
1.39 + unsigned numFunctions_;
1.40 +
1.41 + bool empty() const {
1.42 + return names_.size() == 0;
1.43 + }
1.44 + void unlink();
1.45 +
1.46 + public:
1.47 + enum Kind {
1.48 + GLOBAL,
1.49 + FUNCTION,
1.50 + BLOCK
1.51 + };
1.52 +
1.53 + public:
1.54 + Scope(PoolAllocator &pool, Kind kind, Scope *enclosing);
1.55 + bool initialize();
1.56 +
1.57 + Kind kind() const {
1.58 + return kind_;
1.59 + }
1.60 + Scope *enclosing() const {
1.61 + return enclosing_;
1.62 + }
1.63 + unsigned locals() const {
1.64 + return locals_;
1.65 + }
1.66 + unsigned maxContainedLocals() const {
1.67 + return maxContainedLocals_;
1.68 + }
1.69 + unsigned numFunctions() const {
1.70 + return numFunctions_;
1.71 + }
1.72 + unsigned numArguments() const {
1.73 + return parameters_.size();
1.74 + }
1.75 + Variable *getArgument(size_t index) {
1.76 + return parameters_[index];
1.77 + }
1.78 +
1.79 + Scope *unlinkIfEmpty();
1.80 + BoundName *localLookup(Name *name);
1.81 + BoundName *lookup(Name *name);
1.82 + NamedFunction *declareFunction(Name *name);
1.83 + Variable *declareVariable(Name *name);
1.84 + Variable *declareArgument(Name *name);
1.85 + void allocate();
1.86 + void allocateGlobals();
1.87 +
1.88 + public:
1.89 + static bool BindAndAllocate(ParseTree *tree);
1.90 +
1.91 + private:
1.92 + Kind kind_;
1.93 + Scope *enclosing_;
1.94 +};
1.95 +
1.96 +}
1.97 +
1.98 +#endif // _include_jitcraft_scopes_h_