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 #include "src/ast/scopes.h" | 5 #include "src/ast/scopes.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "src/accessors.h" | 9 #include "src/accessors.h" |
10 #include "src/ast/ast.h" | 10 #include "src/ast/ast.h" |
(...skipping 618 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
629 new (zone()) Variable(this, name, CONST, kind, kCreatedInitialized); | 629 new (zone()) Variable(this, name, CONST, kind, kCreatedInitialized); |
630 if (calls_sloppy_eval()) { | 630 if (calls_sloppy_eval()) { |
631 NonLocal(name, DYNAMIC); | 631 NonLocal(name, DYNAMIC); |
632 } else { | 632 } else { |
633 variables_.Add(zone(), function_); | 633 variables_.Add(zone(), function_); |
634 } | 634 } |
635 return function_; | 635 return function_; |
636 } | 636 } |
637 | 637 |
638 bool Scope::HasBeenRemoved() const { | 638 bool Scope::HasBeenRemoved() const { |
639 // TODO(neis): Store this information somewhere instead of calculating it. | 639 if (sibling() == this) { |
640 | 640 DCHECK_NULL(inner_scope_); |
641 if (!is_block_scope()) return false; // Shortcut. | 641 DCHECK(is_block_scope()); |
642 | 642 return true; |
643 Scope* parent = outer_scope(); | |
644 if (parent == nullptr) { | |
645 DCHECK(is_script_scope()); | |
646 return false; | |
647 } | 643 } |
648 | 644 return false; |
649 Scope* sibling = parent->inner_scope(); | |
650 for (; sibling != nullptr; sibling = sibling->sibling()) { | |
651 if (sibling == this) return false; | |
652 } | |
653 | |
654 DCHECK_NULL(inner_scope_); | |
655 return true; | |
656 } | 645 } |
657 | 646 |
658 Scope* Scope::GetUnremovedScope() { | 647 Scope* Scope::GetUnremovedScope() { |
659 Scope* scope = this; | 648 Scope* scope = this; |
660 while (scope != nullptr && scope->HasBeenRemoved()) { | 649 while (scope != nullptr && scope->HasBeenRemoved()) { |
661 scope = scope->outer_scope(); | 650 scope = scope->outer_scope(); |
662 } | 651 } |
663 DCHECK_NOT_NULL(scope); | 652 DCHECK_NOT_NULL(scope); |
664 return scope; | 653 return scope; |
665 } | 654 } |
666 | 655 |
667 Scope* Scope::FinalizeBlockScope() { | 656 Scope* Scope::FinalizeBlockScope() { |
668 DCHECK(is_block_scope()); | 657 DCHECK(is_block_scope()); |
| 658 DCHECK(!HasBeenRemoved()); |
669 | 659 |
670 if (variables_.occupancy() > 0 || | 660 if (variables_.occupancy() > 0 || |
671 (is_declaration_scope() && calls_sloppy_eval())) { | 661 (is_declaration_scope() && calls_sloppy_eval())) { |
672 return this; | 662 return this; |
673 } | 663 } |
674 | 664 |
675 // Remove this scope from outer scope. | 665 // Remove this scope from outer scope. |
676 outer_scope()->RemoveInnerScope(this); | 666 outer_scope()->RemoveInnerScope(this); |
677 | 667 |
678 // Reparent inner scopes. | 668 // Reparent inner scopes. |
(...skipping 18 matching lines...) Expand all Loading... |
697 } | 687 } |
698 unresolved->set_next_unresolved(outer_scope()->unresolved_); | 688 unresolved->set_next_unresolved(outer_scope()->unresolved_); |
699 } | 689 } |
700 outer_scope()->unresolved_ = unresolved_; | 690 outer_scope()->unresolved_ = unresolved_; |
701 unresolved_ = nullptr; | 691 unresolved_ = nullptr; |
702 } | 692 } |
703 | 693 |
704 PropagateUsageFlagsToScope(outer_scope_); | 694 PropagateUsageFlagsToScope(outer_scope_); |
705 // This block does not need a context. | 695 // This block does not need a context. |
706 num_heap_slots_ = 0; | 696 num_heap_slots_ = 0; |
707 return NULL; | 697 |
| 698 // Mark scope as removed by making it its own sibling. |
| 699 sibling_ = this; |
| 700 DCHECK(HasBeenRemoved()); |
| 701 |
| 702 return nullptr; |
708 } | 703 } |
709 | 704 |
710 void DeclarationScope::AddLocal(Variable* var) { | 705 void DeclarationScope::AddLocal(Variable* var) { |
711 DCHECK(!already_resolved_); | 706 DCHECK(!already_resolved_); |
712 // Temporaries are only placed in ClosureScopes. | 707 // Temporaries are only placed in ClosureScopes. |
713 DCHECK_EQ(GetClosureScope(), this); | 708 DCHECK_EQ(GetClosureScope(), this); |
714 locals_.Add(var); | 709 locals_.Add(var); |
715 } | 710 } |
716 | 711 |
717 Variable* Scope::Declare(Zone* zone, const AstRawString* name, | 712 Variable* Scope::Declare(Zone* zone, const AstRawString* name, |
(...skipping 1353 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2071 Variable* function = | 2066 Variable* function = |
2072 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; | 2067 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; |
2073 bool is_function_var_in_context = | 2068 bool is_function_var_in_context = |
2074 function != nullptr && function->IsContextSlot(); | 2069 function != nullptr && function->IsContextSlot(); |
2075 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 2070 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
2076 (is_function_var_in_context ? 1 : 0); | 2071 (is_function_var_in_context ? 1 : 0); |
2077 } | 2072 } |
2078 | 2073 |
2079 } // namespace internal | 2074 } // namespace internal |
2080 } // namespace v8 | 2075 } // namespace v8 |
OLD | NEW |