| 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 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 // corresponding variable (though some are bound during parse time). Variable | 65 // corresponding variable (though some are bound during parse time). Variable |
| 66 // allocation binds each unresolved VariableProxy to one Variable and assigns | 66 // allocation binds each unresolved VariableProxy to one Variable and assigns |
| 67 // a location. Note that many VariableProxy nodes may refer to the same Java- | 67 // a location. Note that many VariableProxy nodes may refer to the same Java- |
| 68 // Script variable. | 68 // Script variable. |
| 69 | 69 |
| 70 class Scope: public ZoneObject { | 70 class Scope: public ZoneObject { |
| 71 public: | 71 public: |
| 72 // --------------------------------------------------------------------------- | 72 // --------------------------------------------------------------------------- |
| 73 // Construction | 73 // Construction |
| 74 | 74 |
| 75 Scope(Isolate* isolate, Zone* zone, Scope* outer_scope, ScopeType scope_type, | 75 Scope(Zone* zone, Scope* outer_scope, ScopeType scope_type, |
| 76 AstValueFactory* value_factory); | 76 AstValueFactory* value_factory); |
| 77 | 77 |
| 78 // Compute top scope and allocate variables. For lazy compilation the top | 78 // Compute top scope and allocate variables. For lazy compilation the top |
| 79 // scope only contains the single lazily compiled function, so this | 79 // scope only contains the single lazily compiled function, so this |
| 80 // doesn't re-allocate variables repeatedly. | 80 // doesn't re-allocate variables repeatedly. |
| 81 static bool Analyze(CompilationInfo* info); | 81 static bool Analyze(CompilationInfo* info); |
| 82 | 82 |
| 83 static Scope* DeserializeScopeChain(Isolate* isolate, Zone* zone, | 83 static Scope* DeserializeScopeChain(Isolate* isolate, Zone* zone, |
| 84 Context* context, Scope* script_scope); | 84 Context* context, Scope* script_scope); |
| 85 | 85 |
| 86 // The scope name is only used for printing/debugging. | 86 // The scope name is only used for printing/debugging. |
| 87 void SetScopeName(const AstRawString* scope_name) { | 87 void SetScopeName(const AstRawString* scope_name) { |
| 88 scope_name_ = scope_name; | 88 scope_name_ = scope_name; |
| 89 } | 89 } |
| 90 | 90 |
| 91 void Initialize(bool uninitialized_this = false); | 91 void Initialize(bool uninitialized_this = false); |
| 92 | 92 |
| 93 // Checks if the block scope is redundant, i.e. it does not contain any | 93 // Checks if the block scope is redundant, i.e. it does not contain any |
| 94 // block scoped declarations. In that case it is removed from the scope | 94 // block scoped declarations. In that case it is removed from the scope |
| 95 // tree and its children are reparented. | 95 // tree and its children are reparented. |
| 96 Scope* FinalizeBlockScope(); | 96 Scope* FinalizeBlockScope(); |
| 97 | 97 |
| 98 Isolate* isolate() const { return isolate_; } | |
| 99 Zone* zone() const { return zone_; } | 98 Zone* zone() const { return zone_; } |
| 100 | 99 |
| 101 // --------------------------------------------------------------------------- | 100 // --------------------------------------------------------------------------- |
| 102 // Declarations | 101 // Declarations |
| 103 | 102 |
| 104 // Lookup a variable in this scope. Returns the variable or NULL if not found. | 103 // Lookup a variable in this scope. Returns the variable or NULL if not found. |
| 105 Variable* LookupLocal(const AstRawString* name); | 104 Variable* LookupLocal(const AstRawString* name); |
| 106 | 105 |
| 107 // This lookup corresponds to a lookup in the "intermediate" scope sitting | 106 // This lookup corresponds to a lookup in the "intermediate" scope sitting |
| 108 // between this scope and the outer scope. (ECMA-262, 3rd., requires that | 107 // between this scope and the outer scope. (ECMA-262, 3rd., requires that |
| (...skipping 338 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 | 446 |
| 448 // Find the script scope. | 447 // Find the script scope. |
| 449 // Used in modules implemenetation to find hosting scope. | 448 // Used in modules implemenetation to find hosting scope. |
| 450 // TODO(rossberg): is this needed? | 449 // TODO(rossberg): is this needed? |
| 451 Scope* ScriptScope(); | 450 Scope* ScriptScope(); |
| 452 | 451 |
| 453 // Find the first function, global, or eval scope. This is the scope | 452 // Find the first function, global, or eval scope. This is the scope |
| 454 // where var declarations will be hoisted to in the implementation. | 453 // where var declarations will be hoisted to in the implementation. |
| 455 Scope* DeclarationScope(); | 454 Scope* DeclarationScope(); |
| 456 | 455 |
| 457 Handle<ScopeInfo> GetScopeInfo(); | 456 Handle<ScopeInfo> GetScopeInfo(Isolate* isolate); |
| 458 | 457 |
| 459 // Get the chain of nested scopes within this scope for the source statement | 458 // Get the chain of nested scopes within this scope for the source statement |
| 460 // position. The scopes will be added to the list from the outermost scope to | 459 // position. The scopes will be added to the list from the outermost scope to |
| 461 // the innermost scope. Only nested block, catch or with scopes are tracked | 460 // the innermost scope. Only nested block, catch or with scopes are tracked |
| 462 // and will be returned, but no inner function scopes. | 461 // and will be returned, but no inner function scopes. |
| 463 void GetNestedScopeChain(List<Handle<ScopeInfo> >* chain, | 462 void GetNestedScopeChain(Isolate* isolate, List<Handle<ScopeInfo> >* chain, |
| 464 int statement_position); | 463 int statement_position); |
| 465 | 464 |
| 466 // --------------------------------------------------------------------------- | 465 // --------------------------------------------------------------------------- |
| 467 // Strict mode support. | 466 // Strict mode support. |
| 468 bool IsDeclared(const AstRawString* name) { | 467 bool IsDeclared(const AstRawString* name) { |
| 469 // During formal parameter list parsing the scope only contains | 468 // During formal parameter list parsing the scope only contains |
| 470 // two variables inserted at initialization: "this" and "arguments". | 469 // two variables inserted at initialization: "this" and "arguments". |
| 471 // "this" is an invalid parameter name and "arguments" is invalid parameter | 470 // "this" is an invalid parameter name and "arguments" is invalid parameter |
| 472 // name in strict mode. Therefore looking up with the map which includes | 471 // name in strict mode. Therefore looking up with the map which includes |
| 473 // "this" and "arguments" in addition to all formal parameters is safe. | 472 // "this" and "arguments" in addition to all formal parameters is safe. |
| 474 return variables_.Lookup(name) != NULL; | 473 return variables_.Lookup(name) != NULL; |
| 475 } | 474 } |
| 476 | 475 |
| 477 // --------------------------------------------------------------------------- | 476 // --------------------------------------------------------------------------- |
| 478 // Debugging. | 477 // Debugging. |
| 479 | 478 |
| 480 #ifdef DEBUG | 479 #ifdef DEBUG |
| 481 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively | 480 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively |
| 482 #endif | 481 #endif |
| 483 | 482 |
| 484 // --------------------------------------------------------------------------- | 483 // --------------------------------------------------------------------------- |
| 485 // Implementation. | 484 // Implementation. |
| 486 protected: | 485 protected: |
| 487 friend class ParserFactory; | 486 friend class ParserFactory; |
| 488 | 487 |
| 489 Isolate* const isolate_; | |
| 490 | |
| 491 // Scope tree. | 488 // Scope tree. |
| 492 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL | 489 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL |
| 493 ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes | 490 ZoneList<Scope*> inner_scopes_; // the immediately enclosed inner scopes |
| 494 | 491 |
| 495 // The scope type. | 492 // The scope type. |
| 496 ScopeType scope_type_; | 493 ScopeType scope_type_; |
| 497 | 494 |
| 498 // Debugging support. | 495 // Debugging support. |
| 499 const AstRawString* scope_name_; | 496 const AstRawString* scope_name_; |
| 500 | 497 |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 652 bool ResolveVariablesRecursively(CompilationInfo* info, | 649 bool ResolveVariablesRecursively(CompilationInfo* info, |
| 653 AstNodeFactory* factory); | 650 AstNodeFactory* factory); |
| 654 | 651 |
| 655 // Scope analysis. | 652 // Scope analysis. |
| 656 void PropagateScopeInfo(bool outer_scope_calls_sloppy_eval); | 653 void PropagateScopeInfo(bool outer_scope_calls_sloppy_eval); |
| 657 bool HasTrivialContext() const; | 654 bool HasTrivialContext() const; |
| 658 | 655 |
| 659 // Predicates. | 656 // Predicates. |
| 660 bool MustAllocate(Variable* var); | 657 bool MustAllocate(Variable* var); |
| 661 bool MustAllocateInContext(Variable* var); | 658 bool MustAllocateInContext(Variable* var); |
| 662 bool HasArgumentsParameter(); | 659 bool HasArgumentsParameter(Isolate* isolate); |
| 663 | 660 |
| 664 // Variable allocation. | 661 // Variable allocation. |
| 665 void AllocateStackSlot(Variable* var); | 662 void AllocateStackSlot(Variable* var); |
| 666 void AllocateHeapSlot(Variable* var); | 663 void AllocateHeapSlot(Variable* var); |
| 667 void AllocateParameterLocals(); | 664 void AllocateParameterLocals(Isolate* isolate); |
| 668 void AllocateNonParameterLocal(Variable* var); | 665 void AllocateNonParameterLocal(Isolate* isolate, Variable* var); |
| 669 void AllocateNonParameterLocals(); | 666 void AllocateNonParameterLocals(Isolate* isolate); |
| 670 void AllocateVariablesRecursively(); | 667 void AllocateVariablesRecursively(Isolate* isolate); |
| 671 void AllocateModulesRecursively(Scope* host_scope); | 668 void AllocateModulesRecursively(Scope* host_scope); |
| 672 | 669 |
| 673 // Resolve and fill in the allocation information for all variables | 670 // Resolve and fill in the allocation information for all variables |
| 674 // in this scopes. Must be called *after* all scopes have been | 671 // in this scopes. Must be called *after* all scopes have been |
| 675 // processed (parsed) to ensure that unresolved variables can be | 672 // processed (parsed) to ensure that unresolved variables can be |
| 676 // resolved properly. | 673 // resolved properly. |
| 677 // | 674 // |
| 678 // In the case of code compiled and run using 'eval', the context | 675 // In the case of code compiled and run using 'eval', the context |
| 679 // parameter is the context in which eval was called. In all other | 676 // parameter is the context in which eval was called. In all other |
| 680 // cases the context parameter is an empty handle. | 677 // cases the context parameter is an empty handle. |
| 681 MUST_USE_RESULT | 678 MUST_USE_RESULT |
| 682 bool AllocateVariables(CompilationInfo* info, AstNodeFactory* factory); | 679 bool AllocateVariables(CompilationInfo* info, AstNodeFactory* factory); |
| 683 | 680 |
| 684 private: | 681 private: |
| 685 // Construct a scope based on the scope info. | 682 // Construct a scope based on the scope info. |
| 686 Scope(Isolate* isolate, Zone* zone, Scope* inner_scope, ScopeType type, | 683 Scope(Zone* zone, Scope* inner_scope, ScopeType type, |
| 687 Handle<ScopeInfo> scope_info, AstValueFactory* value_factory); | 684 Handle<ScopeInfo> scope_info, AstValueFactory* value_factory); |
| 688 | 685 |
| 689 // Construct a catch scope with a binding for the name. | 686 // Construct a catch scope with a binding for the name. |
| 690 Scope(Isolate* isolate, Zone* zone, Scope* inner_scope, | 687 Scope(Zone* zone, Scope* inner_scope, const AstRawString* catch_variable_name, |
| 691 const AstRawString* catch_variable_name, | |
| 692 AstValueFactory* value_factory); | 688 AstValueFactory* value_factory); |
| 693 | 689 |
| 694 void AddInnerScope(Scope* inner_scope) { | 690 void AddInnerScope(Scope* inner_scope) { |
| 695 if (inner_scope != NULL) { | 691 if (inner_scope != NULL) { |
| 696 inner_scopes_.Add(inner_scope, zone_); | 692 inner_scopes_.Add(inner_scope, zone_); |
| 697 inner_scope->outer_scope_ = this; | 693 inner_scope->outer_scope_ = this; |
| 698 } | 694 } |
| 699 } | 695 } |
| 700 | 696 |
| 701 void SetDefaults(ScopeType type, | 697 void SetDefaults(ScopeType type, |
| 702 Scope* outer_scope, | 698 Scope* outer_scope, |
| 703 Handle<ScopeInfo> scope_info); | 699 Handle<ScopeInfo> scope_info); |
| 704 | 700 |
| 705 AstValueFactory* ast_value_factory_; | 701 AstValueFactory* ast_value_factory_; |
| 706 Zone* zone_; | 702 Zone* zone_; |
| 707 }; | 703 }; |
| 708 | 704 |
| 709 } } // namespace v8::internal | 705 } } // namespace v8::internal |
| 710 | 706 |
| 711 #endif // V8_SCOPES_H_ | 707 #endif // V8_SCOPES_H_ |
| OLD | NEW |