@@ -24,62 +24,56 @@ std::string constructName(const swift::DeclName& declName) {
24
24
25
25
std::variant<codeql::ConcreteFuncDecl, codeql::ConcreteFuncDeclsTrap>
26
26
DeclVisitor::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) ;
30
30
}
31
- ConcreteFuncDecl entry{id};
32
- fillAbstractFunctionDecl (decl, entry);
33
- return entry;
31
+ return ret;
34
32
}
35
33
36
34
std::variant<codeql::ConstructorDecl, codeql::ConstructorDeclsTrap>
37
35
DeclVisitor::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) ;
41
39
}
42
- ConstructorDecl entry{id};
43
- fillAbstractFunctionDecl (decl, entry);
44
- return entry;
40
+ return ret;
45
41
}
46
42
47
43
std::variant<codeql::DestructorDecl, codeql::DestructorDeclsTrap>
48
44
DeclVisitor::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) ;
52
48
}
53
- DestructorDecl entry{id};
54
- fillAbstractFunctionDecl (decl, entry);
55
- return entry;
49
+ return ret;
56
50
}
57
51
58
52
codeql::PrefixOperatorDecl DeclVisitor::translatePrefixOperatorDecl (
59
53
const swift::PrefixOperatorDecl& decl) {
60
- PrefixOperatorDecl entry{dispatcher_. assignNewLabel (decl)} ;
54
+ auto entry = createEntry (decl);
61
55
fillOperatorDecl (decl, entry);
62
56
return entry;
63
57
}
64
58
65
59
codeql::PostfixOperatorDecl DeclVisitor::translatePostfixOperatorDecl (
66
60
const swift::PostfixOperatorDecl& decl) {
67
- PostfixOperatorDecl entry{dispatcher_. assignNewLabel (decl)} ;
61
+ auto entry = createEntry (decl);
68
62
fillOperatorDecl (decl, entry);
69
63
return entry;
70
64
}
71
65
72
66
codeql::InfixOperatorDecl DeclVisitor::translateInfixOperatorDecl (
73
67
const swift::InfixOperatorDecl& decl) {
74
- InfixOperatorDecl entry{dispatcher_. assignNewLabel (decl)} ;
68
+ auto entry = createEntry (decl);
75
69
entry.precedence_group = dispatcher_.fetchOptionalLabel (decl.getPrecedenceGroup ());
76
70
fillOperatorDecl (decl, entry);
77
71
return entry;
78
72
}
79
73
80
74
codeql::PrecedenceGroupDecl DeclVisitor::translatePrecedenceGroupDecl (
81
75
const swift::PrecedenceGroupDecl& decl) {
82
- PrecedenceGroupDecl entry{dispatcher_. assignNewLabel (decl)} ;
76
+ auto entry = createEntry (decl);
83
77
return entry;
84
78
}
85
79
@@ -95,15 +89,15 @@ std::optional<codeql::ParamDecl> DeclVisitor::translateParamDecl(const swift::Pa
95
89
96
90
codeql::TopLevelCodeDecl DeclVisitor::translateTopLevelCodeDecl (
97
91
const swift::TopLevelCodeDecl& decl) {
98
- TopLevelCodeDecl entry{dispatcher_. assignNewLabel (decl)} ;
92
+ auto entry = createEntry (decl);
99
93
assert (decl.getBody () && " Expect top level code to have body" );
100
94
entry.body = dispatcher_.fetchLabel (decl.getBody ());
101
95
return entry;
102
96
}
103
97
104
98
codeql::PatternBindingDecl DeclVisitor::translatePatternBindingDecl (
105
99
const swift::PatternBindingDecl& decl) {
106
- PatternBindingDecl entry{dispatcher_. assignNewLabel (decl)} ;
100
+ auto entry = createEntry (decl);
107
101
for (unsigned i = 0 ; i < decl.getNumPatternEntries (); ++i) {
108
102
auto pattern = decl.getPattern (i);
109
103
assert (pattern && " Expect pattern binding decl to have all patterns" );
@@ -118,7 +112,7 @@ std::optional<codeql::ConcreteVarDecl> DeclVisitor::translateVarDecl(const swift
118
112
// We do not deduplicate variables from non-swift (PCM, clang modules) modules as the mangler
119
113
// crashes sometimes
120
114
if (decl.getDeclContext ()->isLocalContext () || decl.getModuleContext ()->isNonSwiftModule ()) {
121
- entry. emplace (dispatcher_. assignNewLabel ( decl) );
115
+ entry = createEntry ( decl);
122
116
} else {
123
117
entry = createNamedEntry (decl);
124
118
if (!entry) {
@@ -132,122 +126,106 @@ std::optional<codeql::ConcreteVarDecl> DeclVisitor::translateVarDecl(const swift
132
126
133
127
std::variant<codeql::StructDecl, codeql::StructDeclsTrap> DeclVisitor::translateStructDecl (
134
128
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) ;
138
132
}
139
- StructDecl entry{id};
140
- fillNominalTypeDecl (decl, entry);
141
- return entry;
133
+ return ret;
142
134
}
143
135
144
136
std::variant<codeql::ClassDecl, codeql::ClassDeclsTrap> DeclVisitor::translateClassDecl (
145
137
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) ;
149
141
}
150
- ClassDecl entry{id};
151
- fillNominalTypeDecl (decl, entry);
152
- return entry;
142
+ return ret;
153
143
}
154
144
155
145
std::variant<codeql::EnumDecl, codeql::EnumDeclsTrap> DeclVisitor::translateEnumDecl (
156
146
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) ;
160
150
}
161
- EnumDecl entry{id};
162
- fillNominalTypeDecl (decl, entry);
163
- return entry;
151
+ return ret;
164
152
}
165
153
166
154
std::variant<codeql::ProtocolDecl, codeql::ProtocolDeclsTrap> DeclVisitor::translateProtocolDecl (
167
155
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) ;
171
159
}
172
- ProtocolDecl entry{id};
173
- fillNominalTypeDecl (decl, entry);
174
- return entry;
160
+ return ret;
175
161
}
176
162
177
163
codeql::EnumCaseDecl DeclVisitor::translateEnumCaseDecl (const swift::EnumCaseDecl& decl) {
178
- EnumCaseDecl entry{dispatcher_. assignNewLabel (decl)} ;
164
+ auto entry = createEntry (decl);
179
165
entry.elements = dispatcher_.fetchRepeatedLabels (decl.getElements ());
180
166
return entry;
181
167
}
182
168
183
169
std::variant<codeql::EnumElementDecl, codeql::EnumElementDeclsTrap>
184
170
DeclVisitor::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);
193
178
}
194
- fillValueDecl (decl, entry);
195
- return entry;
179
+ return ret;
196
180
}
197
181
198
182
codeql::GenericTypeParamDecl DeclVisitor::translateGenericTypeParamDecl (
199
183
const swift::GenericTypeParamDecl& decl) {
200
184
// TODO: deduplicate
201
- GenericTypeParamDecl entry{dispatcher_. assignNewLabel (decl)} ;
185
+ auto entry = createEntry (decl);
202
186
fillTypeDecl (decl, entry);
203
187
return entry;
204
188
}
205
189
206
190
std::variant<codeql::AssociatedTypeDecl, codeql::AssociatedTypeDeclsTrap>
207
191
DeclVisitor::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) ;
211
195
}
212
- AssociatedTypeDecl entry{id};
213
- fillTypeDecl (decl, entry);
214
- return entry;
196
+ return ret;
215
197
}
216
198
217
199
std::variant<codeql::TypeAliasDecl, codeql::TypeAliasDeclsTrap> DeclVisitor::translateTypeAliasDecl (
218
200
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) ;
222
204
}
223
- TypeAliasDecl entry{id};
224
- fillTypeDecl (decl, entry);
225
- return entry;
205
+ return ret;
226
206
}
227
207
228
208
std::variant<codeql::AccessorDecl, codeql::AccessorDeclsTrap> DeclVisitor::translateAccessorDecl (
229
209
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);
248
227
}
249
- fillAbstractFunctionDecl (decl, entry);
250
- return entry;
228
+ return ret;
251
229
}
252
230
253
231
std::optional<codeql::SubscriptDecl> DeclVisitor::translateSubscriptDecl (
@@ -265,17 +243,17 @@ std::optional<codeql::SubscriptDecl> DeclVisitor::translateSubscriptDecl(
265
243
}
266
244
267
245
codeql::ExtensionDecl DeclVisitor::translateExtensionDecl (const swift::ExtensionDecl& decl) {
268
- ExtensionDecl entry{dispatcher_. assignNewLabel (decl)} ;
246
+ auto entry = createEntry (decl);
269
247
entry.extended_type_decl = dispatcher_.fetchLabel (decl.getExtendedNominal ());
270
248
fillGenericContext (decl, entry);
271
249
fillIterableDeclContext (decl, entry);
272
250
return entry;
273
251
}
274
252
275
253
codeql::ImportDecl DeclVisitor::translateImportDecl (const swift::ImportDecl& decl) {
276
- auto entry = dispatcher_. createEntry (decl);
254
+ auto entry = createEntry (decl);
277
255
entry.is_exported = decl.isExported ();
278
- entry.module = dispatcher_.fetchLabel (decl.getModule ());
256
+ entry.imported_module = dispatcher_.fetchLabel (decl.getModule ());
279
257
entry.declarations = dispatcher_.fetchRepeatedLabels (decl.getDecls ());
280
258
return entry;
281
259
}
@@ -391,7 +369,7 @@ void DeclVisitor::fillAbstractStorageDecl(const swift::AbstractStorageDecl& decl
391
369
}
392
370
393
371
codeql::IfConfigDecl DeclVisitor::translateIfConfigDecl (const swift::IfConfigDecl& decl) {
394
- auto entry = dispatcher_. createEntry (decl);
372
+ auto entry = createEntry (decl);
395
373
entry.clauses = dispatcher_.fetchRepeatedLabels (decl.getClauses ());
396
374
return entry;
397
375
}
0 commit comments