1
- #include " swift/extractor/visitors/DeclVisitor .h"
1
+ #include " swift/extractor/translators/DeclTranslator .h"
2
2
3
3
#include < swift/AST/GenericParamList.h>
4
4
#include < swift/AST/ParameterList.h>
@@ -22,7 +22,7 @@ std::string constructName(const swift::DeclName& declName) {
22
22
}
23
23
} // namespace
24
24
25
- std::optional<codeql::ConcreteFuncDecl> DeclVisitor ::translateFuncDecl (
25
+ std::optional<codeql::ConcreteFuncDecl> DeclTranslator ::translateFuncDecl (
26
26
const swift::FuncDecl& decl) {
27
27
if (auto entry = createNamedEntry (decl)) {
28
28
fillAbstractFunctionDecl (decl, *entry);
@@ -31,7 +31,7 @@ std::optional<codeql::ConcreteFuncDecl> DeclVisitor::translateFuncDecl(
31
31
return std::nullopt;
32
32
}
33
33
34
- std::optional<codeql::ConstructorDecl> DeclVisitor ::translateConstructorDecl (
34
+ std::optional<codeql::ConstructorDecl> DeclTranslator ::translateConstructorDecl (
35
35
const swift::ConstructorDecl& decl) {
36
36
if (auto entry = createNamedEntry (decl)) {
37
37
fillAbstractFunctionDecl (decl, *entry);
@@ -40,7 +40,7 @@ std::optional<codeql::ConstructorDecl> DeclVisitor::translateConstructorDecl(
40
40
return std::nullopt;
41
41
}
42
42
43
- std::optional<codeql::DestructorDecl> DeclVisitor ::translateDestructorDecl (
43
+ std::optional<codeql::DestructorDecl> DeclTranslator ::translateDestructorDecl (
44
44
const swift::DestructorDecl& decl) {
45
45
if (auto entry = createNamedEntry (decl)) {
46
46
fillAbstractFunctionDecl (decl, *entry);
@@ -49,35 +49,35 @@ std::optional<codeql::DestructorDecl> DeclVisitor::translateDestructorDecl(
49
49
return std::nullopt;
50
50
}
51
51
52
- codeql::PrefixOperatorDecl DeclVisitor ::translatePrefixOperatorDecl (
52
+ codeql::PrefixOperatorDecl DeclTranslator ::translatePrefixOperatorDecl (
53
53
const swift::PrefixOperatorDecl& decl) {
54
54
auto entry = createEntry (decl);
55
55
fillOperatorDecl (decl, entry);
56
56
return entry;
57
57
}
58
58
59
- codeql::PostfixOperatorDecl DeclVisitor ::translatePostfixOperatorDecl (
59
+ codeql::PostfixOperatorDecl DeclTranslator ::translatePostfixOperatorDecl (
60
60
const swift::PostfixOperatorDecl& decl) {
61
61
auto entry = createEntry (decl);
62
62
fillOperatorDecl (decl, entry);
63
63
return entry;
64
64
}
65
65
66
- codeql::InfixOperatorDecl DeclVisitor ::translateInfixOperatorDecl (
66
+ codeql::InfixOperatorDecl DeclTranslator ::translateInfixOperatorDecl (
67
67
const swift::InfixOperatorDecl& decl) {
68
68
auto entry = createEntry (decl);
69
69
entry.precedence_group = dispatcher_.fetchOptionalLabel (decl.getPrecedenceGroup ());
70
70
fillOperatorDecl (decl, entry);
71
71
return entry;
72
72
}
73
73
74
- codeql::PrecedenceGroupDecl DeclVisitor ::translatePrecedenceGroupDecl (
74
+ codeql::PrecedenceGroupDecl DeclTranslator ::translatePrecedenceGroupDecl (
75
75
const swift::PrecedenceGroupDecl& decl) {
76
76
auto entry = createEntry (decl);
77
77
return entry;
78
78
}
79
79
80
- std::optional<codeql::ParamDecl> DeclVisitor ::translateParamDecl (const swift::ParamDecl& decl) {
80
+ std::optional<codeql::ParamDecl> DeclTranslator ::translateParamDecl (const swift::ParamDecl& decl) {
81
81
auto entry = createNamedEntry (decl);
82
82
if (!entry) {
83
83
return std::nullopt;
@@ -87,15 +87,15 @@ std::optional<codeql::ParamDecl> DeclVisitor::translateParamDecl(const swift::Pa
87
87
return entry;
88
88
}
89
89
90
- codeql::TopLevelCodeDecl DeclVisitor ::translateTopLevelCodeDecl (
90
+ codeql::TopLevelCodeDecl DeclTranslator ::translateTopLevelCodeDecl (
91
91
const swift::TopLevelCodeDecl& decl) {
92
92
auto entry = createEntry (decl);
93
93
assert (decl.getBody () && " Expect top level code to have body" );
94
94
entry.body = dispatcher_.fetchLabel (decl.getBody ());
95
95
return entry;
96
96
}
97
97
98
- codeql::PatternBindingDecl DeclVisitor ::translatePatternBindingDecl (
98
+ codeql::PatternBindingDecl DeclTranslator ::translatePatternBindingDecl (
99
99
const swift::PatternBindingDecl& decl) {
100
100
auto entry = createEntry (decl);
101
101
for (unsigned i = 0 ; i < decl.getNumPatternEntries (); ++i) {
@@ -107,7 +107,8 @@ codeql::PatternBindingDecl DeclVisitor::translatePatternBindingDecl(
107
107
return entry;
108
108
}
109
109
110
- std::optional<codeql::ConcreteVarDecl> DeclVisitor::translateVarDecl (const swift::VarDecl& decl) {
110
+ std::optional<codeql::ConcreteVarDecl> DeclTranslator::translateVarDecl (
111
+ const swift::VarDecl& decl) {
111
112
std::optional<codeql::ConcreteVarDecl> entry;
112
113
// We do not deduplicate variables from non-swift (PCM, clang modules) modules as the mangler
113
114
// crashes sometimes
@@ -124,31 +125,32 @@ std::optional<codeql::ConcreteVarDecl> DeclVisitor::translateVarDecl(const swift
124
125
return entry;
125
126
}
126
127
127
- std::optional<codeql::StructDecl> DeclVisitor::translateStructDecl (const swift::StructDecl& decl) {
128
+ std::optional<codeql::StructDecl> DeclTranslator::translateStructDecl (
129
+ const swift::StructDecl& decl) {
128
130
if (auto entry = createNamedEntry (decl)) {
129
131
fillNominalTypeDecl (decl, *entry);
130
132
return entry;
131
133
}
132
134
return std::nullopt;
133
135
}
134
136
135
- std::optional<codeql::ClassDecl> DeclVisitor ::translateClassDecl (const swift::ClassDecl& decl) {
137
+ std::optional<codeql::ClassDecl> DeclTranslator ::translateClassDecl (const swift::ClassDecl& decl) {
136
138
if (auto entry = createNamedEntry (decl)) {
137
139
fillNominalTypeDecl (decl, *entry);
138
140
return entry;
139
141
}
140
142
return std::nullopt;
141
143
}
142
144
143
- std::optional<codeql::EnumDecl> DeclVisitor ::translateEnumDecl (const swift::EnumDecl& decl) {
145
+ std::optional<codeql::EnumDecl> DeclTranslator ::translateEnumDecl (const swift::EnumDecl& decl) {
144
146
if (auto entry = createNamedEntry (decl)) {
145
147
fillNominalTypeDecl (decl, *entry);
146
148
return entry;
147
149
}
148
150
return std::nullopt;
149
151
}
150
152
151
- std::optional<codeql::ProtocolDecl> DeclVisitor ::translateProtocolDecl (
153
+ std::optional<codeql::ProtocolDecl> DeclTranslator ::translateProtocolDecl (
152
154
const swift::ProtocolDecl& decl) {
153
155
if (auto entry = createNamedEntry (decl)) {
154
156
fillNominalTypeDecl (decl, *entry);
@@ -157,13 +159,13 @@ std::optional<codeql::ProtocolDecl> DeclVisitor::translateProtocolDecl(
157
159
return std::nullopt;
158
160
}
159
161
160
- codeql::EnumCaseDecl DeclVisitor ::translateEnumCaseDecl (const swift::EnumCaseDecl& decl) {
162
+ codeql::EnumCaseDecl DeclTranslator ::translateEnumCaseDecl (const swift::EnumCaseDecl& decl) {
161
163
auto entry = createEntry (decl);
162
164
entry.elements = dispatcher_.fetchRepeatedLabels (decl.getElements ());
163
165
return entry;
164
166
}
165
167
166
- std::optional<codeql::EnumElementDecl> DeclVisitor ::translateEnumElementDecl (
168
+ std::optional<codeql::EnumElementDecl> DeclTranslator ::translateEnumElementDecl (
167
169
const swift::EnumElementDecl& decl) {
168
170
auto entry = createNamedEntry (decl);
169
171
if (!entry) {
@@ -177,15 +179,15 @@ std::optional<codeql::EnumElementDecl> DeclVisitor::translateEnumElementDecl(
177
179
return entry;
178
180
}
179
181
180
- codeql::GenericTypeParamDecl DeclVisitor ::translateGenericTypeParamDecl (
182
+ codeql::GenericTypeParamDecl DeclTranslator ::translateGenericTypeParamDecl (
181
183
const swift::GenericTypeParamDecl& decl) {
182
184
// TODO: deduplicate
183
185
auto entry = createEntry (decl);
184
186
fillTypeDecl (decl, entry);
185
187
return entry;
186
188
}
187
189
188
- std::optional<codeql::AssociatedTypeDecl> DeclVisitor ::translateAssociatedTypeDecl (
190
+ std::optional<codeql::AssociatedTypeDecl> DeclTranslator ::translateAssociatedTypeDecl (
189
191
const swift::AssociatedTypeDecl& decl) {
190
192
if (auto entry = createNamedEntry (decl)) {
191
193
fillTypeDecl (decl, *entry);
@@ -194,7 +196,7 @@ std::optional<codeql::AssociatedTypeDecl> DeclVisitor::translateAssociatedTypeDe
194
196
return std::nullopt;
195
197
}
196
198
197
- std::optional<codeql::TypeAliasDecl> DeclVisitor ::translateTypeAliasDecl (
199
+ std::optional<codeql::TypeAliasDecl> DeclTranslator ::translateTypeAliasDecl (
198
200
const swift::TypeAliasDecl& decl) {
199
201
if (auto entry = createNamedEntry (decl)) {
200
202
fillTypeDecl (decl, *entry);
@@ -203,7 +205,7 @@ std::optional<codeql::TypeAliasDecl> DeclVisitor::translateTypeAliasDecl(
203
205
return std::nullopt;
204
206
}
205
207
206
- std::optional<codeql::AccessorDecl> DeclVisitor ::translateAccessorDecl (
208
+ std::optional<codeql::AccessorDecl> DeclTranslator ::translateAccessorDecl (
207
209
const swift::AccessorDecl& decl) {
208
210
auto entry = createNamedEntry (decl);
209
211
if (!entry) {
@@ -227,7 +229,7 @@ std::optional<codeql::AccessorDecl> DeclVisitor::translateAccessorDecl(
227
229
return entry;
228
230
}
229
231
230
- std::optional<codeql::SubscriptDecl> DeclVisitor ::translateSubscriptDecl (
232
+ std::optional<codeql::SubscriptDecl> DeclTranslator ::translateSubscriptDecl (
231
233
const swift::SubscriptDecl& decl) {
232
234
auto entry = createNamedEntry (decl);
233
235
if (!entry) {
@@ -241,23 +243,24 @@ std::optional<codeql::SubscriptDecl> DeclVisitor::translateSubscriptDecl(
241
243
return entry;
242
244
}
243
245
244
- codeql::ExtensionDecl DeclVisitor ::translateExtensionDecl (const swift::ExtensionDecl& decl) {
246
+ codeql::ExtensionDecl DeclTranslator ::translateExtensionDecl (const swift::ExtensionDecl& decl) {
245
247
auto entry = createEntry (decl);
246
248
entry.extended_type_decl = dispatcher_.fetchLabel (decl.getExtendedNominal ());
247
249
fillGenericContext (decl, entry);
248
250
fillIterableDeclContext (decl, entry);
249
251
return entry;
250
252
}
251
253
252
- codeql::ImportDecl DeclVisitor ::translateImportDecl (const swift::ImportDecl& decl) {
254
+ codeql::ImportDecl DeclTranslator ::translateImportDecl (const swift::ImportDecl& decl) {
253
255
auto entry = createEntry (decl);
254
256
entry.is_exported = decl.isExported ();
255
257
entry.imported_module = dispatcher_.fetchOptionalLabel (decl.getModule ());
256
258
entry.declarations = dispatcher_.fetchRepeatedLabels (decl.getDecls ());
257
259
return entry;
258
260
}
259
261
260
- std::optional<codeql::ModuleDecl> DeclVisitor::translateModuleDecl (const swift::ModuleDecl& decl) {
262
+ std::optional<codeql::ModuleDecl> DeclTranslator::translateModuleDecl (
263
+ const swift::ModuleDecl& decl) {
261
264
auto entry = createNamedEntry (decl);
262
265
if (!entry) {
263
266
return std::nullopt;
@@ -279,7 +282,7 @@ std::optional<codeql::ModuleDecl> DeclVisitor::translateModuleDecl(const swift::
279
282
return entry;
280
283
}
281
284
282
- std::string DeclVisitor ::mangledName (const swift::ValueDecl& decl) {
285
+ std::string DeclTranslator ::mangledName (const swift::ValueDecl& decl) {
283
286
// ASTMangler::mangleAnyDecl crashes when called on `ModuleDecl`
284
287
// TODO find a more unique string working also when different modules are compiled with the same
285
288
// name
@@ -309,8 +312,8 @@ std::string DeclVisitor::mangledName(const swift::ValueDecl& decl) {
309
312
return ret.str ();
310
313
}
311
314
312
- void DeclVisitor ::fillAbstractFunctionDecl (const swift::AbstractFunctionDecl& decl,
313
- codeql::AbstractFunctionDecl& entry) {
315
+ void DeclTranslator ::fillAbstractFunctionDecl (const swift::AbstractFunctionDecl& decl,
316
+ codeql::AbstractFunctionDecl& entry) {
314
317
assert (decl.hasParameterList () && " Expect functions to have a parameter list" );
315
318
entry.name = !decl.hasName () ? " (unnamed function decl)" : constructName (decl.getName ());
316
319
entry.body = dispatcher_.fetchOptionalLabel (decl.getBody ());
@@ -321,11 +324,12 @@ void DeclVisitor::fillAbstractFunctionDecl(const swift::AbstractFunctionDecl& de
321
324
fillGenericContext (decl, entry);
322
325
}
323
326
324
- void DeclVisitor::fillOperatorDecl (const swift::OperatorDecl& decl, codeql::OperatorDecl& entry) {
327
+ void DeclTranslator::fillOperatorDecl (const swift::OperatorDecl& decl,
328
+ codeql::OperatorDecl& entry) {
325
329
entry.name = decl.getName ().str ().str ();
326
330
}
327
331
328
- void DeclVisitor ::fillTypeDecl (const swift::TypeDecl& decl, codeql::TypeDecl& entry) {
332
+ void DeclTranslator ::fillTypeDecl (const swift::TypeDecl& decl, codeql::TypeDecl& entry) {
329
333
entry.name = decl.getNameStr ().str ();
330
334
for (auto & typeLoc : decl.getInherited ()) {
331
335
if (auto type = typeLoc.getType ()) {
@@ -335,12 +339,12 @@ void DeclVisitor::fillTypeDecl(const swift::TypeDecl& decl, codeql::TypeDecl& en
335
339
fillValueDecl (decl, entry);
336
340
}
337
341
338
- void DeclVisitor ::fillIterableDeclContext (const swift::IterableDeclContext& decl,
339
- codeql::IterableDeclContext& entry) {
342
+ void DeclTranslator ::fillIterableDeclContext (const swift::IterableDeclContext& decl,
343
+ codeql::IterableDeclContext& entry) {
340
344
entry.members = dispatcher_.fetchRepeatedLabels (decl.getAllMembers ());
341
345
}
342
346
343
- void DeclVisitor ::fillVarDecl (const swift::VarDecl& decl, codeql::VarDecl& entry) {
347
+ void DeclTranslator ::fillVarDecl (const swift::VarDecl& decl, codeql::VarDecl& entry) {
344
348
entry.name = decl.getNameStr ().str ();
345
349
entry.type = dispatcher_.fetchLabel (decl.getType ());
346
350
entry.parent_pattern = dispatcher_.fetchOptionalLabel (decl.getParentPattern ());
@@ -352,33 +356,33 @@ void DeclVisitor::fillVarDecl(const swift::VarDecl& decl, codeql::VarDecl& entry
352
356
fillAbstractStorageDecl (decl, entry);
353
357
}
354
358
355
- void DeclVisitor ::fillNominalTypeDecl (const swift::NominalTypeDecl& decl,
356
- codeql::NominalTypeDecl& entry) {
359
+ void DeclTranslator ::fillNominalTypeDecl (const swift::NominalTypeDecl& decl,
360
+ codeql::NominalTypeDecl& entry) {
357
361
entry.type = dispatcher_.fetchLabel (decl.getDeclaredType ());
358
362
fillGenericContext (decl, entry);
359
363
fillIterableDeclContext (decl, entry);
360
364
fillTypeDecl (decl, entry);
361
365
}
362
366
363
- void DeclVisitor ::fillGenericContext (const swift::GenericContext& decl,
364
- codeql::GenericContext& entry) {
367
+ void DeclTranslator ::fillGenericContext (const swift::GenericContext& decl,
368
+ codeql::GenericContext& entry) {
365
369
if (auto params = decl.getGenericParams ()) {
366
370
entry.generic_type_params = dispatcher_.fetchRepeatedLabels (*params);
367
371
}
368
372
}
369
373
370
- void DeclVisitor ::fillValueDecl (const swift::ValueDecl& decl, codeql::ValueDecl& entry) {
374
+ void DeclTranslator ::fillValueDecl (const swift::ValueDecl& decl, codeql::ValueDecl& entry) {
371
375
assert (decl.getInterfaceType () && " Expect ValueDecl to have InterfaceType" );
372
376
entry.interface_type = dispatcher_.fetchLabel (decl.getInterfaceType ());
373
377
}
374
378
375
- void DeclVisitor ::fillAbstractStorageDecl (const swift::AbstractStorageDecl& decl,
376
- codeql::AbstractStorageDecl& entry) {
379
+ void DeclTranslator ::fillAbstractStorageDecl (const swift::AbstractStorageDecl& decl,
380
+ codeql::AbstractStorageDecl& entry) {
377
381
entry.accessor_decls = dispatcher_.fetchRepeatedLabels (decl.getAllAccessors ());
378
382
fillValueDecl (decl, entry);
379
383
}
380
384
381
- codeql::IfConfigDecl DeclVisitor ::translateIfConfigDecl (const swift::IfConfigDecl& decl) {
385
+ codeql::IfConfigDecl DeclTranslator ::translateIfConfigDecl (const swift::IfConfigDecl& decl) {
382
386
auto entry = createEntry (decl);
383
387
if (auto activeClause = decl.getActiveClause ()) {
384
388
entry.active_elements = dispatcher_.fetchRepeatedLabels (activeClause->Elements );
0 commit comments