Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(843)

Side by Side Diff: src/ast/scopes.cc

Issue 2277253003: [modules] Partial scope info support of modules (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@modules-refactor
Patch Set: Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/scopeinfo.h"
10 #include "src/bootstrapper.h" 11 #include "src/bootstrapper.h"
11 #include "src/messages.h" 12 #include "src/messages.h"
12 #include "src/parsing/parse-info.h" 13 #include "src/parsing/parse-info.h"
13 14
14 namespace v8 { 15 namespace v8 {
15 namespace internal { 16 namespace internal {
16 17
17 // ---------------------------------------------------------------------------- 18 // ----------------------------------------------------------------------------
18 // Implementation of LocalsMap 19 // Implementation of LocalsMap
19 // 20 //
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 138
138 ModuleScope::ModuleScope(DeclarationScope* script_scope, 139 ModuleScope::ModuleScope(DeclarationScope* script_scope,
139 AstValueFactory* ast_value_factory) 140 AstValueFactory* ast_value_factory)
140 : DeclarationScope(ast_value_factory->zone(), script_scope, MODULE_SCOPE) { 141 : DeclarationScope(ast_value_factory->zone(), script_scope, MODULE_SCOPE) {
141 Zone* zone = ast_value_factory->zone(); 142 Zone* zone = ast_value_factory->zone();
142 module_descriptor_ = new (zone) ModuleDescriptor(zone); 143 module_descriptor_ = new (zone) ModuleDescriptor(zone);
143 set_language_mode(STRICT); 144 set_language_mode(STRICT);
144 DeclareThis(ast_value_factory); 145 DeclareThis(ast_value_factory);
145 } 146 }
146 147
148 ModuleScope::ModuleScope(Isolate* isolate, Handle<ScopeInfo> scope_info,
149 AstValueFactory* avfactory)
150 : DeclarationScope(avfactory->zone(), MODULE_SCOPE, scope_info) {
151 Zone* zone = avfactory->zone();
152 ModuleInfo* module_info = scope_info->ModuleDescriptorInfo();
153
154 set_language_mode(STRICT);
155 module_descriptor_ = new (zone) ModuleDescriptor(zone);
156
157 // Deserialize regular exports.
158 Handle<FixedArray> regular_exports = handle(module_info->regular_exports());
159 for (int i = 0, n = regular_exports->length(); i < n; ++i) {
160 Handle<FixedArray> serialized_entry(regular_exports->get(i), isolate);
161 module_descriptor_->AddRegularExport(ModuleDescriptor::Entry::Deserialize(
162 isolate, avfactory, serialized_entry));
163 }
164 }
165
147 Scope::Scope(Zone* zone, ScopeType scope_type, Handle<ScopeInfo> scope_info) 166 Scope::Scope(Zone* zone, ScopeType scope_type, Handle<ScopeInfo> scope_info)
148 : zone_(zone), 167 : zone_(zone),
149 outer_scope_(nullptr), 168 outer_scope_(nullptr),
150 variables_(zone), 169 variables_(zone),
151 ordered_variables_(0, zone), 170 ordered_variables_(0, zone),
152 decls_(0, zone), 171 decls_(0, zone),
153 scope_info_(scope_info), 172 scope_info_(scope_info),
154 scope_type_(scope_type) { 173 scope_type_(scope_type) {
155 DCHECK(!scope_info.is_null()); 174 DCHECK(!scope_info.is_null());
156 SetDefaults(); 175 SetDefaults();
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 outer_scope->AsDeclarationScope()->set_asm_module(); 312 outer_scope->AsDeclarationScope()->set_asm_module();
294 } else if (context->IsBlockContext()) { 313 } else if (context->IsBlockContext()) {
295 Handle<ScopeInfo> scope_info(context->scope_info(), isolate); 314 Handle<ScopeInfo> scope_info(context->scope_info(), isolate);
296 DCHECK_EQ(scope_info->scope_type(), BLOCK_SCOPE); 315 DCHECK_EQ(scope_info->scope_type(), BLOCK_SCOPE);
297 if (scope_info->is_declaration_scope()) { 316 if (scope_info->is_declaration_scope()) {
298 outer_scope = 317 outer_scope =
299 new (zone) DeclarationScope(zone, BLOCK_SCOPE, scope_info); 318 new (zone) DeclarationScope(zone, BLOCK_SCOPE, scope_info);
300 } else { 319 } else {
301 outer_scope = new (zone) Scope(zone, BLOCK_SCOPE, scope_info); 320 outer_scope = new (zone) Scope(zone, BLOCK_SCOPE, scope_info);
302 } 321 }
322 } else if (context->IsModuleContext()) {
323 ScopeInfo* scope_info = context->closure()->shared()->scope_info();
324 DCHECK_EQ(scope_info->scope_type(), MODULE_SCOPE);
325 outer_scope = new (zone) ModuleScope(
326 isolate, Handle<ScopeInfo>(scope_info), ast_value_factory);
303 } else { 327 } else {
304 DCHECK(context->IsCatchContext()); 328 DCHECK(context->IsCatchContext());
305 String* name = context->catch_name(); 329 String* name = context->catch_name();
306 outer_scope = new (zone) 330 outer_scope = new (zone)
307 Scope(zone, ast_value_factory->GetString(handle(name, isolate))); 331 Scope(zone, ast_value_factory->GetString(handle(name, isolate)));
308 } 332 }
309 if (current_scope != nullptr) { 333 if (current_scope != nullptr) {
310 outer_scope->AddInnerScope(current_scope); 334 outer_scope->AddInnerScope(current_scope);
311 } 335 }
312 current_scope = outer_scope; 336 current_scope = outer_scope;
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 DeclarationScope* scope = info->literal()->scope(); 444 DeclarationScope* scope = info->literal()->scope();
421 445
422 // We are compiling one of three cases: 446 // We are compiling one of three cases:
423 // 1) top-level code, 447 // 1) top-level code,
424 // 2) a function/eval/module on the top-level 448 // 2) a function/eval/module on the top-level
425 // 3) a function/eval in a scope that was already resolved. 449 // 3) a function/eval in a scope that was already resolved.
426 DCHECK(scope->scope_type() == SCRIPT_SCOPE || 450 DCHECK(scope->scope_type() == SCRIPT_SCOPE ||
427 scope->outer_scope()->scope_type() == SCRIPT_SCOPE || 451 scope->outer_scope()->scope_type() == SCRIPT_SCOPE ||
428 scope->outer_scope()->already_resolved_); 452 scope->outer_scope()->already_resolved_);
429 453
454 // For modules, we want to start variable allocation on the surrounding script
455 // scope.
456 if (scope->is_module_scope())
adamk 2016/08/25 16:19:43 Please add braces if this doesn't fit on one line
neis 2016/08/26 09:52:07 Done.
457 scope = scope->outer_scope()->AsDeclarationScope();
458
430 scope->AllocateVariables(info); 459 scope->AllocateVariables(info);
431 460
432 #ifdef DEBUG 461 #ifdef DEBUG
433 if (info->script_is_native() ? FLAG_print_builtin_scopes 462 if (info->script_is_native() ? FLAG_print_builtin_scopes
434 : FLAG_print_scopes) { 463 : FLAG_print_scopes) {
435 scope->Print(); 464 scope->Print();
436 } 465 }
437 scope->CheckScopePositions(); 466 scope->CheckScopePositions();
438 scope->CheckZones(); 467 scope->CheckZones();
439 #endif 468 #endif
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
620 mode = DYNAMIC; 649 mode = DYNAMIC;
621 init_flag = kCreatedInitialized; 650 init_flag = kCreatedInitialized;
622 // Be conservative and flag parameters as maybe assigned. Better 651 // Be conservative and flag parameters as maybe assigned. Better
623 // information would require ScopeInfo to serialize the maybe_assigned bit 652 // information would require ScopeInfo to serialize the maybe_assigned bit
624 // also for parameters. 653 // also for parameters.
625 maybe_assigned_flag = kMaybeAssigned; 654 maybe_assigned_flag = kMaybeAssigned;
626 } 655 }
627 } 656 }
628 if (index < 0 && scope_type() == MODULE_SCOPE) { 657 if (index < 0 && scope_type() == MODULE_SCOPE) {
629 location = VariableLocation::MODULE; 658 location = VariableLocation::MODULE;
630 index = -1; // TODO(neis): Find module variables in scope info. 659 index = ScopeInfo::ModuleIndex(scope_info_, name_handle, &mode, &init_flag,
660 &maybe_assigned_flag);
631 } 661 }
632 if (index < 0) return nullptr; // Nowhere found. 662 if (index < 0) return nullptr; // Nowhere found.
633 663
634 Variable::Kind kind = Variable::NORMAL; 664 Variable::Kind kind = Variable::NORMAL;
635 if (location == VariableLocation::CONTEXT && 665 if (location == VariableLocation::CONTEXT &&
636 index == scope_info_->ReceiverContextSlotIndex()) { 666 index == scope_info_->ReceiverContextSlotIndex()) {
637 kind = Variable::THIS; 667 kind = Variable::THIS;
638 } 668 }
639 // TODO(marja, rossberg): Correctly declare FUNCTION, CLASS, NEW_TARGET, and 669 // TODO(marja, rossberg): Correctly declare FUNCTION, CLASS, NEW_TARGET, and
640 // ARGUMENTS bindings as their corresponding Variable::Kind. 670 // ARGUMENTS bindings as their corresponding Variable::Kind.
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
800 } 830 }
801 } 831 }
802 DCHECK(false); 832 DCHECK(false);
803 } 833 }
804 } 834 }
805 return nullptr; 835 return nullptr;
806 } 836 }
807 837
808 void Scope::CollectVariables(ZoneList<Variable*>* stack_locals, 838 void Scope::CollectVariables(ZoneList<Variable*>* stack_locals,
809 ZoneList<Variable*>* context_locals, 839 ZoneList<Variable*>* context_locals,
810 ZoneList<Variable*>* context_globals) { 840 ZoneList<Variable*>* context_globals,
841 ZoneList<Variable*>* module_vars) {
811 DCHECK(stack_locals != NULL); 842 DCHECK(stack_locals != NULL);
812 DCHECK(context_locals != NULL); 843 DCHECK(context_locals != NULL);
813 DCHECK(context_globals != NULL); 844 DCHECK(context_globals != NULL);
845 DCHECK(module_vars != NULL || scope_type() != MODULE_SCOPE);
814 846
815 // Collect temporaries which are always allocated on the stack, unless the 847 // Collect temporaries which are always allocated on the stack, unless the
816 // context as a whole has forced context allocation. 848 // context as a whole has forced context allocation.
817 if (is_declaration_scope()) { 849 if (is_declaration_scope()) {
818 ZoneList<Variable*>* temps = AsDeclarationScope()->temps(); 850 ZoneList<Variable*>* temps = AsDeclarationScope()->temps();
819 for (int i = 0; i < temps->length(); i++) { 851 for (int i = 0; i < temps->length(); i++) {
820 Variable* var = (*temps)[i]; 852 Variable* var = (*temps)[i];
821 if (var->is_used()) { 853 if (var->is_used()) {
822 if (var->IsContextSlot()) { 854 if (var->IsContextSlot()) {
823 DCHECK(has_forced_context_allocation()); 855 DCHECK(has_forced_context_allocation());
824 context_locals->Add(var, zone()); 856 context_locals->Add(var, zone());
825 } else if (var->IsStackLocal()) { 857 } else if (var->IsStackLocal()) {
826 stack_locals->Add(var, zone()); 858 stack_locals->Add(var, zone());
827 } else { 859 } else {
828 DCHECK(var->IsParameter()); 860 DCHECK(var->IsParameter());
829 } 861 }
830 } 862 }
831 } 863 }
832 } 864 }
833 865
834 for (int i = 0; i < ordered_variables_.length(); i++) { 866 for (int i = 0; i < ordered_variables_.length(); i++) {
835 Variable* var = ordered_variables_[i]; 867 Variable* var = ordered_variables_[i];
836 if (var->IsStackLocal()) { 868 if (var->IsStackLocal()) { // XXX use switch
adamk 2016/08/25 16:19:43 Remove this comment before landing (or switch to a
neis 2016/08/26 09:52:07 Turned it into a switch (above as well).
837 stack_locals->Add(var, zone()); 869 stack_locals->Add(var, zone());
838 } else if (var->IsContextSlot()) { 870 } else if (var->IsContextSlot()) {
839 context_locals->Add(var, zone()); 871 context_locals->Add(var, zone());
840 } else if (var->IsGlobalSlot()) { 872 } else if (var->IsGlobalSlot()) {
841 context_globals->Add(var, zone()); 873 context_globals->Add(var, zone());
874 } else if (var->location() == VariableLocation::MODULE) {
875 module_vars->Add(var, zone());
842 } 876 }
843 } 877 }
844 } 878 }
845 879
846 void DeclarationScope::AllocateVariables(ParseInfo* info) { 880 void DeclarationScope::AllocateVariables(ParseInfo* info) {
847 // 1) Propagate scope information. 881 // 1) Propagate scope information.
848 PropagateScopeInfo(); 882 PropagateScopeInfo();
849 883
850 // 2) Resolve variables. 884 // 2) Resolve variables.
851 ResolveVariablesRecursively(info); 885 ResolveVariablesRecursively(info);
(...skipping 817 matching lines...) Expand 10 before | Expand all | Expand 10 after
1669 function != nullptr && function->IsContextSlot(); 1703 function != nullptr && function->IsContextSlot();
1670 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - num_global_slots() - 1704 return num_heap_slots() - Context::MIN_CONTEXT_SLOTS - num_global_slots() -
1671 (is_function_var_in_context ? 1 : 0); 1705 (is_function_var_in_context ? 1 : 0);
1672 } 1706 }
1673 1707
1674 1708
1675 int Scope::ContextGlobalCount() const { return num_global_slots(); } 1709 int Scope::ContextGlobalCount() const { return num_global_slots(); }
1676 1710
1677 } // namespace internal 1711 } // namespace internal
1678 } // namespace v8 1712 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698