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-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 int module_request; |
| 82 | 83 |
| 83 // TODO(neis): Remove local_name component? | 84 // TODO(neis): Remove local_name component? |
| 84 explicit Entry(Scanner::Location loc) | 85 explicit Entry(Scanner::Location loc) |
| 85 : location(loc), | 86 : location(loc), |
| 86 export_name(nullptr), | 87 export_name(nullptr), |
| 87 local_name(nullptr), | 88 local_name(nullptr), |
| 88 import_name(nullptr), | 89 import_name(nullptr), |
| 89 module_request(nullptr) {} | 90 module_request(-1) {} |
|
adamk
2016/09/19 19:57:20
Would be good to document somewhere that "-1" mean
neis
2016/09/19 21:13:46
I added a comment. Negative means no module reque
| |
| 90 | 91 |
| 91 // (De-)serialization support. | 92 // (De-)serialization support. |
| 92 // Note that the location value is not preserved as it's only needed by the | 93 // 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.) | 94 // parser. (A Deserialize'd entry has an invalid location.) |
| 94 Handle<ModuleInfoEntry> Serialize(Isolate* isolate) const; | 95 Handle<ModuleInfoEntry> Serialize(Isolate* isolate) const; |
| 95 static Entry* Deserialize(Isolate* isolate, AstValueFactory* avfactory, | 96 static Entry* Deserialize(Isolate* isolate, AstValueFactory* avfactory, |
| 96 Handle<ModuleInfoEntry> entry); | 97 Handle<ModuleInfoEntry> entry); |
| 97 }; | 98 }; |
| 98 | 99 |
| 100 // Module requests. | |
| 101 const ZoneMap<const AstRawString*, int>& module_requests() const { | |
| 102 return module_requests_; | |
| 103 } | |
| 104 | |
| 99 // Empty imports and namespace imports. | 105 // Empty imports and namespace imports. |
| 100 const ZoneList<const Entry*>& special_imports() const { | 106 const ZoneList<const Entry*>& special_imports() const { |
| 101 return special_imports_; | 107 return special_imports_; |
| 102 } | 108 } |
| 103 | 109 |
| 104 // All the remaining imports, indexed by local name. | 110 // All the remaining imports, indexed by local name. |
| 105 const ZoneMap<const AstRawString*, const Entry*>& regular_imports() const { | 111 const ZoneMap<const AstRawString*, const Entry*>& regular_imports() const { |
| 106 return regular_imports_; | 112 return regular_imports_; |
| 107 } | 113 } |
| 108 | 114 |
| 109 // Star exports and explicitly indirect exports. | 115 // Star exports and explicitly indirect exports. |
| 110 const ZoneList<const Entry*>& special_exports() const { | 116 const ZoneList<const Entry*>& special_exports() const { |
| 111 return special_exports_; | 117 return special_exports_; |
| 112 } | 118 } |
| 113 | 119 |
| 114 // All the remaining exports, indexed by local name. | 120 // All the remaining exports, indexed by local name. |
| 115 const ZoneMultimap<const AstRawString*, Entry*>& regular_exports() const { | 121 const ZoneMultimap<const AstRawString*, Entry*>& regular_exports() const { |
| 116 return regular_exports_; | 122 return regular_exports_; |
| 117 } | 123 } |
| 118 | 124 |
| 119 void AddRegularExport(Entry* entry) { | 125 void AddRegularExport(Entry* entry) { |
| 120 DCHECK_NOT_NULL(entry->export_name); | 126 DCHECK_NOT_NULL(entry->export_name); |
| 121 DCHECK_NOT_NULL(entry->local_name); | 127 DCHECK_NOT_NULL(entry->local_name); |
| 122 DCHECK_NULL(entry->import_name); | 128 DCHECK_NULL(entry->import_name); |
| 129 DCHECK_LT(entry->module_request, 0); | |
| 123 regular_exports_.insert(std::make_pair(entry->local_name, entry)); | 130 regular_exports_.insert(std::make_pair(entry->local_name, entry)); |
| 124 } | 131 } |
| 125 | 132 |
| 126 void AddSpecialExport(const Entry* entry, Zone* zone) { | 133 void AddSpecialExport(const Entry* entry, Zone* zone) { |
| 127 DCHECK_NOT_NULL(entry->module_request); | 134 DCHECK_LE(0, entry->module_request); |
| 128 special_exports_.Add(entry, zone); | 135 special_exports_.Add(entry, zone); |
| 129 } | 136 } |
| 130 | 137 |
| 131 void AddRegularImport(const Entry* entry) { | 138 void AddRegularImport(const Entry* entry) { |
| 132 DCHECK_NOT_NULL(entry->import_name); | 139 DCHECK_NOT_NULL(entry->import_name); |
| 133 DCHECK_NOT_NULL(entry->local_name); | 140 DCHECK_NOT_NULL(entry->local_name); |
| 134 DCHECK_NOT_NULL(entry->module_request); | |
| 135 DCHECK_NULL(entry->export_name); | 141 DCHECK_NULL(entry->export_name); |
| 142 DCHECK_LE(0, entry->module_request); | |
| 136 regular_imports_.insert(std::make_pair(entry->local_name, entry)); | 143 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 | 144 // 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. | 145 // case we will report an error when declaring the variable. |
| 139 } | 146 } |
| 140 | 147 |
| 141 void AddSpecialImport(const Entry* entry, Zone* zone) { | 148 void AddSpecialImport(const Entry* entry, Zone* zone) { |
| 142 DCHECK_NOT_NULL(entry->module_request); | |
| 143 DCHECK_NULL(entry->export_name); | 149 DCHECK_NULL(entry->export_name); |
| 150 DCHECK_LE(0, entry->module_request); | |
| 144 special_imports_.Add(entry, zone); | 151 special_imports_.Add(entry, zone); |
| 145 } | 152 } |
| 146 | 153 |
| 147 private: | 154 private: |
| 148 // TODO(neis): Use STL datastructure instead of ZoneList? | 155 // TODO(neis): Use STL datastructure instead of ZoneList? |
| 156 ZoneMap<const AstRawString*, int> module_requests_; | |
| 149 ZoneList<const Entry*> special_exports_; | 157 ZoneList<const Entry*> special_exports_; |
| 150 ZoneList<const Entry*> special_imports_; | 158 ZoneList<const Entry*> special_imports_; |
| 151 ZoneMultimap<const AstRawString*, Entry*> regular_exports_; | 159 ZoneMultimap<const AstRawString*, Entry*> regular_exports_; |
| 152 ZoneMap<const AstRawString*, const Entry*> regular_imports_; | 160 ZoneMap<const AstRawString*, const Entry*> regular_imports_; |
| 153 | 161 |
| 154 // If there are multiple export entries with the same export name, return the | 162 // If there are multiple export entries with the same export name, return the |
| 155 // last of them (in source order). Otherwise return nullptr. | 163 // last of them (in source order). Otherwise return nullptr. |
| 156 const Entry* FindDuplicateExport(Zone* zone) const; | 164 const Entry* FindDuplicateExport(Zone* zone) const; |
| 157 | 165 |
| 158 // Find any implicitly indirect exports and make them explicit. | 166 // Find any implicitly indirect exports and make them explicit. |
| 159 // | 167 // |
| 160 // An explicitly indirect export is an export entry arising from an export | 168 // An explicitly indirect export is an export entry arising from an export |
| 161 // statement of the following form: | 169 // statement of the following form: |
| 162 // export {a as c} from "X"; | 170 // export {a as c} from "X"; |
| 163 // An implicitly indirect export corresponds to | 171 // An implicitly indirect export corresponds to |
| 164 // export {b as c}; | 172 // export {b as c}; |
| 165 // in the presence of an import statement of the form | 173 // in the presence of an import statement of the form |
| 166 // import {a as b} from "X"; | 174 // import {a as b} from "X"; |
| 167 // This function finds such implicitly indirect export entries and rewrites | 175 // 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 | 176 // them by filling in the import name and module request, as well as nulling |
| 169 // out the local name. Effectively, it turns | 177 // out the local name. Effectively, it turns |
| 170 // import {a as b} from "X"; export {b as c}; | 178 // import {a as b} from "X"; export {b as c}; |
| 171 // into: | 179 // into: |
| 172 // import {a as b} from "X"; export {a as c} from "X"; | 180 // import {a as b} from "X"; export {a as c} from "X"; |
| 173 // (The import entry is never deleted.) | 181 // (The import entry is never deleted.) |
| 174 void MakeIndirectExportsExplicit(Zone* zone); | 182 void MakeIndirectExportsExplicit(Zone* zone); |
| 183 | |
| 184 int AddModuleRequest(const AstRawString* specifier) { | |
| 185 DCHECK_NOT_NULL(specifier); | |
| 186 auto it = module_requests_ | |
| 187 .insert(std::make_pair(specifier, module_requests_.size())) | |
| 188 .first; | |
| 189 return it->second; | |
| 190 } | |
| 175 }; | 191 }; |
| 176 | 192 |
| 177 } // namespace internal | 193 } // namespace internal |
| 178 } // namespace v8 | 194 } // namespace v8 |
| 179 | 195 |
| 180 #endif // V8_AST_MODULES_H_ | 196 #endif // V8_AST_MODULES_H_ |
| OLD | NEW |