1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef ES2PANDA_PARSER_CORE_MODULERECORD_H 17 #define ES2PANDA_PARSER_CORE_MODULERECORD_H 18 19 #include <util/ustring.h> 20 21 namespace panda::es2panda::binder { 22 class ModuleScope; 23 } 24 25 namespace panda::es2panda::ir { 26 class Identifier; 27 } // namespace panda::es2panda::ir 28 29 namespace panda::es2panda::parser { 30 class SourceTextModuleRecord { 31 public: SourceTextModuleRecord(ArenaAllocator * allocator)32 explicit SourceTextModuleRecord(ArenaAllocator *allocator) 33 : allocator_(allocator), 34 moduleRequestsMap_(allocator_->Adapter()), 35 moduleRequestsIdxMap_(allocator_->Adapter()), 36 moduleRequests_(allocator_->Adapter()), 37 localExportEntries_(allocator_->Adapter()), 38 regularImportEntries_(allocator_->Adapter()), 39 namespaceImportEntries_(allocator_->Adapter()), 40 starExportEntries_(allocator_->Adapter()), 41 indirectExportEntries_(allocator_->Adapter()) 42 { 43 } 44 45 ~SourceTextModuleRecord() = default; 46 NO_COPY_SEMANTIC(SourceTextModuleRecord); 47 NO_MOVE_SEMANTIC(SourceTextModuleRecord); 48 49 struct ImportEntry { 50 int moduleRequestIdx_; 51 util::StringView localName_; 52 util::StringView importName_; 53 const ir::Identifier *localId_; 54 const ir::Identifier *importId_; 55 ImportEntryImportEntry56 ImportEntry(const util::StringView localName, const util::StringView importName, int moduleRequestIdx, 57 const ir::Identifier *localId, const ir::Identifier *importId) 58 : moduleRequestIdx_(moduleRequestIdx), localName_(localName), importName_(importName), 59 localId_(localId), importId_(importId) {} ImportEntryImportEntry60 ImportEntry(const util::StringView localName, int moduleRequestIdx, const ir::Identifier *localId) 61 : moduleRequestIdx_(moduleRequestIdx), localName_(localName), localId_(localId) {} 62 }; 63 64 struct ExportEntry { 65 int moduleRequestIdx_; 66 util::StringView exportName_; 67 util::StringView localName_; 68 util::StringView importName_; 69 const ir::Identifier *exportId_; 70 const ir::Identifier *localId_; 71 const ir::Identifier *importId_; 72 ExportEntryExportEntry73 explicit ExportEntry(int moduleRequest) : moduleRequestIdx_(moduleRequest) {} ExportEntryExportEntry74 ExportEntry(const util::StringView exportName, const util::StringView localName, 75 const ir::Identifier *exportId, const ir::Identifier *localId) 76 : moduleRequestIdx_(-1), exportName_(exportName), localName_(localName), 77 exportId_(exportId), localId_(localId) {} ExportEntryExportEntry78 ExportEntry(const util::StringView exportName, const util::StringView importName, int moduleRequest, 79 const ir::Identifier *exportId, const ir::Identifier *importId) 80 : moduleRequestIdx_(moduleRequest), exportName_(exportName), importName_(importName), 81 exportId_(exportId), importId_(importId) {} 82 }; 83 84 template <typename T, typename... Args> NewEntry(Args &&...args)85 T *NewEntry(Args &&... args) 86 { 87 return allocator_->New<T>(std::forward<Args>(args)...); 88 } 89 90 int AddModuleRequest(const util::StringView source); 91 void AddImportEntry(ImportEntry *entry); 92 void AddStarImportEntry(ImportEntry *entry); 93 bool AddLocalExportEntry(ExportEntry *entry); 94 bool AddIndirectExportEntry(ExportEntry *entry); 95 void AddStarExportEntry(ExportEntry *entry); 96 97 bool CheckImplicitIndirectExport(ExportEntry *exportEntry); 98 void CheckImplicitIndirectExport(ImportEntry *importEntry); 99 100 void AssignIndexToModuleVariable(binder::ModuleScope *moduleScope); 101 102 using ModuleRequestList = ArenaVector<util::StringView>; 103 using ModuleRequestMap = ArenaMap<const util::StringView, uint32_t>; 104 using ModuleRequestIdxMap = ArenaMap<uint32_t, const util::StringView>; 105 using LocalExportEntryMap = ArenaMultiMap<const util::StringView, ExportEntry *>; 106 using RegularImportEntryMap = ArenaMap<const util::StringView, ImportEntry *>; 107 using NamespaceImportEntryList = ArenaVector<ImportEntry *>; 108 using SpecialExportEntryList = ArenaVector<ExportEntry *>; 109 GetModuleRequests()110 const ArenaVector<util::StringView> &GetModuleRequests() const 111 { 112 return moduleRequests_; 113 } 114 GetModuleRequestIdxMap()115 const ModuleRequestIdxMap &GetModuleRequestIdxMap() const 116 { 117 return moduleRequestsIdxMap_; 118 } 119 GetLocalExportEntries()120 const LocalExportEntryMap &GetLocalExportEntries() const 121 { 122 return localExportEntries_; 123 } 124 GetRegularImportEntries()125 const RegularImportEntryMap &GetRegularImportEntries() const 126 { 127 return regularImportEntries_; 128 } 129 GetNamespaceImportEntries()130 const NamespaceImportEntryList &GetNamespaceImportEntries() const 131 { 132 return namespaceImportEntries_; 133 } 134 GetStarExportEntries()135 const SpecialExportEntryList &GetStarExportEntries() const 136 { 137 return starExportEntries_; 138 } 139 GetIndirectExportEntries()140 const SpecialExportEntryList &GetIndirectExportEntries() const 141 { 142 return indirectExportEntries_; 143 } 144 145 static constexpr std::string_view DEFAULT_LOCAL_NAME = "*default*"; 146 static constexpr std::string_view DEFAULT_EXTERNAL_NAME = "default"; 147 static constexpr std::string_view ANONY_NAMESPACE_NAME = "=ens"; 148 149 private: 150 bool HasDuplicateExport(util::StringView exportName) const; 151 void ConvertLocalExportToIndirect(ImportEntry *importEntry, ExportEntry *exportEntry); 152 153 ArenaAllocator *allocator_; 154 ModuleRequestMap moduleRequestsMap_; 155 ModuleRequestIdxMap moduleRequestsIdxMap_; 156 ModuleRequestList moduleRequests_; 157 LocalExportEntryMap localExportEntries_; 158 RegularImportEntryMap regularImportEntries_; 159 NamespaceImportEntryList namespaceImportEntries_; 160 SpecialExportEntryList starExportEntries_; 161 SpecialExportEntryList indirectExportEntries_; 162 }; 163 } // namespace panda::es2panda::parser 164 165 #endif