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 void RemoveDefaultLocalExportEntry(); 95 bool AddIndirectExportEntry(ExportEntry *entry); 96 void AddStarExportEntry(ExportEntry *entry); 97 98 bool CheckImplicitIndirectExport(ExportEntry *exportEntry); 99 void CheckImplicitIndirectExport(ImportEntry *importEntry); 100 101 void AssignIndexToModuleVariable(binder::ModuleScope *moduleScope); 102 int GetModuleRequestIdx(const util::StringView localName); 103 104 using ModuleRequestList = ArenaVector<util::StringView>; 105 using ModuleRequestMap = ArenaMap<const util::StringView, uint32_t>; 106 using ModuleRequestIdxMap = ArenaMap<uint32_t, const util::StringView>; 107 using LocalExportEntryMap = ArenaMultiMap<const util::StringView, ExportEntry *>; 108 using RegularImportEntryMap = ArenaMap<const util::StringView, ImportEntry *>; 109 using NamespaceImportEntryList = ArenaVector<ImportEntry *>; 110 using SpecialExportEntryList = ArenaVector<ExportEntry *>; 111 GetModuleRequests()112 const ArenaVector<util::StringView> &GetModuleRequests() const 113 { 114 return moduleRequests_; 115 } 116 GetModuleRequestIdxMap()117 const ModuleRequestIdxMap &GetModuleRequestIdxMap() const 118 { 119 return moduleRequestsIdxMap_; 120 } 121 GetLocalExportEntries()122 const LocalExportEntryMap &GetLocalExportEntries() const 123 { 124 return localExportEntries_; 125 } 126 GetRegularImportEntries()127 const RegularImportEntryMap &GetRegularImportEntries() const 128 { 129 return regularImportEntries_; 130 } 131 GetNamespaceImportEntries()132 const NamespaceImportEntryList &GetNamespaceImportEntries() const 133 { 134 return namespaceImportEntries_; 135 } 136 GetStarExportEntries()137 const SpecialExportEntryList &GetStarExportEntries() const 138 { 139 return starExportEntries_; 140 } 141 GetIndirectExportEntries()142 const SpecialExportEntryList &GetIndirectExportEntries() const 143 { 144 return indirectExportEntries_; 145 } 146 147 static constexpr std::string_view DEFAULT_LOCAL_NAME = "*default*"; 148 static constexpr std::string_view DEFAULT_EXTERNAL_NAME = "default"; 149 static constexpr std::string_view ANONY_NAMESPACE_NAME = "=ens"; 150 static constexpr int INVALID_MODULEREQUEST_ID = -1; 151 152 private: 153 bool HasDuplicateExport(util::StringView exportName) const; 154 void ConvertLocalExportToIndirect(ImportEntry *importEntry, ExportEntry *exportEntry); 155 void CheckAndAssignIndex(binder::ModuleScope *moduleScope, util::StringView name, uint32_t *inde) const; 156 157 ArenaAllocator *allocator_; 158 ModuleRequestMap moduleRequestsMap_; 159 ModuleRequestIdxMap moduleRequestsIdxMap_; 160 ModuleRequestList moduleRequests_; 161 LocalExportEntryMap localExportEntries_; 162 RegularImportEntryMap regularImportEntries_; 163 NamespaceImportEntryList namespaceImportEntries_; 164 SpecialExportEntryList starExportEntries_; 165 SpecialExportEntryList indirectExportEntries_; 166 }; 167 } // namespace panda::es2panda::parser 168 169 #endif