Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_SCOPES_H_ | 5 #ifndef V8_SCOPES_H_ |
| 6 #define V8_SCOPES_H_ | 6 #define V8_SCOPES_H_ |
| 7 | 7 |
| 8 #include "src/ast.h" | 8 #include "src/ast.h" |
| 9 #include "src/zone.h" | 9 #include "src/zone.h" |
| 10 | 10 |
| 11 namespace v8 { | 11 namespace v8 { |
| 12 namespace internal { | 12 namespace internal { |
| 13 | 13 |
| 14 class CompilationInfo; | 14 class CompilationInfo; |
| 15 | 15 |
| 16 | 16 |
| 17 // A hash map to support fast variable declaration and lookup. | 17 // A hash map to support fast variable declaration and lookup. |
| 18 class VariableMap: public ZoneHashMap { | 18 class VariableMap: public ZoneHashMap { |
| 19 public: | 19 public: |
| 20 explicit VariableMap(Zone* zone); | 20 explicit VariableMap(Zone* zone); |
| 21 | 21 |
| 22 virtual ~VariableMap(); | 22 virtual ~VariableMap(); |
| 23 | 23 |
| 24 Variable* Declare(Scope* scope, const AstRawString* name, VariableMode mode, | 24 Variable* Declare(Scope* scope, const AstRawString* name, VariableMode mode, |
| 25 bool is_valid_lhs, Variable::Kind kind, | 25 Variable::Kind kind, |
| 26 InitializationFlag initialization_flag, | 26 InitializationFlag initialization_flag, |
| 27 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned, | 27 MaybeAssignedFlag maybe_assigned_flag = kNotAssigned, |
| 28 Interface* interface = Interface::NewValue()); | 28 Interface* interface = Interface::NewValue()); |
| 29 | 29 |
| 30 Variable* Lookup(const AstRawString* name); | 30 Variable* Lookup(const AstRawString* name); |
| 31 | 31 |
| 32 Zone* zone() const { return zone_; } | 32 Zone* zone() const { return zone_; } |
| 33 | 33 |
| 34 private: | 34 private: |
| 35 Zone* zone_; | 35 Zone* zone_; |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 145 // Create a new unresolved variable. | 145 // Create a new unresolved variable. |
| 146 VariableProxy* NewUnresolved(AstNodeFactory* factory, | 146 VariableProxy* NewUnresolved(AstNodeFactory* factory, |
| 147 const AstRawString* name, | 147 const AstRawString* name, |
| 148 Interface* interface = Interface::NewValue(), | 148 Interface* interface = Interface::NewValue(), |
| 149 int position = RelocInfo::kNoPosition) { | 149 int position = RelocInfo::kNoPosition) { |
| 150 // Note that we must not share the unresolved variables with | 150 // Note that we must not share the unresolved variables with |
| 151 // the same name because they may be removed selectively via | 151 // the same name because they may be removed selectively via |
| 152 // RemoveUnresolved(). | 152 // RemoveUnresolved(). |
| 153 DCHECK(!already_resolved()); | 153 DCHECK(!already_resolved()); |
| 154 VariableProxy* proxy = | 154 VariableProxy* proxy = |
| 155 factory->NewVariableProxy(name, false, interface, position); | 155 factory->NewVariableProxy(name, interface, position); |
|
wingo
2015/02/06 17:31:19
Please make this function take an additional Varia
| |
| 156 unresolved_.Add(proxy, zone_); | 156 unresolved_.Add(proxy, zone_); |
| 157 return proxy; | 157 return proxy; |
| 158 } | 158 } |
| 159 | 159 |
| 160 // Remove a unresolved variable. During parsing, an unresolved variable | 160 // Remove a unresolved variable. During parsing, an unresolved variable |
| 161 // may have been added optimistically, but then only the variable name | 161 // may have been added optimistically, but then only the variable name |
| 162 // was used (typically for labels). If the variable was not declared, the | 162 // was used (typically for labels). If the variable was not declared, the |
| 163 // addition introduced a new unresolved variable which may end up being | 163 // addition introduced a new unresolved variable which may end up being |
| 164 // allocated globally as a "ghost" variable. RemoveUnresolved removes | 164 // allocated globally as a "ghost" variable. RemoveUnresolved removes |
| 165 // such a variable again if it was added; otherwise this is a no-op. | 165 // such a variable again if it was added; otherwise this is a no-op. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 216 void RecordArgumentsUsage() { scope_uses_arguments_ = true; } | 216 void RecordArgumentsUsage() { scope_uses_arguments_ = true; } |
| 217 | 217 |
| 218 // Inform the scope that the corresponding code uses "super". | 218 // Inform the scope that the corresponding code uses "super". |
| 219 void RecordSuperPropertyUsage() { scope_uses_super_property_ = true; } | 219 void RecordSuperPropertyUsage() { scope_uses_super_property_ = true; } |
| 220 | 220 |
| 221 // Inform the scope that the corresponding code invokes "super" constructor. | 221 // Inform the scope that the corresponding code invokes "super" constructor. |
| 222 void RecordSuperConstructorCallUsage() { | 222 void RecordSuperConstructorCallUsage() { |
| 223 scope_uses_super_constructor_call_ = true; | 223 scope_uses_super_constructor_call_ = true; |
| 224 } | 224 } |
| 225 | 225 |
| 226 // Inform the scope that the corresponding code uses "this". | |
| 227 void RecordThisUsage() { scope_uses_this_ = true; } | |
| 228 | |
| 229 // Set the strict mode flag (unless disabled by a global flag). | 226 // Set the strict mode flag (unless disabled by a global flag). |
| 230 void SetStrictMode(StrictMode strict_mode) { strict_mode_ = strict_mode; } | 227 void SetStrictMode(StrictMode strict_mode) { strict_mode_ = strict_mode; } |
| 231 | 228 |
| 232 // Set the ASM module flag. | 229 // Set the ASM module flag. |
| 233 void SetAsmModule() { asm_module_ = true; } | 230 void SetAsmModule() { asm_module_ = true; } |
| 234 | 231 |
| 235 // Position in the source where this scope begins and ends. | 232 // Position in the source where this scope begins and ends. |
| 236 // | 233 // |
| 237 // * For the scope of a with statement | 234 // * For the scope of a with statement |
| 238 // with (obj) stmt | 235 // with (obj) stmt |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 321 return inner_scope_uses_super_property_; | 318 return inner_scope_uses_super_property_; |
| 322 } | 319 } |
| 323 // Does this scope calls "super" constructor. | 320 // Does this scope calls "super" constructor. |
| 324 bool uses_super_constructor_call() const { | 321 bool uses_super_constructor_call() const { |
| 325 return scope_uses_super_constructor_call_; | 322 return scope_uses_super_constructor_call_; |
| 326 } | 323 } |
| 327 // Does any inner scope calls "super" constructor. | 324 // Does any inner scope calls "super" constructor. |
| 328 bool inner_uses_super_constructor_call() const { | 325 bool inner_uses_super_constructor_call() const { |
| 329 return inner_scope_uses_super_constructor_call_; | 326 return inner_scope_uses_super_constructor_call_; |
| 330 } | 327 } |
| 331 // Does this scope access "this". | |
| 332 bool uses_this() const { return scope_uses_this_; } | |
| 333 // Does any inner scope access "this". | |
| 334 bool inner_uses_this() const { return inner_scope_uses_this_; } | |
| 335 | 328 |
| 336 // --------------------------------------------------------------------------- | 329 // --------------------------------------------------------------------------- |
| 337 // Accessors. | 330 // Accessors. |
| 338 | 331 |
| 339 // The type of this scope. | 332 // The type of this scope. |
| 340 ScopeType scope_type() const { return scope_type_; } | 333 ScopeType scope_type() const { return scope_type_; } |
| 341 | 334 |
| 342 // The language mode of this scope. | 335 // The language mode of this scope. |
| 343 StrictMode strict_mode() const { return strict_mode_; } | 336 StrictMode strict_mode() const { return strict_mode_; } |
| 344 | 337 |
| 345 // The variable corresponding the 'this' value. | 338 // The variable corresponding the 'this' value. |
| 346 Variable* receiver() { return receiver_; } | 339 Variable* receiver() { |
| 340 DCHECK(has_this_declaration()); | |
| 341 DCHECK_NOT_NULL(receiver_); | |
| 342 return receiver_; | |
| 343 } | |
| 344 | |
| 345 bool has_this_declaration() const { | |
| 346 return !is_arrow_scope() && is_declaration_scope(); | |
| 347 } | |
| 347 | 348 |
| 348 // The variable holding the function literal for named function | 349 // The variable holding the function literal for named function |
| 349 // literals, or NULL. Only valid for function scopes. | 350 // literals, or NULL. Only valid for function scopes. |
| 350 VariableDeclaration* function() const { | 351 VariableDeclaration* function() const { |
| 351 DCHECK(is_function_scope()); | 352 DCHECK(is_function_scope()); |
| 352 return function_; | 353 return function_; |
| 353 } | 354 } |
| 354 | 355 |
| 355 // Parameters. The left-most parameter has index 0. | 356 // Parameters. The left-most parameter has index 0. |
| 356 // Only valid for function scopes. | 357 // Only valid for function scopes. |
| (...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 534 bool scope_contains_with_; | 535 bool scope_contains_with_; |
| 535 // This scope or a nested catch scope or with scope contain an 'eval' call. At | 536 // This scope or a nested catch scope or with scope contain an 'eval' call. At |
| 536 // the 'eval' call site this scope is the declaration scope. | 537 // the 'eval' call site this scope is the declaration scope. |
| 537 bool scope_calls_eval_; | 538 bool scope_calls_eval_; |
| 538 // This scope uses "arguments". | 539 // This scope uses "arguments". |
| 539 bool scope_uses_arguments_; | 540 bool scope_uses_arguments_; |
| 540 // This scope uses "super" property ('super.foo'). | 541 // This scope uses "super" property ('super.foo'). |
| 541 bool scope_uses_super_property_; | 542 bool scope_uses_super_property_; |
| 542 // This scope uses "super" constructor ('super(..)'). | 543 // This scope uses "super" constructor ('super(..)'). |
| 543 bool scope_uses_super_constructor_call_; | 544 bool scope_uses_super_constructor_call_; |
| 544 // This scope uses "this". | |
| 545 bool scope_uses_this_; | |
| 546 // This scope contains an "use asm" annotation. | 545 // This scope contains an "use asm" annotation. |
| 547 bool asm_module_; | 546 bool asm_module_; |
| 548 // This scope's outer context is an asm module. | 547 // This scope's outer context is an asm module. |
| 549 bool asm_function_; | 548 bool asm_function_; |
| 550 // The strict mode of this scope. | 549 // The strict mode of this scope. |
| 551 StrictMode strict_mode_; | 550 StrictMode strict_mode_; |
| 552 // Source positions. | 551 // Source positions. |
| 553 int start_position_; | 552 int start_position_; |
| 554 int end_position_; | 553 int end_position_; |
| 555 | 554 |
| 556 // Computed via PropagateScopeInfo. | 555 // Computed via PropagateScopeInfo. |
| 557 bool outer_scope_calls_sloppy_eval_; | 556 bool outer_scope_calls_sloppy_eval_; |
| 558 bool inner_scope_calls_eval_; | 557 bool inner_scope_calls_eval_; |
| 559 bool inner_scope_uses_arguments_; | 558 bool inner_scope_uses_arguments_; |
| 560 bool inner_scope_uses_super_property_; | 559 bool inner_scope_uses_super_property_; |
| 561 bool inner_scope_uses_super_constructor_call_; | 560 bool inner_scope_uses_super_constructor_call_; |
| 562 bool inner_scope_uses_this_; | |
| 563 bool force_eager_compilation_; | 561 bool force_eager_compilation_; |
| 564 bool force_context_allocation_; | 562 bool force_context_allocation_; |
| 565 | 563 |
| 566 // True if it doesn't need scope resolution (e.g., if the scope was | 564 // True if it doesn't need scope resolution (e.g., if the scope was |
| 567 // constructed based on a serialized scope info or a catch context). | 565 // constructed based on a serialized scope info or a catch context). |
| 568 bool already_resolved_; | 566 bool already_resolved_; |
| 569 | 567 |
| 570 // Computed as variables are declared. | 568 // Computed as variables are declared. |
| 571 int num_var_or_const_; | 569 int num_var_or_const_; |
| 572 | 570 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 656 | 654 |
| 657 // Predicates. | 655 // Predicates. |
| 658 bool MustAllocate(Variable* var); | 656 bool MustAllocate(Variable* var); |
| 659 bool MustAllocateInContext(Variable* var); | 657 bool MustAllocateInContext(Variable* var); |
| 660 bool HasArgumentsParameter(); | 658 bool HasArgumentsParameter(); |
| 661 | 659 |
| 662 // Variable allocation. | 660 // Variable allocation. |
| 663 void AllocateStackSlot(Variable* var); | 661 void AllocateStackSlot(Variable* var); |
| 664 void AllocateHeapSlot(Variable* var); | 662 void AllocateHeapSlot(Variable* var); |
| 665 void AllocateParameterLocals(); | 663 void AllocateParameterLocals(); |
| 664 void AllocateParameter(Variable* var, int index); | |
| 665 void AllocateReceiver(); | |
| 666 void AllocateNonParameterLocal(Variable* var); | 666 void AllocateNonParameterLocal(Variable* var); |
| 667 void AllocateNonParameterLocals(); | 667 void AllocateNonParameterLocals(); |
| 668 void AllocateVariablesRecursively(); | 668 void AllocateVariablesRecursively(); |
| 669 void AllocateModulesRecursively(Scope* host_scope); | 669 void AllocateModulesRecursively(Scope* host_scope); |
| 670 | 670 |
| 671 // Resolve and fill in the allocation information for all variables | 671 // Resolve and fill in the allocation information for all variables |
| 672 // in this scopes. Must be called *after* all scopes have been | 672 // in this scopes. Must be called *after* all scopes have been |
| 673 // processed (parsed) to ensure that unresolved variables can be | 673 // processed (parsed) to ensure that unresolved variables can be |
| 674 // resolved properly. | 674 // resolved properly. |
| 675 // | 675 // |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 700 Scope* outer_scope, | 700 Scope* outer_scope, |
| 701 Handle<ScopeInfo> scope_info); | 701 Handle<ScopeInfo> scope_info); |
| 702 | 702 |
| 703 AstValueFactory* ast_value_factory_; | 703 AstValueFactory* ast_value_factory_; |
| 704 Zone* zone_; | 704 Zone* zone_; |
| 705 }; | 705 }; |
| 706 | 706 |
| 707 } } // namespace v8::internal | 707 } } // namespace v8::internal |
| 708 | 708 |
| 709 #endif // V8_SCOPES_H_ | 709 #endif // V8_SCOPES_H_ |
| OLD | NEW |