Chromium Code Reviews| 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 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 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 // The module_request value records the order in which modules are | 80 // 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 | 81 // 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 | 82 // module specifiers and into the Module's array of requested modules. A |
| 83 // negative value means no module request. | 83 // negative value means no module request. |
| 84 int module_request; | 84 int module_request; |
| 85 int cell_index; | |
|
adamk
2016/10/31 18:25:04
Maybe add a comment here too, especially since 0 i
neis
2016/11/03 10:43:48
Done.
| |
| 85 | 86 |
| 86 // TODO(neis): Remove local_name component? | 87 // TODO(neis): Remove local_name component? |
| 87 explicit Entry(Scanner::Location loc) | 88 explicit Entry(Scanner::Location loc) |
| 88 : location(loc), | 89 : location(loc), |
| 89 export_name(nullptr), | 90 export_name(nullptr), |
| 90 local_name(nullptr), | 91 local_name(nullptr), |
| 91 import_name(nullptr), | 92 import_name(nullptr), |
| 92 module_request(-1) {} | 93 module_request(-1), |
| 94 cell_index(0) {} | |
| 93 | 95 |
| 94 // (De-)serialization support. | 96 // (De-)serialization support. |
| 95 // 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 |
| 96 // parser. (A Deserialize'd entry has an invalid location.) | 98 // parser. (A Deserialize'd entry has an invalid location.) |
| 97 Handle<ModuleInfoEntry> Serialize(Isolate* isolate) const; | 99 Handle<ModuleInfoEntry> Serialize(Isolate* isolate) const; |
| 98 static Entry* Deserialize(Isolate* isolate, AstValueFactory* avfactory, | 100 static Entry* Deserialize(Isolate* isolate, AstValueFactory* avfactory, |
| 99 Handle<ModuleInfoEntry> entry); | 101 Handle<ModuleInfoEntry> entry); |
| 100 }; | 102 }; |
| 101 | 103 |
| 102 // Module requests. | 104 // Module requests. |
| 103 const ZoneMap<const AstRawString*, int>& module_requests() const { | 105 const ZoneMap<const AstRawString*, int>& module_requests() const { |
| 104 return module_requests_; | 106 return module_requests_; |
| 105 } | 107 } |
| 106 | 108 |
| 107 // Namespace imports. | 109 // Namespace imports. |
| 108 const ZoneList<const Entry*>& namespace_imports() const { | 110 const ZoneList<const Entry*>& namespace_imports() const { |
| 109 return namespace_imports_; | 111 return namespace_imports_; |
| 110 } | 112 } |
| 111 | 113 |
| 112 // All the remaining imports, indexed by local name. | 114 // All the remaining imports, indexed by local name. |
| 113 const ZoneMap<const AstRawString*, const Entry*>& regular_imports() const { | 115 const ZoneMap<const AstRawString*, Entry*>& regular_imports() const { |
| 114 return regular_imports_; | 116 return regular_imports_; |
| 115 } | 117 } |
| 116 | 118 |
| 117 // Star exports and explicitly indirect exports. | 119 // Star exports and explicitly indirect exports. |
| 118 const ZoneList<const Entry*>& special_exports() const { | 120 const ZoneList<const Entry*>& special_exports() const { |
| 119 return special_exports_; | 121 return special_exports_; |
| 120 } | 122 } |
| 121 | 123 |
| 122 // All the remaining exports, indexed by local name. | 124 // All the remaining exports, indexed by local name. |
| 123 // After canonicalization (see Validate), these are exactly the local exports. | 125 // After canonicalization (see Validate), these are exactly the local exports. |
| 124 const ZoneMultimap<const AstRawString*, Entry*>& regular_exports() const { | 126 const ZoneMultimap<const AstRawString*, Entry*>& regular_exports() const { |
| 125 return regular_exports_; | 127 return regular_exports_; |
| 126 } | 128 } |
| 127 | 129 |
| 128 void AddRegularExport(Entry* entry) { | 130 void AddRegularExport(Entry* entry) { |
| 129 DCHECK_NOT_NULL(entry->export_name); | 131 DCHECK_NOT_NULL(entry->export_name); |
| 130 DCHECK_NOT_NULL(entry->local_name); | 132 DCHECK_NOT_NULL(entry->local_name); |
| 131 DCHECK_NULL(entry->import_name); | 133 DCHECK_NULL(entry->import_name); |
| 132 DCHECK_LT(entry->module_request, 0); | 134 DCHECK_LT(entry->module_request, 0); |
| 133 regular_exports_.insert(std::make_pair(entry->local_name, entry)); | 135 regular_exports_.insert(std::make_pair(entry->local_name, entry)); |
| 134 } | 136 } |
| 135 | 137 |
| 136 void AddSpecialExport(const Entry* entry, Zone* zone) { | 138 void AddSpecialExport(const Entry* entry, Zone* zone) { |
| 137 DCHECK_NULL(entry->local_name); | 139 DCHECK_NULL(entry->local_name); |
| 138 DCHECK_LE(0, entry->module_request); | 140 DCHECK_LE(0, entry->module_request); |
| 139 special_exports_.Add(entry, zone); | 141 special_exports_.Add(entry, zone); |
| 140 } | 142 } |
| 141 | 143 |
| 142 void AddRegularImport(const Entry* entry) { | 144 void AddRegularImport(Entry* entry) { |
| 143 DCHECK_NOT_NULL(entry->import_name); | 145 DCHECK_NOT_NULL(entry->import_name); |
| 144 DCHECK_NOT_NULL(entry->local_name); | 146 DCHECK_NOT_NULL(entry->local_name); |
| 145 DCHECK_NULL(entry->export_name); | 147 DCHECK_NULL(entry->export_name); |
| 146 DCHECK_LE(0, entry->module_request); | 148 DCHECK_LE(0, entry->module_request); |
| 147 regular_imports_.insert(std::make_pair(entry->local_name, entry)); | 149 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 | 150 // 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. | 151 // case we will report an error when declaring the variable. |
| 150 } | 152 } |
| 151 | 153 |
| 152 void AddNamespaceImport(const Entry* entry, Zone* zone) { | 154 void AddNamespaceImport(const Entry* entry, Zone* zone) { |
| 153 DCHECK_NULL(entry->import_name); | 155 DCHECK_NULL(entry->import_name); |
| 154 DCHECK_NULL(entry->export_name); | 156 DCHECK_NULL(entry->export_name); |
| 155 DCHECK_NOT_NULL(entry->local_name); | 157 DCHECK_NOT_NULL(entry->local_name); |
| 156 DCHECK_LE(0, entry->module_request); | 158 DCHECK_LE(0, entry->module_request); |
| 157 namespace_imports_.Add(entry, zone); | 159 namespace_imports_.Add(entry, zone); |
| 158 } | 160 } |
| 159 | 161 |
| 160 Handle<FixedArray> SerializeRegularExports(Isolate* isolate, | 162 Handle<FixedArray> SerializeRegularExports(Isolate* isolate, |
| 161 Zone* zone) const; | 163 Zone* zone) const; |
| 162 void DeserializeRegularExports(Isolate* isolate, AstValueFactory* avfactory, | 164 void DeserializeRegularExports(Isolate* isolate, AstValueFactory* avfactory, |
| 163 Handle<FixedArray> data); | 165 Handle<FixedArray> data); |
| 164 | 166 |
| 165 private: | 167 private: |
| 166 // TODO(neis): Use STL datastructure instead of ZoneList? | 168 // TODO(neis): Use STL datastructure instead of ZoneList? |
| 167 ZoneMap<const AstRawString*, int> module_requests_; | 169 ZoneMap<const AstRawString*, int> module_requests_; |
| 168 ZoneList<const Entry*> special_exports_; | 170 ZoneList<const Entry*> special_exports_; |
| 169 ZoneList<const Entry*> namespace_imports_; | 171 ZoneList<const Entry*> namespace_imports_; |
| 170 ZoneMultimap<const AstRawString*, Entry*> regular_exports_; | 172 ZoneMultimap<const AstRawString*, Entry*> regular_exports_; |
| 171 ZoneMap<const AstRawString*, const Entry*> regular_imports_; | 173 ZoneMap<const AstRawString*, Entry*> regular_imports_; |
| 172 | 174 |
| 173 // If there are multiple export entries with the same export name, return the | 175 // If there are multiple export entries with the same export name, return the |
| 174 // last of them (in source order). Otherwise return nullptr. | 176 // last of them (in source order). Otherwise return nullptr. |
| 175 const Entry* FindDuplicateExport(Zone* zone) const; | 177 const Entry* FindDuplicateExport(Zone* zone) const; |
| 176 | 178 |
| 177 // Find any implicitly indirect exports and make them explicit. | 179 // Find any implicitly indirect exports and make them explicit. |
| 178 // | 180 // |
| 179 // An explicitly indirect export is an export entry arising from an export | 181 // An explicitly indirect export is an export entry arising from an export |
| 180 // statement of the following form: | 182 // statement of the following form: |
| 181 // export {a as c} from "X"; | 183 // export {a as c} from "X"; |
| 182 // An implicitly indirect export corresponds to | 184 // An implicitly indirect export corresponds to |
| 183 // export {b as c}; | 185 // export {b as c}; |
| 184 // in the presence of an import statement of the form | 186 // in the presence of an import statement of the form |
| 185 // import {a as b} from "X"; | 187 // import {a as b} from "X"; |
| 186 // This function finds such implicitly indirect export entries and rewrites | 188 // 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 | 189 // them by filling in the import name and module request, as well as nulling |
| 188 // out the local name. Effectively, it turns | 190 // out the local name. Effectively, it turns |
| 189 // import {a as b} from "X"; export {b as c}; | 191 // import {a as b} from "X"; export {b as c}; |
| 190 // into: | 192 // into: |
| 191 // import {a as b} from "X"; export {a as c} from "X"; | 193 // import {a as b} from "X"; export {a as c} from "X"; |
| 192 // (The import entry is never deleted.) | 194 // (The import entry is never deleted.) |
| 193 void MakeIndirectExportsExplicit(Zone* zone); | 195 void MakeIndirectExportsExplicit(Zone* zone); |
| 194 | 196 |
| 197 void AssignCellIndices(); | |
|
adamk
2016/10/31 18:25:04
Please give this a comment: how are cell indices a
neis
2016/11/03 10:43:48
Done.
| |
| 198 | |
| 195 int AddModuleRequest(const AstRawString* specifier) { | 199 int AddModuleRequest(const AstRawString* specifier) { |
| 196 DCHECK_NOT_NULL(specifier); | 200 DCHECK_NOT_NULL(specifier); |
| 197 auto it = module_requests_ | 201 auto it = module_requests_ |
| 198 .insert(std::make_pair(specifier, module_requests_.size())) | 202 .insert(std::make_pair(specifier, module_requests_.size())) |
| 199 .first; | 203 .first; |
| 200 return it->second; | 204 return it->second; |
| 201 } | 205 } |
| 202 }; | 206 }; |
| 203 | 207 |
| 204 } // namespace internal | 208 } // namespace internal |
| 205 } // namespace v8 | 209 } // namespace v8 |
| 206 | 210 |
| 207 #endif // V8_AST_MODULES_H_ | 211 #endif // V8_AST_MODULES_H_ |
| OLD | NEW |