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/bootstrapper.h" | 10 #include "src/bootstrapper.h" |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
137 | 137 |
138 ModuleScope::ModuleScope(DeclarationScope* script_scope, | 138 ModuleScope::ModuleScope(DeclarationScope* script_scope, |
139 AstValueFactory* ast_value_factory) | 139 AstValueFactory* ast_value_factory) |
140 : DeclarationScope(ast_value_factory->zone(), script_scope, MODULE_SCOPE) { | 140 : DeclarationScope(ast_value_factory->zone(), script_scope, MODULE_SCOPE) { |
141 Zone* zone = ast_value_factory->zone(); | 141 Zone* zone = ast_value_factory->zone(); |
142 module_descriptor_ = new (zone) ModuleDescriptor(zone); | 142 module_descriptor_ = new (zone) ModuleDescriptor(zone); |
143 set_language_mode(STRICT); | 143 set_language_mode(STRICT); |
144 DeclareThis(ast_value_factory); | 144 DeclareThis(ast_value_factory); |
145 } | 145 } |
146 | 146 |
147 ModuleScope::ModuleScope(Isolate* isolate, Handle<ScopeInfo> scope_info, | |
148 AstValueFactory* avfactory) | |
149 : DeclarationScope(avfactory->zone(), MODULE_SCOPE, scope_info) { | |
150 Zone* zone = avfactory->zone(); | |
151 ModuleInfo* module_info = scope_info->ModuleDescriptorInfo(); | |
152 | |
153 set_language_mode(STRICT); | |
154 module_descriptor_ = new (zone) ModuleDescriptor(zone); | |
155 | |
156 // Deserialize special exports. | |
157 Handle<FixedArray> special_exports = handle(module_info->special_exports()); | |
158 for (int i = 0, n = special_exports->length(); i < n; ++i) { | |
159 Handle<FixedArray> serialized_entry( | |
160 FixedArray::cast(special_exports->get(i)), isolate); | |
161 module_descriptor_->AddSpecialExport( | |
162 ModuleDescriptor::Entry::Deserialize(isolate, avfactory, | |
163 serialized_entry), | |
164 avfactory->zone()); | |
165 } | |
166 | |
167 // Deserialize regular exports. | |
168 Handle<FixedArray> regular_exports = handle(module_info->regular_exports()); | |
169 for (int i = 0, n = regular_exports->length(); i < n; ++i) { | |
170 Handle<FixedArray> serialized_entry( | |
171 FixedArray::cast(regular_exports->get(i)), isolate); | |
172 module_descriptor_->AddRegularExport(ModuleDescriptor::Entry::Deserialize( | |
173 isolate, avfactory, serialized_entry)); | |
174 } | |
175 } | |
176 | |
147 Scope::Scope(Zone* zone, ScopeType scope_type, Handle<ScopeInfo> scope_info) | 177 Scope::Scope(Zone* zone, ScopeType scope_type, Handle<ScopeInfo> scope_info) |
148 : zone_(zone), | 178 : zone_(zone), |
149 outer_scope_(nullptr), | 179 outer_scope_(nullptr), |
150 variables_(zone), | 180 variables_(zone), |
151 ordered_variables_(0, zone), | 181 ordered_variables_(0, zone), |
152 decls_(0, zone), | 182 decls_(0, zone), |
153 scope_info_(scope_info), | 183 scope_info_(scope_info), |
154 scope_type_(scope_type) { | 184 scope_type_(scope_type) { |
155 DCHECK(!scope_info.is_null()); | 185 DCHECK(!scope_info.is_null()); |
156 SetDefaults(); | 186 SetDefaults(); |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
293 outer_scope->AsDeclarationScope()->set_asm_module(); | 323 outer_scope->AsDeclarationScope()->set_asm_module(); |
294 } else if (context->IsBlockContext()) { | 324 } else if (context->IsBlockContext()) { |
295 Handle<ScopeInfo> scope_info(context->scope_info(), isolate); | 325 Handle<ScopeInfo> scope_info(context->scope_info(), isolate); |
296 DCHECK_EQ(scope_info->scope_type(), BLOCK_SCOPE); | 326 DCHECK_EQ(scope_info->scope_type(), BLOCK_SCOPE); |
297 if (scope_info->is_declaration_scope()) { | 327 if (scope_info->is_declaration_scope()) { |
298 outer_scope = | 328 outer_scope = |
299 new (zone) DeclarationScope(zone, BLOCK_SCOPE, scope_info); | 329 new (zone) DeclarationScope(zone, BLOCK_SCOPE, scope_info); |
300 } else { | 330 } else { |
301 outer_scope = new (zone) Scope(zone, BLOCK_SCOPE, scope_info); | 331 outer_scope = new (zone) Scope(zone, BLOCK_SCOPE, scope_info); |
302 } | 332 } |
333 } else if (context->IsModuleContext()) { | |
334 ScopeInfo* scope_info = context->closure()->shared()->scope_info(); | |
335 DCHECK_EQ(scope_info->scope_type(), MODULE_SCOPE); | |
336 outer_scope = new (zone) ModuleScope( | |
337 isolate, Handle<ScopeInfo>(scope_info), ast_value_factory); | |
303 } else { | 338 } else { |
304 DCHECK(context->IsCatchContext()); | 339 DCHECK(context->IsCatchContext()); |
305 String* name = context->catch_name(); | 340 String* name = context->catch_name(); |
306 outer_scope = new (zone) | 341 outer_scope = new (zone) |
307 Scope(zone, ast_value_factory->GetString(handle(name, isolate))); | 342 Scope(zone, ast_value_factory->GetString(handle(name, isolate))); |
308 } | 343 } |
309 if (current_scope != nullptr) { | 344 if (current_scope != nullptr) { |
310 outer_scope->AddInnerScope(current_scope); | 345 outer_scope->AddInnerScope(current_scope); |
311 } | 346 } |
312 current_scope = outer_scope; | 347 current_scope = outer_scope; |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
420 DeclarationScope* scope = info->literal()->scope(); | 455 DeclarationScope* scope = info->literal()->scope(); |
421 | 456 |
422 // We are compiling one of three cases: | 457 // We are compiling one of three cases: |
423 // 1) top-level code, | 458 // 1) top-level code, |
424 // 2) a function/eval/module on the top-level | 459 // 2) a function/eval/module on the top-level |
425 // 3) a function/eval in a scope that was already resolved. | 460 // 3) a function/eval in a scope that was already resolved. |
426 DCHECK(scope->scope_type() == SCRIPT_SCOPE || | 461 DCHECK(scope->scope_type() == SCRIPT_SCOPE || |
427 scope->outer_scope()->scope_type() == SCRIPT_SCOPE || | 462 scope->outer_scope()->scope_type() == SCRIPT_SCOPE || |
428 scope->outer_scope()->already_resolved_); | 463 scope->outer_scope()->already_resolved_); |
429 | 464 |
465 // For modules, we want to start variable allocation at the surrounding script | |
466 // scope. | |
467 if (scope->is_module_scope()) { | |
468 scope = scope->outer_scope()->AsDeclarationScope(); | |
469 } | |
470 | |
430 scope->AllocateVariables(info); | 471 scope->AllocateVariables(info); |
431 | 472 |
432 #ifdef DEBUG | 473 #ifdef DEBUG |
433 if (info->script_is_native() ? FLAG_print_builtin_scopes | 474 if (info->script_is_native() ? FLAG_print_builtin_scopes |
434 : FLAG_print_scopes) { | 475 : FLAG_print_scopes) { |
435 scope->Print(); | 476 scope->Print(); |
436 } | 477 } |
437 scope->CheckScopePositions(); | 478 scope->CheckScopePositions(); |
438 scope->CheckZones(); | 479 scope->CheckZones(); |
439 #endif | 480 #endif |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
620 mode = DYNAMIC; | 661 mode = DYNAMIC; |
621 init_flag = kCreatedInitialized; | 662 init_flag = kCreatedInitialized; |
622 // Be conservative and flag parameters as maybe assigned. Better | 663 // Be conservative and flag parameters as maybe assigned. Better |
623 // information would require ScopeInfo to serialize the maybe_assigned bit | 664 // information would require ScopeInfo to serialize the maybe_assigned bit |
624 // also for parameters. | 665 // also for parameters. |
625 maybe_assigned_flag = kMaybeAssigned; | 666 maybe_assigned_flag = kMaybeAssigned; |
626 } | 667 } |
627 } | 668 } |
628 if (index < 0 && scope_type() == MODULE_SCOPE) { | 669 if (index < 0 && scope_type() == MODULE_SCOPE) { |
629 location = VariableLocation::MODULE; | 670 location = VariableLocation::MODULE; |
630 index = -1; // TODO(neis): Find module variables in scope info. | 671 index = ScopeInfo::ModuleIndex(scope_info_, name_handle, &mode, &init_flag, |
672 &maybe_assigned_flag); | |
631 } | 673 } |
632 if (index < 0) return nullptr; // Nowhere found. | 674 if (index < 0) return nullptr; // Nowhere found. |
633 | 675 |
634 Variable::Kind kind = Variable::NORMAL; | 676 Variable::Kind kind = Variable::NORMAL; |
635 if (location == VariableLocation::CONTEXT && | 677 if (location == VariableLocation::CONTEXT && |
636 index == scope_info_->ReceiverContextSlotIndex()) { | 678 index == scope_info_->ReceiverContextSlotIndex()) { |
637 kind = Variable::THIS; | 679 kind = Variable::THIS; |
638 } | 680 } |
639 // TODO(marja, rossberg): Correctly declare FUNCTION, CLASS, NEW_TARGET, and | 681 // TODO(marja, rossberg): Correctly declare FUNCTION, CLASS, NEW_TARGET, and |
640 // ARGUMENTS bindings as their corresponding Variable::Kind. | 682 // ARGUMENTS bindings as their corresponding Variable::Kind. |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
800 } | 842 } |
801 } | 843 } |
802 DCHECK(false); | 844 DCHECK(false); |
803 } | 845 } |
804 } | 846 } |
805 return nullptr; | 847 return nullptr; |
806 } | 848 } |
807 | 849 |
808 void Scope::CollectVariables(ZoneList<Variable*>* stack_locals, | 850 void Scope::CollectVariables(ZoneList<Variable*>* stack_locals, |
809 ZoneList<Variable*>* context_locals, | 851 ZoneList<Variable*>* context_locals, |
810 ZoneList<Variable*>* context_globals) { | 852 ZoneList<Variable*>* context_globals, |
853 ZoneList<Variable*>* module_vars) { | |
811 DCHECK(stack_locals != NULL); | 854 DCHECK(stack_locals != NULL); |
812 DCHECK(context_locals != NULL); | 855 DCHECK(context_locals != NULL); |
813 DCHECK(context_globals != NULL); | 856 DCHECK(context_globals != NULL); |
857 DCHECK(module_vars != NULL || scope_type() != MODULE_SCOPE); | |
814 | 858 |
815 // Collect temporaries which are always allocated on the stack, unless the | 859 // Collect temporaries which are always allocated on the stack, unless the |
816 // context as a whole has forced context allocation. | 860 // context as a whole has forced context allocation. |
817 if (is_declaration_scope()) { | 861 if (is_declaration_scope()) { |
818 ZoneList<Variable*>* temps = AsDeclarationScope()->temps(); | 862 ZoneList<Variable*>* temps = AsDeclarationScope()->temps(); |
819 for (int i = 0; i < temps->length(); i++) { | 863 for (int i = 0; i < temps->length(); i++) { |
adamk
2016/08/26 16:54:44
I think this loop is gone if you merge to ToT.
| |
820 Variable* var = (*temps)[i]; | 864 Variable* var = (*temps)[i]; |
821 if (var->is_used()) { | 865 if (var->is_used()) { |
822 if (var->IsContextSlot()) { | 866 switch (var->location()) { |
823 DCHECK(has_forced_context_allocation()); | 867 case VariableLocation::LOCAL: |
824 context_locals->Add(var, zone()); | 868 stack_locals->Add(var, zone()); |
825 } else if (var->IsStackLocal()) { | 869 break; |
826 stack_locals->Add(var, zone()); | 870 case VariableLocation::CONTEXT: |
827 } else { | 871 DCHECK(has_forced_context_allocation()); |
828 DCHECK(var->IsParameter()); | 872 context_locals->Add(var, zone()); |
873 break; | |
874 default: | |
875 DCHECK(var->IsParameter()); | |
876 break; | |
829 } | 877 } |
830 } | 878 } |
831 } | 879 } |
832 } | 880 } |
833 | 881 |
834 for (int i = 0; i < ordered_variables_.length(); i++) { | 882 for (int i = 0; i < ordered_variables_.length(); i++) { |
835 Variable* var = ordered_variables_[i]; | 883 Variable* var = ordered_variables_[i]; |
836 if (var->IsStackLocal()) { | 884 switch (var->location()) { |
837 stack_locals->Add(var, zone()); | 885 case VariableLocation::LOCAL: |
838 } else if (var->IsContextSlot()) { | 886 stack_locals->Add(var, zone()); |
839 context_locals->Add(var, zone()); | 887 break; |
840 } else if (var->IsGlobalSlot()) { | 888 case VariableLocation::CONTEXT: |
841 context_globals->Add(var, zone()); | 889 context_locals->Add(var, zone()); |
890 break; | |
891 case VariableLocation::GLOBAL: | |
892 context_globals->Add(var, zone()); | |
893 break; | |
894 case VariableLocation::MODULE: | |
895 module_vars->Add(var, zone()); | |
896 break; | |
897 default: | |
898 break; | |
842 } | 899 } |
843 } | 900 } |
844 } | 901 } |
845 | 902 |
846 void DeclarationScope::AllocateVariables(ParseInfo* info) { | 903 void DeclarationScope::AllocateVariables(ParseInfo* info) { |
847 // 1) Propagate scope information. | 904 // 1) Propagate scope information. |
848 PropagateScopeInfo(); | 905 PropagateScopeInfo(); |
849 | 906 |
850 // 2) Resolve variables. | 907 // 2) Resolve variables. |
851 ResolveVariablesRecursively(info); | 908 ResolveVariablesRecursively(info); |
(...skipping 817 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1669 function != nullptr && function->IsContextSlot(); | 1726 function != nullptr && function->IsContextSlot(); |
1670 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - num_global_slots() - | 1727 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - num_global_slots() - |
1671 (is_function_var_in_context ? 1 : 0); | 1728 (is_function_var_in_context ? 1 : 0); |
1672 } | 1729 } |
1673 | 1730 |
1674 | 1731 |
1675 int Scope::ContextGlobalCount() const { return num_global_slots(); } | 1732 int Scope::ContextGlobalCount() const { return num_global_slots(); } |
1676 | 1733 |
1677 } // namespace internal | 1734 } // namespace internal |
1678 } // namespace v8 | 1735 } // namespace v8 |
OLD | NEW |