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 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 variables_(zone), | 108 variables_(zone), |
109 locals_(4, zone), | 109 locals_(4, zone), |
110 decls_(4, zone), | 110 decls_(4, zone), |
111 scope_type_(scope_type) { | 111 scope_type_(scope_type) { |
112 DCHECK_NE(SCRIPT_SCOPE, scope_type); | 112 DCHECK_NE(SCRIPT_SCOPE, scope_type); |
113 SetDefaults(); | 113 SetDefaults(); |
114 set_language_mode(outer_scope->language_mode()); | 114 set_language_mode(outer_scope->language_mode()); |
115 force_context_allocation_ = | 115 force_context_allocation_ = |
116 !is_function_scope() && outer_scope->has_forced_context_allocation(); | 116 !is_function_scope() && outer_scope->has_forced_context_allocation(); |
117 outer_scope_->AddInnerScope(this); | 117 outer_scope_->AddInnerScope(this); |
118 if (outer_scope_->is_lazily_parsed_) is_lazily_parsed_ = true; | |
119 } | 118 } |
120 | 119 |
121 Scope::Snapshot::Snapshot(Scope* scope) | 120 Scope::Snapshot::Snapshot(Scope* scope) |
122 : outer_scope_(scope), | 121 : outer_scope_(scope), |
123 top_inner_scope_(scope->inner_scope_), | 122 top_inner_scope_(scope->inner_scope_), |
124 top_unresolved_(scope->unresolved_), | 123 top_unresolved_(scope->unresolved_), |
125 top_local_(scope->GetClosureScope()->locals_.length()), | 124 top_local_(scope->GetClosureScope()->locals_.length()), |
126 top_decl_(scope->GetClosureScope()->decls_.length()) {} | 125 top_decl_(scope->GetClosureScope()->decls_.length()) {} |
127 | 126 |
128 DeclarationScope::DeclarationScope(Zone* zone, | 127 DeclarationScope::DeclarationScope(Zone* zone, |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 function_ = nullptr; | 270 function_ = nullptr; |
272 arguments_ = nullptr; | 271 arguments_ = nullptr; |
273 this_function_ = nullptr; | 272 this_function_ = nullptr; |
274 arity_ = 0; | 273 arity_ = 0; |
275 } | 274 } |
276 | 275 |
277 void Scope::SetDefaults() { | 276 void Scope::SetDefaults() { |
278 #ifdef DEBUG | 277 #ifdef DEBUG |
279 scope_name_ = nullptr; | 278 scope_name_ = nullptr; |
280 already_resolved_ = false; | 279 already_resolved_ = false; |
| 280 needs_migration_ = false; |
281 #endif | 281 #endif |
282 inner_scope_ = nullptr; | 282 inner_scope_ = nullptr; |
283 sibling_ = nullptr; | 283 sibling_ = nullptr; |
284 unresolved_ = nullptr; | 284 unresolved_ = nullptr; |
285 | 285 |
286 start_position_ = kNoSourcePosition; | 286 start_position_ = kNoSourcePosition; |
287 end_position_ = kNoSourcePosition; | 287 end_position_ = kNoSourcePosition; |
288 | 288 |
289 num_stack_slots_ = 0; | 289 num_stack_slots_ = 0; |
290 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; | 290 num_heap_slots_ = Context::MIN_CONTEXT_SLOTS; |
(...skipping 655 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
946 } | 946 } |
947 | 947 |
948 VariableProxy* Scope::NewUnresolved(AstNodeFactory* factory, | 948 VariableProxy* Scope::NewUnresolved(AstNodeFactory* factory, |
949 const AstRawString* name, | 949 const AstRawString* name, |
950 int start_position, int end_position, | 950 int start_position, int end_position, |
951 VariableKind kind) { | 951 VariableKind kind) { |
952 // Note that we must not share the unresolved variables with | 952 // Note that we must not share the unresolved variables with |
953 // the same name because they may be removed selectively via | 953 // the same name because they may be removed selectively via |
954 // RemoveUnresolved(). | 954 // RemoveUnresolved(). |
955 DCHECK(!already_resolved_); | 955 DCHECK(!already_resolved_); |
956 DCHECK_EQ(factory->zone(), zone()); | 956 DCHECK_EQ(!needs_migration_, factory->zone() == zone()); |
957 VariableProxy* proxy = | 957 VariableProxy* proxy = |
958 factory->NewVariableProxy(name, kind, start_position, end_position); | 958 factory->NewVariableProxy(name, kind, start_position, end_position); |
959 proxy->set_next_unresolved(unresolved_); | 959 proxy->set_next_unresolved(unresolved_); |
960 unresolved_ = proxy; | 960 unresolved_ = proxy; |
961 return proxy; | 961 return proxy; |
962 } | 962 } |
963 | 963 |
964 void Scope::AddUnresolved(VariableProxy* proxy) { | 964 void Scope::AddUnresolved(VariableProxy* proxy) { |
965 DCHECK(!already_resolved_); | 965 DCHECK(!already_resolved_); |
966 DCHECK(!proxy->is_resolved()); | 966 DCHECK(!proxy->is_resolved()); |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1205 Variable* var = params_[i]; | 1205 Variable* var = params_[i]; |
1206 if (var->mode() == TEMPORARY) { | 1206 if (var->mode() == TEMPORARY) { |
1207 locals_.Add(var, zone()); | 1207 locals_.Add(var, zone()); |
1208 } else if (variables_.Lookup(var->raw_name()) == nullptr) { | 1208 } else if (variables_.Lookup(var->raw_name()) == nullptr) { |
1209 variables_.Add(zone(), var); | 1209 variables_.Add(zone(), var); |
1210 locals_.Add(var, zone()); | 1210 locals_.Add(var, zone()); |
1211 } | 1211 } |
1212 } | 1212 } |
1213 } else { | 1213 } else { |
1214 params_.Clear(); | 1214 params_.Clear(); |
1215 // Make sure we won't try to allocate the rest parameter. {params_} was | |
1216 // cleared above. | |
1217 has_rest_ = false; | |
1218 } | 1215 } |
| 1216 |
| 1217 #ifdef DEBUG |
| 1218 needs_migration_ = false; |
| 1219 #endif |
| 1220 |
| 1221 is_lazily_parsed_ = !aborted; |
1219 } | 1222 } |
1220 | 1223 |
1221 void DeclarationScope::AnalyzePartially(DeclarationScope* migrate_to, | 1224 void DeclarationScope::AnalyzePartially(AstNodeFactory* ast_node_factory) { |
1222 AstNodeFactory* ast_node_factory) { | 1225 DCHECK(!force_eager_compilation_); |
1223 // Try to resolve unresolved variables for this Scope and migrate those which | 1226 VariableProxy* unresolved = nullptr; |
1224 // cannot be resolved inside. It doesn't make sense to try to resolve them in | 1227 |
1225 // the outer Scopes here, because they are incomplete. | 1228 if (!outer_scope_->is_script_scope()) { |
1226 for (VariableProxy* proxy = | 1229 // Try to resolve unresolved variables for this Scope and migrate those |
1227 FetchFreeVariables(this, !FLAG_lazy_inner_functions); | 1230 // which cannot be resolved inside. It doesn't make sense to try to resolve |
1228 proxy != nullptr; proxy = proxy->next_unresolved()) { | 1231 // them in the outer Scopes here, because they are incomplete. |
1229 DCHECK(!proxy->is_resolved()); | 1232 for (VariableProxy* proxy = |
1230 VariableProxy* copy = ast_node_factory->CopyVariableProxy(proxy); | 1233 FetchFreeVariables(this, !FLAG_lazy_inner_functions); |
1231 migrate_to->AddUnresolved(copy); | 1234 proxy != nullptr; proxy = proxy->next_unresolved()) { |
| 1235 DCHECK(!proxy->is_resolved()); |
| 1236 VariableProxy* copy = ast_node_factory->CopyVariableProxy(proxy); |
| 1237 copy->set_next_unresolved(unresolved); |
| 1238 unresolved = copy; |
| 1239 } |
| 1240 |
| 1241 // Clear arguments_ if unused. This is used as a signal for optimization. |
| 1242 if (arguments_ != nullptr && |
| 1243 !(MustAllocate(arguments_) && !has_arguments_parameter_)) { |
| 1244 arguments_ = nullptr; |
| 1245 } |
1232 } | 1246 } |
1233 | 1247 |
1234 // Push scope data up to migrate_to. Note that migrate_to and this Scope | 1248 ResetAfterPreparsing(false); |
1235 // describe the same Scope, just in different Zones. | 1249 |
1236 PropagateUsageFlagsToScope(migrate_to); | 1250 unresolved_ = unresolved; |
1237 if (scope_uses_super_property_) migrate_to->scope_uses_super_property_ = true; | |
1238 if (inner_scope_calls_eval_) migrate_to->inner_scope_calls_eval_ = true; | |
1239 if (is_lazily_parsed_) migrate_to->is_lazily_parsed_ = true; | |
1240 DCHECK(!force_eager_compilation_); | |
1241 migrate_to->set_start_position(start_position_); | |
1242 migrate_to->set_end_position(end_position_); | |
1243 migrate_to->set_language_mode(language_mode()); | |
1244 migrate_to->arity_ = arity_; | |
1245 migrate_to->force_context_allocation_ = force_context_allocation_; | |
1246 outer_scope_->RemoveInnerScope(this); | |
1247 DCHECK_EQ(outer_scope_, migrate_to->outer_scope_); | |
1248 DCHECK_EQ(outer_scope_->zone(), migrate_to->zone()); | |
1249 DCHECK_EQ(NeedsHomeObject(), migrate_to->NeedsHomeObject()); | |
1250 DCHECK_EQ(asm_function_, migrate_to->asm_function_); | |
1251 } | 1251 } |
1252 | 1252 |
1253 #ifdef DEBUG | 1253 #ifdef DEBUG |
1254 static const char* Header(ScopeType scope_type, FunctionKind function_kind, | 1254 static const char* Header(ScopeType scope_type, FunctionKind function_kind, |
1255 bool is_declaration_scope) { | 1255 bool is_declaration_scope) { |
1256 switch (scope_type) { | 1256 switch (scope_type) { |
1257 case EVAL_SCOPE: return "eval"; | 1257 case EVAL_SCOPE: return "eval"; |
1258 // TODO(adamk): Should we print concise method scopes specially? | 1258 // TODO(adamk): Should we print concise method scopes specially? |
1259 case FUNCTION_SCOPE: | 1259 case FUNCTION_SCOPE: |
1260 if (IsGeneratorFunction(function_kind)) return "function*"; | 1260 if (IsGeneratorFunction(function_kind)) return "function*"; |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1439 if (!is_hidden() && inner_scope_ == nullptr) { | 1439 if (!is_hidden() && inner_scope_ == nullptr) { |
1440 CHECK_NE(kNoSourcePosition, start_position()); | 1440 CHECK_NE(kNoSourcePosition, start_position()); |
1441 CHECK_NE(kNoSourcePosition, end_position()); | 1441 CHECK_NE(kNoSourcePosition, end_position()); |
1442 } | 1442 } |
1443 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { | 1443 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { |
1444 scope->CheckScopePositions(); | 1444 scope->CheckScopePositions(); |
1445 } | 1445 } |
1446 } | 1446 } |
1447 | 1447 |
1448 void Scope::CheckZones() { | 1448 void Scope::CheckZones() { |
| 1449 DCHECK(!needs_migration_); |
1449 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { | 1450 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { |
1450 CHECK_EQ(scope->zone(), zone()); | 1451 CHECK_EQ(scope->zone(), zone()); |
1451 } | 1452 } |
1452 } | 1453 } |
1453 #endif // DEBUG | 1454 #endif // DEBUG |
1454 | 1455 |
1455 Variable* Scope::NonLocal(const AstRawString* name, VariableMode mode) { | 1456 Variable* Scope::NonLocal(const AstRawString* name, VariableMode mode) { |
1456 // Declare a new non-local. | 1457 // Declare a new non-local. |
1457 DCHECK(IsDynamicVariableMode(mode)); | 1458 DCHECK(IsDynamicVariableMode(mode)); |
1458 Variable* var = variables_.Declare(zone(), NULL, name, mode, NORMAL_VARIABLE, | 1459 Variable* var = variables_.Declare(zone(), NULL, name, mode, NORMAL_VARIABLE, |
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1624 DCHECK(!proxy->is_resolved()); | 1625 DCHECK(!proxy->is_resolved()); |
1625 Variable* var = nullptr; | 1626 Variable* var = nullptr; |
1626 if (try_to_resolve) { | 1627 if (try_to_resolve) { |
1627 var = LookupRecursive(proxy, max_outer_scope->outer_scope()); | 1628 var = LookupRecursive(proxy, max_outer_scope->outer_scope()); |
1628 } | 1629 } |
1629 if (var == nullptr) { | 1630 if (var == nullptr) { |
1630 proxy->set_next_unresolved(stack); | 1631 proxy->set_next_unresolved(stack); |
1631 stack = proxy; | 1632 stack = proxy; |
1632 } else if (info != nullptr) { | 1633 } else if (info != nullptr) { |
1633 ResolveTo(info, proxy, var); | 1634 ResolveTo(info, proxy, var); |
| 1635 } else { |
| 1636 var->set_is_used(); |
1634 } | 1637 } |
1635 } | 1638 } |
1636 | 1639 |
1637 // Clear unresolved_ as it's in an inconsistent state. | 1640 // Clear unresolved_ as it's in an inconsistent state. |
1638 unresolved_ = nullptr; | 1641 unresolved_ = nullptr; |
1639 | 1642 |
1640 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { | 1643 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { |
1641 stack = | 1644 stack = |
1642 scope->FetchFreeVariables(max_outer_scope, try_to_resolve, info, stack); | 1645 scope->FetchFreeVariables(max_outer_scope, try_to_resolve, info, stack); |
1643 } | 1646 } |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1810 | 1813 |
1811 for (const auto& it : module()->regular_exports()) { | 1814 for (const auto& it : module()->regular_exports()) { |
1812 Variable* var = LookupLocal(it.first); | 1815 Variable* var = LookupLocal(it.first); |
1813 var->AllocateTo(VariableLocation::MODULE, 0); | 1816 var->AllocateTo(VariableLocation::MODULE, 0); |
1814 } | 1817 } |
1815 } | 1818 } |
1816 | 1819 |
1817 void Scope::AllocateVariablesRecursively() { | 1820 void Scope::AllocateVariablesRecursively() { |
1818 DCHECK(!already_resolved_); | 1821 DCHECK(!already_resolved_); |
1819 DCHECK_EQ(0, num_stack_slots_); | 1822 DCHECK_EQ(0, num_stack_slots_); |
| 1823 // Don't allocate variables of preparsed scopes. |
| 1824 if (is_lazily_parsed_) return; |
1820 | 1825 |
1821 // Allocate variables for inner scopes. | 1826 // Allocate variables for inner scopes. |
1822 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { | 1827 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { |
1823 scope->AllocateVariablesRecursively(); | 1828 scope->AllocateVariablesRecursively(); |
1824 } | 1829 } |
1825 | 1830 |
1826 DCHECK(!already_resolved_); | 1831 DCHECK(!already_resolved_); |
1827 DCHECK_EQ(Context::MIN_CONTEXT_SLOTS, num_heap_slots_); | 1832 DCHECK_EQ(Context::MIN_CONTEXT_SLOTS, num_heap_slots_); |
1828 | 1833 |
1829 // Allocate variables for this scope. | 1834 // Allocate variables for this scope. |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1888 Variable* function = | 1893 Variable* function = |
1889 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; | 1894 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; |
1890 bool is_function_var_in_context = | 1895 bool is_function_var_in_context = |
1891 function != nullptr && function->IsContextSlot(); | 1896 function != nullptr && function->IsContextSlot(); |
1892 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 1897 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
1893 (is_function_var_in_context ? 1 : 0); | 1898 (is_function_var_in_context ? 1 : 0); |
1894 } | 1899 } |
1895 | 1900 |
1896 } // namespace internal | 1901 } // namespace internal |
1897 } // namespace v8 | 1902 } // namespace v8 |
OLD | NEW |