Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +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 | |
| 34 | namespace v8 { |
| 35 | namespace internal { |
| 36 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 37 | class CompilationInfo; |
| 38 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 39 | |
| 40 | // A hash map to support fast variable declaration and lookup. |
| 41 | class VariableMap: public HashMap { |
| 42 | public: |
| 43 | VariableMap(); |
| 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. |
| 47 | explicit VariableMap(bool gotta_love_static_overloading); |
| 48 | |
| 49 | virtual ~VariableMap(); |
| 50 | |
| 51 | Variable* Declare(Scope* scope, |
| 52 | Handle<String> name, |
| 53 | Variable::Mode mode, |
| 54 | bool is_valid_lhs, |
| 55 | Variable::Kind kind); |
| 56 | |
| 57 | Variable* Lookup(Handle<String> name); |
| 58 | }; |
| 59 | |
| 60 | |
| 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: |
| 67 | VariableMap* GetMap(Variable::Mode mode) { |
| 68 | int index = mode - Variable::DYNAMIC; |
| 69 | ASSERT(index >= 0 && index < 3); |
| 70 | return &maps_[index]; |
| 71 | } |
| 72 | |
| 73 | private: |
| 74 | VariableMap maps_[3]; |
| 75 | }; |
| 76 | |
| 77 | |
| 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 | |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 98 | enum LocalType { |
| 99 | PARAMETER, |
| 100 | VAR_OR_CONST |
| 101 | }; |
| 102 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 103 | Scope(Scope* outer_scope, Type type); |
| 104 | |
| 105 | virtual ~Scope() { } |
| 106 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 107 | // Compute top scope and allocate variables. For lazy compilation the top |
| 108 | // scope only contains the single lazily compiled function, so this |
| 109 | // doesn't re-allocate variables repeatedly. |
| 110 | static bool Analyze(CompilationInfo* info); |
| 111 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 112 | static Scope* DeserializeScopeChain(CompilationInfo* info, |
| 113 | Scope* innermost_scope); |
| 114 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 115 | // The scope name is only used for printing/debugging. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 116 | void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 117 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 118 | virtual void Initialize(bool inside_with); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 119 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 120 | // Called just before leaving a scope. |
| 121 | virtual void Leave() { |
| 122 | // No cleanup or fixup necessary. |
| 123 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 124 | |
| 125 | // --------------------------------------------------------------------------- |
| 126 | // Declarations |
| 127 | |
| 128 | // Lookup a variable in this scope. Returns the variable or NULL if not found. |
| 129 | virtual Variable* LocalLookup(Handle<String> name); |
| 130 | |
| 131 | // Lookup a variable in this scope or outer scopes. |
| 132 | // Returns the variable or NULL if not found. |
| 133 | virtual Variable* Lookup(Handle<String> name); |
| 134 | |
| 135 | // Declare the function variable for a function literal. This variable |
| 136 | // is in an intermediate scope between this function scope and the the |
| 137 | // outer scope. Only possible for function scopes; at most one variable. |
| 138 | Variable* DeclareFunctionVar(Handle<String> name); |
| 139 | |
| 140 | // Declare a local variable in this scope. If the variable has been |
| 141 | // declared before, the previously declared variable is returned. |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 142 | virtual Variable* DeclareLocal(Handle<String> name, |
| 143 | Variable::Mode mode, |
| 144 | LocalType type); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 145 | |
| 146 | // Declare an implicit global variable in this scope which must be a |
| 147 | // global scope. The variable was introduced (possibly from an inner |
| 148 | // scope) by a reference to an unresolved variable with no intervening |
| 149 | // with statements or eval calls. |
| 150 | Variable* DeclareGlobal(Handle<String> name); |
| 151 | |
| 152 | // Add a parameter to the parameter list. The parameter must have been |
| 153 | // declared via Declare. The same parameter may occur more than once in |
| 154 | // the parameter list; they must be added in source order, from left to |
| 155 | // right. |
| 156 | void AddParameter(Variable* var); |
| 157 | |
| 158 | // Create a new unresolved variable. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 159 | virtual VariableProxy* NewUnresolved(Handle<String> name, |
| 160 | bool inside_with, |
| 161 | int position = RelocInfo::kNoPosition); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 162 | |
| 163 | // Remove a unresolved variable. During parsing, an unresolved variable |
| 164 | // may have been added optimistically, but then only the variable name |
| 165 | // was used (typically for labels). If the variable was not declared, the |
| 166 | // addition introduced a new unresolved variable which may end up being |
| 167 | // allocated globally as a "ghost" variable. RemoveUnresolved removes |
| 168 | // such a variable again if it was added; otherwise this is a no-op. |
| 169 | void RemoveUnresolved(VariableProxy* var); |
| 170 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 171 | // Creates a new temporary variable in this scope. The name is only used |
| 172 | // for printing and cannot be used to find the variable. In particular, |
| 173 | // the only way to get hold of the temporary is by keeping the Variable* |
| 174 | // around. |
| 175 | virtual Variable* NewTemporary(Handle<String> name); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 176 | |
| 177 | // Adds the specific declaration node to the list of declarations in |
| 178 | // this scope. The declarations are processed as part of entering |
| 179 | // the scope; see codegen.cc:ProcessDeclarations. |
| 180 | void AddDeclaration(Declaration* declaration); |
| 181 | |
| 182 | // --------------------------------------------------------------------------- |
| 183 | // Illegal redeclaration support. |
| 184 | |
| 185 | // Set an expression node that will be executed when the scope is |
| 186 | // entered. We only keep track of one illegal redeclaration node per |
| 187 | // scope - the first one - so if you try to set it multiple times |
| 188 | // the additional requests will be silently ignored. |
| 189 | void SetIllegalRedeclaration(Expression* expression); |
| 190 | |
| 191 | // Visit the illegal redeclaration expression. Do not call if the |
| 192 | // scope doesn't have an illegal redeclaration node. |
| 193 | void VisitIllegalRedeclaration(AstVisitor* visitor); |
| 194 | |
| 195 | // Check if the scope has (at least) one illegal redeclaration. |
| 196 | bool HasIllegalRedeclaration() const { return illegal_redecl_ != NULL; } |
| 197 | |
| 198 | |
| 199 | // --------------------------------------------------------------------------- |
| 200 | // Scope-specific info. |
| 201 | |
| 202 | // Inform the scope that the corresponding code contains a with statement. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 203 | void RecordWithStatement() { scope_contains_with_ = true; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 204 | |
| 205 | // Inform the scope that the corresponding code contains an eval call. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 206 | void RecordEvalCall() { scope_calls_eval_ = true; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 207 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 208 | // Enable strict mode for the scope (unless disabled by a global flag). |
| 209 | void EnableStrictMode() { |
| 210 | strict_mode_ = FLAG_strict_mode; |
| 211 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 212 | |
| 213 | // --------------------------------------------------------------------------- |
| 214 | // Predicates. |
| 215 | |
| 216 | // Specific scope types. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 217 | bool is_eval_scope() const { return type_ == EVAL_SCOPE; } |
| 218 | bool is_function_scope() const { return type_ == FUNCTION_SCOPE; } |
| 219 | bool is_global_scope() const { return type_ == GLOBAL_SCOPE; } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 220 | bool is_strict_mode() const { return strict_mode_; } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 221 | bool is_strict_mode_eval_scope() const { |
| 222 | return is_eval_scope() && is_strict_mode(); |
| 223 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 224 | |
| 225 | // Information about which scopes calls eval. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 226 | bool calls_eval() const { return scope_calls_eval_; } |
| 227 | bool outer_scope_calls_eval() const { return outer_scope_calls_eval_; } |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 228 | bool outer_scope_calls_non_strict_eval() const { |
| 229 | return outer_scope_calls_non_strict_eval_; |
| 230 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 231 | |
| 232 | // Is this scope inside a with statement. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 233 | bool inside_with() const { return scope_inside_with_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 234 | // Does this scope contain a with statement. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 235 | bool contains_with() const { return scope_contains_with_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 236 | |
| 237 | // The scope immediately surrounding this scope, or NULL. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 238 | Scope* outer_scope() const { return outer_scope_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 239 | |
| 240 | // --------------------------------------------------------------------------- |
| 241 | // Accessors. |
| 242 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 243 | // A new variable proxy corresponding to the (function) receiver. |
| 244 | VariableProxy* receiver() const { |
| 245 | VariableProxy* proxy = |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 246 | new VariableProxy(FACTORY->this_symbol(), true, false); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 247 | proxy->BindTo(receiver_); |
| 248 | return proxy; |
| 249 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 250 | |
| 251 | // The variable holding the function literal for named function |
| 252 | // literals, or NULL. |
| 253 | // Only valid for function scopes. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 254 | Variable* function() const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 255 | ASSERT(is_function_scope()); |
| 256 | return function_; |
| 257 | } |
| 258 | |
| 259 | // Parameters. The left-most parameter has index 0. |
| 260 | // Only valid for function scopes. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 261 | Variable* parameter(int index) const { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 262 | ASSERT(is_function_scope()); |
| 263 | return params_[index]; |
| 264 | } |
| 265 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 266 | int num_parameters() const { return params_.length(); } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 267 | |
| 268 | // The local variable 'arguments' if we need to allocate it; NULL otherwise. |
| 269 | // If arguments() exist, arguments_shadow() exists, too. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 270 | Variable* arguments() const { return arguments_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 271 | |
| 272 | // The '.arguments' shadow variable if we need to allocate it; NULL otherwise. |
| 273 | // If arguments_shadow() exist, arguments() exists, too. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 274 | Variable* arguments_shadow() const { return arguments_shadow_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 275 | |
| 276 | // Declarations list. |
| 277 | ZoneList<Declaration*>* declarations() { return &decls_; } |
| 278 | |
| 279 | |
| 280 | |
| 281 | // --------------------------------------------------------------------------- |
| 282 | // Variable allocation. |
| 283 | |
| 284 | // Collect all used locals in this scope. |
| 285 | template<class Allocator> |
| 286 | void CollectUsedVariables(List<Variable*, Allocator>* locals); |
| 287 | |
| 288 | // Resolve and fill in the allocation information for all variables |
| 289 | // in this scopes. Must be called *after* all scopes have been |
| 290 | // processed (parsed) to ensure that unresolved variables can be |
| 291 | // resolved properly. |
| 292 | // |
| 293 | // In the case of code compiled and run using 'eval', the context |
| 294 | // parameter is the context in which eval was called. In all other |
| 295 | // cases the context parameter is an empty handle. |
| 296 | void AllocateVariables(Handle<Context> context); |
| 297 | |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 298 | // Current number of var or const locals. |
| 299 | int num_var_or_const() { return num_var_or_const_; } |
| 300 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 301 | // Result of variable allocation. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 302 | int num_stack_slots() const { return num_stack_slots_; } |
| 303 | int num_heap_slots() const { return num_heap_slots_; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 304 | |
| 305 | // Make sure this scope and all outer scopes are eagerly compiled. |
| 306 | void ForceEagerCompilation() { force_eager_compilation_ = true; } |
| 307 | |
| 308 | // Determine if we can use lazy compilation for this scope. |
| 309 | bool AllowsLazyCompilation() const; |
| 310 | |
| 311 | // True if the outer context of this scope is always the global context. |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 312 | virtual bool HasTrivialOuterContext() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 313 | |
| 314 | // The number of contexts between this and scope; zero if this == scope. |
| 315 | int ContextChainLength(Scope* scope); |
| 316 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 317 | // --------------------------------------------------------------------------- |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 318 | // Strict mode support. |
| 319 | bool IsDeclared(Handle<String> name) { |
| 320 | // During formal parameter list parsing the scope only contains |
| 321 | // two variables inserted at initialization: "this" and "arguments". |
| 322 | // "this" is an invalid parameter name and "arguments" is invalid parameter |
| 323 | // name in strict mode. Therefore looking up with the map which includes |
| 324 | // "this" and "arguments" in addition to all formal parameters is safe. |
| 325 | return variables_.Lookup(name) != NULL; |
| 326 | } |
| 327 | |
| 328 | // --------------------------------------------------------------------------- |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 329 | // Debugging. |
| 330 | |
| 331 | #ifdef DEBUG |
| 332 | void Print(int n = 0); // n = indentation; n < 0 => don't print recursively |
| 333 | #endif |
| 334 | |
| 335 | // --------------------------------------------------------------------------- |
| 336 | // Implementation. |
| 337 | protected: |
| 338 | friend class ParserFactory; |
| 339 | |
| 340 | explicit Scope(Type type); |
| 341 | |
| 342 | // Scope tree. |
| 343 | Scope* outer_scope_; // the immediately enclosing outer scope, or NULL |
| 344 | ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes |
| 345 | |
| 346 | // The scope type. |
| 347 | Type type_; |
| 348 | |
| 349 | // Debugging support. |
| 350 | Handle<String> scope_name_; |
| 351 | |
| 352 | // The variables declared in this scope: |
| 353 | // |
| 354 | // All user-declared variables (incl. parameters). For global scopes |
| 355 | // variables may be implicitly 'declared' by being used (possibly in |
| 356 | // an inner scope) with no intervening with statements or eval calls. |
| 357 | VariableMap variables_; |
| 358 | // Compiler-allocated (user-invisible) temporaries. |
| 359 | ZoneList<Variable*> temps_; |
| 360 | // Parameter list in source order. |
| 361 | ZoneList<Variable*> params_; |
| 362 | // Variables that must be looked up dynamically. |
| 363 | DynamicScopePart* dynamics_; |
| 364 | // Unresolved variables referred to from this scope. |
| 365 | ZoneList<VariableProxy*> unresolved_; |
| 366 | // Declarations. |
| 367 | ZoneList<Declaration*> decls_; |
| 368 | // Convenience variable. |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 369 | Variable* receiver_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 370 | // Function variable, if any; function scopes only. |
| 371 | Variable* function_; |
| 372 | // Convenience variable; function scopes only. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 373 | Variable* arguments_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 374 | // Convenience variable; function scopes only. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 375 | Variable* arguments_shadow_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 376 | |
| 377 | // Illegal redeclaration. |
| 378 | Expression* illegal_redecl_; |
| 379 | |
| 380 | // Scope-specific information. |
| 381 | bool scope_inside_with_; // this scope is inside a 'with' of some outer scope |
| 382 | bool scope_contains_with_; // this scope contains a 'with' statement |
| 383 | bool scope_calls_eval_; // this scope contains an 'eval' call |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 384 | bool strict_mode_; // this scope is a strict mode scope |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 385 | |
| 386 | // Computed via PropagateScopeInfo. |
| 387 | bool outer_scope_calls_eval_; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 388 | bool outer_scope_calls_non_strict_eval_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 389 | bool inner_scope_calls_eval_; |
| 390 | bool outer_scope_is_eval_scope_; |
| 391 | bool force_eager_compilation_; |
| 392 | |
Steve Block | 053d10c | 2011-06-13 19:13:29 +0100 | [diff] [blame] | 393 | // Computed as variables are declared. |
| 394 | int num_var_or_const_; |
| 395 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 396 | // Computed via AllocateVariables; function scopes only. |
| 397 | int num_stack_slots_; |
| 398 | int num_heap_slots_; |
| 399 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 400 | // Serialized scopes support. |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 401 | Handle<SerializedScopeInfo> scope_info_; |
| 402 | bool resolved() { return !scope_info_.is_null(); } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 403 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 404 | // Create a non-local variable with a given name. |
| 405 | // These variables are looked up dynamically at runtime. |
| 406 | Variable* NonLocal(Handle<String> name, Variable::Mode mode); |
| 407 | |
| 408 | // Variable resolution. |
| 409 | Variable* LookupRecursive(Handle<String> name, |
| 410 | bool inner_lookup, |
| 411 | Variable** invalidated_local); |
| 412 | void ResolveVariable(Scope* global_scope, |
| 413 | Handle<Context> context, |
| 414 | VariableProxy* proxy); |
| 415 | void ResolveVariablesRecursively(Scope* global_scope, |
| 416 | Handle<Context> context); |
| 417 | |
| 418 | // Scope analysis. |
| 419 | bool PropagateScopeInfo(bool outer_scope_calls_eval, |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 420 | bool outer_scope_calls_non_strict_eval, |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 421 | bool outer_scope_is_eval_scope); |
| 422 | bool HasTrivialContext() const; |
| 423 | |
| 424 | // Predicates. |
| 425 | bool MustAllocate(Variable* var); |
| 426 | bool MustAllocateInContext(Variable* var); |
| 427 | bool HasArgumentsParameter(); |
| 428 | |
| 429 | // Variable allocation. |
| 430 | void AllocateStackSlot(Variable* var); |
| 431 | void AllocateHeapSlot(Variable* var); |
| 432 | void AllocateParameterLocals(); |
| 433 | void AllocateNonParameterLocal(Variable* var); |
| 434 | void AllocateNonParameterLocals(); |
| 435 | void AllocateVariablesRecursively(); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 436 | |
| 437 | private: |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 438 | Scope(Scope* inner_scope, Handle<SerializedScopeInfo> scope_info); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 439 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 440 | void AddInnerScope(Scope* inner_scope) { |
| 441 | if (inner_scope != NULL) { |
| 442 | inner_scopes_.Add(inner_scope); |
| 443 | inner_scope->outer_scope_ = this; |
| 444 | } |
| 445 | } |
| 446 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 447 | void SetDefaults(Type type, |
| 448 | Scope* outer_scope, |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 449 | Handle<SerializedScopeInfo> scope_info); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 450 | }; |
| 451 | |
| 452 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 453 | // Scope used during pre-parsing. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 454 | class DummyScope : public Scope { |
| 455 | public: |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 456 | DummyScope() |
| 457 | : Scope(GLOBAL_SCOPE), |
| 458 | nesting_level_(1), // Allows us to Leave the initial scope. |
| 459 | inside_with_level_(kNotInsideWith) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 460 | outer_scope_ = this; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 461 | scope_inside_with_ = false; |
| 462 | } |
| 463 | |
| 464 | virtual void Initialize(bool inside_with) { |
| 465 | nesting_level_++; |
| 466 | if (inside_with && inside_with_level_ == kNotInsideWith) { |
| 467 | inside_with_level_ = nesting_level_; |
| 468 | } |
| 469 | ASSERT(inside_with_level_ <= nesting_level_); |
| 470 | } |
| 471 | |
| 472 | virtual void Leave() { |
| 473 | nesting_level_--; |
| 474 | ASSERT(nesting_level_ >= 0); |
| 475 | if (nesting_level_ < inside_with_level_) { |
| 476 | inside_with_level_ = kNotInsideWith; |
| 477 | } |
| 478 | ASSERT(inside_with_level_ <= nesting_level_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | virtual Variable* Lookup(Handle<String> name) { return NULL; } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 482 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 483 | virtual VariableProxy* NewUnresolved(Handle<String> name, |
| 484 | bool inside_with, |
| 485 | int position = RelocInfo::kNoPosition) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 486 | return NULL; |
| 487 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 488 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 489 | virtual Variable* NewTemporary(Handle<String> name) { return NULL; } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 490 | |
| 491 | virtual bool HasTrivialOuterContext() const { |
| 492 | return (nesting_level_ == 0 || inside_with_level_ <= 0); |
| 493 | } |
| 494 | |
| 495 | private: |
| 496 | static const int kNotInsideWith = -1; |
| 497 | // Number of surrounding scopes of the current scope. |
| 498 | int nesting_level_; |
| 499 | // Nesting level of outermost scope that is contained in a with statement, |
| 500 | // or kNotInsideWith if there are no with's around the current scope. |
| 501 | int inside_with_level_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 502 | }; |
| 503 | |
| 504 | |
| 505 | } } // namespace v8::internal |
| 506 | |
| 507 | #endif // V8_SCOPES_H_ |