| 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 18 matching lines...) Expand all Loading... |
| 29 | 29 |
| 30 Variable* VariableMap::Declare(Zone* zone, Scope* scope, | 30 Variable* VariableMap::Declare(Zone* zone, Scope* scope, |
| 31 const AstRawString* name, VariableMode mode, | 31 const AstRawString* name, VariableMode mode, |
| 32 VariableKind kind, | 32 VariableKind kind, |
| 33 InitializationFlag initialization_flag, | 33 InitializationFlag initialization_flag, |
| 34 MaybeAssignedFlag maybe_assigned_flag, | 34 MaybeAssignedFlag maybe_assigned_flag, |
| 35 bool* added) { | 35 bool* added) { |
| 36 // AstRawStrings are unambiguous, i.e., the same string is always represented | 36 // AstRawStrings are unambiguous, i.e., the same string is always represented |
| 37 // by the same AstRawString*. | 37 // by the same AstRawString*. |
| 38 // FIXME(marja): fix the type of Lookup. | 38 // FIXME(marja): fix the type of Lookup. |
| 39 Entry* p = ZoneHashMap::LookupOrInsert(const_cast<AstRawString*>(name), | 39 Entry* p = |
| 40 name->hash()); | 40 ZoneHashMap::LookupOrInsert(const_cast<AstRawString*>(name), name->hash(), |
| 41 ZoneAllocationPolicy(zone)); |
| 41 if (added) *added = p->value == nullptr; | 42 if (added) *added = p->value == nullptr; |
| 42 if (p->value == nullptr) { | 43 if (p->value == nullptr) { |
| 43 // The variable has not been declared yet -> insert it. | 44 // The variable has not been declared yet -> insert it. |
| 44 DCHECK_EQ(name, p->key); | 45 DCHECK_EQ(name, p->key); |
| 45 p->value = new (zone) Variable(scope, name, mode, kind, initialization_flag, | 46 p->value = new (zone) Variable(scope, name, mode, kind, initialization_flag, |
| 46 maybe_assigned_flag); | 47 maybe_assigned_flag); |
| 47 } | 48 } |
| 48 return reinterpret_cast<Variable*>(p->value); | 49 return reinterpret_cast<Variable*>(p->value); |
| 49 } | 50 } |
| 50 | 51 |
| 51 void VariableMap::Remove(Variable* var) { | 52 void VariableMap::Remove(Variable* var) { |
| 52 const AstRawString* name = var->raw_name(); | 53 const AstRawString* name = var->raw_name(); |
| 53 ZoneHashMap::Remove(const_cast<AstRawString*>(name), name->hash()); | 54 ZoneHashMap::Remove(const_cast<AstRawString*>(name), name->hash()); |
| 54 } | 55 } |
| 55 | 56 |
| 56 void VariableMap::Add(Variable* var) { | 57 void VariableMap::Add(Zone* zone, Variable* var) { |
| 57 const AstRawString* name = var->raw_name(); | 58 const AstRawString* name = var->raw_name(); |
| 58 Entry* p = ZoneHashMap::LookupOrInsert(const_cast<AstRawString*>(name), | 59 Entry* p = |
| 59 name->hash()); | 60 ZoneHashMap::LookupOrInsert(const_cast<AstRawString*>(name), name->hash(), |
| 61 ZoneAllocationPolicy(zone)); |
| 60 DCHECK_NULL(p->value); | 62 DCHECK_NULL(p->value); |
| 61 DCHECK_EQ(name, p->key); | 63 DCHECK_EQ(name, p->key); |
| 62 p->value = var; | 64 p->value = var; |
| 63 } | 65 } |
| 64 | 66 |
| 65 Variable* VariableMap::Lookup(const AstRawString* name) { | 67 Variable* VariableMap::Lookup(const AstRawString* name) { |
| 66 Entry* p = ZoneHashMap::Lookup(const_cast<AstRawString*>(name), name->hash()); | 68 Entry* p = ZoneHashMap::Lookup(const_cast<AstRawString*>(name), name->hash()); |
| 67 if (p != NULL) { | 69 if (p != NULL) { |
| 68 DCHECK(reinterpret_cast<const AstRawString*>(p->key) == name); | 70 DCHECK(reinterpret_cast<const AstRawString*>(p->key) == name); |
| 69 DCHECK(p->value != NULL); | 71 DCHECK(p->value != NULL); |
| 70 return reinterpret_cast<Variable*>(p->value); | 72 return reinterpret_cast<Variable*>(p->value); |
| 71 } | 73 } |
| 72 return NULL; | 74 return NULL; |
| 73 } | 75 } |
| 74 | 76 |
| 75 SloppyBlockFunctionMap::SloppyBlockFunctionMap(Zone* zone) | 77 SloppyBlockFunctionMap::SloppyBlockFunctionMap(Zone* zone) |
| 76 : ZoneHashMap(ZoneHashMap::PointersMatch, 8, ZoneAllocationPolicy(zone)) {} | 78 : ZoneHashMap(ZoneHashMap::PointersMatch, 8, ZoneAllocationPolicy(zone)) {} |
| 77 | 79 |
| 78 void SloppyBlockFunctionMap::Declare(const AstRawString* name, | 80 void SloppyBlockFunctionMap::Declare(Zone* zone, const AstRawString* name, |
| 79 SloppyBlockFunctionStatement* stmt) { | 81 SloppyBlockFunctionStatement* stmt) { |
| 80 // AstRawStrings are unambiguous, i.e., the same string is always represented | 82 // AstRawStrings are unambiguous, i.e., the same string is always represented |
| 81 // by the same AstRawString*. | 83 // by the same AstRawString*. |
| 82 Entry* p = ZoneHashMap::LookupOrInsert(const_cast<AstRawString*>(name), | 84 Entry* p = |
| 83 name->hash()); | 85 ZoneHashMap::LookupOrInsert(const_cast<AstRawString*>(name), name->hash(), |
| 86 ZoneAllocationPolicy(zone)); |
| 84 stmt->set_next(static_cast<SloppyBlockFunctionStatement*>(p->value)); | 87 stmt->set_next(static_cast<SloppyBlockFunctionStatement*>(p->value)); |
| 85 p->value = stmt; | 88 p->value = stmt; |
| 86 } | 89 } |
| 87 | 90 |
| 88 | 91 |
| 89 // ---------------------------------------------------------------------------- | 92 // ---------------------------------------------------------------------------- |
| 90 // Implementation of Scope | 93 // Implementation of Scope |
| 91 | 94 |
| 92 Scope::Scope(Zone* zone) | 95 Scope::Scope(Zone* zone) |
| 93 : zone_(zone), | 96 : zone_(zone), |
| (...skipping 531 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 625 DCHECK(is_function_scope()); | 628 DCHECK(is_function_scope()); |
| 626 DCHECK_NULL(function_); | 629 DCHECK_NULL(function_); |
| 627 DCHECK_NULL(variables_.Lookup(name)); | 630 DCHECK_NULL(variables_.Lookup(name)); |
| 628 VariableKind kind = is_sloppy(language_mode()) ? SLOPPY_FUNCTION_NAME_VARIABLE | 631 VariableKind kind = is_sloppy(language_mode()) ? SLOPPY_FUNCTION_NAME_VARIABLE |
| 629 : NORMAL_VARIABLE; | 632 : NORMAL_VARIABLE; |
| 630 function_ = | 633 function_ = |
| 631 new (zone()) Variable(this, name, CONST, kind, kCreatedInitialized); | 634 new (zone()) Variable(this, name, CONST, kind, kCreatedInitialized); |
| 632 if (calls_sloppy_eval()) { | 635 if (calls_sloppy_eval()) { |
| 633 NonLocal(name, DYNAMIC); | 636 NonLocal(name, DYNAMIC); |
| 634 } else { | 637 } else { |
| 635 variables_.Add(function_); | 638 variables_.Add(zone(), function_); |
| 636 } | 639 } |
| 637 return function_; | 640 return function_; |
| 638 } | 641 } |
| 639 | 642 |
| 640 Scope* Scope::FinalizeBlockScope() { | 643 Scope* Scope::FinalizeBlockScope() { |
| 641 DCHECK(is_block_scope()); | 644 DCHECK(is_block_scope()); |
| 642 | 645 |
| 643 if (variables_.occupancy() > 0 || | 646 if (variables_.occupancy() > 0 || |
| 644 (is_declaration_scope() && calls_sloppy_eval())) { | 647 (is_declaration_scope() && calls_sloppy_eval())) { |
| 645 return this; | 648 return this; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 720 DeclarationScope* outer_closure = outer_scope_->GetClosureScope(); | 723 DeclarationScope* outer_closure = outer_scope_->GetClosureScope(); |
| 721 for (int i = top_local_; i < outer_closure->locals_.length(); i++) { | 724 for (int i = top_local_; i < outer_closure->locals_.length(); i++) { |
| 722 Variable* local = outer_closure->locals_.at(i); | 725 Variable* local = outer_closure->locals_.at(i); |
| 723 DCHECK(local->mode() == TEMPORARY || local->mode() == VAR); | 726 DCHECK(local->mode() == TEMPORARY || local->mode() == VAR); |
| 724 DCHECK_EQ(local->scope(), local->scope()->GetClosureScope()); | 727 DCHECK_EQ(local->scope(), local->scope()->GetClosureScope()); |
| 725 DCHECK_NE(local->scope(), new_parent); | 728 DCHECK_NE(local->scope(), new_parent); |
| 726 local->set_scope(new_parent); | 729 local->set_scope(new_parent); |
| 727 new_parent->AddLocal(local); | 730 new_parent->AddLocal(local); |
| 728 if (local->mode() == VAR) { | 731 if (local->mode() == VAR) { |
| 729 outer_closure->variables_.Remove(local); | 732 outer_closure->variables_.Remove(local); |
| 730 new_parent->variables_.Add(local); | 733 new_parent->variables_.Add(new_parent->zone(), local); |
| 731 } | 734 } |
| 732 } | 735 } |
| 733 outer_closure->locals_.Rewind(top_local_); | 736 outer_closure->locals_.Rewind(top_local_); |
| 734 outer_closure->decls_.Rewind(top_decl_); | 737 outer_closure->decls_.Rewind(top_decl_); |
| 735 } | 738 } |
| 736 | 739 |
| 737 void Scope::ReplaceOuterScope(Scope* outer) { | 740 void Scope::ReplaceOuterScope(Scope* outer) { |
| 738 DCHECK_NOT_NULL(outer); | 741 DCHECK_NOT_NULL(outer); |
| 739 DCHECK_NOT_NULL(outer_scope_); | 742 DCHECK_NOT_NULL(outer_scope_); |
| 740 DCHECK(!already_resolved_); | 743 DCHECK(!already_resolved_); |
| (...skipping 1092 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1833 Variable* function = | 1836 Variable* function = |
| 1834 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; | 1837 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; |
| 1835 bool is_function_var_in_context = | 1838 bool is_function_var_in_context = |
| 1836 function != nullptr && function->IsContextSlot(); | 1839 function != nullptr && function->IsContextSlot(); |
| 1837 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 1840 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
| 1838 (is_function_var_in_context ? 1 : 0); | 1841 (is_function_var_in_context ? 1 : 0); |
| 1839 } | 1842 } |
| 1840 | 1843 |
| 1841 } // namespace internal | 1844 } // namespace internal |
| 1842 } // namespace v8 | 1845 } // namespace v8 |
| OLD | NEW |