@@ -5,6 +5,229 @@ namespace codeql {
55class TypeVisitor : public TypeVisitorBase <TypeVisitor> {
66 public:
77 using TypeVisitorBase<TypeVisitor>::TypeVisitorBase;
8+
9+ void visit (swift::TypeBase* type) {
10+ TypeVisitorBase<TypeVisitor>::visit (type);
11+ auto label = dispatcher_.fetchLabel (type);
12+ auto canonical = type->getCanonicalType ().getPointer ();
13+ auto canonicalLabel = (canonical == type) ? label : dispatcher_.fetchLabel (canonical);
14+ dispatcher_.emit (TypesTrap{label, type->getString (), canonicalLabel});
15+ }
16+
17+ void visitProtocolType (swift::ProtocolType* type) {
18+ auto label = dispatcher_.assignNewLabel (type);
19+ dispatcher_.emit (ProtocolTypesTrap{label});
20+ emitAnyGenericType (type, label);
21+ }
22+
23+ void visitEnumType (swift::EnumType* type) {
24+ auto label = dispatcher_.assignNewLabel (type);
25+ dispatcher_.emit (EnumTypesTrap{label});
26+ emitAnyGenericType (type, label);
27+ }
28+
29+ void visitStructType (swift::StructType* type) {
30+ auto label = dispatcher_.assignNewLabel (type);
31+ dispatcher_.emit (StructTypesTrap{label, dispatcher_.fetchLabel (type->getDecl ())});
32+ emitAnyGenericType (type, label);
33+ }
34+
35+ void visitClassType (swift::ClassType* type) {
36+ auto label = dispatcher_.assignNewLabel (type);
37+ dispatcher_.emit (ClassTypesTrap{label, dispatcher_.fetchLabel (type->getDecl ())});
38+ emitAnyGenericType (type, label);
39+ }
40+
41+ void visitFunctionType (swift::FunctionType* type) {
42+ auto label = dispatcher_.assignNewLabel (type);
43+ dispatcher_.emit (FunctionTypesTrap{label});
44+ emitAnyFunctionType (type, label);
45+ }
46+
47+ void visitTupleType (swift::TupleType* type) {
48+ auto label = dispatcher_.assignNewLabel (type);
49+ dispatcher_.emit (TupleTypesTrap{label});
50+ auto i = 0u ;
51+ for (const auto & e : type->getElements ()) {
52+ auto typeTag = dispatcher_.fetchLabel (e.getType ());
53+ dispatcher_.emit (TupleTypeTypesTrap{label, i, typeTag});
54+ if (e.hasName ()) {
55+ dispatcher_.emit (TupleTypeNamesTrap{label, i, e.getName ().str ().str ()});
56+ }
57+ ++i;
58+ }
59+ }
60+
61+ void visitBoundGenericEnumType (swift::BoundGenericEnumType* type) {
62+ auto label = dispatcher_.assignNewLabel (type);
63+ dispatcher_.emit (BoundGenericEnumTypesTrap{label});
64+ emitBoundGenericType (type, label);
65+ }
66+
67+ void visitMetatypeType (swift::MetatypeType* type) {
68+ auto label = dispatcher_.assignNewLabel (type);
69+ dispatcher_.emit (MetatypeTypesTrap{label});
70+ }
71+
72+ void visitExistentialMetatypeType (swift::ExistentialMetatypeType* type) {
73+ auto label = dispatcher_.assignNewLabel (type);
74+ dispatcher_.emit (ExistentialMetatypeTypesTrap{label});
75+ }
76+
77+ void visitBoundGenericStructType (swift::BoundGenericStructType* type) {
78+ auto label = dispatcher_.assignNewLabel (type);
79+ dispatcher_.emit (BoundGenericStructTypesTrap{label});
80+ emitBoundGenericType (type, label);
81+ }
82+
83+ void visitTypeAliasType (swift::TypeAliasType* type) {
84+ auto label = dispatcher_.assignNewLabel (type);
85+ assert (type->getDecl () && " expect TypeAliasType to have Decl" );
86+ dispatcher_.emit (TypeAliasTypesTrap{label, dispatcher_.fetchLabel (type->getDecl ())});
87+ }
88+
89+ void visitBuiltinIntegerLiteralType (swift::BuiltinIntegerLiteralType* type) {
90+ auto label = dispatcher_.assignNewLabel (type);
91+ dispatcher_.emit (BuiltinIntegerLiteralTypesTrap{label});
92+ }
93+
94+ void visitBuiltinFloatType (swift::BuiltinFloatType* type) {
95+ auto label = dispatcher_.assignNewLabel (type);
96+ dispatcher_.emit (BuiltinFloatTypesTrap{label});
97+ }
98+
99+ void visitBuiltinIntegerType (swift::BuiltinIntegerType* type) {
100+ auto label = dispatcher_.assignNewLabel (type);
101+ auto width = type->getWidth ();
102+ if (width.isFixedWidth ()) {
103+ dispatcher_.emit (BuiltinIntegerTypeWidthsTrap{label, width.getFixedWidth ()});
104+ }
105+ dispatcher_.emit (BuiltinIntegerTypesTrap{label});
106+ }
107+
108+ void visitBoundGenericClassType (swift::BoundGenericClassType* type) {
109+ auto label = dispatcher_.assignNewLabel (type);
110+ dispatcher_.emit (BoundGenericClassTypesTrap{label});
111+ emitBoundGenericType (type, label);
112+ }
113+
114+ void visitDependentMemberType (swift::DependentMemberType* type) {
115+ auto label = dispatcher_.assignNewLabel (type);
116+ assert (type->getBase () && " expect TypeAliasType to have Base" );
117+ assert (type->getAssocType () && " expect TypeAliasType to have AssocType" );
118+ auto baseLabel = dispatcher_.fetchLabel (type->getBase ());
119+ auto assocTypeDeclLabel = dispatcher_.fetchLabel (type->getAssocType ());
120+ dispatcher_.emit (DependentMemberTypesTrap{label, baseLabel, assocTypeDeclLabel});
121+ }
122+
123+ void visitParenType (swift::ParenType* type) {
124+ auto label = dispatcher_.assignNewLabel (type);
125+ assert (type->getUnderlyingType () && " expect ParenType to have UnderlyingType" );
126+ auto typeLabel = dispatcher_.fetchLabel (type->getUnderlyingType ());
127+ dispatcher_.emit (ParenTypesTrap{label, typeLabel});
128+ }
129+
130+ void visitUnarySyntaxSugarType (swift::UnarySyntaxSugarType* type) {
131+ auto label = dispatcher_.assignNewLabel (type);
132+ emitUnarySyntaxSugarType (type, label);
133+ }
134+
135+ void visitOptionalType (swift::OptionalType* type) {
136+ auto label = dispatcher_.assignNewLabel (type);
137+ dispatcher_.emit (OptionalTypesTrap{label});
138+ emitUnarySyntaxSugarType (type, label);
139+ }
140+
141+ void visitArraySliceType (swift::ArraySliceType* type) {
142+ auto label = dispatcher_.assignNewLabel (type);
143+ dispatcher_.emit (ArraySliceTypesTrap{label});
144+ emitUnarySyntaxSugarType (type, label);
145+ }
146+
147+ void visitDictionaryType (swift::DictionaryType* type) {
148+ auto label = dispatcher_.assignNewLabel (type);
149+ auto keyLabel = dispatcher_.fetchLabel (type->getKeyType ());
150+ auto valueLabel = dispatcher_.fetchLabel (type->getValueType ());
151+ dispatcher_.emit (DictionaryTypesTrap{label, keyLabel, valueLabel});
152+ }
153+
154+ void visitGenericFunctionType (swift::GenericFunctionType* type) {
155+ auto label = dispatcher_.assignNewLabel (type);
156+ dispatcher_.emit (GenericFunctionTypesTrap{label});
157+ emitAnyFunctionType (type, label);
158+ auto i = 0u ;
159+ for (auto p : type->getGenericParams ()) {
160+ dispatcher_.emit (GenericFunctionTypeGenericParamsTrap{label, i++, dispatcher_.fetchLabel (p)});
161+ }
162+ }
163+
164+ void visitGenericTypeParamType (swift::GenericTypeParamType* type) {
165+ auto label = dispatcher_.assignNewLabel (type);
166+ dispatcher_.emit (GenericTypeParamTypesTrap{label, type->getName ().str ().str ()});
167+ }
168+
169+ void visitLValueType (swift::LValueType* type) {
170+ auto label = dispatcher_.assignNewLabel (type);
171+ assert (type->getObjectType () && " expect LValueType to have ObjectType" );
172+ dispatcher_.emit (LValueTypesTrap{label, dispatcher_.fetchLabel (type->getObjectType ())});
173+ }
174+
175+ void visitPrimaryArchetypeType (swift::PrimaryArchetypeType* type) {
176+ auto label = dispatcher_.assignNewLabel (type);
177+ assert (type->getInterfaceType () && " expect PrimaryArchetypeType to have InterfaceType" );
178+ dispatcher_.emit (
179+ PrimaryArchetypeTypesTrap{label, dispatcher_.fetchLabel (type->getInterfaceType ())});
180+ }
181+
182+ void visitUnboundGenericType (swift::UnboundGenericType* type) {
183+ auto label = dispatcher_.assignNewLabel (type);
184+ dispatcher_.emit (UnboundGenericTypesTrap{label});
185+ emitAnyGenericType (type, label);
186+ }
187+
188+ void visitBoundGenericType (swift::BoundGenericType* type) {
189+ auto label = dispatcher_.assignNewLabel (type);
190+ emitBoundGenericType (type, label);
191+ }
192+
193+ private:
194+ void emitUnarySyntaxSugarType (const swift::UnarySyntaxSugarType* type,
195+ TrapLabel<UnarySyntaxSugarTypeTag> label) {
196+ assert (type->getBaseType () && " expect UnarySyntaxSugarType to have BaseType" );
197+ dispatcher_.emit (UnarySyntaxSugarTypesTrap{label, dispatcher_.fetchLabel (type->getBaseType ())});
198+ }
199+
200+ void emitAnyFunctionType (const swift::AnyFunctionType* type,
201+ TrapLabel<AnyFunctionTypeTag> label) {
202+ assert (type->getResult () && " expect FunctionType to have Result" );
203+ dispatcher_.emit (AnyFunctionTypesTrap{label, dispatcher_.fetchLabel (type->getResult ())});
204+ auto i = 0u ;
205+ for (const auto & p : type->getParams ()) {
206+ assert (p.getPlainType () && " expect Param to have PlainType" );
207+ dispatcher_.emit (
208+ AnyFunctionTypeParamTypesTrap{label, i, dispatcher_.fetchLabel (p.getPlainType ())});
209+ if (p.hasLabel ()) {
210+ dispatcher_.emit (AnyFunctionTypeParamLabelsTrap{label, i, p.getLabel ().str ().str ()});
211+ }
212+ ++i;
213+ }
214+ }
215+
216+ void emitBoundGenericType (swift::BoundGenericType* type, TrapLabel<BoundGenericTypeTag> label) {
217+ auto i = 0u ;
218+ for (const auto & t : type->getGenericArgs ()) {
219+ dispatcher_.emit (BoundGenericTypeArgTypesTrap{label, i++, dispatcher_.fetchLabel (t)});
220+ }
221+ emitAnyGenericType (type, label);
222+ }
223+
224+ void emitAnyGenericType (swift::AnyGenericType* type, TrapLabel<AnyGenericTypeTag> label) {
225+ assert (type->getDecl () && " expect AnyGenericType to have Decl" );
226+ dispatcher_.emit (AnyGenericTypesTrap{label, dispatcher_.fetchLabel (type->getDecl ())});
227+ if (auto parent = type->getParent ()) {
228+ dispatcher_.emit (AnyGenericTypeParentsTrap{label, dispatcher_.fetchLabel (parent)});
229+ }
230+ }
8231};
9232
10233} // namespace codeql
0 commit comments