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-containers.h" | 10 #include "src/zone-containers.h" |
11 | 11 |
12 namespace v8 { | 12 namespace v8 { |
13 namespace internal { | 13 namespace internal { |
14 | 14 |
15 | 15 |
16 class AstRawString; | 16 class AstRawString; |
17 class ModuleInfoEntry; | 17 class ModuleInfoEntry; |
18 | 18 |
19 class ModuleDescriptor : public ZoneObject { | 19 class ModuleDescriptor : public ZoneObject { |
20 public: | 20 public: |
21 explicit ModuleDescriptor(Zone* zone) | 21 explicit ModuleDescriptor(Zone* zone) |
22 : special_exports_(1, zone), | 22 : module_requests_(zone), |
| 23 special_exports_(1, zone), |
23 special_imports_(1, zone), | 24 special_imports_(1, zone), |
24 regular_exports_(zone), | 25 regular_exports_(zone), |
25 regular_imports_(zone) {} | 26 regular_imports_(zone) {} |
26 | 27 |
27 // The following Add* methods are high-level convenience functions for use by | 28 // The following Add* methods are high-level convenience functions for use by |
28 // the parser. | 29 // the parser. |
29 | 30 |
30 // import x from "foo.js"; | 31 // import x from "foo.js"; |
31 // import {x} from "foo.js"; | 32 // import {x} from "foo.js"; |
32 // import {x as y} from "foo.js"; | 33 // import {x as y} from "foo.js"; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 // Check if module is well-formed and report error if not. | 72 // Check if module is well-formed and report error if not. |
72 // Also canonicalize indirect exports. | 73 // Also canonicalize indirect exports. |
73 bool Validate(ModuleScope* module_scope, | 74 bool Validate(ModuleScope* module_scope, |
74 PendingCompilationErrorHandler* error_handler, Zone* zone); | 75 PendingCompilationErrorHandler* error_handler, Zone* zone); |
75 | 76 |
76 struct Entry : public ZoneObject { | 77 struct Entry : public ZoneObject { |
77 const Scanner::Location location; | 78 const Scanner::Location location; |
78 const AstRawString* export_name; | 79 const AstRawString* export_name; |
79 const AstRawString* local_name; | 80 const AstRawString* local_name; |
80 const AstRawString* import_name; | 81 const AstRawString* import_name; |
81 const AstRawString* module_request; | 82 // The module_request value records the order in which modules are |
| 83 // requested. It also functions as an index into the ModuleInfo's array of |
| 84 // module specifiers and into the Module's array of requested modules. A |
| 85 // negative value means no module request. |
| 86 int module_request; |
82 | 87 |
83 // TODO(neis): Remove local_name component? | 88 // TODO(neis): Remove local_name component? |
84 explicit Entry(Scanner::Location loc) | 89 explicit Entry(Scanner::Location loc) |
85 : location(loc), | 90 : location(loc), |
86 export_name(nullptr), | 91 export_name(nullptr), |
87 local_name(nullptr), | 92 local_name(nullptr), |
88 import_name(nullptr), | 93 import_name(nullptr), |
89 module_request(nullptr) {} | 94 module_request(-1) {} |
90 | 95 |
91 // (De-)serialization support. | 96 // (De-)serialization support. |
92 // Note that the location value is not preserved as it's only needed by the | 97 // Note that the location value is not preserved as it's only needed by the |
93 // parser. (A Deserialize'd entry has an invalid location.) | 98 // parser. (A Deserialize'd entry has an invalid location.) |
94 Handle<ModuleInfoEntry> Serialize(Isolate* isolate) const; | 99 Handle<ModuleInfoEntry> Serialize(Isolate* isolate) const; |
95 static Entry* Deserialize(Isolate* isolate, AstValueFactory* avfactory, | 100 static Entry* Deserialize(Isolate* isolate, AstValueFactory* avfactory, |
96 Handle<ModuleInfoEntry> entry); | 101 Handle<ModuleInfoEntry> entry); |
97 }; | 102 }; |
98 | 103 |
| 104 // Module requests. |
| 105 const ZoneMap<const AstRawString*, int>& module_requests() const { |
| 106 return module_requests_; |
| 107 } |
| 108 |
99 // Empty imports and namespace imports. | 109 // Empty imports and namespace imports. |
100 const ZoneList<const Entry*>& special_imports() const { | 110 const ZoneList<const Entry*>& special_imports() const { |
101 return special_imports_; | 111 return special_imports_; |
102 } | 112 } |
103 | 113 |
104 // All the remaining imports, indexed by local name. | 114 // All the remaining imports, indexed by local name. |
105 const ZoneMap<const AstRawString*, const Entry*>& regular_imports() const { | 115 const ZoneMap<const AstRawString*, const Entry*>& regular_imports() const { |
106 return regular_imports_; | 116 return regular_imports_; |
107 } | 117 } |
108 | 118 |
109 // Star exports and explicitly indirect exports. | 119 // Star exports and explicitly indirect exports. |
110 const ZoneList<const Entry*>& special_exports() const { | 120 const ZoneList<const Entry*>& special_exports() const { |
111 return special_exports_; | 121 return special_exports_; |
112 } | 122 } |
113 | 123 |
114 // All the remaining exports, indexed by local name. | 124 // All the remaining exports, indexed by local name. |
115 const ZoneMultimap<const AstRawString*, Entry*>& regular_exports() const { | 125 const ZoneMultimap<const AstRawString*, Entry*>& regular_exports() const { |
116 return regular_exports_; | 126 return regular_exports_; |
117 } | 127 } |
118 | 128 |
119 void AddRegularExport(Entry* entry) { | 129 void AddRegularExport(Entry* entry) { |
120 DCHECK_NOT_NULL(entry->export_name); | 130 DCHECK_NOT_NULL(entry->export_name); |
121 DCHECK_NOT_NULL(entry->local_name); | 131 DCHECK_NOT_NULL(entry->local_name); |
122 DCHECK_NULL(entry->import_name); | 132 DCHECK_NULL(entry->import_name); |
| 133 DCHECK_LT(entry->module_request, 0); |
123 regular_exports_.insert(std::make_pair(entry->local_name, entry)); | 134 regular_exports_.insert(std::make_pair(entry->local_name, entry)); |
124 } | 135 } |
125 | 136 |
126 void AddSpecialExport(const Entry* entry, Zone* zone) { | 137 void AddSpecialExport(const Entry* entry, Zone* zone) { |
127 DCHECK_NOT_NULL(entry->module_request); | 138 DCHECK_LE(0, entry->module_request); |
128 special_exports_.Add(entry, zone); | 139 special_exports_.Add(entry, zone); |
129 } | 140 } |
130 | 141 |
131 void AddRegularImport(const Entry* entry) { | 142 void AddRegularImport(const Entry* entry) { |
132 DCHECK_NOT_NULL(entry->import_name); | 143 DCHECK_NOT_NULL(entry->import_name); |
133 DCHECK_NOT_NULL(entry->local_name); | 144 DCHECK_NOT_NULL(entry->local_name); |
134 DCHECK_NOT_NULL(entry->module_request); | |
135 DCHECK_NULL(entry->export_name); | 145 DCHECK_NULL(entry->export_name); |
| 146 DCHECK_LE(0, entry->module_request); |
136 regular_imports_.insert(std::make_pair(entry->local_name, entry)); | 147 regular_imports_.insert(std::make_pair(entry->local_name, entry)); |
137 // We don't care if there's already an entry for this local name, as in that | 148 // We don't care if there's already an entry for this local name, as in that |
138 // case we will report an error when declaring the variable. | 149 // case we will report an error when declaring the variable. |
139 } | 150 } |
140 | 151 |
141 void AddSpecialImport(const Entry* entry, Zone* zone) { | 152 void AddSpecialImport(const Entry* entry, Zone* zone) { |
142 DCHECK_NOT_NULL(entry->module_request); | |
143 DCHECK_NULL(entry->export_name); | 153 DCHECK_NULL(entry->export_name); |
| 154 DCHECK_LE(0, entry->module_request); |
144 special_imports_.Add(entry, zone); | 155 special_imports_.Add(entry, zone); |
145 } | 156 } |
146 | 157 |
147 private: | 158 private: |
148 // TODO(neis): Use STL datastructure instead of ZoneList? | 159 // TODO(neis): Use STL datastructure instead of ZoneList? |
| 160 ZoneMap<const AstRawString*, int> module_requests_; |
149 ZoneList<const Entry*> special_exports_; | 161 ZoneList<const Entry*> special_exports_; |
150 ZoneList<const Entry*> special_imports_; | 162 ZoneList<const Entry*> special_imports_; |
151 ZoneMultimap<const AstRawString*, Entry*> regular_exports_; | 163 ZoneMultimap<const AstRawString*, Entry*> regular_exports_; |
152 ZoneMap<const AstRawString*, const Entry*> regular_imports_; | 164 ZoneMap<const AstRawString*, const Entry*> regular_imports_; |
153 | 165 |
154 // If there are multiple export entries with the same export name, return the | 166 // If there are multiple export entries with the same export name, return the |
155 // last of them (in source order). Otherwise return nullptr. | 167 // last of them (in source order). Otherwise return nullptr. |
156 const Entry* FindDuplicateExport(Zone* zone) const; | 168 const Entry* FindDuplicateExport(Zone* zone) const; |
157 | 169 |
158 // Find any implicitly indirect exports and make them explicit. | 170 // Find any implicitly indirect exports and make them explicit. |
159 // | 171 // |
160 // An explicitly indirect export is an export entry arising from an export | 172 // An explicitly indirect export is an export entry arising from an export |
161 // statement of the following form: | 173 // statement of the following form: |
162 // export {a as c} from "X"; | 174 // export {a as c} from "X"; |
163 // An implicitly indirect export corresponds to | 175 // An implicitly indirect export corresponds to |
164 // export {b as c}; | 176 // export {b as c}; |
165 // in the presence of an import statement of the form | 177 // in the presence of an import statement of the form |
166 // import {a as b} from "X"; | 178 // import {a as b} from "X"; |
167 // This function finds such implicitly indirect export entries and rewrites | 179 // This function finds such implicitly indirect export entries and rewrites |
168 // them by filling in the import name and module request, as well as nulling | 180 // them by filling in the import name and module request, as well as nulling |
169 // out the local name. Effectively, it turns | 181 // out the local name. Effectively, it turns |
170 // import {a as b} from "X"; export {b as c}; | 182 // import {a as b} from "X"; export {b as c}; |
171 // into: | 183 // into: |
172 // import {a as b} from "X"; export {a as c} from "X"; | 184 // import {a as b} from "X"; export {a as c} from "X"; |
173 // (The import entry is never deleted.) | 185 // (The import entry is never deleted.) |
174 void MakeIndirectExportsExplicit(Zone* zone); | 186 void MakeIndirectExportsExplicit(Zone* zone); |
| 187 |
| 188 int AddModuleRequest(const AstRawString* specifier) { |
| 189 DCHECK_NOT_NULL(specifier); |
| 190 auto it = module_requests_ |
| 191 .insert(std::make_pair(specifier, module_requests_.size())) |
| 192 .first; |
| 193 return it->second; |
| 194 } |
175 }; | 195 }; |
176 | 196 |
177 } // namespace internal | 197 } // namespace internal |
178 } // namespace v8 | 198 } // namespace v8 |
179 | 199 |
180 #endif // V8_AST_MODULES_H_ | 200 #endif // V8_AST_MODULES_H_ |
OLD | NEW |