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