33#include < swift/AST/Decl.h>
44#include < swift/AST/GenericParamList.h>
55#include < swift/AST/ParameterList.h>
6+ #include < swift/AST/ASTMangler.h>
67
78#include " swift/extractor/visitors/VisitorBase.h"
89
@@ -20,20 +21,29 @@ class DeclVisitor : public AstVisitorBase<DeclVisitor> {
2021 using AstVisitorBase<DeclVisitor>::AstVisitorBase;
2122
2223 void visitFuncDecl (swift::FuncDecl* decl) {
23- auto label = dispatcher_.assignNewLabel (decl);
24+ auto label = dispatcher_.assignNewLabel (decl, mangledName (decl) );
2425 dispatcher_.emit (ConcreteFuncDeclsTrap{label});
26+ if (!dispatcher_.shouldEmitDeclBody (decl)) {
27+ return ;
28+ }
2529 emitAbstractFunctionDecl (decl, label);
2630 }
2731
2832 void visitConstructorDecl (swift::ConstructorDecl* decl) {
29- auto label = dispatcher_.assignNewLabel (decl);
33+ auto label = dispatcher_.assignNewLabel (decl, mangledName (decl) );
3034 dispatcher_.emit (ConstructorDeclsTrap{label});
35+ if (!dispatcher_.shouldEmitDeclBody (decl)) {
36+ return ;
37+ }
3138 emitConstructorDecl (decl, label);
3239 }
3340
3441 void visitDestructorDecl (swift::DestructorDecl* decl) {
35- auto label = dispatcher_.assignNewLabel (decl);
42+ auto label = dispatcher_.assignNewLabel (decl, mangledName (decl) );
3643 dispatcher_.emit (DestructorDeclsTrap{label});
44+ if (!dispatcher_.shouldEmitDeclBody (decl)) {
45+ return ;
46+ }
3747 emitDestructorDecl (decl, label);
3848 }
3949
@@ -64,6 +74,7 @@ class DeclVisitor : public AstVisitorBase<DeclVisitor> {
6474 }
6575
6676 void visitParamDecl (swift::ParamDecl* decl) {
77+ // TODO: deduplicate
6778 auto label = dispatcher_.assignNewLabel (decl);
6879 dispatcher_.emit (ParamDeclsTrap{label});
6980 if (decl->isInOut ()) {
@@ -92,33 +103,46 @@ class DeclVisitor : public AstVisitorBase<DeclVisitor> {
92103 }
93104
94105 void visitVarDecl (swift::VarDecl* decl) {
106+ // TODO: deduplicate all non-local variables
95107 auto label = dispatcher_.assignNewLabel (decl);
96108 auto introducer = static_cast <uint8_t >(decl->getIntroducer ());
97109 dispatcher_.emit (ConcreteVarDeclsTrap{label, introducer});
98110 emitVarDecl (decl, label);
99111 }
100112
101113 void visitStructDecl (swift::StructDecl* decl) {
102- auto label = dispatcher_.assignNewLabel (decl);
114+ auto label = dispatcher_.assignNewLabel (decl, mangledName (decl) );
103115 dispatcher_.emit (StructDeclsTrap{label});
116+ if (!dispatcher_.shouldEmitDeclBody (decl)) {
117+ return ;
118+ }
104119 emitNominalTypeDecl (decl, label);
105120 }
106121
107122 void visitClassDecl (swift::ClassDecl* decl) {
108- auto label = dispatcher_.assignNewLabel (decl);
123+ auto label = dispatcher_.assignNewLabel (decl, mangledName (decl) );
109124 dispatcher_.emit (ClassDeclsTrap{label});
125+ if (!dispatcher_.shouldEmitDeclBody (decl)) {
126+ return ;
127+ }
110128 emitNominalTypeDecl (decl, label);
111129 }
112130
113131 void visitEnumDecl (swift::EnumDecl* decl) {
114- auto label = dispatcher_.assignNewLabel (decl);
132+ auto label = dispatcher_.assignNewLabel (decl, mangledName (decl) );
115133 dispatcher_.emit (EnumDeclsTrap{label});
134+ if (!dispatcher_.shouldEmitDeclBody (decl)) {
135+ return ;
136+ }
116137 emitNominalTypeDecl (decl, label);
117138 }
118139
119140 void visitProtocolDecl (swift::ProtocolDecl* decl) {
120- auto label = dispatcher_.assignNewLabel (decl);
141+ auto label = dispatcher_.assignNewLabel (decl, mangledName (decl) );
121142 dispatcher_.emit (ProtocolDeclsTrap{label});
143+ if (!dispatcher_.shouldEmitDeclBody (decl)) {
144+ return ;
145+ }
122146 emitNominalTypeDecl (decl, label);
123147 }
124148
@@ -132,8 +156,11 @@ class DeclVisitor : public AstVisitorBase<DeclVisitor> {
132156 }
133157
134158 void visitEnumElementDecl (swift::EnumElementDecl* decl) {
135- auto label = dispatcher_.assignNewLabel (decl);
159+ auto label = dispatcher_.assignNewLabel (decl, mangledName (decl) );
136160 dispatcher_.emit (EnumElementDeclsTrap{label, decl->getNameStr ().str ()});
161+ if (!dispatcher_.shouldEmitDeclBody (decl)) {
162+ return ;
163+ }
137164 if (decl->hasParameterList ()) {
138165 auto i = 0u ;
139166 for (auto p : *decl->getParameterList ()) {
@@ -143,26 +170,36 @@ class DeclVisitor : public AstVisitorBase<DeclVisitor> {
143170 }
144171
145172 void visitGenericTypeParamDecl (swift::GenericTypeParamDecl* decl) {
173+ // TODO: deduplicate
146174 auto label = dispatcher_.assignNewLabel (decl);
147175 dispatcher_.emit (GenericTypeParamDeclsTrap{label});
148176 emitTypeDecl (decl, label);
149177 }
150178
151179 void visitAssociatedTypeDecl (swift::AssociatedTypeDecl* decl) {
152- auto label = dispatcher_.assignNewLabel (decl);
180+ auto label = dispatcher_.assignNewLabel (decl, mangledName (decl) );
153181 dispatcher_.emit (AssociatedTypeDeclsTrap{label});
182+ if (!dispatcher_.shouldEmitDeclBody (decl)) {
183+ return ;
184+ }
154185 emitTypeDecl (decl, label);
155186 }
156187
157188 void visitTypeAliasDecl (swift::TypeAliasDecl* decl) {
158- auto label = dispatcher_.assignNewLabel (decl);
189+ auto label = dispatcher_.assignNewLabel (decl, mangledName (decl) );
159190 dispatcher_.emit (TypeAliasDeclsTrap{label});
191+ if (!dispatcher_.shouldEmitDeclBody (decl)) {
192+ return ;
193+ }
160194 emitTypeDecl (decl, label);
161195 }
162196
163197 void visitAccessorDecl (swift::AccessorDecl* decl) {
164- auto label = dispatcher_.assignNewLabel (decl);
198+ auto label = dispatcher_.assignNewLabel (decl, mangledName (decl) );
165199 dispatcher_.emit (AccessorDeclsTrap{label});
200+ if (!dispatcher_.shouldEmitDeclBody (decl)) {
201+ return ;
202+ }
166203 switch (decl->getAccessorKind ()) {
167204 case swift::AccessorKind::Get:
168205 dispatcher_.emit (AccessorDeclIsGetterTrap{label});
@@ -181,7 +218,10 @@ class DeclVisitor : public AstVisitorBase<DeclVisitor> {
181218 }
182219
183220 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+ }
185225 auto elementTypeLabel = dispatcher_.fetchLabel (decl->getElementInterfaceType ());
186226 dispatcher_.emit (SubscriptDeclsTrap{label, elementTypeLabel});
187227 if (auto indices = decl->getIndices ()) {
@@ -202,6 +242,11 @@ class DeclVisitor : public AstVisitorBase<DeclVisitor> {
202242 }
203243
204244 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+
205250 void emitConstructorDecl (swift::ConstructorDecl* decl, TrapLabel<ConstructorDeclTag> label) {
206251 emitAbstractFunctionDecl (decl, label);
207252 }
@@ -309,6 +354,9 @@ class DeclVisitor : public AstVisitorBase<DeclVisitor> {
309354 }
310355 emitValueDecl (decl, label);
311356 }
357+
358+ private:
359+ swift::Mangle::ASTMangler mangler;
312360};
313361
314362} // namespace codeql
0 commit comments