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(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; | |
adamk
2016/09/27 20:33:17
Was this just unnecessary?
Toon Verwaest
2016/09/27 20:41:44
It was necessary to make allocation of these scope
| |
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 } | |
1232 } | 1240 } |
1233 | 1241 |
1234 // Push scope data up to migrate_to. Note that migrate_to and this Scope | 1242 ResetAfterPreparsing(false); |
1235 // describe the same Scope, just in different Zones. | 1243 |
1236 PropagateUsageFlagsToScope(migrate_to); | 1244 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 } | 1245 } |
1252 | 1246 |
1253 #ifdef DEBUG | 1247 #ifdef DEBUG |
1254 static const char* Header(ScopeType scope_type, FunctionKind function_kind, | 1248 static const char* Header(ScopeType scope_type, FunctionKind function_kind, |
1255 bool is_declaration_scope) { | 1249 bool is_declaration_scope) { |
1256 switch (scope_type) { | 1250 switch (scope_type) { |
1257 case EVAL_SCOPE: return "eval"; | 1251 case EVAL_SCOPE: return "eval"; |
1258 // TODO(adamk): Should we print concise method scopes specially? | 1252 // TODO(adamk): Should we print concise method scopes specially? |
1259 case FUNCTION_SCOPE: | 1253 case FUNCTION_SCOPE: |
1260 if (IsGeneratorFunction(function_kind)) return "function*"; | 1254 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) { | 1433 if (!is_hidden() && inner_scope_ == nullptr) { |
1440 CHECK_NE(kNoSourcePosition, start_position()); | 1434 CHECK_NE(kNoSourcePosition, start_position()); |
1441 CHECK_NE(kNoSourcePosition, end_position()); | 1435 CHECK_NE(kNoSourcePosition, end_position()); |
1442 } | 1436 } |
1443 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { | 1437 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { |
1444 scope->CheckScopePositions(); | 1438 scope->CheckScopePositions(); |
1445 } | 1439 } |
1446 } | 1440 } |
1447 | 1441 |
1448 void Scope::CheckZones() { | 1442 void Scope::CheckZones() { |
1443 DCHECK(!needs_migration_); | |
1449 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { | 1444 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { |
1450 CHECK_EQ(scope->zone(), zone()); | 1445 CHECK_EQ(scope->zone(), zone()); |
1451 } | 1446 } |
1452 } | 1447 } |
1453 #endif // DEBUG | 1448 #endif // DEBUG |
1454 | 1449 |
1455 Variable* Scope::NonLocal(const AstRawString* name, VariableMode mode) { | 1450 Variable* Scope::NonLocal(const AstRawString* name, VariableMode mode) { |
1456 // Declare a new non-local. | 1451 // Declare a new non-local. |
1457 DCHECK(IsDynamicVariableMode(mode)); | 1452 DCHECK(IsDynamicVariableMode(mode)); |
1458 Variable* var = variables_.Declare(zone(), NULL, name, mode, NORMAL_VARIABLE, | 1453 Variable* var = variables_.Declare(zone(), NULL, name, mode, NORMAL_VARIABLE, |
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1810 | 1805 |
1811 for (const auto& it : module()->regular_exports()) { | 1806 for (const auto& it : module()->regular_exports()) { |
1812 Variable* var = LookupLocal(it.first); | 1807 Variable* var = LookupLocal(it.first); |
1813 var->AllocateTo(VariableLocation::MODULE, 0); | 1808 var->AllocateTo(VariableLocation::MODULE, 0); |
1814 } | 1809 } |
1815 } | 1810 } |
1816 | 1811 |
1817 void Scope::AllocateVariablesRecursively() { | 1812 void Scope::AllocateVariablesRecursively() { |
1818 DCHECK(!already_resolved_); | 1813 DCHECK(!already_resolved_); |
1819 DCHECK_EQ(0, num_stack_slots_); | 1814 DCHECK_EQ(0, num_stack_slots_); |
1815 // Don't allocate variables of preparsed scopes. | |
1816 if (is_lazily_parsed_) return; | |
1820 | 1817 |
1821 // Allocate variables for inner scopes. | 1818 // Allocate variables for inner scopes. |
1822 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { | 1819 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { |
1823 scope->AllocateVariablesRecursively(); | 1820 scope->AllocateVariablesRecursively(); |
1824 } | 1821 } |
1825 | 1822 |
1826 DCHECK(!already_resolved_); | 1823 DCHECK(!already_resolved_); |
1827 DCHECK_EQ(Context::MIN_CONTEXT_SLOTS, num_heap_slots_); | 1824 DCHECK_EQ(Context::MIN_CONTEXT_SLOTS, num_heap_slots_); |
1828 | 1825 |
1829 // Allocate variables for this scope. | 1826 // Allocate variables for this scope. |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1888 Variable* function = | 1885 Variable* function = |
1889 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; | 1886 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; |
1890 bool is_function_var_in_context = | 1887 bool is_function_var_in_context = |
1891 function != nullptr && function->IsContextSlot(); | 1888 function != nullptr && function->IsContextSlot(); |
1892 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 1889 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
1893 (is_function_var_in_context ? 1 : 0); | 1890 (is_function_var_in_context ? 1 : 0); |
1894 } | 1891 } |
1895 | 1892 |
1896 } // namespace internal | 1893 } // namespace internal |
1897 } // namespace v8 | 1894 } // namespace v8 |
OLD | NEW |