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_AST_SCOPES_H_ | 5 #ifndef V8_AST_SCOPES_H_ |
6 #define V8_AST_SCOPES_H_ | 6 #define V8_AST_SCOPES_H_ |
7 | 7 |
8 #include "src/ast/ast.h" | 8 #include "src/ast/ast.h" |
9 #include "src/base/hashmap.h" | 9 #include "src/base/hashmap.h" |
10 #include "src/globals.h" | 10 #include "src/globals.h" |
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 // Create a new unresolved variable. | 197 // Create a new unresolved variable. |
198 VariableProxy* NewUnresolved(AstNodeFactory* factory, | 198 VariableProxy* NewUnresolved(AstNodeFactory* factory, |
199 const AstRawString* name, | 199 const AstRawString* name, |
200 Variable::Kind kind = Variable::NORMAL, | 200 Variable::Kind kind = Variable::NORMAL, |
201 int start_position = kNoSourcePosition, | 201 int start_position = kNoSourcePosition, |
202 int end_position = kNoSourcePosition) { | 202 int end_position = kNoSourcePosition) { |
203 // Note that we must not share the unresolved variables with | 203 // Note that we must not share the unresolved variables with |
204 // the same name because they may be removed selectively via | 204 // the same name because they may be removed selectively via |
205 // RemoveUnresolved(). | 205 // RemoveUnresolved(). |
206 DCHECK(!already_resolved()); | 206 DCHECK(!already_resolved()); |
| 207 DCHECK_EQ(factory->zone(), zone()); |
207 VariableProxy* proxy = | 208 VariableProxy* proxy = |
208 factory->NewVariableProxy(name, kind, start_position, end_position); | 209 factory->NewVariableProxy(name, kind, start_position, end_position); |
209 proxy->set_next_unresolved(unresolved_); | 210 proxy->set_next_unresolved(unresolved_); |
210 unresolved_ = proxy; | 211 unresolved_ = proxy; |
211 return proxy; | 212 return proxy; |
212 } | 213 } |
213 | 214 |
214 void AddUnresolved(VariableProxy* proxy) { | 215 void AddUnresolved(VariableProxy* proxy) { |
215 DCHECK(!already_resolved()); | 216 DCHECK(!already_resolved()); |
216 DCHECK(!proxy->is_resolved()); | 217 DCHECK(!proxy->is_resolved()); |
(...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
605 // the assumptions explained above do not hold. | 606 // the assumptions explained above do not hold. |
606 return params_.Contains(variables_.Lookup(name)); | 607 return params_.Contains(variables_.Lookup(name)); |
607 } | 608 } |
608 | 609 |
609 int num_var() const { return variables_.occupancy(); } | 610 int num_var() const { return variables_.occupancy(); } |
610 | 611 |
611 SloppyBlockFunctionMap* sloppy_block_function_map() { | 612 SloppyBlockFunctionMap* sloppy_block_function_map() { |
612 return &sloppy_block_function_map_; | 613 return &sloppy_block_function_map_; |
613 } | 614 } |
614 | 615 |
| 616 // To be called during parsing. Do just enough scope analysis that we can |
| 617 // discard the Scope for lazily compiled functions. In particular, this |
| 618 // records variables which cannot be resolved inside the Scope (we don't yet |
| 619 // know what they will resolve to since the outer Scopes are incomplete) and |
| 620 // migrates them into migrate_to. |
| 621 void AnalyzePartially(Scope* migrate_to, AstNodeFactory* ast_node_factory); |
| 622 |
615 // --------------------------------------------------------------------------- | 623 // --------------------------------------------------------------------------- |
616 // Debugging. | 624 // Debugging. |
617 | 625 |
618 #ifdef DEBUG | 626 #ifdef DEBUG |
619 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively | 627 void Print(int n = 0); // n = indentation; n < 0 => don't print recursively |
620 | 628 |
621 // Check that the scope has positions assigned. | 629 // Check that the scope has positions assigned. |
622 void CheckScopePositions(); | 630 void CheckScopePositions(); |
| 631 |
| 632 // Check that all Scopes in the scope tree use the same Zone. |
| 633 void CheckZones(); |
623 #endif | 634 #endif |
624 | 635 |
625 // --------------------------------------------------------------------------- | 636 // --------------------------------------------------------------------------- |
626 // Implementation. | 637 // Implementation. |
627 private: | 638 private: |
628 // Scope tree. | 639 // Scope tree. |
629 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL | 640 Scope* outer_scope_; // the immediately enclosing outer scope, or NULL |
630 Scope* inner_scope_; // an inner scope of this scope | 641 Scope* inner_scope_; // an inner scope of this scope |
631 Scope* sibling_; // a sibling inner scope of the outer scope of this scope. | 642 Scope* sibling_; // a sibling inner scope of the outer scope of this scope. |
632 | 643 |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
773 // * A 'with' statement has been encountered and there is no variable | 784 // * A 'with' statement has been encountered and there is no variable |
774 // binding for the name between the variable reference and the 'with'. | 785 // binding for the name between the variable reference and the 'with'. |
775 // The variable potentially references a property of the 'with' object. | 786 // The variable potentially references a property of the 'with' object. |
776 // * The code is being executed as part of a call to 'eval' and the calling | 787 // * The code is being executed as part of a call to 'eval' and the calling |
777 // context chain contains either a variable binding for the name or it | 788 // context chain contains either a variable binding for the name or it |
778 // contains a 'with' context. | 789 // contains a 'with' context. |
779 DYNAMIC_LOOKUP | 790 DYNAMIC_LOOKUP |
780 }; | 791 }; |
781 | 792 |
782 // Lookup a variable reference given by name recursively starting with this | 793 // Lookup a variable reference given by name recursively starting with this |
783 // scope. If the code is executed because of a call to 'eval', the context | 794 // scope, but only until max_outer_scope (if not nullptr). If the code is |
784 // parameter should be set to the calling context of 'eval'. | 795 // executed because of a call to 'eval', the context parameter should be set |
| 796 // to the calling context of 'eval'. |
785 Variable* LookupRecursive(VariableProxy* proxy, BindingKind* binding_kind, | 797 Variable* LookupRecursive(VariableProxy* proxy, BindingKind* binding_kind, |
786 AstNodeFactory* factory); | 798 AstNodeFactory* factory, |
| 799 Scope* max_outer_scope = nullptr); |
787 MUST_USE_RESULT | 800 MUST_USE_RESULT |
788 bool ResolveVariable(ParseInfo* info, VariableProxy* proxy, | 801 bool ResolveVariable(ParseInfo* info, VariableProxy* proxy, |
789 AstNodeFactory* factory); | 802 AstNodeFactory* factory); |
790 MUST_USE_RESULT | 803 MUST_USE_RESULT |
791 bool ResolveVariablesRecursively(ParseInfo* info, AstNodeFactory* factory); | 804 bool ResolveVariablesRecursively(ParseInfo* info, AstNodeFactory* factory); |
792 | 805 |
| 806 // Tries to resolve local variables inside max_outer_scope; migrates those |
| 807 // which cannot be resolved into migrate_to. |
| 808 void MigrateUnresolvableLocals(Scope* migrate_to, |
| 809 AstNodeFactory* ast_node_factory, |
| 810 Scope* max_outer_scope); |
| 811 |
793 // Scope analysis. | 812 // Scope analysis. |
794 void PropagateScopeInfo(bool outer_scope_calls_sloppy_eval); | 813 void PropagateScopeInfo(bool outer_scope_calls_sloppy_eval); |
795 bool HasTrivialContext() const; | 814 bool HasTrivialContext() const; |
796 | 815 |
797 // Predicates. | 816 // Predicates. |
798 bool MustAllocate(Variable* var); | 817 bool MustAllocate(Variable* var); |
799 bool MustAllocateInContext(Variable* var); | 818 bool MustAllocateInContext(Variable* var); |
800 | 819 |
801 // Variable allocation. | 820 // Variable allocation. |
802 void AllocateStackSlot(Variable* var); | 821 void AllocateStackSlot(Variable* var); |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
859 void DeserializeScopeInfo(Isolate* isolate, | 878 void DeserializeScopeInfo(Isolate* isolate, |
860 AstValueFactory* ast_value_factory); | 879 AstValueFactory* ast_value_factory); |
861 | 880 |
862 PendingCompilationErrorHandler pending_error_handler_; | 881 PendingCompilationErrorHandler pending_error_handler_; |
863 }; | 882 }; |
864 | 883 |
865 } // namespace internal | 884 } // namespace internal |
866 } // namespace v8 | 885 } // namespace v8 |
867 | 886 |
868 #endif // V8_AST_SCOPES_H_ | 887 #endif // V8_AST_SCOPES_H_ |
OLD | NEW |