1- #include " swift/extractor/visitors/DeclVisitor .h"
1+ #include " swift/extractor/translators/DeclTranslator .h"
22
33#include < swift/AST/GenericParamList.h>
44#include < swift/AST/ParameterList.h>
@@ -22,7 +22,7 @@ std::string constructName(const swift::DeclName& declName) {
2222}
2323} // namespace
2424
25- std::optional<codeql::ConcreteFuncDecl> DeclVisitor ::translateFuncDecl (
25+ std::optional<codeql::ConcreteFuncDecl> DeclTranslator ::translateFuncDecl (
2626 const swift::FuncDecl& decl) {
2727 if (auto entry = createNamedEntry (decl)) {
2828 fillAbstractFunctionDecl (decl, *entry);
@@ -31,7 +31,7 @@ std::optional<codeql::ConcreteFuncDecl> DeclVisitor::translateFuncDecl(
3131 return std::nullopt ;
3232}
3333
34- std::optional<codeql::ConstructorDecl> DeclVisitor ::translateConstructorDecl (
34+ std::optional<codeql::ConstructorDecl> DeclTranslator ::translateConstructorDecl (
3535 const swift::ConstructorDecl& decl) {
3636 if (auto entry = createNamedEntry (decl)) {
3737 fillAbstractFunctionDecl (decl, *entry);
@@ -40,7 +40,7 @@ std::optional<codeql::ConstructorDecl> DeclVisitor::translateConstructorDecl(
4040 return std::nullopt ;
4141}
4242
43- std::optional<codeql::DestructorDecl> DeclVisitor ::translateDestructorDecl (
43+ std::optional<codeql::DestructorDecl> DeclTranslator ::translateDestructorDecl (
4444 const swift::DestructorDecl& decl) {
4545 if (auto entry = createNamedEntry (decl)) {
4646 fillAbstractFunctionDecl (decl, *entry);
@@ -49,35 +49,35 @@ std::optional<codeql::DestructorDecl> DeclVisitor::translateDestructorDecl(
4949 return std::nullopt ;
5050}
5151
52- codeql::PrefixOperatorDecl DeclVisitor ::translatePrefixOperatorDecl (
52+ codeql::PrefixOperatorDecl DeclTranslator ::translatePrefixOperatorDecl (
5353 const swift::PrefixOperatorDecl& decl) {
5454 auto entry = createEntry (decl);
5555 fillOperatorDecl (decl, entry);
5656 return entry;
5757}
5858
59- codeql::PostfixOperatorDecl DeclVisitor ::translatePostfixOperatorDecl (
59+ codeql::PostfixOperatorDecl DeclTranslator ::translatePostfixOperatorDecl (
6060 const swift::PostfixOperatorDecl& decl) {
6161 auto entry = createEntry (decl);
6262 fillOperatorDecl (decl, entry);
6363 return entry;
6464}
6565
66- codeql::InfixOperatorDecl DeclVisitor ::translateInfixOperatorDecl (
66+ codeql::InfixOperatorDecl DeclTranslator ::translateInfixOperatorDecl (
6767 const swift::InfixOperatorDecl& decl) {
6868 auto entry = createEntry (decl);
6969 entry.precedence_group = dispatcher_.fetchOptionalLabel (decl.getPrecedenceGroup ());
7070 fillOperatorDecl (decl, entry);
7171 return entry;
7272}
7373
74- codeql::PrecedenceGroupDecl DeclVisitor ::translatePrecedenceGroupDecl (
74+ codeql::PrecedenceGroupDecl DeclTranslator ::translatePrecedenceGroupDecl (
7575 const swift::PrecedenceGroupDecl& decl) {
7676 auto entry = createEntry (decl);
7777 return entry;
7878}
7979
80- std::optional<codeql::ParamDecl> DeclVisitor ::translateParamDecl (const swift::ParamDecl& decl) {
80+ std::optional<codeql::ParamDecl> DeclTranslator ::translateParamDecl (const swift::ParamDecl& decl) {
8181 auto entry = createNamedEntry (decl);
8282 if (!entry) {
8383 return std::nullopt ;
@@ -87,15 +87,15 @@ std::optional<codeql::ParamDecl> DeclVisitor::translateParamDecl(const swift::Pa
8787 return entry;
8888}
8989
90- codeql::TopLevelCodeDecl DeclVisitor ::translateTopLevelCodeDecl (
90+ codeql::TopLevelCodeDecl DeclTranslator ::translateTopLevelCodeDecl (
9191 const swift::TopLevelCodeDecl& decl) {
9292 auto entry = createEntry (decl);
9393 assert (decl.getBody () && " Expect top level code to have body" );
9494 entry.body = dispatcher_.fetchLabel (decl.getBody ());
9595 return entry;
9696}
9797
98- codeql::PatternBindingDecl DeclVisitor ::translatePatternBindingDecl (
98+ codeql::PatternBindingDecl DeclTranslator ::translatePatternBindingDecl (
9999 const swift::PatternBindingDecl& decl) {
100100 auto entry = createEntry (decl);
101101 for (unsigned i = 0 ; i < decl.getNumPatternEntries (); ++i) {
@@ -107,7 +107,8 @@ codeql::PatternBindingDecl DeclVisitor::translatePatternBindingDecl(
107107 return entry;
108108}
109109
110- std::optional<codeql::ConcreteVarDecl> DeclVisitor::translateVarDecl (const swift::VarDecl& decl) {
110+ std::optional<codeql::ConcreteVarDecl> DeclTranslator::translateVarDecl (
111+ const swift::VarDecl& decl) {
111112 std::optional<codeql::ConcreteVarDecl> entry;
112113 // We do not deduplicate variables from non-swift (PCM, clang modules) modules as the mangler
113114 // crashes sometimes
@@ -124,31 +125,32 @@ std::optional<codeql::ConcreteVarDecl> DeclVisitor::translateVarDecl(const swift
124125 return entry;
125126}
126127
127- std::optional<codeql::StructDecl> DeclVisitor::translateStructDecl (const swift::StructDecl& decl) {
128+ std::optional<codeql::StructDecl> DeclTranslator::translateStructDecl (
129+ const swift::StructDecl& decl) {
128130 if (auto entry = createNamedEntry (decl)) {
129131 fillNominalTypeDecl (decl, *entry);
130132 return entry;
131133 }
132134 return std::nullopt ;
133135}
134136
135- std::optional<codeql::ClassDecl> DeclVisitor ::translateClassDecl (const swift::ClassDecl& decl) {
137+ std::optional<codeql::ClassDecl> DeclTranslator ::translateClassDecl (const swift::ClassDecl& decl) {
136138 if (auto entry = createNamedEntry (decl)) {
137139 fillNominalTypeDecl (decl, *entry);
138140 return entry;
139141 }
140142 return std::nullopt ;
141143}
142144
143- std::optional<codeql::EnumDecl> DeclVisitor ::translateEnumDecl (const swift::EnumDecl& decl) {
145+ std::optional<codeql::EnumDecl> DeclTranslator ::translateEnumDecl (const swift::EnumDecl& decl) {
144146 if (auto entry = createNamedEntry (decl)) {
145147 fillNominalTypeDecl (decl, *entry);
146148 return entry;
147149 }
148150 return std::nullopt ;
149151}
150152
151- std::optional<codeql::ProtocolDecl> DeclVisitor ::translateProtocolDecl (
153+ std::optional<codeql::ProtocolDecl> DeclTranslator ::translateProtocolDecl (
152154 const swift::ProtocolDecl& decl) {
153155 if (auto entry = createNamedEntry (decl)) {
154156 fillNominalTypeDecl (decl, *entry);
@@ -157,13 +159,13 @@ std::optional<codeql::ProtocolDecl> DeclVisitor::translateProtocolDecl(
157159 return std::nullopt ;
158160}
159161
160- codeql::EnumCaseDecl DeclVisitor ::translateEnumCaseDecl (const swift::EnumCaseDecl& decl) {
162+ codeql::EnumCaseDecl DeclTranslator ::translateEnumCaseDecl (const swift::EnumCaseDecl& decl) {
161163 auto entry = createEntry (decl);
162164 entry.elements = dispatcher_.fetchRepeatedLabels (decl.getElements ());
163165 return entry;
164166}
165167
166- std::optional<codeql::EnumElementDecl> DeclVisitor ::translateEnumElementDecl (
168+ std::optional<codeql::EnumElementDecl> DeclTranslator ::translateEnumElementDecl (
167169 const swift::EnumElementDecl& decl) {
168170 auto entry = createNamedEntry (decl);
169171 if (!entry) {
@@ -177,15 +179,15 @@ std::optional<codeql::EnumElementDecl> DeclVisitor::translateEnumElementDecl(
177179 return entry;
178180}
179181
180- codeql::GenericTypeParamDecl DeclVisitor ::translateGenericTypeParamDecl (
182+ codeql::GenericTypeParamDecl DeclTranslator ::translateGenericTypeParamDecl (
181183 const swift::GenericTypeParamDecl& decl) {
182184 // TODO: deduplicate
183185 auto entry = createEntry (decl);
184186 fillTypeDecl (decl, entry);
185187 return entry;
186188}
187189
188- std::optional<codeql::AssociatedTypeDecl> DeclVisitor ::translateAssociatedTypeDecl (
190+ std::optional<codeql::AssociatedTypeDecl> DeclTranslator ::translateAssociatedTypeDecl (
189191 const swift::AssociatedTypeDecl& decl) {
190192 if (auto entry = createNamedEntry (decl)) {
191193 fillTypeDecl (decl, *entry);
@@ -194,7 +196,7 @@ std::optional<codeql::AssociatedTypeDecl> DeclVisitor::translateAssociatedTypeDe
194196 return std::nullopt ;
195197}
196198
197- std::optional<codeql::TypeAliasDecl> DeclVisitor ::translateTypeAliasDecl (
199+ std::optional<codeql::TypeAliasDecl> DeclTranslator ::translateTypeAliasDecl (
198200 const swift::TypeAliasDecl& decl) {
199201 if (auto entry = createNamedEntry (decl)) {
200202 fillTypeDecl (decl, *entry);
@@ -203,7 +205,7 @@ std::optional<codeql::TypeAliasDecl> DeclVisitor::translateTypeAliasDecl(
203205 return std::nullopt ;
204206}
205207
206- std::optional<codeql::AccessorDecl> DeclVisitor ::translateAccessorDecl (
208+ std::optional<codeql::AccessorDecl> DeclTranslator ::translateAccessorDecl (
207209 const swift::AccessorDecl& decl) {
208210 auto entry = createNamedEntry (decl);
209211 if (!entry) {
@@ -227,7 +229,7 @@ std::optional<codeql::AccessorDecl> DeclVisitor::translateAccessorDecl(
227229 return entry;
228230}
229231
230- std::optional<codeql::SubscriptDecl> DeclVisitor ::translateSubscriptDecl (
232+ std::optional<codeql::SubscriptDecl> DeclTranslator ::translateSubscriptDecl (
231233 const swift::SubscriptDecl& decl) {
232234 auto entry = createNamedEntry (decl);
233235 if (!entry) {
@@ -241,23 +243,24 @@ std::optional<codeql::SubscriptDecl> DeclVisitor::translateSubscriptDecl(
241243 return entry;
242244}
243245
244- codeql::ExtensionDecl DeclVisitor ::translateExtensionDecl (const swift::ExtensionDecl& decl) {
246+ codeql::ExtensionDecl DeclTranslator ::translateExtensionDecl (const swift::ExtensionDecl& decl) {
245247 auto entry = createEntry (decl);
246248 entry.extended_type_decl = dispatcher_.fetchLabel (decl.getExtendedNominal ());
247249 fillGenericContext (decl, entry);
248250 fillIterableDeclContext (decl, entry);
249251 return entry;
250252}
251253
252- codeql::ImportDecl DeclVisitor ::translateImportDecl (const swift::ImportDecl& decl) {
254+ codeql::ImportDecl DeclTranslator ::translateImportDecl (const swift::ImportDecl& decl) {
253255 auto entry = createEntry (decl);
254256 entry.is_exported = decl.isExported ();
255257 entry.imported_module = dispatcher_.fetchOptionalLabel (decl.getModule ());
256258 entry.declarations = dispatcher_.fetchRepeatedLabels (decl.getDecls ());
257259 return entry;
258260}
259261
260- std::optional<codeql::ModuleDecl> DeclVisitor::translateModuleDecl (const swift::ModuleDecl& decl) {
262+ std::optional<codeql::ModuleDecl> DeclTranslator::translateModuleDecl (
263+ const swift::ModuleDecl& decl) {
261264 auto entry = createNamedEntry (decl);
262265 if (!entry) {
263266 return std::nullopt ;
@@ -279,7 +282,7 @@ std::optional<codeql::ModuleDecl> DeclVisitor::translateModuleDecl(const swift::
279282 return entry;
280283}
281284
282- std::string DeclVisitor ::mangledName (const swift::ValueDecl& decl) {
285+ std::string DeclTranslator ::mangledName (const swift::ValueDecl& decl) {
283286 // ASTMangler::mangleAnyDecl crashes when called on `ModuleDecl`
284287 // TODO find a more unique string working also when different modules are compiled with the same
285288 // name
@@ -309,8 +312,8 @@ std::string DeclVisitor::mangledName(const swift::ValueDecl& decl) {
309312 return ret.str ();
310313}
311314
312- void DeclVisitor ::fillAbstractFunctionDecl (const swift::AbstractFunctionDecl& decl,
313- codeql::AbstractFunctionDecl& entry) {
315+ void DeclTranslator ::fillAbstractFunctionDecl (const swift::AbstractFunctionDecl& decl,
316+ codeql::AbstractFunctionDecl& entry) {
314317 assert (decl.hasParameterList () && " Expect functions to have a parameter list" );
315318 entry.name = !decl.hasName () ? " (unnamed function decl)" : constructName (decl.getName ());
316319 entry.body = dispatcher_.fetchOptionalLabel (decl.getBody ());
@@ -321,11 +324,12 @@ void DeclVisitor::fillAbstractFunctionDecl(const swift::AbstractFunctionDecl& de
321324 fillGenericContext (decl, entry);
322325}
323326
324- void DeclVisitor::fillOperatorDecl (const swift::OperatorDecl& decl, codeql::OperatorDecl& entry) {
327+ void DeclTranslator::fillOperatorDecl (const swift::OperatorDecl& decl,
328+ codeql::OperatorDecl& entry) {
325329 entry.name = decl.getName ().str ().str ();
326330}
327331
328- void DeclVisitor ::fillTypeDecl (const swift::TypeDecl& decl, codeql::TypeDecl& entry) {
332+ void DeclTranslator ::fillTypeDecl (const swift::TypeDecl& decl, codeql::TypeDecl& entry) {
329333 entry.name = decl.getNameStr ().str ();
330334 for (auto & typeLoc : decl.getInherited ()) {
331335 if (auto type = typeLoc.getType ()) {
@@ -335,12 +339,12 @@ void DeclVisitor::fillTypeDecl(const swift::TypeDecl& decl, codeql::TypeDecl& en
335339 fillValueDecl (decl, entry);
336340}
337341
338- void DeclVisitor ::fillIterableDeclContext (const swift::IterableDeclContext& decl,
339- codeql::IterableDeclContext& entry) {
342+ void DeclTranslator ::fillIterableDeclContext (const swift::IterableDeclContext& decl,
343+ codeql::IterableDeclContext& entry) {
340344 entry.members = dispatcher_.fetchRepeatedLabels (decl.getAllMembers ());
341345}
342346
343- void DeclVisitor ::fillVarDecl (const swift::VarDecl& decl, codeql::VarDecl& entry) {
347+ void DeclTranslator ::fillVarDecl (const swift::VarDecl& decl, codeql::VarDecl& entry) {
344348 entry.name = decl.getNameStr ().str ();
345349 entry.type = dispatcher_.fetchLabel (decl.getType ());
346350 entry.parent_pattern = dispatcher_.fetchOptionalLabel (decl.getParentPattern ());
@@ -352,33 +356,33 @@ void DeclVisitor::fillVarDecl(const swift::VarDecl& decl, codeql::VarDecl& entry
352356 fillAbstractStorageDecl (decl, entry);
353357}
354358
355- void DeclVisitor ::fillNominalTypeDecl (const swift::NominalTypeDecl& decl,
356- codeql::NominalTypeDecl& entry) {
359+ void DeclTranslator ::fillNominalTypeDecl (const swift::NominalTypeDecl& decl,
360+ codeql::NominalTypeDecl& entry) {
357361 entry.type = dispatcher_.fetchLabel (decl.getDeclaredType ());
358362 fillGenericContext (decl, entry);
359363 fillIterableDeclContext (decl, entry);
360364 fillTypeDecl (decl, entry);
361365}
362366
363- void DeclVisitor ::fillGenericContext (const swift::GenericContext& decl,
364- codeql::GenericContext& entry) {
367+ void DeclTranslator ::fillGenericContext (const swift::GenericContext& decl,
368+ codeql::GenericContext& entry) {
365369 if (auto params = decl.getGenericParams ()) {
366370 entry.generic_type_params = dispatcher_.fetchRepeatedLabels (*params);
367371 }
368372}
369373
370- void DeclVisitor ::fillValueDecl (const swift::ValueDecl& decl, codeql::ValueDecl& entry) {
374+ void DeclTranslator ::fillValueDecl (const swift::ValueDecl& decl, codeql::ValueDecl& entry) {
371375 assert (decl.getInterfaceType () && " Expect ValueDecl to have InterfaceType" );
372376 entry.interface_type = dispatcher_.fetchLabel (decl.getInterfaceType ());
373377}
374378
375- void DeclVisitor ::fillAbstractStorageDecl (const swift::AbstractStorageDecl& decl,
376- codeql::AbstractStorageDecl& entry) {
379+ void DeclTranslator ::fillAbstractStorageDecl (const swift::AbstractStorageDecl& decl,
380+ codeql::AbstractStorageDecl& entry) {
377381 entry.accessor_decls = dispatcher_.fetchRepeatedLabels (decl.getAllAccessors ());
378382 fillValueDecl (decl, entry);
379383}
380384
381- codeql::IfConfigDecl DeclVisitor ::translateIfConfigDecl (const swift::IfConfigDecl& decl) {
385+ codeql::IfConfigDecl DeclTranslator ::translateIfConfigDecl (const swift::IfConfigDecl& decl) {
382386 auto entry = createEntry (decl);
383387 if (auto activeClause = decl.getActiveClause ()) {
384388 entry.active_elements = dispatcher_.fetchRepeatedLabels (activeClause->Elements );
0 commit comments