@@ -22,64 +22,58 @@ std::string constructName(const swift::DeclName& declName) {
22
22
}
23
23
} // namespace
24
24
25
- std::variant <codeql::ConcreteFuncDecl, codeql::ConcreteFuncDeclsTrap>
26
- DeclVisitor::translateFuncDecl ( const swift::FuncDecl& decl) {
27
- auto id = dispatcher_. assignNewLabel (decl, mangledName (decl));
28
- if (!dispatcher_. shouldEmitDeclBody ( decl)) {
29
- return ConcreteFuncDeclsTrap{id} ;
25
+ std::optional <codeql::ConcreteFuncDecl> DeclVisitor::translateFuncDecl (
26
+ const swift::FuncDecl& decl) {
27
+ if ( auto entry = createNamedEntry (decl)) {
28
+ fillAbstractFunctionDecl ( decl, *entry);
29
+ return entry ;
30
30
}
31
- ConcreteFuncDecl entry{id};
32
- fillAbstractFunctionDecl (decl, entry);
33
- return entry;
31
+ return std::nullopt;
34
32
}
35
33
36
- std::variant <codeql::ConstructorDecl, codeql::ConstructorDeclsTrap>
37
- DeclVisitor::translateConstructorDecl ( const swift::ConstructorDecl& decl) {
38
- auto id = dispatcher_. assignNewLabel (decl, mangledName (decl));
39
- if (!dispatcher_. shouldEmitDeclBody ( decl)) {
40
- return ConstructorDeclsTrap{id} ;
34
+ std::optional <codeql::ConstructorDecl> DeclVisitor::translateConstructorDecl (
35
+ const swift::ConstructorDecl& decl) {
36
+ if ( auto entry = createNamedEntry (decl)) {
37
+ fillAbstractFunctionDecl ( decl, *entry);
38
+ return entry ;
41
39
}
42
- ConstructorDecl entry{id};
43
- fillAbstractFunctionDecl (decl, entry);
44
- return entry;
40
+ return std::nullopt;
45
41
}
46
42
47
- std::variant <codeql::DestructorDecl, codeql::DestructorDeclsTrap>
48
- DeclVisitor::translateDestructorDecl ( const swift::DestructorDecl& decl) {
49
- auto id = dispatcher_. assignNewLabel (decl, mangledName (decl));
50
- if (!dispatcher_. shouldEmitDeclBody ( decl)) {
51
- return DestructorDeclsTrap{id} ;
43
+ std::optional <codeql::DestructorDecl> DeclVisitor::translateDestructorDecl (
44
+ const swift::DestructorDecl& decl) {
45
+ if ( auto entry = createNamedEntry (decl)) {
46
+ fillAbstractFunctionDecl ( decl, *entry);
47
+ return entry ;
52
48
}
53
- DestructorDecl entry{id};
54
- fillAbstractFunctionDecl (decl, entry);
55
- return entry;
49
+ return std::nullopt;
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) {
@@ -130,123 +124,106 @@ std::optional<codeql::ConcreteVarDecl> DeclVisitor::translateVarDecl(const swift
130
124
return entry;
131
125
}
132
126
133
- std::variant<codeql::StructDecl, codeql::StructDeclsTrap> DeclVisitor::translateStructDecl (
134
- const swift::StructDecl& decl) {
135
- auto id = dispatcher_.assignNewLabel (decl, mangledName (decl));
136
- if (!dispatcher_.shouldEmitDeclBody (decl)) {
137
- return StructDeclsTrap{id};
127
+ std::optional<codeql::StructDecl> DeclVisitor::translateStructDecl (const swift::StructDecl& decl) {
128
+ if (auto entry = createNamedEntry (decl)) {
129
+ fillNominalTypeDecl (decl, *entry);
130
+ return entry;
138
131
}
139
- StructDecl entry{id};
140
- fillNominalTypeDecl (decl, entry);
141
- return entry;
132
+ return std::nullopt;
142
133
}
143
134
144
- std::variant<codeql::ClassDecl, codeql::ClassDeclsTrap> DeclVisitor::translateClassDecl (
145
- const swift::ClassDecl& decl) {
146
- auto id = dispatcher_.assignNewLabel (decl, mangledName (decl));
147
- if (!dispatcher_.shouldEmitDeclBody (decl)) {
148
- return ClassDeclsTrap{id};
135
+ std::optional<codeql::ClassDecl> DeclVisitor::translateClassDecl (const swift::ClassDecl& decl) {
136
+ if (auto entry = createNamedEntry (decl)) {
137
+ fillNominalTypeDecl (decl, *entry);
138
+ return entry;
149
139
}
150
- ClassDecl entry{id};
151
- fillNominalTypeDecl (decl, entry);
152
- return entry;
140
+ return std::nullopt;
153
141
}
154
142
155
- std::variant<codeql::EnumDecl, codeql::EnumDeclsTrap> DeclVisitor::translateEnumDecl (
156
- const swift::EnumDecl& decl) {
157
- auto id = dispatcher_.assignNewLabel (decl, mangledName (decl));
158
- if (!dispatcher_.shouldEmitDeclBody (decl)) {
159
- return EnumDeclsTrap{id};
143
+ std::optional<codeql::EnumDecl> DeclVisitor::translateEnumDecl (const swift::EnumDecl& decl) {
144
+ if (auto entry = createNamedEntry (decl)) {
145
+ fillNominalTypeDecl (decl, *entry);
146
+ return entry;
160
147
}
161
- EnumDecl entry{id};
162
- fillNominalTypeDecl (decl, entry);
163
- return entry;
148
+ return std::nullopt;
164
149
}
165
150
166
- std::variant <codeql::ProtocolDecl, codeql::ProtocolDeclsTrap > DeclVisitor::translateProtocolDecl (
151
+ std::optional <codeql::ProtocolDecl> DeclVisitor::translateProtocolDecl (
167
152
const swift::ProtocolDecl& decl) {
168
- auto id = dispatcher_. assignNewLabel (decl, mangledName (decl));
169
- if (!dispatcher_. shouldEmitDeclBody ( decl)) {
170
- return ProtocolDeclsTrap{id} ;
153
+ if ( auto entry = createNamedEntry (decl)) {
154
+ fillNominalTypeDecl ( decl, *entry);
155
+ return entry ;
171
156
}
172
- ProtocolDecl entry{id};
173
- fillNominalTypeDecl (decl, entry);
174
- return entry;
157
+ return std::nullopt;
175
158
}
176
159
177
160
codeql::EnumCaseDecl DeclVisitor::translateEnumCaseDecl (const swift::EnumCaseDecl& decl) {
178
- EnumCaseDecl entry{dispatcher_. assignNewLabel (decl)} ;
161
+ auto entry = createEntry (decl);
179
162
entry.elements = dispatcher_.fetchRepeatedLabels (decl.getElements ());
180
163
return entry;
181
164
}
182
165
183
- std::variant <codeql::EnumElementDecl, codeql::EnumElementDeclsTrap>
184
- 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 ()} ;
166
+ std::optional <codeql::EnumElementDecl> DeclVisitor::translateEnumElementDecl (
167
+ const swift::EnumElementDecl& decl) {
168
+ auto entry = createNamedEntry (decl);
169
+ if (!entry ) {
170
+ return std::nullopt ;
188
171
}
189
- EnumElementDecl entry{id};
190
- entry.name = decl.getNameStr ().str ();
172
+ entry->name = decl.getNameStr ().str ();
191
173
if (decl.hasParameterList ()) {
192
- entry. params = dispatcher_.fetchRepeatedLabels (*decl.getParameterList ());
174
+ entry-> params = dispatcher_.fetchRepeatedLabels (*decl.getParameterList ());
193
175
}
194
- fillValueDecl (decl, entry);
176
+ fillValueDecl (decl, * entry);
195
177
return entry;
196
178
}
197
179
198
180
codeql::GenericTypeParamDecl DeclVisitor::translateGenericTypeParamDecl (
199
181
const swift::GenericTypeParamDecl& decl) {
200
182
// TODO: deduplicate
201
- GenericTypeParamDecl entry{dispatcher_. assignNewLabel (decl)} ;
183
+ auto entry = createEntry (decl);
202
184
fillTypeDecl (decl, entry);
203
185
return entry;
204
186
}
205
187
206
- std::variant <codeql::AssociatedTypeDecl, codeql::AssociatedTypeDeclsTrap>
207
- DeclVisitor::translateAssociatedTypeDecl ( const swift::AssociatedTypeDecl& decl) {
208
- auto id = dispatcher_. assignNewLabel (decl, mangledName (decl));
209
- if (!dispatcher_. shouldEmitDeclBody ( decl)) {
210
- return AssociatedTypeDeclsTrap{id} ;
188
+ std::optional <codeql::AssociatedTypeDecl> DeclVisitor::translateAssociatedTypeDecl (
189
+ const swift::AssociatedTypeDecl& decl) {
190
+ if ( auto entry = createNamedEntry (decl)) {
191
+ fillTypeDecl ( decl, *entry);
192
+ return entry ;
211
193
}
212
- AssociatedTypeDecl entry{id};
213
- fillTypeDecl (decl, entry);
214
- return entry;
194
+ return std::nullopt;
215
195
}
216
196
217
- std::variant <codeql::TypeAliasDecl, codeql::TypeAliasDeclsTrap > DeclVisitor::translateTypeAliasDecl (
197
+ std::optional <codeql::TypeAliasDecl> DeclVisitor::translateTypeAliasDecl (
218
198
const swift::TypeAliasDecl& decl) {
219
- auto id = dispatcher_. assignNewLabel (decl, mangledName (decl));
220
- if (!dispatcher_. shouldEmitDeclBody ( decl)) {
221
- return TypeAliasDeclsTrap{id} ;
199
+ if ( auto entry = createNamedEntry (decl)) {
200
+ fillTypeDecl ( decl, *entry);
201
+ return entry ;
222
202
}
223
- TypeAliasDecl entry{id};
224
- fillTypeDecl (decl, entry);
225
- return entry;
203
+ return std::nullopt;
226
204
}
227
205
228
- std::variant <codeql::AccessorDecl, codeql::AccessorDeclsTrap > DeclVisitor::translateAccessorDecl (
206
+ std::optional <codeql::AccessorDecl> DeclVisitor::translateAccessorDecl (
229
207
const swift::AccessorDecl& decl) {
230
- auto id = dispatcher_. assignNewLabel (decl, mangledName (decl) );
231
- if (!dispatcher_. shouldEmitDeclBody (decl) ) {
232
- return AccessorDeclsTrap{id} ;
208
+ auto entry = createNamedEntry (decl);
209
+ if (!entry ) {
210
+ return std::nullopt ;
233
211
}
234
- AccessorDecl entry{id};
235
212
switch (decl.getAccessorKind ()) {
236
213
case swift::AccessorKind::Get:
237
- entry. is_getter = true ;
214
+ entry-> is_getter = true ;
238
215
break ;
239
216
case swift::AccessorKind::Set:
240
- entry. is_setter = true ;
217
+ entry-> is_setter = true ;
241
218
break ;
242
219
case swift::AccessorKind::WillSet:
243
- entry. is_will_set = true ;
220
+ entry-> is_will_set = true ;
244
221
break ;
245
222
case swift::AccessorKind::DidSet:
246
- entry. is_did_set = true ;
223
+ entry-> is_did_set = true ;
247
224
break ;
248
225
}
249
- fillAbstractFunctionDecl (decl, entry);
226
+ fillAbstractFunctionDecl (decl, * entry);
250
227
return entry;
251
228
}
252
229
@@ -265,17 +242,17 @@ std::optional<codeql::SubscriptDecl> DeclVisitor::translateSubscriptDecl(
265
242
}
266
243
267
244
codeql::ExtensionDecl DeclVisitor::translateExtensionDecl (const swift::ExtensionDecl& decl) {
268
- ExtensionDecl entry{dispatcher_. assignNewLabel (decl)} ;
245
+ auto entry = createEntry (decl);
269
246
entry.extended_type_decl = dispatcher_.fetchLabel (decl.getExtendedNominal ());
270
247
fillGenericContext (decl, entry);
271
248
fillIterableDeclContext (decl, entry);
272
249
return entry;
273
250
}
274
251
275
252
codeql::ImportDecl DeclVisitor::translateImportDecl (const swift::ImportDecl& decl) {
276
- auto entry = dispatcher_. createEntry (decl);
253
+ auto entry = createEntry (decl);
277
254
entry.is_exported = decl.isExported ();
278
- entry.module = dispatcher_.fetchLabel (decl.getModule ());
255
+ entry.imported_module = dispatcher_.fetchLabel (decl.getModule ());
279
256
entry.declarations = dispatcher_.fetchRepeatedLabels (decl.getDecls ());
280
257
return entry;
281
258
}
@@ -389,7 +366,7 @@ void DeclVisitor::fillAbstractStorageDecl(const swift::AbstractStorageDecl& decl
389
366
}
390
367
391
368
codeql::IfConfigDecl DeclVisitor::translateIfConfigDecl (const swift::IfConfigDecl& decl) {
392
- auto entry = dispatcher_. createEntry (decl);
369
+ auto entry = createEntry (decl);
393
370
entry.clauses = dispatcher_.fetchRepeatedLabels (decl.getClauses ());
394
371
return entry;
395
372
}
0 commit comments