Skip to content

Commit 553930d

Browse files
committed
Swift: type visitor
This transfers the current state of `TypeVisitor` from the proof-of-concept.
1 parent 922608c commit 553930d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+673
-112
lines changed

swift/codegen/schema.yml

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ Location:
2828
end_line: int
2929
end_column: int
3030

31-
Type: {}
32-
# diagnostics_name: string
33-
# canonical_type: Type
31+
Type:
32+
diagnostics_name: string
33+
canonical_type: Type
3434

3535
IterableDeclContext:
3636
members: Decl*
@@ -59,15 +59,15 @@ ConditionElement:
5959

6060
AnyFunctionType:
6161
_extends: Type
62-
# result: Type
63-
# param_types: Type*
64-
# param_labels: string*
65-
# is_throwing: predicate
62+
result: Type
63+
param_types: Type*
64+
param_labels: string*
65+
is_throwing: predicate
6666

6767
AnyGenericType:
6868
_extends: Type
69-
# parent: Type?
70-
# declaration: Decl
69+
parent: Type?
70+
declaration: Decl
7171

7272
AnyMetatypeType:
7373
_extends: Type
@@ -77,6 +77,8 @@ BuiltinType:
7777

7878
DependentMemberType:
7979
_extends: Type
80+
baseType: Type
81+
associated_type_decl: AssociatedTypeDecl
8082

8183
DynamicSelfType:
8284
_extends: Type
@@ -89,7 +91,7 @@ InOutType:
8991

9092
LValueType:
9193
_extends: Type
92-
# object_type: Type
94+
object_type: Type
9395

9496
ModuleType:
9597
_extends: Type
@@ -126,6 +128,8 @@ SugarType:
126128

127129
TupleType:
128130
_extends: Type
131+
types: Type*
132+
names: string*
129133

130134
TypeVariableType:
131135
_extends: Type
@@ -166,7 +170,7 @@ FunctionType:
166170

167171
GenericFunctionType:
168172
_extends: AnyFunctionType
169-
# generic_params: GenericTypeParamType*
173+
generic_params: GenericTypeParamType*
170174

171175
NominalOrBoundGenericNominalType:
172176
_extends: AnyGenericType
@@ -227,16 +231,18 @@ ArchetypeType:
227231

228232
GenericTypeParamType:
229233
_extends: SubstitutableType
230-
# name: string
234+
name: string
231235

232236
ParenType:
233237
_extends: SugarType
238+
type: Type
234239

235240
SyntaxSugarType:
236241
_extends: SugarType
237242

238243
TypeAliasType:
239244
_extends: SugarType
245+
decl: TypeAliasDecl
240246

241247
EnumCaseDecl:
242248
_extends: Decl
@@ -590,6 +596,7 @@ YieldStmt:
590596

591597
BoundGenericType:
592598
_extends: NominalOrBoundGenericNominalType
599+
arg_types: Type*
593600

594601
NominalType:
595602
_extends: NominalOrBoundGenericNominalType
@@ -599,6 +606,7 @@ BuiltinIntegerLiteralType:
599606

600607
BuiltinIntegerType:
601608
_extends: AnyBuiltinIntegerType
609+
width: int?
602610

603611
NestedArchetypeType:
604612
_extends: ArchetypeType
@@ -614,13 +622,16 @@ OpenedArchetypeType:
614622

615623
PrimaryArchetypeType:
616624
_extends: ArchetypeType
617-
# interface_type: GenericTypeParamType
625+
interface_type: GenericTypeParamType
618626

619627
DictionaryType:
620628
_extends: SyntaxSugarType
629+
key_type: Type
630+
value_type: Type
621631

622632
UnarySyntaxSugarType:
623633
_extends: SyntaxSugarType
634+
base_type: Type
624635

625636
InfixOperatorDecl:
626637
_extends: OperatorDecl
@@ -887,7 +898,7 @@ BoundGenericStructType:
887898

888899
ClassType:
889900
_extends: NominalType
890-
# decl: ClassDecl
901+
decl: ClassDecl
891902

892903
EnumType:
893904
_extends: NominalType
@@ -897,7 +908,7 @@ ProtocolType:
897908

898909
StructType:
899910
_extends: NominalType
900-
# decl: StructDecl
911+
decl: StructDecl
901912

902913
ArraySliceType:
903914
_extends: UnarySyntaxSugarType

swift/extractor/trap/TrapLabel.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,18 @@ class UntypedTrapLabel {
1313
template <typename Tag>
1414
friend class TrapLabel;
1515

16+
static constexpr uint64_t undefined = 0xffffffffffffffff;
17+
1618
protected:
17-
UntypedTrapLabel() : id_{0xffffffffffffffff} {}
19+
UntypedTrapLabel() : id_{undefined} {}
1820
UntypedTrapLabel(uint64_t id) : id_{id} {}
1921

2022
public:
2123
friend std::ostream& operator<<(std::ostream& out, UntypedTrapLabel l) {
24+
// TODO: this is a temporary fix to catch us from outputting undefined labels to trap
25+
// this should be moved to a validity check, probably aided by code generation and carried out
26+
// by `SwiftDispatcher`
27+
assert(l.id_ != undefined && "outputting an undefined label!");
2228
out << '#' << std::hex << l.id_ << std::dec;
2329
return out;
2430
}

swift/extractor/visitors/TypeVisitor.h

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,229 @@ namespace codeql {
55
class 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

Comments
 (0)