| OLD | NEW |
| 1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. | 11 // with the distribution. |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 GLOBAL_SCOPE // the top-level scope for a program or a top-level eval | 95 GLOBAL_SCOPE // the top-level scope for a program or a top-level eval |
| 96 }; | 96 }; |
| 97 | 97 |
| 98 enum LocalType { | 98 enum LocalType { |
| 99 PARAMETER, | 99 PARAMETER, |
| 100 VAR_OR_CONST | 100 VAR_OR_CONST |
| 101 }; | 101 }; |
| 102 | 102 |
| 103 Scope(Scope* outer_scope, Type type); | 103 Scope(Scope* outer_scope, Type type); |
| 104 | 104 |
| 105 virtual ~Scope() { } | |
| 106 | |
| 107 // Compute top scope and allocate variables. For lazy compilation the top | 105 // Compute top scope and allocate variables. For lazy compilation the top |
| 108 // scope only contains the single lazily compiled function, so this | 106 // scope only contains the single lazily compiled function, so this |
| 109 // doesn't re-allocate variables repeatedly. | 107 // doesn't re-allocate variables repeatedly. |
| 110 static bool Analyze(CompilationInfo* info); | 108 static bool Analyze(CompilationInfo* info); |
| 111 | 109 |
| 112 static Scope* DeserializeScopeChain(CompilationInfo* info, | 110 static Scope* DeserializeScopeChain(CompilationInfo* info, |
| 113 Scope* innermost_scope); | 111 Scope* innermost_scope); |
| 114 | 112 |
| 115 // The scope name is only used for printing/debugging. | 113 // The scope name is only used for printing/debugging. |
| 116 void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; } | 114 void SetScopeName(Handle<String> scope_name) { scope_name_ = scope_name; } |
| 117 | 115 |
| 118 virtual void Initialize(bool inside_with); | 116 void Initialize(bool inside_with); |
| 119 | |
| 120 // Called just before leaving a scope. | |
| 121 virtual void Leave() { | |
| 122 // No cleanup or fixup necessary. | |
| 123 } | |
| 124 | 117 |
| 125 // --------------------------------------------------------------------------- | 118 // --------------------------------------------------------------------------- |
| 126 // Declarations | 119 // Declarations |
| 127 | 120 |
| 128 // Lookup a variable in this scope. Returns the variable or NULL if not found. | 121 // Lookup a variable in this scope. Returns the variable or NULL if not found. |
| 129 virtual Variable* LocalLookup(Handle<String> name); | 122 Variable* LocalLookup(Handle<String> name); |
| 130 | 123 |
| 131 // Lookup a variable in this scope or outer scopes. | 124 // Lookup a variable in this scope or outer scopes. |
| 132 // Returns the variable or NULL if not found. | 125 // Returns the variable or NULL if not found. |
| 133 virtual Variable* Lookup(Handle<String> name); | 126 Variable* Lookup(Handle<String> name); |
| 134 | 127 |
| 135 // Declare the function variable for a function literal. This variable | 128 // Declare the function variable for a function literal. This variable |
| 136 // is in an intermediate scope between this function scope and the the | 129 // is in an intermediate scope between this function scope and the the |
| 137 // outer scope. Only possible for function scopes; at most one variable. | 130 // outer scope. Only possible for function scopes; at most one variable. |
| 138 Variable* DeclareFunctionVar(Handle<String> name); | 131 Variable* DeclareFunctionVar(Handle<String> name); |
| 139 | 132 |
| 140 // Declare a local variable in this scope. If the variable has been | 133 // Declare a local variable in this scope. If the variable has been |
| 141 // declared before, the previously declared variable is returned. | 134 // declared before, the previously declared variable is returned. |
| 142 virtual Variable* DeclareLocal(Handle<String> name, | 135 Variable* DeclareLocal(Handle<String> name, |
| 143 Variable::Mode mode, | 136 Variable::Mode mode, |
| 144 LocalType type); | 137 LocalType type); |
| 145 | 138 |
| 146 // Declare an implicit global variable in this scope which must be a | 139 // Declare an implicit global variable in this scope which must be a |
| 147 // global scope. The variable was introduced (possibly from an inner | 140 // global scope. The variable was introduced (possibly from an inner |
| 148 // scope) by a reference to an unresolved variable with no intervening | 141 // scope) by a reference to an unresolved variable with no intervening |
| 149 // with statements or eval calls. | 142 // with statements or eval calls. |
| 150 Variable* DeclareGlobal(Handle<String> name); | 143 Variable* DeclareGlobal(Handle<String> name); |
| 151 | 144 |
| 152 // Add a parameter to the parameter list. The parameter must have been | 145 // 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 | 146 // 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 | 147 // the parameter list; they must be added in source order, from left to |
| 155 // right. | 148 // right. |
| 156 void AddParameter(Variable* var); | 149 void AddParameter(Variable* var); |
| 157 | 150 |
| 158 // Create a new unresolved variable. | 151 // Create a new unresolved variable. |
| 159 virtual VariableProxy* NewUnresolved(Handle<String> name, | 152 VariableProxy* NewUnresolved(Handle<String> name, |
| 160 bool inside_with, | 153 bool inside_with, |
| 161 int position = RelocInfo::kNoPosition); | 154 int position = RelocInfo::kNoPosition); |
| 162 | 155 |
| 163 // Remove a unresolved variable. During parsing, an unresolved variable | 156 // Remove a unresolved variable. During parsing, an unresolved variable |
| 164 // may have been added optimistically, but then only the variable name | 157 // may have been added optimistically, but then only the variable name |
| 165 // was used (typically for labels). If the variable was not declared, the | 158 // was used (typically for labels). If the variable was not declared, the |
| 166 // addition introduced a new unresolved variable which may end up being | 159 // addition introduced a new unresolved variable which may end up being |
| 167 // allocated globally as a "ghost" variable. RemoveUnresolved removes | 160 // allocated globally as a "ghost" variable. RemoveUnresolved removes |
| 168 // such a variable again if it was added; otherwise this is a no-op. | 161 // such a variable again if it was added; otherwise this is a no-op. |
| 169 void RemoveUnresolved(VariableProxy* var); | 162 void RemoveUnresolved(VariableProxy* var); |
| 170 | 163 |
| 171 // Creates a new temporary variable in this scope. The name is only used | 164 // 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, | 165 // 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* | 166 // the only way to get hold of the temporary is by keeping the Variable* |
| 174 // around. | 167 // around. |
| 175 virtual Variable* NewTemporary(Handle<String> name); | 168 Variable* NewTemporary(Handle<String> name); |
| 176 | 169 |
| 177 // Adds the specific declaration node to the list of declarations in | 170 // Adds the specific declaration node to the list of declarations in |
| 178 // this scope. The declarations are processed as part of entering | 171 // this scope. The declarations are processed as part of entering |
| 179 // the scope; see codegen.cc:ProcessDeclarations. | 172 // the scope; see codegen.cc:ProcessDeclarations. |
| 180 void AddDeclaration(Declaration* declaration); | 173 void AddDeclaration(Declaration* declaration); |
| 181 | 174 |
| 182 // --------------------------------------------------------------------------- | 175 // --------------------------------------------------------------------------- |
| 183 // Illegal redeclaration support. | 176 // Illegal redeclaration support. |
| 184 | 177 |
| 185 // Set an expression node that will be executed when the scope is | 178 // Set an expression node that will be executed when the scope is |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 Variable* arguments() const { return arguments_; } | 263 Variable* arguments() const { return arguments_; } |
| 271 | 264 |
| 272 // The '.arguments' shadow variable if we need to allocate it; NULL otherwise. | 265 // The '.arguments' shadow variable if we need to allocate it; NULL otherwise. |
| 273 // If arguments_shadow() exist, arguments() exists, too. | 266 // If arguments_shadow() exist, arguments() exists, too. |
| 274 Variable* arguments_shadow() const { return arguments_shadow_; } | 267 Variable* arguments_shadow() const { return arguments_shadow_; } |
| 275 | 268 |
| 276 // Declarations list. | 269 // Declarations list. |
| 277 ZoneList<Declaration*>* declarations() { return &decls_; } | 270 ZoneList<Declaration*>* declarations() { return &decls_; } |
| 278 | 271 |
| 279 | 272 |
| 280 | |
| 281 // --------------------------------------------------------------------------- | 273 // --------------------------------------------------------------------------- |
| 282 // Variable allocation. | 274 // Variable allocation. |
| 283 | 275 |
| 284 // Collect all used locals in this scope. | 276 // Collect all used locals in this scope. |
| 285 template<class Allocator> | 277 template<class Allocator> |
| 286 void CollectUsedVariables(List<Variable*, Allocator>* locals); | 278 void CollectUsedVariables(List<Variable*, Allocator>* locals); |
| 287 | 279 |
| 288 // Resolve and fill in the allocation information for all variables | 280 // Resolve and fill in the allocation information for all variables |
| 289 // in this scopes. Must be called *after* all scopes have been | 281 // in this scopes. Must be called *after* all scopes have been |
| 290 // processed (parsed) to ensure that unresolved variables can be | 282 // processed (parsed) to ensure that unresolved variables can be |
| (...skipping 11 matching lines...) Expand all Loading... |
| 302 int num_stack_slots() const { return num_stack_slots_; } | 294 int num_stack_slots() const { return num_stack_slots_; } |
| 303 int num_heap_slots() const { return num_heap_slots_; } | 295 int num_heap_slots() const { return num_heap_slots_; } |
| 304 | 296 |
| 305 // Make sure this scope and all outer scopes are eagerly compiled. | 297 // Make sure this scope and all outer scopes are eagerly compiled. |
| 306 void ForceEagerCompilation() { force_eager_compilation_ = true; } | 298 void ForceEagerCompilation() { force_eager_compilation_ = true; } |
| 307 | 299 |
| 308 // Determine if we can use lazy compilation for this scope. | 300 // Determine if we can use lazy compilation for this scope. |
| 309 bool AllowsLazyCompilation() const; | 301 bool AllowsLazyCompilation() const; |
| 310 | 302 |
| 311 // True if the outer context of this scope is always the global context. | 303 // True if the outer context of this scope is always the global context. |
| 312 virtual bool HasTrivialOuterContext() const; | 304 bool HasTrivialOuterContext() const; |
| 313 | 305 |
| 314 // The number of contexts between this and scope; zero if this == scope. | 306 // The number of contexts between this and scope; zero if this == scope. |
| 315 int ContextChainLength(Scope* scope); | 307 int ContextChainLength(Scope* scope); |
| 316 | 308 |
| 317 // --------------------------------------------------------------------------- | 309 // --------------------------------------------------------------------------- |
| 318 // Strict mode support. | 310 // Strict mode support. |
| 319 bool IsDeclared(Handle<String> name) { | 311 bool IsDeclared(Handle<String> name) { |
| 320 // During formal parameter list parsing the scope only contains | 312 // During formal parameter list parsing the scope only contains |
| 321 // two variables inserted at initialization: "this" and "arguments". | 313 // two variables inserted at initialization: "this" and "arguments". |
| 322 // "this" is an invalid parameter name and "arguments" is invalid parameter | 314 // "this" is an invalid parameter name and "arguments" is invalid parameter |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 442 inner_scopes_.Add(inner_scope); | 434 inner_scopes_.Add(inner_scope); |
| 443 inner_scope->outer_scope_ = this; | 435 inner_scope->outer_scope_ = this; |
| 444 } | 436 } |
| 445 } | 437 } |
| 446 | 438 |
| 447 void SetDefaults(Type type, | 439 void SetDefaults(Type type, |
| 448 Scope* outer_scope, | 440 Scope* outer_scope, |
| 449 Handle<SerializedScopeInfo> scope_info); | 441 Handle<SerializedScopeInfo> scope_info); |
| 450 }; | 442 }; |
| 451 | 443 |
| 452 | |
| 453 // Scope used during pre-parsing. | |
| 454 class DummyScope : public Scope { | |
| 455 public: | |
| 456 DummyScope() | |
| 457 : Scope(GLOBAL_SCOPE), | |
| 458 nesting_level_(1), // Allows us to Leave the initial scope. | |
| 459 inside_with_level_(kNotInsideWith) { | |
| 460 outer_scope_ = this; | |
| 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_); | |
| 479 } | |
| 480 | |
| 481 virtual Variable* Lookup(Handle<String> name) { return NULL; } | |
| 482 | |
| 483 virtual VariableProxy* NewUnresolved(Handle<String> name, | |
| 484 bool inside_with, | |
| 485 int position = RelocInfo::kNoPosition) { | |
| 486 return NULL; | |
| 487 } | |
| 488 | |
| 489 virtual Variable* NewTemporary(Handle<String> name) { return NULL; } | |
| 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_; | |
| 502 }; | |
| 503 | |
| 504 | |
| 505 } } // namespace v8::internal | 444 } } // namespace v8::internal |
| 506 | 445 |
| 507 #endif // V8_SCOPES_H_ | 446 #endif // V8_SCOPES_H_ |
| OLD | NEW |