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 #ifndef V8_AST_MODULES_H_ | 5 #ifndef V8_AST_MODULES_H_ |
6 #define V8_AST_MODULES_H_ | 6 #define V8_AST_MODULES_H_ |
7 | 7 |
8 #include "src/parsing/scanner.h" // Only for Scanner::Location. | 8 #include "src/parsing/scanner.h" // Only for Scanner::Location. |
9 #include "src/pending-compilation-error-handler.h" | 9 #include "src/pending-compilation-error-handler.h" |
10 #include "src/zone/zone-containers.h" | 10 #include "src/zone/zone-containers.h" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 // Check if module is well-formed and report error if not. | 70 // Check if module is well-formed and report error if not. |
71 // Also canonicalize indirect exports. | 71 // Also canonicalize indirect exports. |
72 bool Validate(ModuleScope* module_scope, | 72 bool Validate(ModuleScope* module_scope, |
73 PendingCompilationErrorHandler* error_handler, Zone* zone); | 73 PendingCompilationErrorHandler* error_handler, Zone* zone); |
74 | 74 |
75 struct Entry : public ZoneObject { | 75 struct Entry : public ZoneObject { |
76 Scanner::Location location; | 76 Scanner::Location location; |
77 const AstRawString* export_name; | 77 const AstRawString* export_name; |
78 const AstRawString* local_name; | 78 const AstRawString* local_name; |
79 const AstRawString* import_name; | 79 const AstRawString* import_name; |
| 80 |
80 // The module_request value records the order in which modules are | 81 // The module_request value records the order in which modules are |
81 // requested. It also functions as an index into the ModuleInfo's array of | 82 // requested. It also functions as an index into the ModuleInfo's array of |
82 // module specifiers and into the Module's array of requested modules. A | 83 // module specifiers and into the Module's array of requested modules. A |
83 // negative value means no module request. | 84 // negative value means no module request. |
84 int module_request; | 85 int module_request; |
85 | 86 |
| 87 // Import/export entries that are associated with a MODULE-allocated |
| 88 // variable use the cell_index value to encode the location of their cell. |
| 89 // Negative values are used for imports and positive values for exports. |
| 90 // For entries that are not associated with a MODULE-allocated variable, |
| 91 // cell_index is 0. |
| 92 int cell_index; |
| 93 |
86 // TODO(neis): Remove local_name component? | 94 // TODO(neis): Remove local_name component? |
87 explicit Entry(Scanner::Location loc) | 95 explicit Entry(Scanner::Location loc) |
88 : location(loc), | 96 : location(loc), |
89 export_name(nullptr), | 97 export_name(nullptr), |
90 local_name(nullptr), | 98 local_name(nullptr), |
91 import_name(nullptr), | 99 import_name(nullptr), |
92 module_request(-1) {} | 100 module_request(-1), |
| 101 cell_index(0) {} |
93 | 102 |
94 // (De-)serialization support. | 103 // (De-)serialization support. |
95 // Note that the location value is not preserved as it's only needed by the | 104 // Note that the location value is not preserved as it's only needed by the |
96 // parser. (A Deserialize'd entry has an invalid location.) | 105 // parser. (A Deserialize'd entry has an invalid location.) |
97 Handle<ModuleInfoEntry> Serialize(Isolate* isolate) const; | 106 Handle<ModuleInfoEntry> Serialize(Isolate* isolate) const; |
98 static Entry* Deserialize(Isolate* isolate, AstValueFactory* avfactory, | 107 static Entry* Deserialize(Isolate* isolate, AstValueFactory* avfactory, |
99 Handle<ModuleInfoEntry> entry); | 108 Handle<ModuleInfoEntry> entry); |
100 }; | 109 }; |
101 | 110 |
102 // Module requests. | 111 // Module requests. |
103 const ZoneMap<const AstRawString*, int>& module_requests() const { | 112 const ZoneMap<const AstRawString*, int>& module_requests() const { |
104 return module_requests_; | 113 return module_requests_; |
105 } | 114 } |
106 | 115 |
107 // Namespace imports. | 116 // Namespace imports. |
108 const ZoneList<const Entry*>& namespace_imports() const { | 117 const ZoneList<const Entry*>& namespace_imports() const { |
109 return namespace_imports_; | 118 return namespace_imports_; |
110 } | 119 } |
111 | 120 |
112 // All the remaining imports, indexed by local name. | 121 // All the remaining imports, indexed by local name. |
113 const ZoneMap<const AstRawString*, const Entry*>& regular_imports() const { | 122 const ZoneMap<const AstRawString*, Entry*>& regular_imports() const { |
114 return regular_imports_; | 123 return regular_imports_; |
115 } | 124 } |
116 | 125 |
117 // Star exports and explicitly indirect exports. | 126 // Star exports and explicitly indirect exports. |
118 const ZoneList<const Entry*>& special_exports() const { | 127 const ZoneList<const Entry*>& special_exports() const { |
119 return special_exports_; | 128 return special_exports_; |
120 } | 129 } |
121 | 130 |
122 // All the remaining exports, indexed by local name. | 131 // All the remaining exports, indexed by local name. |
123 // After canonicalization (see Validate), these are exactly the local exports. | 132 // After canonicalization (see Validate), these are exactly the local exports. |
124 const ZoneMultimap<const AstRawString*, Entry*>& regular_exports() const { | 133 const ZoneMultimap<const AstRawString*, Entry*>& regular_exports() const { |
125 return regular_exports_; | 134 return regular_exports_; |
126 } | 135 } |
127 | 136 |
128 void AddRegularExport(Entry* entry) { | 137 void AddRegularExport(Entry* entry) { |
129 DCHECK_NOT_NULL(entry->export_name); | 138 DCHECK_NOT_NULL(entry->export_name); |
130 DCHECK_NOT_NULL(entry->local_name); | 139 DCHECK_NOT_NULL(entry->local_name); |
131 DCHECK_NULL(entry->import_name); | 140 DCHECK_NULL(entry->import_name); |
132 DCHECK_LT(entry->module_request, 0); | 141 DCHECK_LT(entry->module_request, 0); |
133 regular_exports_.insert(std::make_pair(entry->local_name, entry)); | 142 regular_exports_.insert(std::make_pair(entry->local_name, entry)); |
134 } | 143 } |
135 | 144 |
136 void AddSpecialExport(const Entry* entry, Zone* zone) { | 145 void AddSpecialExport(const Entry* entry, Zone* zone) { |
137 DCHECK_NULL(entry->local_name); | 146 DCHECK_NULL(entry->local_name); |
138 DCHECK_LE(0, entry->module_request); | 147 DCHECK_LE(0, entry->module_request); |
139 special_exports_.Add(entry, zone); | 148 special_exports_.Add(entry, zone); |
140 } | 149 } |
141 | 150 |
142 void AddRegularImport(const Entry* entry) { | 151 void AddRegularImport(Entry* entry) { |
143 DCHECK_NOT_NULL(entry->import_name); | 152 DCHECK_NOT_NULL(entry->import_name); |
144 DCHECK_NOT_NULL(entry->local_name); | 153 DCHECK_NOT_NULL(entry->local_name); |
145 DCHECK_NULL(entry->export_name); | 154 DCHECK_NULL(entry->export_name); |
146 DCHECK_LE(0, entry->module_request); | 155 DCHECK_LE(0, entry->module_request); |
147 regular_imports_.insert(std::make_pair(entry->local_name, entry)); | 156 regular_imports_.insert(std::make_pair(entry->local_name, entry)); |
148 // We don't care if there's already an entry for this local name, as in that | 157 // We don't care if there's already an entry for this local name, as in that |
149 // case we will report an error when declaring the variable. | 158 // case we will report an error when declaring the variable. |
150 } | 159 } |
151 | 160 |
152 void AddNamespaceImport(const Entry* entry, Zone* zone) { | 161 void AddNamespaceImport(const Entry* entry, Zone* zone) { |
153 DCHECK_NULL(entry->import_name); | 162 DCHECK_NULL(entry->import_name); |
154 DCHECK_NULL(entry->export_name); | 163 DCHECK_NULL(entry->export_name); |
155 DCHECK_NOT_NULL(entry->local_name); | 164 DCHECK_NOT_NULL(entry->local_name); |
156 DCHECK_LE(0, entry->module_request); | 165 DCHECK_LE(0, entry->module_request); |
157 namespace_imports_.Add(entry, zone); | 166 namespace_imports_.Add(entry, zone); |
158 } | 167 } |
159 | 168 |
160 Handle<FixedArray> SerializeRegularExports(Isolate* isolate, | 169 Handle<FixedArray> SerializeRegularExports(Isolate* isolate, |
161 Zone* zone) const; | 170 Zone* zone) const; |
162 void DeserializeRegularExports(Isolate* isolate, AstValueFactory* avfactory, | 171 void DeserializeRegularExports(Isolate* isolate, AstValueFactory* avfactory, |
163 Handle<FixedArray> data); | 172 Handle<FixedArray> data); |
164 | 173 |
165 private: | 174 private: |
166 // TODO(neis): Use STL datastructure instead of ZoneList? | 175 // TODO(neis): Use STL datastructure instead of ZoneList? |
167 ZoneMap<const AstRawString*, int> module_requests_; | 176 ZoneMap<const AstRawString*, int> module_requests_; |
168 ZoneList<const Entry*> special_exports_; | 177 ZoneList<const Entry*> special_exports_; |
169 ZoneList<const Entry*> namespace_imports_; | 178 ZoneList<const Entry*> namespace_imports_; |
170 ZoneMultimap<const AstRawString*, Entry*> regular_exports_; | 179 ZoneMultimap<const AstRawString*, Entry*> regular_exports_; |
171 ZoneMap<const AstRawString*, const Entry*> regular_imports_; | 180 ZoneMap<const AstRawString*, Entry*> regular_imports_; |
172 | 181 |
173 // If there are multiple export entries with the same export name, return the | 182 // If there are multiple export entries with the same export name, return the |
174 // last of them (in source order). Otherwise return nullptr. | 183 // last of them (in source order). Otherwise return nullptr. |
175 const Entry* FindDuplicateExport(Zone* zone) const; | 184 const Entry* FindDuplicateExport(Zone* zone) const; |
176 | 185 |
177 // Find any implicitly indirect exports and make them explicit. | 186 // Find any implicitly indirect exports and make them explicit. |
178 // | 187 // |
179 // An explicitly indirect export is an export entry arising from an export | 188 // An explicitly indirect export is an export entry arising from an export |
180 // statement of the following form: | 189 // statement of the following form: |
181 // export {a as c} from "X"; | 190 // export {a as c} from "X"; |
182 // An implicitly indirect export corresponds to | 191 // An implicitly indirect export corresponds to |
183 // export {b as c}; | 192 // export {b as c}; |
184 // in the presence of an import statement of the form | 193 // in the presence of an import statement of the form |
185 // import {a as b} from "X"; | 194 // import {a as b} from "X"; |
186 // This function finds such implicitly indirect export entries and rewrites | 195 // This function finds such implicitly indirect export entries and rewrites |
187 // them by filling in the import name and module request, as well as nulling | 196 // them by filling in the import name and module request, as well as nulling |
188 // out the local name. Effectively, it turns | 197 // out the local name. Effectively, it turns |
189 // import {a as b} from "X"; export {b as c}; | 198 // import {a as b} from "X"; export {b as c}; |
190 // into: | 199 // into: |
191 // import {a as b} from "X"; export {a as c} from "X"; | 200 // import {a as b} from "X"; export {a as c} from "X"; |
192 // (The import entry is never deleted.) | 201 // (The import entry is never deleted.) |
193 void MakeIndirectExportsExplicit(Zone* zone); | 202 void MakeIndirectExportsExplicit(Zone* zone); |
194 | 203 |
| 204 // Assign a cell_index of -1,-2,... to regular imports. |
| 205 // Assign a cell_index of +1,+2,... to regular (local) exports. |
| 206 // Assign a cell_index of 0 to anything else. |
| 207 void AssignCellIndices(); |
| 208 |
195 int AddModuleRequest(const AstRawString* specifier) { | 209 int AddModuleRequest(const AstRawString* specifier) { |
196 DCHECK_NOT_NULL(specifier); | 210 DCHECK_NOT_NULL(specifier); |
197 auto it = module_requests_ | 211 auto it = module_requests_ |
198 .insert(std::make_pair(specifier, module_requests_.size())) | 212 .insert(std::make_pair(specifier, module_requests_.size())) |
199 .first; | 213 .first; |
200 return it->second; | 214 return it->second; |
201 } | 215 } |
202 }; | 216 }; |
203 | 217 |
204 } // namespace internal | 218 } // namespace internal |
205 } // namespace v8 | 219 } // namespace v8 |
206 | 220 |
207 #endif // V8_AST_MODULES_H_ | 221 #endif // V8_AST_MODULES_H_ |
OLD | NEW |