@@ -24,62 +24,56 @@ std::string constructName(const swift::DeclName& declName) {
2424
2525std::variant<codeql::ConcreteFuncDecl, codeql::ConcreteFuncDeclsTrap>
2626DeclVisitor::translateFuncDecl (const swift::FuncDecl& decl) {
27- auto id = dispatcher_. assignNewLabel (decl, mangledName (decl) );
28- if (!dispatcher_. shouldEmitDeclBody (decl )) {
29- return ConcreteFuncDeclsTrap{id} ;
27+ auto ret = createNamedEntryOr<ConcreteFuncDeclsTrap> (decl);
28+ if (auto entry = get_if<ConcreteFuncDecl>(&ret )) {
29+ fillAbstractFunctionDecl (decl, *entry) ;
3030 }
31- ConcreteFuncDecl entry{id};
32- fillAbstractFunctionDecl (decl, entry);
33- return entry;
31+ return ret;
3432}
3533
3634std::variant<codeql::ConstructorDecl, codeql::ConstructorDeclsTrap>
3735DeclVisitor::translateConstructorDecl (const swift::ConstructorDecl& decl) {
38- auto id = dispatcher_. assignNewLabel (decl, mangledName (decl) );
39- if (!dispatcher_. shouldEmitDeclBody (decl )) {
40- return ConstructorDeclsTrap{id} ;
36+ auto ret = createNamedEntryOr<ConstructorDeclsTrap> (decl);
37+ if (auto entry = get_if<ConstructorDecl>(&ret )) {
38+ fillAbstractFunctionDecl (decl, *entry) ;
4139 }
42- ConstructorDecl entry{id};
43- fillAbstractFunctionDecl (decl, entry);
44- return entry;
40+ return ret;
4541}
4642
4743std::variant<codeql::DestructorDecl, codeql::DestructorDeclsTrap>
4844DeclVisitor::translateDestructorDecl (const swift::DestructorDecl& decl) {
49- auto id = dispatcher_. assignNewLabel (decl, mangledName (decl) );
50- if (!dispatcher_. shouldEmitDeclBody (decl )) {
51- return DestructorDeclsTrap{id} ;
45+ auto ret = createNamedEntryOr<DestructorDeclsTrap> (decl);
46+ if (auto entry = get_if<DestructorDecl>(&ret )) {
47+ fillAbstractFunctionDecl (decl, *entry) ;
5248 }
53- DestructorDecl entry{id};
54- fillAbstractFunctionDecl (decl, entry);
55- return entry;
49+ return ret;
5650}
5751
5852codeql::PrefixOperatorDecl DeclVisitor::translatePrefixOperatorDecl (
5953 const swift::PrefixOperatorDecl& decl) {
60- PrefixOperatorDecl entry{dispatcher_. assignNewLabel (decl)} ;
54+ auto entry = createEntry (decl);
6155 fillOperatorDecl (decl, entry);
6256 return entry;
6357}
6458
6559codeql::PostfixOperatorDecl DeclVisitor::translatePostfixOperatorDecl (
6660 const swift::PostfixOperatorDecl& decl) {
67- PostfixOperatorDecl entry{dispatcher_. assignNewLabel (decl)} ;
61+ auto entry = createEntry (decl);
6862 fillOperatorDecl (decl, entry);
6963 return entry;
7064}
7165
7266codeql::InfixOperatorDecl DeclVisitor::translateInfixOperatorDecl (
7367 const swift::InfixOperatorDecl& decl) {
74- InfixOperatorDecl entry{dispatcher_. assignNewLabel (decl)} ;
68+ auto entry = createEntry (decl);
7569 entry.precedence_group = dispatcher_.fetchOptionalLabel (decl.getPrecedenceGroup ());
7670 fillOperatorDecl (decl, entry);
7771 return entry;
7872}
7973
8074codeql::PrecedenceGroupDecl DeclVisitor::translatePrecedenceGroupDecl (
8175 const swift::PrecedenceGroupDecl& decl) {
82- PrecedenceGroupDecl entry{dispatcher_. assignNewLabel (decl)} ;
76+ auto entry = createEntry (decl);
8377 return entry;
8478}
8579
@@ -95,15 +89,15 @@ std::optional<codeql::ParamDecl> DeclVisitor::translateParamDecl(const swift::Pa
9589
9690codeql::TopLevelCodeDecl DeclVisitor::translateTopLevelCodeDecl (
9791 const swift::TopLevelCodeDecl& decl) {
98- TopLevelCodeDecl entry{dispatcher_. assignNewLabel (decl)} ;
92+ auto entry = createEntry (decl);
9993 assert (decl.getBody () && " Expect top level code to have body" );
10094 entry.body = dispatcher_.fetchLabel (decl.getBody ());
10195 return entry;
10296}
10397
10498codeql::PatternBindingDecl DeclVisitor::translatePatternBindingDecl (
10599 const swift::PatternBindingDecl& decl) {
106- PatternBindingDecl entry{dispatcher_. assignNewLabel (decl)} ;
100+ auto entry = createEntry (decl);
107101 for (unsigned i = 0 ; i < decl.getNumPatternEntries (); ++i) {
108102 auto pattern = decl.getPattern (i);
109103 assert (pattern && " Expect pattern binding decl to have all patterns" );
@@ -118,7 +112,7 @@ std::optional<codeql::ConcreteVarDecl> DeclVisitor::translateVarDecl(const swift
118112 // We do not deduplicate variables from non-swift (PCM, clang modules) modules as the mangler
119113 // crashes sometimes
120114 if (decl.getDeclContext ()->isLocalContext () || decl.getModuleContext ()->isNonSwiftModule ()) {
121- entry. emplace (dispatcher_. assignNewLabel ( decl) );
115+ entry = createEntry ( decl);
122116 } else {
123117 entry = createNamedEntry (decl);
124118 if (!entry) {
@@ -132,122 +126,106 @@ std::optional<codeql::ConcreteVarDecl> DeclVisitor::translateVarDecl(const swift
132126
133127std::variant<codeql::StructDecl, codeql::StructDeclsTrap> DeclVisitor::translateStructDecl (
134128 const swift::StructDecl& decl) {
135- auto id = dispatcher_. assignNewLabel (decl, mangledName (decl) );
136- if (!dispatcher_. shouldEmitDeclBody (decl )) {
137- return StructDeclsTrap{id} ;
129+ auto ret = createNamedEntryOr<StructDeclsTrap> (decl);
130+ if (auto entry = get_if<StructDecl>(&ret )) {
131+ fillNominalTypeDecl (decl, *entry) ;
138132 }
139- StructDecl entry{id};
140- fillNominalTypeDecl (decl, entry);
141- return entry;
133+ return ret;
142134}
143135
144136std::variant<codeql::ClassDecl, codeql::ClassDeclsTrap> DeclVisitor::translateClassDecl (
145137 const swift::ClassDecl& decl) {
146- auto id = dispatcher_. assignNewLabel (decl, mangledName (decl) );
147- if (!dispatcher_. shouldEmitDeclBody (decl )) {
148- return ClassDeclsTrap{id} ;
138+ auto ret = createNamedEntryOr<ClassDeclsTrap> (decl);
139+ if (auto entry = get_if<ClassDecl>(&ret )) {
140+ fillNominalTypeDecl (decl, *entry) ;
149141 }
150- ClassDecl entry{id};
151- fillNominalTypeDecl (decl, entry);
152- return entry;
142+ return ret;
153143}
154144
155145std::variant<codeql::EnumDecl, codeql::EnumDeclsTrap> DeclVisitor::translateEnumDecl (
156146 const swift::EnumDecl& decl) {
157- auto id = dispatcher_. assignNewLabel (decl, mangledName (decl) );
158- if (!dispatcher_. shouldEmitDeclBody (decl )) {
159- return EnumDeclsTrap{id} ;
147+ auto ret = createNamedEntryOr<EnumDeclsTrap> (decl);
148+ if (auto entry = get_if<EnumDecl>(&ret )) {
149+ fillNominalTypeDecl (decl, *entry) ;
160150 }
161- EnumDecl entry{id};
162- fillNominalTypeDecl (decl, entry);
163- return entry;
151+ return ret;
164152}
165153
166154std::variant<codeql::ProtocolDecl, codeql::ProtocolDeclsTrap> DeclVisitor::translateProtocolDecl (
167155 const swift::ProtocolDecl& decl) {
168- auto id = dispatcher_. assignNewLabel (decl, mangledName (decl) );
169- if (!dispatcher_. shouldEmitDeclBody (decl )) {
170- return ProtocolDeclsTrap{id} ;
156+ auto ret = createNamedEntryOr<ProtocolDeclsTrap> (decl);
157+ if (auto entry = get_if<ProtocolDecl>(&ret )) {
158+ fillNominalTypeDecl (decl, *entry) ;
171159 }
172- ProtocolDecl entry{id};
173- fillNominalTypeDecl (decl, entry);
174- return entry;
160+ return ret;
175161}
176162
177163codeql::EnumCaseDecl DeclVisitor::translateEnumCaseDecl (const swift::EnumCaseDecl& decl) {
178- EnumCaseDecl entry{dispatcher_. assignNewLabel (decl)} ;
164+ auto entry = createEntry (decl);
179165 entry.elements = dispatcher_.fetchRepeatedLabels (decl.getElements ());
180166 return entry;
181167}
182168
183169std::variant<codeql::EnumElementDecl, codeql::EnumElementDeclsTrap>
184170DeclVisitor::translateEnumElementDecl (const swift::EnumElementDecl& decl) {
185- auto id = dispatcher_.assignNewLabel (decl, mangledName (decl));
186- if (!dispatcher_.shouldEmitDeclBody (decl)) {
187- return EnumElementDeclsTrap{id, decl.getNameStr ().str ()};
188- }
189- EnumElementDecl entry{id};
190- entry.name = decl.getNameStr ().str ();
191- if (decl.hasParameterList ()) {
192- entry.params = dispatcher_.fetchRepeatedLabels (*decl.getParameterList ());
171+ auto ret = createNamedEntryOr<EnumElementDeclsTrap>(decl);
172+ std::visit ([&](auto & entry) { entry.name = decl.getNameStr ().str (); }, ret);
173+ if (auto entry = get_if<EnumElementDecl>(&ret)) {
174+ if (decl.hasParameterList ()) {
175+ entry->params = dispatcher_.fetchRepeatedLabels (*decl.getParameterList ());
176+ }
177+ fillValueDecl (decl, *entry);
193178 }
194- fillValueDecl (decl, entry);
195- return entry;
179+ return ret;
196180}
197181
198182codeql::GenericTypeParamDecl DeclVisitor::translateGenericTypeParamDecl (
199183 const swift::GenericTypeParamDecl& decl) {
200184 // TODO: deduplicate
201- GenericTypeParamDecl entry{dispatcher_. assignNewLabel (decl)} ;
185+ auto entry = createEntry (decl);
202186 fillTypeDecl (decl, entry);
203187 return entry;
204188}
205189
206190std::variant<codeql::AssociatedTypeDecl, codeql::AssociatedTypeDeclsTrap>
207191DeclVisitor::translateAssociatedTypeDecl (const swift::AssociatedTypeDecl& decl) {
208- auto id = dispatcher_. assignNewLabel (decl, mangledName (decl) );
209- if (!dispatcher_. shouldEmitDeclBody (decl )) {
210- return AssociatedTypeDeclsTrap{id} ;
192+ auto ret = createNamedEntryOr<AssociatedTypeDeclsTrap> (decl);
193+ if (auto entry = get_if<AssociatedTypeDecl>(&ret )) {
194+ fillTypeDecl (decl, *entry) ;
211195 }
212- AssociatedTypeDecl entry{id};
213- fillTypeDecl (decl, entry);
214- return entry;
196+ return ret;
215197}
216198
217199std::variant<codeql::TypeAliasDecl, codeql::TypeAliasDeclsTrap> DeclVisitor::translateTypeAliasDecl (
218200 const swift::TypeAliasDecl& decl) {
219- auto id = dispatcher_. assignNewLabel (decl, mangledName (decl) );
220- if (!dispatcher_. shouldEmitDeclBody (decl )) {
221- return TypeAliasDeclsTrap{id} ;
201+ auto ret = createNamedEntryOr<TypeAliasDeclsTrap> (decl);
202+ if (auto entry = get_if<TypeAliasDecl>(&ret )) {
203+ fillTypeDecl (decl, *entry) ;
222204 }
223- TypeAliasDecl entry{id};
224- fillTypeDecl (decl, entry);
225- return entry;
205+ return ret;
226206}
227207
228208std::variant<codeql::AccessorDecl, codeql::AccessorDeclsTrap> DeclVisitor::translateAccessorDecl (
229209 const swift::AccessorDecl& decl) {
230- auto id = dispatcher_.assignNewLabel (decl, mangledName (decl));
231- if (!dispatcher_.shouldEmitDeclBody (decl)) {
232- return AccessorDeclsTrap{id};
233- }
234- AccessorDecl entry{id};
235- switch (decl.getAccessorKind ()) {
236- case swift::AccessorKind::Get:
237- entry.is_getter = true ;
238- break ;
239- case swift::AccessorKind::Set:
240- entry.is_setter = true ;
241- break ;
242- case swift::AccessorKind::WillSet:
243- entry.is_will_set = true ;
244- break ;
245- case swift::AccessorKind::DidSet:
246- entry.is_did_set = true ;
247- break ;
210+ auto ret = createNamedEntryOr<AccessorDeclsTrap>(decl);
211+ if (auto entry = get_if<AccessorDecl>(&ret)) {
212+ switch (decl.getAccessorKind ()) {
213+ case swift::AccessorKind::Get:
214+ entry->is_getter = true ;
215+ break ;
216+ case swift::AccessorKind::Set:
217+ entry->is_setter = true ;
218+ break ;
219+ case swift::AccessorKind::WillSet:
220+ entry->is_will_set = true ;
221+ break ;
222+ case swift::AccessorKind::DidSet:
223+ entry->is_did_set = true ;
224+ break ;
225+ }
226+ fillAbstractFunctionDecl (decl, *entry);
248227 }
249- fillAbstractFunctionDecl (decl, entry);
250- return entry;
228+ return ret;
251229}
252230
253231std::optional<codeql::SubscriptDecl> DeclVisitor::translateSubscriptDecl (
@@ -265,17 +243,17 @@ std::optional<codeql::SubscriptDecl> DeclVisitor::translateSubscriptDecl(
265243}
266244
267245codeql::ExtensionDecl DeclVisitor::translateExtensionDecl (const swift::ExtensionDecl& decl) {
268- ExtensionDecl entry{dispatcher_. assignNewLabel (decl)} ;
246+ auto entry = createEntry (decl);
269247 entry.extended_type_decl = dispatcher_.fetchLabel (decl.getExtendedNominal ());
270248 fillGenericContext (decl, entry);
271249 fillIterableDeclContext (decl, entry);
272250 return entry;
273251}
274252
275253codeql::ImportDecl DeclVisitor::translateImportDecl (const swift::ImportDecl& decl) {
276- auto entry = dispatcher_. createEntry (decl);
254+ auto entry = createEntry (decl);
277255 entry.is_exported = decl.isExported ();
278- entry.module = dispatcher_.fetchLabel (decl.getModule ());
256+ entry.imported_module = dispatcher_.fetchLabel (decl.getModule ());
279257 entry.declarations = dispatcher_.fetchRepeatedLabels (decl.getDecls ());
280258 return entry;
281259}
@@ -391,7 +369,7 @@ void DeclVisitor::fillAbstractStorageDecl(const swift::AbstractStorageDecl& decl
391369}
392370
393371codeql::IfConfigDecl DeclVisitor::translateIfConfigDecl (const swift::IfConfigDecl& decl) {
394- auto entry = dispatcher_. createEntry (decl);
372+ auto entry = createEntry (decl);
395373 entry.clauses = dispatcher_.fetchRepeatedLabels (decl.getClauses ());
396374 return entry;
397375}
0 commit comments