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; |
118 } | 119 } |
119 | 120 |
120 Scope::Snapshot::Snapshot(Scope* scope) | 121 Scope::Snapshot::Snapshot(Scope* scope) |
121 : outer_scope_(scope), | 122 : outer_scope_(scope), |
122 top_inner_scope_(scope->inner_scope_), | 123 top_inner_scope_(scope->inner_scope_), |
123 top_unresolved_(scope->unresolved_), | 124 top_unresolved_(scope->unresolved_), |
124 top_local_(scope->GetClosureScope()->locals_.length()), | 125 top_local_(scope->GetClosureScope()->locals_.length()), |
125 top_decl_(scope->GetClosureScope()->decls_.length()) {} | 126 top_decl_(scope->GetClosureScope()->decls_.length()) {} |
126 | 127 |
127 DeclarationScope::DeclarationScope(Zone* zone, | 128 DeclarationScope::DeclarationScope(Zone* zone, |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
270 function_ = nullptr; | 271 function_ = nullptr; |
271 arguments_ = nullptr; | 272 arguments_ = nullptr; |
272 this_function_ = nullptr; | 273 this_function_ = nullptr; |
273 arity_ = 0; | 274 arity_ = 0; |
274 } | 275 } |
275 | 276 |
276 void Scope::SetDefaults() { | 277 void Scope::SetDefaults() { |
277 #ifdef DEBUG | 278 #ifdef DEBUG |
278 scope_name_ = nullptr; | 279 scope_name_ = nullptr; |
279 already_resolved_ = false; | 280 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(!needs_migration_, factory->zone() == zone()); | 956 DCHECK_EQ(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 } |
| 1219 } |
| 1220 |
| 1221 void DeclarationScope::AnalyzePartially(DeclarationScope* migrate_to, |
| 1222 AstNodeFactory* ast_node_factory) { |
| 1223 // Try to resolve unresolved variables for this Scope and migrate those which |
| 1224 // cannot be resolved inside. It doesn't make sense to try to resolve them in |
| 1225 // the outer Scopes here, because they are incomplete. |
| 1226 for (VariableProxy* proxy = |
| 1227 FetchFreeVariables(this, !FLAG_lazy_inner_functions); |
| 1228 proxy != nullptr; proxy = proxy->next_unresolved()) { |
| 1229 DCHECK(!proxy->is_resolved()); |
| 1230 VariableProxy* copy = ast_node_factory->CopyVariableProxy(proxy); |
| 1231 migrate_to->AddUnresolved(copy); |
1215 } | 1232 } |
1216 | 1233 |
1217 #ifdef DEBUG | 1234 // Push scope data up to migrate_to. Note that migrate_to and this Scope |
1218 needs_migration_ = false; | 1235 // describe the same Scope, just in different Zones. |
1219 #endif | 1236 PropagateUsageFlagsToScope(migrate_to); |
1220 | 1237 if (scope_uses_super_property_) migrate_to->scope_uses_super_property_ = true; |
1221 is_lazily_parsed_ = !aborted; | 1238 if (inner_scope_calls_eval_) migrate_to->inner_scope_calls_eval_ = true; |
1222 } | 1239 if (is_lazily_parsed_) migrate_to->is_lazily_parsed_ = true; |
1223 | |
1224 void DeclarationScope::AnalyzePartially(AstNodeFactory* ast_node_factory) { | |
1225 DCHECK(!force_eager_compilation_); | 1240 DCHECK(!force_eager_compilation_); |
1226 VariableProxy* unresolved = nullptr; | 1241 migrate_to->set_start_position(start_position_); |
1227 | 1242 migrate_to->set_end_position(end_position_); |
1228 if (!outer_scope_->is_script_scope()) { | 1243 migrate_to->set_language_mode(language_mode()); |
1229 // Try to resolve unresolved variables for this Scope and migrate those | 1244 migrate_to->arity_ = arity_; |
1230 // which cannot be resolved inside. It doesn't make sense to try to resolve | 1245 migrate_to->force_context_allocation_ = force_context_allocation_; |
1231 // them in the outer Scopes here, because they are incomplete. | 1246 outer_scope_->RemoveInnerScope(this); |
1232 for (VariableProxy* proxy = | 1247 DCHECK_EQ(outer_scope_, migrate_to->outer_scope_); |
1233 FetchFreeVariables(this, !FLAG_lazy_inner_functions); | 1248 DCHECK_EQ(outer_scope_->zone(), migrate_to->zone()); |
1234 proxy != nullptr; proxy = proxy->next_unresolved()) { | 1249 DCHECK_EQ(NeedsHomeObject(), migrate_to->NeedsHomeObject()); |
1235 DCHECK(!proxy->is_resolved()); | 1250 DCHECK_EQ(asm_function_, migrate_to->asm_function_); |
1236 VariableProxy* copy = ast_node_factory->CopyVariableProxy(proxy); | |
1237 copy->set_next_unresolved(unresolved); | |
1238 unresolved = copy; | |
1239 } | |
1240 } | |
1241 | |
1242 ResetAfterPreparsing(false); | |
1243 | |
1244 unresolved_ = unresolved; | |
1245 } | 1251 } |
1246 | 1252 |
1247 #ifdef DEBUG | 1253 #ifdef DEBUG |
1248 static const char* Header(ScopeType scope_type, FunctionKind function_kind, | 1254 static const char* Header(ScopeType scope_type, FunctionKind function_kind, |
1249 bool is_declaration_scope) { | 1255 bool is_declaration_scope) { |
1250 switch (scope_type) { | 1256 switch (scope_type) { |
1251 case EVAL_SCOPE: return "eval"; | 1257 case EVAL_SCOPE: return "eval"; |
1252 // TODO(adamk): Should we print concise method scopes specially? | 1258 // TODO(adamk): Should we print concise method scopes specially? |
1253 case FUNCTION_SCOPE: | 1259 case FUNCTION_SCOPE: |
1254 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... |
1433 if (!is_hidden() && inner_scope_ == nullptr) { | 1439 if (!is_hidden() && inner_scope_ == nullptr) { |
1434 CHECK_NE(kNoSourcePosition, start_position()); | 1440 CHECK_NE(kNoSourcePosition, start_position()); |
1435 CHECK_NE(kNoSourcePosition, end_position()); | 1441 CHECK_NE(kNoSourcePosition, end_position()); |
1436 } | 1442 } |
1437 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { | 1443 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { |
1438 scope->CheckScopePositions(); | 1444 scope->CheckScopePositions(); |
1439 } | 1445 } |
1440 } | 1446 } |
1441 | 1447 |
1442 void Scope::CheckZones() { | 1448 void Scope::CheckZones() { |
1443 DCHECK(!needs_migration_); | |
1444 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { | 1449 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { |
1445 CHECK_EQ(scope->zone(), zone()); | 1450 CHECK_EQ(scope->zone(), zone()); |
1446 } | 1451 } |
1447 } | 1452 } |
1448 #endif // DEBUG | 1453 #endif // DEBUG |
1449 | 1454 |
1450 Variable* Scope::NonLocal(const AstRawString* name, VariableMode mode) { | 1455 Variable* Scope::NonLocal(const AstRawString* name, VariableMode mode) { |
1451 // Declare a new non-local. | 1456 // Declare a new non-local. |
1452 DCHECK(IsDynamicVariableMode(mode)); | 1457 DCHECK(IsDynamicVariableMode(mode)); |
1453 Variable* var = variables_.Declare(zone(), NULL, name, mode, NORMAL_VARIABLE, | 1458 Variable* var = variables_.Declare(zone(), NULL, name, mode, NORMAL_VARIABLE, |
(...skipping 351 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1805 | 1810 |
1806 for (const auto& it : module()->regular_exports()) { | 1811 for (const auto& it : module()->regular_exports()) { |
1807 Variable* var = LookupLocal(it.first); | 1812 Variable* var = LookupLocal(it.first); |
1808 var->AllocateTo(VariableLocation::MODULE, 0); | 1813 var->AllocateTo(VariableLocation::MODULE, 0); |
1809 } | 1814 } |
1810 } | 1815 } |
1811 | 1816 |
1812 void Scope::AllocateVariablesRecursively() { | 1817 void Scope::AllocateVariablesRecursively() { |
1813 DCHECK(!already_resolved_); | 1818 DCHECK(!already_resolved_); |
1814 DCHECK_EQ(0, num_stack_slots_); | 1819 DCHECK_EQ(0, num_stack_slots_); |
1815 // Don't allocate variables of preparsed scopes. | |
1816 if (is_lazily_parsed_) return; | |
1817 | 1820 |
1818 // Allocate variables for inner scopes. | 1821 // Allocate variables for inner scopes. |
1819 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { | 1822 for (Scope* scope = inner_scope_; scope != nullptr; scope = scope->sibling_) { |
1820 scope->AllocateVariablesRecursively(); | 1823 scope->AllocateVariablesRecursively(); |
1821 } | 1824 } |
1822 | 1825 |
1823 DCHECK(!already_resolved_); | 1826 DCHECK(!already_resolved_); |
1824 DCHECK_EQ(Context::MIN_CONTEXT_SLOTS, num_heap_slots_); | 1827 DCHECK_EQ(Context::MIN_CONTEXT_SLOTS, num_heap_slots_); |
1825 | 1828 |
1826 // Allocate variables for this scope. | 1829 // Allocate variables for this scope. |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1885 Variable* function = | 1888 Variable* function = |
1886 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; | 1889 is_function_scope() ? AsDeclarationScope()->function_var() : nullptr; |
1887 bool is_function_var_in_context = | 1890 bool is_function_var_in_context = |
1888 function != nullptr && function->IsContextSlot(); | 1891 function != nullptr && function->IsContextSlot(); |
1889 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - | 1892 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - |
1890 (is_function_var_in_context ? 1 : 0); | 1893 (is_function_var_in_context ? 1 : 0); |
1891 } | 1894 } |
1892 | 1895 |
1893 } // namespace internal | 1896 } // namespace internal |
1894 } // namespace v8 | 1897 } // namespace v8 |
OLD | NEW |