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/modules.h" | 5 #include "src/ast/modules.h" |
6 #include "src/ast/ast-value-factory.h" | 6 #include "src/ast/ast-value-factory.h" |
7 #include "src/ast/scopes.h" | 7 #include "src/ast/scopes.h" |
8 | 8 |
9 namespace v8 { | 9 namespace v8 { |
10 namespace internal { | 10 namespace internal { |
11 | 11 |
12 void ModuleDescriptor::AddImport( | 12 void ModuleDescriptor::AddImport( |
13 const AstRawString* import_name, const AstRawString* local_name, | 13 const AstRawString* import_name, const AstRawString* local_name, |
14 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { | 14 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { |
15 DCHECK_NOT_NULL(import_name); | 15 DCHECK_NOT_NULL(import_name); |
16 DCHECK_NOT_NULL(local_name); | 16 DCHECK_NOT_NULL(local_name); |
17 DCHECK_NOT_NULL(module_request); | 17 DCHECK_NOT_NULL(module_request); |
18 ModuleEntry* entry = new (zone) ModuleEntry(loc); | 18 Entry* entry = new (zone) Entry(loc); |
19 entry->local_name = local_name; | 19 entry->local_name = local_name; |
20 entry->import_name = import_name; | 20 entry->import_name = import_name; |
21 entry->module_request = module_request; | 21 entry->module_request = module_request; |
22 regular_imports_.insert(std::make_pair(entry->local_name, entry)); | 22 regular_imports_.insert(std::make_pair(entry->local_name, entry)); |
23 // We don't care if there's already an entry for this local name, as in that | 23 // We don't care if there's already an entry for this local name, as in that |
24 // case we will report an error when declaring the variable. | 24 // case we will report an error when declaring the variable. |
25 } | 25 } |
26 | 26 |
27 | 27 |
28 void ModuleDescriptor::AddStarImport( | 28 void ModuleDescriptor::AddStarImport( |
29 const AstRawString* local_name, const AstRawString* module_request, | 29 const AstRawString* local_name, const AstRawString* module_request, |
30 Scanner::Location loc, Zone* zone) { | 30 Scanner::Location loc, Zone* zone) { |
31 DCHECK_NOT_NULL(local_name); | 31 DCHECK_NOT_NULL(local_name); |
32 DCHECK_NOT_NULL(module_request); | 32 DCHECK_NOT_NULL(module_request); |
33 ModuleEntry* entry = new (zone) ModuleEntry(loc); | 33 Entry* entry = new (zone) Entry(loc); |
34 entry->local_name = local_name; | 34 entry->local_name = local_name; |
35 entry->module_request = module_request; | 35 entry->module_request = module_request; |
36 special_imports_.Add(entry, zone); | 36 special_imports_.Add(entry, zone); |
37 } | 37 } |
38 | 38 |
39 | 39 |
40 void ModuleDescriptor::AddEmptyImport( | 40 void ModuleDescriptor::AddEmptyImport( |
41 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { | 41 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { |
42 DCHECK_NOT_NULL(module_request); | 42 DCHECK_NOT_NULL(module_request); |
43 ModuleEntry* entry = new (zone) ModuleEntry(loc); | 43 Entry* entry = new (zone) Entry(loc); |
44 entry->module_request = module_request; | 44 entry->module_request = module_request; |
45 special_imports_.Add(entry, zone); | 45 special_imports_.Add(entry, zone); |
46 } | 46 } |
47 | 47 |
48 | 48 |
49 void ModuleDescriptor::AddExport( | 49 void ModuleDescriptor::AddExport( |
50 const AstRawString* local_name, const AstRawString* export_name, | 50 const AstRawString* local_name, const AstRawString* export_name, |
51 Scanner::Location loc, Zone* zone) { | 51 Scanner::Location loc, Zone* zone) { |
52 DCHECK_NOT_NULL(local_name); | 52 DCHECK_NOT_NULL(local_name); |
53 DCHECK_NOT_NULL(export_name); | 53 DCHECK_NOT_NULL(export_name); |
54 ModuleEntry* entry = new (zone) ModuleEntry(loc); | 54 Entry* entry = new (zone) Entry(loc); |
55 entry->export_name = export_name; | 55 entry->export_name = export_name; |
56 entry->local_name = local_name; | 56 entry->local_name = local_name; |
57 regular_exports_.insert(std::make_pair(entry->local_name, entry)); | 57 regular_exports_.insert(std::make_pair(entry->local_name, entry)); |
58 } | 58 } |
59 | 59 |
60 | 60 |
61 void ModuleDescriptor::AddExport( | 61 void ModuleDescriptor::AddExport( |
62 const AstRawString* import_name, const AstRawString* export_name, | 62 const AstRawString* import_name, const AstRawString* export_name, |
63 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { | 63 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { |
64 DCHECK_NOT_NULL(import_name); | 64 DCHECK_NOT_NULL(import_name); |
65 DCHECK_NOT_NULL(export_name); | 65 DCHECK_NOT_NULL(export_name); |
66 DCHECK_NOT_NULL(module_request); | 66 DCHECK_NOT_NULL(module_request); |
67 ModuleEntry* entry = new (zone) ModuleEntry(loc); | 67 Entry* entry = new (zone) Entry(loc); |
68 entry->export_name = export_name; | 68 entry->export_name = export_name; |
69 entry->import_name = import_name; | 69 entry->import_name = import_name; |
70 entry->module_request = module_request; | 70 entry->module_request = module_request; |
71 special_exports_.Add(entry, zone); | 71 special_exports_.Add(entry, zone); |
72 } | 72 } |
73 | 73 |
74 | 74 |
75 void ModuleDescriptor::AddStarExport( | 75 void ModuleDescriptor::AddStarExport( |
76 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { | 76 const AstRawString* module_request, Scanner::Location loc, Zone* zone) { |
77 DCHECK_NOT_NULL(module_request); | 77 DCHECK_NOT_NULL(module_request); |
78 ModuleEntry* entry = new (zone) ModuleEntry(loc); | 78 Entry* entry = new (zone) Entry(loc); |
79 entry->module_request = module_request; | 79 entry->module_request = module_request; |
80 special_exports_.Add(entry, zone); | 80 special_exports_.Add(entry, zone); |
81 } | 81 } |
82 | 82 |
83 void ModuleDescriptor::MakeIndirectExportsExplicit(Zone* zone) { | 83 void ModuleDescriptor::MakeIndirectExportsExplicit(Zone* zone) { |
84 for (auto it = regular_exports_.begin(); it != regular_exports_.end();) { | 84 for (auto it = regular_exports_.begin(); it != regular_exports_.end();) { |
85 ModuleEntry* entry = it->second; | 85 Entry* entry = it->second; |
86 DCHECK_NOT_NULL(entry->local_name); | 86 DCHECK_NOT_NULL(entry->local_name); |
87 auto import = regular_imports_.find(entry->local_name); | 87 auto import = regular_imports_.find(entry->local_name); |
88 if (import != regular_imports_.end()) { | 88 if (import != regular_imports_.end()) { |
89 // Found an indirect export. Patch export entry and move it from regular | 89 // Found an indirect export. Patch export entry and move it from regular |
90 // to special. | 90 // to special. |
91 DCHECK_NULL(entry->import_name); | 91 DCHECK_NULL(entry->import_name); |
92 DCHECK_NULL(entry->module_request); | 92 DCHECK_NULL(entry->module_request); |
93 DCHECK_NOT_NULL(import->second->import_name); | 93 DCHECK_NOT_NULL(import->second->import_name); |
94 DCHECK_NOT_NULL(import->second->module_request); | 94 DCHECK_NOT_NULL(import->second->module_request); |
95 entry->import_name = import->second->import_name; | 95 entry->import_name = import->second->import_name; |
96 entry->module_request = import->second->module_request; | 96 entry->module_request = import->second->module_request; |
97 entry->local_name = nullptr; | 97 entry->local_name = nullptr; |
98 special_exports_.Add(entry, zone); | 98 special_exports_.Add(entry, zone); |
99 it = regular_exports_.erase(it); | 99 it = regular_exports_.erase(it); |
100 } else { | 100 } else { |
101 it++; | 101 it++; |
102 } | 102 } |
103 } | 103 } |
104 } | 104 } |
105 | 105 |
106 const ModuleDescriptor::ModuleEntry* ModuleDescriptor::FindDuplicateExport( | 106 const ModuleDescriptor::Entry* ModuleDescriptor::FindDuplicateExport( |
107 Zone* zone) const { | 107 Zone* zone) const { |
108 ZoneSet<const AstRawString*> export_names(zone); | 108 ZoneSet<const AstRawString*> export_names(zone); |
109 for (const auto& it : regular_exports_) { | 109 for (const auto& it : regular_exports_) { |
110 const ModuleEntry* entry = it.second; | 110 const Entry* entry = it.second; |
111 DCHECK_NOT_NULL(entry->export_name); | 111 DCHECK_NOT_NULL(entry->export_name); |
112 if (!export_names.insert(entry->export_name).second) return entry; | 112 if (!export_names.insert(entry->export_name).second) return entry; |
113 } | 113 } |
114 for (auto entry : special_exports_) { | 114 for (auto entry : special_exports_) { |
115 if (entry->export_name == nullptr) continue; // Star export. | 115 if (entry->export_name == nullptr) continue; // Star export. |
116 if (!export_names.insert(entry->export_name).second) return entry; | 116 if (!export_names.insert(entry->export_name).second) return entry; |
117 } | 117 } |
118 return nullptr; | 118 return nullptr; |
119 } | 119 } |
120 | 120 |
121 bool ModuleDescriptor::Validate(ModuleScope* module_scope, | 121 bool ModuleDescriptor::Validate(ModuleScope* module_scope, |
122 PendingCompilationErrorHandler* error_handler, | 122 PendingCompilationErrorHandler* error_handler, |
123 Zone* zone) { | 123 Zone* zone) { |
124 DCHECK_EQ(this, module_scope->module()); | 124 DCHECK_EQ(this, module_scope->module()); |
125 DCHECK_NOT_NULL(error_handler); | 125 DCHECK_NOT_NULL(error_handler); |
126 | 126 |
127 // Report error iff there are duplicate exports. | 127 // Report error iff there are duplicate exports. |
128 { | 128 { |
129 const ModuleEntry* entry = FindDuplicateExport(zone); | 129 const Entry* entry = FindDuplicateExport(zone); |
130 if (entry != nullptr) { | 130 if (entry != nullptr) { |
131 error_handler->ReportMessageAt( | 131 error_handler->ReportMessageAt( |
132 entry->location.beg_pos, entry->location.end_pos, | 132 entry->location.beg_pos, entry->location.end_pos, |
133 MessageTemplate::kDuplicateExport, entry->export_name); | 133 MessageTemplate::kDuplicateExport, entry->export_name); |
134 return false; | 134 return false; |
135 } | 135 } |
136 } | 136 } |
137 | 137 |
138 // Report error iff there are exports of non-existent local names. | 138 // Report error iff there are exports of non-existent local names. |
139 for (const auto& it : regular_exports_) { | 139 for (const auto& it : regular_exports_) { |
140 const ModuleEntry* entry = it.second; | 140 const Entry* entry = it.second; |
141 DCHECK_NOT_NULL(entry->local_name); | 141 DCHECK_NOT_NULL(entry->local_name); |
142 if (module_scope->LookupLocal(entry->local_name) == nullptr) { | 142 if (module_scope->LookupLocal(entry->local_name) == nullptr) { |
143 error_handler->ReportMessageAt( | 143 error_handler->ReportMessageAt( |
144 entry->location.beg_pos, entry->location.end_pos, | 144 entry->location.beg_pos, entry->location.end_pos, |
145 MessageTemplate::kModuleExportUndefined, entry->local_name); | 145 MessageTemplate::kModuleExportUndefined, entry->local_name); |
146 return false; | 146 return false; |
147 } | 147 } |
148 } | 148 } |
149 | 149 |
150 MakeIndirectExportsExplicit(zone); | 150 MakeIndirectExportsExplicit(zone); |
151 return true; | 151 return true; |
152 } | 152 } |
153 | 153 |
154 } // namespace internal | 154 } // namespace internal |
155 } // namespace v8 | 155 } // namespace v8 |
OLD | NEW |