ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #ifndef V8_SCOPES_H_ |
| 29 | #define V8_SCOPES_H_ |
| 30 | |
| 31 | #include "ast.h" |
| 32 | #include "hashmap.h" |
| 33 | |
kasperl@chromium.org | 71affb5 | 2009-05-26 05:44:31 +0000 | [diff] [blame] | 34 | namespace v8 { |
| 35 | namespace internal { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 36 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 37 | class CompilationInfo; |
| 38 | |
| 39 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 40 | // A hash map to support fast variable declaration and lookup. |
| 41 | class VariableMap: public HashMap { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 42 | public: |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 43 | VariableMap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 44 | |
| 45 | // Dummy constructor. This constructor doesn't set up the map |
| 46 | // properly so don't use it unless you have a good reason. |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 47 | explicit VariableMap(bool gotta_love_static_overloading); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 48 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 49 | virtual ~VariableMap(); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 50 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 51 | Variable* Declare(Scope* scope, |
| 52 | Handle<String> name, |
| 53 | Variable::Mode mode, |
| 54 | bool is_valid_lhs, |
| 55 | Variable::Kind kind); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 56 | |
| 57 | Variable* Lookup(Handle<String> name); |
| 58 | }; |
| 59 | |
| 60 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 61 | // The dynamic scope part holds hash maps for the variables that will |
| 62 | // be looked up dynamically from within eval and with scopes. The objects |
| 63 | // are allocated on-demand from Scope::NonLocal to avoid wasting memory |
| 64 | // and setup time for scopes that don't need them. |
| 65 | class DynamicScopePart : public ZoneObject { |
| 66 | public: |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 67 | VariableMap* GetMap(Variable::Mode mode) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 68 | int index = mode - Variable::DYNAMIC; |
| 69 | ASSERT(index >= 0 && index < 3); |
| 70 | return &maps_[index]; |
| 71 | } |
| 72 | |
| 73 | private: |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 74 | VariableMap maps_[3]; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 78 | // Global invariants after AST construction: Each reference (i.e. identifier) |
| 79 | // to a JavaScript variable (including global properties) is represented by a |
| 80 | // VariableProxy node. Immediately after AST construction and before variable |
| 81 | // allocation, most VariableProxy nodes are "unresolved", i.e. not bound to a |
| 82 | // corresponding variable (though some are bound during parse time). Variable |
| 83 | // allocation binds each unresolved VariableProxy to one Variable and assigns |
| 84 | // a location. Note that many VariableProxy nodes may refer to the same Java- |
| 85 | // Script variable. |
| 86 | |
| 87 | class Scope: public ZoneObject { |
| 88 | public: |
| 89 | // --------------------------------------------------------------------------- |
| 90 | // Construction |
| 91 | |
| 92 | enum Type { |
| 93 | EVAL_SCOPE, // the top-level scope for an 'eval' source |
| 94 | FUNCTION_SCOPE, // the top-level scope for a function |
| 95 | GLOBAL_SCOPE // the top-level scope for a program or a top-level eval |
| 96 | }; |
| 97 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 98 | Scope(Scope* outer_scope, Type type); |
| 99 | |
| 100 | virtual ~Scope() { } |
| 101 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 102 | // Compute top scope and allocate variables. For lazy compilation the top |
| 103 | // scope only contains the single lazily compiled function, so this |
| 104 | // doesn't re-allocate variables repeatedly. |
| 105 | static bool Analyze(CompilationInfo* info); |
| 106 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 107 | // The scope name is only used for printing/debugging. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 108 | void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 109 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 110 | virtual void Initialize(bool inside_with); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 111 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 112 | // Called just before leaving a scope. |
| 113 | virtual void Leave() { |
| 114 | // No cleanup or fixup necessary. |
| 115 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 116 | |
| 117 | // --------------------------------------------------------------------------- |
| 118 | // Declarations |
| 119 | |
| 120 | // Lookup a variable in this scope. Returns the variable or NULL if not found. |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 121 | virtual Variable* LocalLookup(Handle<String> name); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 122 | |
| 123 | // Lookup a variable in this scope or outer scopes. |
| 124 | // Returns the variable or NULL if not found. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 125 | virtual Variable* Lookup(Handle<String> name); |
| 126 | |
| 127 | // Declare the function variable for a function literal. This variable |
| 128 | // is in an intermediate scope between this function scope and the the |
| 129 | // outer scope. Only possible for function scopes; at most one variable. |
| 130 | Variable* DeclareFunctionVar(Handle<String> name); |
| 131 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 132 | // Declare a local variable in this scope. If the variable has been |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 133 | // declared before, the previously declared variable is returned. |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 134 | virtual Variable* DeclareLocal(Handle<String> name, Variable::Mode mode); |
| 135 | |
| 136 | // Declare an implicit global variable in this scope which must be a |
| 137 | // global scope. The variable was introduced (possibly from an inner |
| 138 | // scope) by a reference to an unresolved variable with no intervening |
| 139 | // with statements or eval calls. |
| 140 | Variable* DeclareGlobal(Handle<String> name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 141 | |
| 142 | // Add a parameter to the parameter list. The parameter must have been |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 143 | // declared via Declare. The same parameter may occur more than once in |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 144 | // the parameter list; they must be added in source order, from left to |
| 145 | // right. |
| 146 | void AddParameter(Variable* var); |
| 147 | |
| 148 | // Create a new unresolved variable. |
| 149 | virtual VariableProxy* NewUnresolved(Handle<String> name, bool inside_with); |
| 150 | |
| 151 | // Remove a unresolved variable. During parsing, an unresolved variable |
| 152 | // may have been added optimistically, but then only the variable name |
| 153 | // was used (typically for labels). If the variable was not declared, the |
| 154 | // addition introduced a new unresolved variable which may end up being |
| 155 | // allocated globally as a "ghost" variable. RemoveUnresolved removes |
| 156 | // such a variable again if it was added; otherwise this is a no-op. |
| 157 | void RemoveUnresolved(VariableProxy* var); |
| 158 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 159 | // Creates a new temporary variable in this scope. The name is only used |
| 160 | // for printing and cannot be used to find the variable. In particular, |
| 161 | // the only way to get hold of the temporary is by keeping the Variable* |
| 162 | // around. |
| 163 | virtual Variable* NewTemporary(Handle<String> name); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 164 | |
| 165 | // Adds the specific declaration node to the list of declarations in |
| 166 | // this scope. The declarations are processed as part of entering |
| 167 | // the scope; see codegen.cc:ProcessDeclarations. |
| 168 | void AddDeclaration(Declaration* declaration); |
| 169 | |
| 170 | // --------------------------------------------------------------------------- |
| 171 | // Illegal redeclaration support. |
| 172 | |
| 173 | // Set an expression node that will be executed when the scope is |
| 174 | // entered. We only keep track of one illegal redeclaration node per |
| 175 | // scope - the first one - so if you try to set it multiple times |
| 176 | // the additional requests will be silently ignored. |
| 177 | void SetIllegalRedeclaration(Expression* expression); |
| 178 | |
| 179 | // Visit the illegal redeclaration expression. Do not call if the |
| 180 | // scope doesn't have an illegal redeclaration node. |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 181 | void VisitIllegalRedeclaration(AstVisitor* visitor); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 182 | |
| 183 | // Check if the scope has (at least) one illegal redeclaration. |
| 184 | bool HasIllegalRedeclaration() const { return illegal_redecl_ != NULL; } |
| 185 | |
| 186 | |
| 187 | // --------------------------------------------------------------------------- |
| 188 | // Scope-specific info. |
| 189 | |
| 190 | // Inform the scope that the corresponding code contains a with statement. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 191 | void RecordWithStatement() { scope_contains_with_ = true; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 192 | |
| 193 | // Inform the scope that the corresponding code contains an eval call. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 194 | void RecordEvalCall() { scope_calls_eval_ = true; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 195 | |
| 196 | |
| 197 | // --------------------------------------------------------------------------- |
| 198 | // Predicates. |
| 199 | |
| 200 | // Specific scope types. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 201 | bool is_eval_scope() const { return type_ == EVAL_SCOPE; } |
| 202 | bool is_function_scope() const { return type_ == FUNCTION_SCOPE; } |
| 203 | bool is_global_scope() const { return type_ == GLOBAL_SCOPE; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 204 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 205 | // Information about which scopes calls eval. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 206 | bool calls_eval() const { return scope_calls_eval_; } |
| 207 | bool outer_scope_calls_eval() const { return outer_scope_calls_eval_; } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 208 | |
| 209 | // Is this scope inside a with statement. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 210 | bool inside_with() const { return scope_inside_with_; } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 211 | // Does this scope contain a with statement. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 212 | bool contains_with() const { return scope_contains_with_; } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 213 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 214 | // The scope immediately surrounding this scope, or NULL. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 215 | Scope* outer_scope() const { return outer_scope_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 216 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 217 | // --------------------------------------------------------------------------- |
| 218 | // Accessors. |
| 219 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 220 | // A new variable proxy corresponding to the (function) receiver. |
| 221 | VariableProxy* receiver() const { |
| 222 | VariableProxy* proxy = |
| 223 | new VariableProxy(Factory::this_symbol(), true, false); |
| 224 | proxy->BindTo(receiver_); |
| 225 | return proxy; |
| 226 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 227 | |
| 228 | // The variable holding the function literal for named function |
| 229 | // literals, or NULL. |
| 230 | // Only valid for function scopes. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 231 | Variable* function() const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 232 | ASSERT(is_function_scope()); |
| 233 | return function_; |
| 234 | } |
| 235 | |
| 236 | // Parameters. The left-most parameter has index 0. |
| 237 | // Only valid for function scopes. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 238 | Variable* parameter(int index) const { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 239 | ASSERT(is_function_scope()); |
| 240 | return params_[index]; |
| 241 | } |
| 242 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 243 | int num_parameters() const { return params_.length(); } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 244 | |
| 245 | // The local variable 'arguments' if we need to allocate it; NULL otherwise. |
| 246 | // If arguments() exist, arguments_shadow() exists, too. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 247 | Variable* arguments() const { return arguments_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 248 | |
| 249 | // The '.arguments' shadow variable if we need to allocate it; NULL otherwise. |
| 250 | // If arguments_shadow() exist, arguments() exists, too. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 251 | Variable* arguments_shadow() const { return arguments_shadow_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 252 | |
| 253 | // Declarations list. |
| 254 | ZoneList<Declaration*>* declarations() { return &decls_; } |
| 255 | |
| 256 | |
| 257 | |
| 258 | // --------------------------------------------------------------------------- |
| 259 | // Variable allocation. |
| 260 | |
| 261 | // Collect all used locals in this scope. |
| 262 | template<class Allocator> |
| 263 | void CollectUsedVariables(List<Variable*, Allocator>* locals); |
| 264 | |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 265 | // Resolve and fill in the allocation information for all variables |
| 266 | // in this scopes. Must be called *after* all scopes have been |
| 267 | // processed (parsed) to ensure that unresolved variables can be |
| 268 | // resolved properly. |
| 269 | // |
| 270 | // In the case of code compiled and run using 'eval', the context |
| 271 | // parameter is the context in which eval was called. In all other |
| 272 | // cases the context parameter is an empty handle. |
| 273 | void AllocateVariables(Handle<Context> context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 274 | |
| 275 | // Result of variable allocation. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 276 | int num_stack_slots() const { return num_stack_slots_; } |
| 277 | int num_heap_slots() const { return num_heap_slots_; } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 278 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 279 | // Make sure this scope and all outer scopes are eagerly compiled. |
| 280 | void ForceEagerCompilation() { force_eager_compilation_ = true; } |
| 281 | |
| 282 | // Determine if we can use lazy compilation for this scope. |
| 283 | bool AllowsLazyCompilation() const; |
| 284 | |
| 285 | // True if the outer context of this scope is always the global context. |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 286 | virtual bool HasTrivialOuterContext() const; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 287 | |
| 288 | // The number of contexts between this and scope; zero if this == scope. |
| 289 | int ContextChainLength(Scope* scope); |
| 290 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 291 | // --------------------------------------------------------------------------- |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 292 | // Strict mode support. |
| 293 | bool IsDeclared(Handle<String> name) { |
| 294 | // During formal parameter list parsing the scope only contains |
| 295 | // two variables inserted at initialization: "this" and "arguments". |
| 296 | // "this" is an invalid parameter name and "arguments" is invalid parameter |
| 297 | // name in strict mode. Therefore looking up with the map which includes |
| 298 | // "this" and "arguments" in addition to all formal parameters is safe. |
| 299 | return variables_.Lookup(name) != NULL; |
| 300 | } |
| 301 | |
| 302 | // --------------------------------------------------------------------------- |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 303 | // Debugging. |
| 304 | |
| 305 | #ifdef DEBUG |
| 306 | void Print(int n = 0); // n = indentation; n < 0 => don't print recursively |
| 307 | #endif |
| 308 | |
| 309 | // --------------------------------------------------------------------------- |
| 310 | // Implementation. |
| 311 | protected: |
| 312 | friend class ParserFactory; |
| 313 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 314 | explicit Scope(Type type); |
| 315 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 316 | void InsertAfterScope(Scope* scope) { |
| 317 | inner_scopes_.Add(scope); |
| 318 | outer_scope_ = scope->outer_scope_; |
| 319 | outer_scope_->inner_scopes_.RemoveElement(scope); |
| 320 | outer_scope_->inner_scopes_.Add(this); |
| 321 | scope->outer_scope_ = this; |
| 322 | } |
| 323 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 324 | // Scope tree. |
| 325 | Scope* outer_scope_; // the immediately enclosing outer scope, or NULL |
| 326 | ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes |
| 327 | |
| 328 | // The scope type. |
| 329 | Type type_; |
| 330 | |
| 331 | // Debugging support. |
| 332 | Handle<String> scope_name_; |
| 333 | |
| 334 | // The variables declared in this scope: |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 335 | // |
| 336 | // All user-declared variables (incl. parameters). For global scopes |
| 337 | // variables may be implicitly 'declared' by being used (possibly in |
| 338 | // an inner scope) with no intervening with statements or eval calls. |
| 339 | VariableMap variables_; |
| 340 | // Compiler-allocated (user-invisible) temporaries. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 341 | ZoneList<Variable*> temps_; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 342 | // Parameter list in source order. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 343 | ZoneList<Variable*> params_; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 344 | // Variables that must be looked up dynamically. |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 345 | DynamicScopePart* dynamics_; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 346 | // Unresolved variables referred to from this scope. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 347 | ZoneList<VariableProxy*> unresolved_; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 348 | // Declarations. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 349 | ZoneList<Declaration*> decls_; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 350 | // Convenience variable. |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 351 | Variable* receiver_; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 352 | // Function variable, if any; function scopes only. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 353 | Variable* function_; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 354 | // Convenience variable; function scopes only. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 355 | Variable* arguments_; |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 356 | // Convenience variable; function scopes only. |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 357 | Variable* arguments_shadow_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 358 | |
| 359 | // Illegal redeclaration. |
| 360 | Expression* illegal_redecl_; |
| 361 | |
| 362 | // Scope-specific information. |
| 363 | bool scope_inside_with_; // this scope is inside a 'with' of some outer scope |
| 364 | bool scope_contains_with_; // this scope contains a 'with' statement |
| 365 | bool scope_calls_eval_; // this scope contains an 'eval' call |
| 366 | |
| 367 | // Computed via PropagateScopeInfo. |
| 368 | bool outer_scope_calls_eval_; |
| 369 | bool inner_scope_calls_eval_; |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 370 | bool outer_scope_is_eval_scope_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 371 | bool force_eager_compilation_; |
| 372 | |
| 373 | // Computed via AllocateVariables; function scopes only. |
| 374 | int num_stack_slots_; |
| 375 | int num_heap_slots_; |
| 376 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 377 | // Serialized scopes support. |
| 378 | SerializedScopeInfo* scope_info_; |
| 379 | bool resolved() { return scope_info_ != NULL; } |
| 380 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 381 | // Create a non-local variable with a given name. |
| 382 | // These variables are looked up dynamically at runtime. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 383 | Variable* NonLocal(Handle<String> name, Variable::Mode mode); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 384 | |
| 385 | // Variable resolution. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 386 | Variable* LookupRecursive(Handle<String> name, |
| 387 | bool inner_lookup, |
| 388 | Variable** invalidated_local); |
| 389 | void ResolveVariable(Scope* global_scope, |
| 390 | Handle<Context> context, |
| 391 | VariableProxy* proxy); |
| 392 | void ResolveVariablesRecursively(Scope* global_scope, |
| 393 | Handle<Context> context); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 394 | |
| 395 | // Scope analysis. |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 396 | bool PropagateScopeInfo(bool outer_scope_calls_eval, |
| 397 | bool outer_scope_is_eval_scope); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 398 | bool HasTrivialContext() const; |
| 399 | |
| 400 | // Predicates. |
| 401 | bool MustAllocate(Variable* var); |
| 402 | bool MustAllocateInContext(Variable* var); |
| 403 | bool HasArgumentsParameter(); |
| 404 | |
| 405 | // Variable allocation. |
| 406 | void AllocateStackSlot(Variable* var); |
| 407 | void AllocateHeapSlot(Variable* var); |
| 408 | void AllocateParameterLocals(); |
| 409 | void AllocateNonParameterLocal(Variable* var); |
| 410 | void AllocateNonParameterLocals(); |
| 411 | void AllocateVariablesRecursively(); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 412 | |
| 413 | private: |
| 414 | Scope(Scope* inner_scope, SerializedScopeInfo* scope_info); |
| 415 | |
| 416 | void SetDefaults(Type type, |
| 417 | Scope* outer_scope, |
| 418 | SerializedScopeInfo* scope_info) { |
| 419 | outer_scope_ = outer_scope; |
| 420 | type_ = type; |
| 421 | scope_name_ = Factory::empty_symbol(); |
| 422 | dynamics_ = NULL; |
| 423 | receiver_ = NULL; |
| 424 | function_ = NULL; |
| 425 | arguments_ = NULL; |
| 426 | arguments_shadow_ = NULL; |
| 427 | illegal_redecl_ = NULL; |
| 428 | scope_inside_with_ = false; |
| 429 | scope_contains_with_ = false; |
| 430 | scope_calls_eval_ = false; |
| 431 | outer_scope_calls_eval_ = false; |
| 432 | inner_scope_calls_eval_ = false; |
| 433 | outer_scope_is_eval_scope_ = false; |
| 434 | force_eager_compilation_ = false; |
| 435 | num_stack_slots_ = 0; |
| 436 | num_heap_slots_ = 0; |
| 437 | scope_info_ = scope_info; |
| 438 | } |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 439 | }; |
| 440 | |
| 441 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 442 | // Scope used during pre-parsing. |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 443 | class DummyScope : public Scope { |
| 444 | public: |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 445 | DummyScope() |
| 446 | : Scope(GLOBAL_SCOPE), |
| 447 | nesting_level_(1), // Allows us to Leave the initial scope. |
| 448 | inside_with_level_(kNotInsideWith) { |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 449 | outer_scope_ = this; |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 450 | scope_inside_with_ = false; |
| 451 | } |
| 452 | |
| 453 | virtual void Initialize(bool inside_with) { |
| 454 | nesting_level_++; |
| 455 | if (inside_with && inside_with_level_ == kNotInsideWith) { |
| 456 | inside_with_level_ = nesting_level_; |
| 457 | } |
| 458 | ASSERT(inside_with_level_ <= nesting_level_); |
| 459 | } |
| 460 | |
| 461 | virtual void Leave() { |
| 462 | nesting_level_--; |
| 463 | ASSERT(nesting_level_ >= 0); |
| 464 | if (nesting_level_ < inside_with_level_) { |
| 465 | inside_with_level_ = kNotInsideWith; |
| 466 | } |
| 467 | ASSERT(inside_with_level_ <= nesting_level_); |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | virtual Variable* Lookup(Handle<String> name) { return NULL; } |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 471 | |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 472 | virtual VariableProxy* NewUnresolved(Handle<String> name, bool inside_with) { |
| 473 | return NULL; |
| 474 | } |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 475 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 476 | virtual Variable* NewTemporary(Handle<String> name) { return NULL; } |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 477 | |
| 478 | virtual bool HasTrivialOuterContext() const { |
| 479 | return (nesting_level_ == 0 || inside_with_level_ <= 0); |
| 480 | } |
| 481 | |
| 482 | private: |
| 483 | static const int kNotInsideWith = -1; |
| 484 | // Number of surrounding scopes of the current scope. |
| 485 | int nesting_level_; |
| 486 | // Nesting level of outermost scope that is contained in a with statement, |
| 487 | // or kNotInsideWith if there are no with's around the current scope. |
| 488 | int inside_with_level_; |
christian.plesner.hansen | 43d26ec | 2008-07-03 15:10:15 +0000 | [diff] [blame] | 489 | }; |
| 490 | |
| 491 | |
| 492 | } } // namespace v8::internal |
| 493 | |
| 494 | #endif // V8_SCOPES_H_ |