3
3
#include < swift/AST/Decl.h>
4
4
#include < swift/AST/GenericParamList.h>
5
5
#include < swift/AST/ParameterList.h>
6
+ #include < swift/AST/ASTMangler.h>
6
7
7
8
#include " swift/extractor/visitors/VisitorBase.h"
8
9
@@ -20,20 +21,29 @@ class DeclVisitor : public AstVisitorBase<DeclVisitor> {
20
21
using AstVisitorBase<DeclVisitor>::AstVisitorBase;
21
22
22
23
void visitFuncDecl (swift::FuncDecl* decl) {
23
- auto label = dispatcher_.assignNewLabel (decl);
24
+ auto label = dispatcher_.assignNewLabel (decl, mangledName (decl) );
24
25
dispatcher_.emit (ConcreteFuncDeclsTrap{label});
26
+ if (!dispatcher_.shouldEmitDeclBody (decl)) {
27
+ return ;
28
+ }
25
29
emitAbstractFunctionDecl (decl, label);
26
30
}
27
31
28
32
void visitConstructorDecl (swift::ConstructorDecl* decl) {
29
- auto label = dispatcher_.assignNewLabel (decl);
33
+ auto label = dispatcher_.assignNewLabel (decl, mangledName (decl) );
30
34
dispatcher_.emit (ConstructorDeclsTrap{label});
35
+ if (!dispatcher_.shouldEmitDeclBody (decl)) {
36
+ return ;
37
+ }
31
38
emitConstructorDecl (decl, label);
32
39
}
33
40
34
41
void visitDestructorDecl (swift::DestructorDecl* decl) {
35
- auto label = dispatcher_.assignNewLabel (decl);
42
+ auto label = dispatcher_.assignNewLabel (decl, mangledName (decl) );
36
43
dispatcher_.emit (DestructorDeclsTrap{label});
44
+ if (!dispatcher_.shouldEmitDeclBody (decl)) {
45
+ return ;
46
+ }
37
47
emitDestructorDecl (decl, label);
38
48
}
39
49
@@ -64,6 +74,7 @@ class DeclVisitor : public AstVisitorBase<DeclVisitor> {
64
74
}
65
75
66
76
void visitParamDecl (swift::ParamDecl* decl) {
77
+ // TODO: deduplicate
67
78
auto label = dispatcher_.assignNewLabel (decl);
68
79
dispatcher_.emit (ParamDeclsTrap{label});
69
80
if (decl->isInOut ()) {
@@ -92,33 +103,46 @@ class DeclVisitor : public AstVisitorBase<DeclVisitor> {
92
103
}
93
104
94
105
void visitVarDecl (swift::VarDecl* decl) {
106
+ // TODO: deduplicate all non-local variables
95
107
auto label = dispatcher_.assignNewLabel (decl);
96
108
auto introducer = static_cast <uint8_t >(decl->getIntroducer ());
97
109
dispatcher_.emit (ConcreteVarDeclsTrap{label, introducer});
98
110
emitVarDecl (decl, label);
99
111
}
100
112
101
113
void visitStructDecl (swift::StructDecl* decl) {
102
- auto label = dispatcher_.assignNewLabel (decl);
114
+ auto label = dispatcher_.assignNewLabel (decl, mangledName (decl) );
103
115
dispatcher_.emit (StructDeclsTrap{label});
116
+ if (!dispatcher_.shouldEmitDeclBody (decl)) {
117
+ return ;
118
+ }
104
119
emitNominalTypeDecl (decl, label);
105
120
}
106
121
107
122
void visitClassDecl (swift::ClassDecl* decl) {
108
- auto label = dispatcher_.assignNewLabel (decl);
123
+ auto label = dispatcher_.assignNewLabel (decl, mangledName (decl) );
109
124
dispatcher_.emit (ClassDeclsTrap{label});
125
+ if (!dispatcher_.shouldEmitDeclBody (decl)) {
126
+ return ;
127
+ }
110
128
emitNominalTypeDecl (decl, label);
111
129
}
112
130
113
131
void visitEnumDecl (swift::EnumDecl* decl) {
114
- auto label = dispatcher_.assignNewLabel (decl);
132
+ auto label = dispatcher_.assignNewLabel (decl, mangledName (decl) );
115
133
dispatcher_.emit (EnumDeclsTrap{label});
134
+ if (!dispatcher_.shouldEmitDeclBody (decl)) {
135
+ return ;
136
+ }
116
137
emitNominalTypeDecl (decl, label);
117
138
}
118
139
119
140
void visitProtocolDecl (swift::ProtocolDecl* decl) {
120
- auto label = dispatcher_.assignNewLabel (decl);
141
+ auto label = dispatcher_.assignNewLabel (decl, mangledName (decl) );
121
142
dispatcher_.emit (ProtocolDeclsTrap{label});
143
+ if (!dispatcher_.shouldEmitDeclBody (decl)) {
144
+ return ;
145
+ }
122
146
emitNominalTypeDecl (decl, label);
123
147
}
124
148
@@ -132,8 +156,11 @@ class DeclVisitor : public AstVisitorBase<DeclVisitor> {
132
156
}
133
157
134
158
void visitEnumElementDecl (swift::EnumElementDecl* decl) {
135
- auto label = dispatcher_.assignNewLabel (decl);
159
+ auto label = dispatcher_.assignNewLabel (decl, mangledName (decl) );
136
160
dispatcher_.emit (EnumElementDeclsTrap{label, decl->getNameStr ().str ()});
161
+ if (!dispatcher_.shouldEmitDeclBody (decl)) {
162
+ return ;
163
+ }
137
164
if (decl->hasParameterList ()) {
138
165
auto i = 0u ;
139
166
for (auto p : *decl->getParameterList ()) {
@@ -143,26 +170,36 @@ class DeclVisitor : public AstVisitorBase<DeclVisitor> {
143
170
}
144
171
145
172
void visitGenericTypeParamDecl (swift::GenericTypeParamDecl* decl) {
173
+ // TODO: deduplicate
146
174
auto label = dispatcher_.assignNewLabel (decl);
147
175
dispatcher_.emit (GenericTypeParamDeclsTrap{label});
148
176
emitTypeDecl (decl, label);
149
177
}
150
178
151
179
void visitAssociatedTypeDecl (swift::AssociatedTypeDecl* decl) {
152
- auto label = dispatcher_.assignNewLabel (decl);
180
+ auto label = dispatcher_.assignNewLabel (decl, mangledName (decl) );
153
181
dispatcher_.emit (AssociatedTypeDeclsTrap{label});
182
+ if (!dispatcher_.shouldEmitDeclBody (decl)) {
183
+ return ;
184
+ }
154
185
emitTypeDecl (decl, label);
155
186
}
156
187
157
188
void visitTypeAliasDecl (swift::TypeAliasDecl* decl) {
158
- auto label = dispatcher_.assignNewLabel (decl);
189
+ auto label = dispatcher_.assignNewLabel (decl, mangledName (decl) );
159
190
dispatcher_.emit (TypeAliasDeclsTrap{label});
191
+ if (!dispatcher_.shouldEmitDeclBody (decl)) {
192
+ return ;
193
+ }
160
194
emitTypeDecl (decl, label);
161
195
}
162
196
163
197
void visitAccessorDecl (swift::AccessorDecl* decl) {
164
- auto label = dispatcher_.assignNewLabel (decl);
198
+ auto label = dispatcher_.assignNewLabel (decl, mangledName (decl) );
165
199
dispatcher_.emit (AccessorDeclsTrap{label});
200
+ if (!dispatcher_.shouldEmitDeclBody (decl)) {
201
+ return ;
202
+ }
166
203
switch (decl->getAccessorKind ()) {
167
204
case swift::AccessorKind::Get:
168
205
dispatcher_.emit (AccessorDeclIsGetterTrap{label});
@@ -181,7 +218,10 @@ class DeclVisitor : public AstVisitorBase<DeclVisitor> {
181
218
}
182
219
183
220
void visitSubscriptDecl (swift::SubscriptDecl* decl) {
184
- auto label = dispatcher_.assignNewLabel (decl);
221
+ auto label = dispatcher_.assignNewLabel (decl, mangledName (decl));
222
+ if (!dispatcher_.shouldEmitDeclBody (decl)) {
223
+ return ;
224
+ }
185
225
auto elementTypeLabel = dispatcher_.fetchLabel (decl->getElementInterfaceType ());
186
226
dispatcher_.emit (SubscriptDeclsTrap{label, elementTypeLabel});
187
227
if (auto indices = decl->getIndices ()) {
@@ -202,6 +242,11 @@ class DeclVisitor : public AstVisitorBase<DeclVisitor> {
202
242
}
203
243
204
244
private:
245
+ std::string mangledName (swift::ValueDecl* decl) {
246
+ // prefix adds a couple of special symbols, we don't necessary need them
247
+ return mangler.mangleAnyDecl (decl, /* prefix = */ false );
248
+ }
249
+
205
250
void emitConstructorDecl (swift::ConstructorDecl* decl, TrapLabel<ConstructorDeclTag> label) {
206
251
emitAbstractFunctionDecl (decl, label);
207
252
}
@@ -309,6 +354,9 @@ class DeclVisitor : public AstVisitorBase<DeclVisitor> {
309
354
}
310
355
emitValueDecl (decl, label);
311
356
}
357
+
358
+ private:
359
+ swift::Mangle::ASTMangler mangler;
312
360
};
313
361
314
362
} // namespace codeql
0 commit comments