Skip to content

Commit a6b1806

Browse files
committed
Swift: refactor TypeVisitor to use translations
1 parent c8cb30f commit a6b1806

File tree

8 files changed

+161
-299
lines changed

8 files changed

+161
-299
lines changed

swift/extractor/visitors/TypeVisitor.cpp

Lines changed: 91 additions & 233 deletions
Large diffs are not rendered by default.

swift/extractor/visitors/TypeVisitor.h

Lines changed: 45 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -8,67 +8,62 @@ class TypeVisitor : public TypeVisitorBase<TypeVisitor> {
88
public:
99
using TypeVisitorBase<TypeVisitor>::TypeVisitorBase;
1010

11-
void visit(swift::TypeBase* type);
1211
codeql::TypeRepr translateTypeRepr(const swift::TypeRepr& typeRepr, swift::Type type);
1312

14-
void visitProtocolType(swift::ProtocolType* type);
15-
void visitEnumType(swift::EnumType* type);
16-
void visitStructType(swift::StructType* type);
17-
void visitClassType(swift::ClassType* type);
18-
void visitFunctionType(swift::FunctionType* type);
19-
void visitTupleType(swift::TupleType* type);
20-
void visitBoundGenericEnumType(swift::BoundGenericEnumType* type);
21-
void visitMetatypeType(swift::MetatypeType* type);
22-
void visitExistentialMetatypeType(swift::ExistentialMetatypeType* type);
23-
void visitBoundGenericStructType(swift::BoundGenericStructType* type);
24-
void visitTypeAliasType(swift::TypeAliasType* type);
25-
void visitBuiltinIntegerLiteralType(swift::BuiltinIntegerLiteralType* type);
26-
void visitBuiltinFloatType(swift::BuiltinFloatType* type);
27-
void visitBuiltinIntegerType(swift::BuiltinIntegerType* type);
28-
void visitBoundGenericClassType(swift::BoundGenericClassType* type);
29-
void visitDependentMemberType(swift::DependentMemberType* type);
30-
void visitParenType(swift::ParenType* type);
31-
void visitUnarySyntaxSugarType(swift::UnarySyntaxSugarType* type);
13+
codeql::ProtocolType translateProtocolType(const swift::ProtocolType& type);
14+
codeql::EnumType translateEnumType(const swift::EnumType& type);
15+
codeql::StructType translateStructType(const swift::StructType& type);
16+
codeql::ClassType translateClassType(const swift::ClassType& type);
17+
codeql::FunctionType translateFunctionType(const swift::FunctionType& type);
18+
codeql::TupleType translateTupleType(const swift::TupleType& type);
19+
codeql::MetatypeType translateMetatypeType(const swift::MetatypeType& type);
20+
codeql::ExistentialMetatypeType translateExistentialMetatypeType(
21+
const swift::ExistentialMetatypeType& type);
22+
codeql::TypeAliasType translateTypeAliasType(const swift::TypeAliasType& type);
23+
codeql::DependentMemberType translateDependentMemberType(const swift::DependentMemberType& type);
24+
codeql::ParenType translateParenType(const swift::ParenType& type);
25+
codeql::UnarySyntaxSugarType translateUnarySyntaxSugarType(
26+
const swift::UnarySyntaxSugarType& type);
3227
codeql::OptionalType translateOptionalType(const swift::OptionalType& type);
3328
codeql::ArraySliceType translateArraySliceType(const swift::ArraySliceType& type);
34-
void visitDictionaryType(swift::DictionaryType* type);
35-
void visitGenericFunctionType(swift::GenericFunctionType* type);
36-
void visitGenericTypeParamType(swift::GenericTypeParamType* type);
37-
void visitLValueType(swift::LValueType* type);
38-
void visitUnboundGenericType(swift::UnboundGenericType* type);
39-
void visitBoundGenericType(swift::BoundGenericType* type);
29+
codeql::DictionaryType translateDictionaryType(const swift::DictionaryType& type);
30+
codeql::GenericFunctionType translateGenericFunctionType(const swift::GenericFunctionType& type);
31+
codeql::GenericTypeParamType translateGenericTypeParamType(
32+
const swift::GenericTypeParamType& type);
33+
codeql::LValueType translateLValueType(const swift::LValueType& type);
34+
codeql::UnboundGenericType translateUnboundGenericType(const swift::UnboundGenericType& type);
35+
36+
template <typename Type>
37+
codeql::TrapClassOf<Type> translateBoundGenericType(const Type& type) {
38+
auto entry = createTypeEntry(type);
39+
fillBoundGenericType(type, entry);
40+
return entry;
41+
}
42+
4043
codeql::PrimaryArchetypeType translatePrimaryArchetypeType(
4144
const swift::PrimaryArchetypeType& type);
4245
codeql::ExistentialType translateExistentialType(const swift::ExistentialType& type);
4346
codeql::DynamicSelfType translateDynamicSelfType(const swift::DynamicSelfType& type);
4447
codeql::VariadicSequenceType translateVariadicSequenceType(
4548
const swift::VariadicSequenceType& type);
4649
codeql::InOutType translateInOutType(const swift::InOutType& type);
47-
codeql::UnmanagedStorageType translateUnmanagedStorageType(
48-
const swift::UnmanagedStorageType& type);
49-
codeql::WeakStorageType translateWeakStorageType(const swift::WeakStorageType& type);
50-
codeql::UnownedStorageType translateUnownedStorageType(const swift::UnownedStorageType& type);
50+
51+
template <typename Type>
52+
codeql::TrapClassOf<Type> translateReferenceStorageType(const Type& type) {
53+
auto entry = createTypeEntry(type);
54+
fillReferenceStorageType(type, entry);
55+
return entry;
56+
}
57+
5158
codeql::ProtocolCompositionType translateProtocolCompositionType(
5259
const swift::ProtocolCompositionType& type);
53-
codeql::BuiltinIntegerLiteralType translateBuiltinIntegerLiteralType(
54-
const swift::BuiltinIntegerLiteralType& type);
60+
61+
template <typename Type>
62+
codeql::TrapClassOf<Type> translateBuiltinType(const Type& type) {
63+
return createTypeEntry(type);
64+
}
65+
5566
codeql::BuiltinIntegerType translateBuiltinIntegerType(const swift::BuiltinIntegerType& type);
56-
codeql::BuiltinBridgeObjectType translateBuiltinBridgeObjectType(
57-
const swift::BuiltinBridgeObjectType& type);
58-
codeql::BuiltinDefaultActorStorageType translateBuiltinDefaultActorStorageType(
59-
const swift::BuiltinDefaultActorStorageType& type);
60-
codeql::BuiltinExecutorType translateBuiltinExecutorType(const swift::BuiltinExecutorType& type);
61-
codeql::BuiltinFloatType translateBuiltinFloatType(const swift::BuiltinFloatType& type);
62-
codeql::BuiltinJobType translateBuiltinJobType(const swift::BuiltinJobType& type);
63-
codeql::BuiltinNativeObjectType translateBuiltinNativeObjectType(
64-
const swift::BuiltinNativeObjectType& type);
65-
codeql::BuiltinRawPointerType translateBuiltinRawPointerType(
66-
const swift::BuiltinRawPointerType& type);
67-
codeql::BuiltinRawUnsafeContinuationType translateBuiltinRawUnsafeContinuationType(
68-
const swift::BuiltinRawUnsafeContinuationType& type);
69-
codeql::BuiltinUnsafeValueBufferType translateBuiltinUnsafeValueBufferType(
70-
const swift::BuiltinUnsafeValueBufferType& type);
71-
codeql::BuiltinVectorType translateBuiltinVectorType(const swift::BuiltinVectorType& type);
7267
codeql::OpenedArchetypeType translateOpenedArchetypeType(const swift::OpenedArchetypeType& type);
7368
codeql::ModuleType translateModuleType(const swift::ModuleType& type);
7469

@@ -79,9 +74,9 @@ class TypeVisitor : public TypeVisitorBase<TypeVisitor> {
7974
codeql::UnarySyntaxSugarType& entry);
8075
void fillReferenceStorageType(const swift::ReferenceStorageType& type,
8176
codeql::ReferenceStorageType& entry);
82-
void emitAnyFunctionType(const swift::AnyFunctionType* type, TrapLabel<AnyFunctionTypeTag> label);
83-
void emitBoundGenericType(swift::BoundGenericType* type, TrapLabel<BoundGenericTypeTag> label);
84-
void emitAnyGenericType(swift::AnyGenericType* type, TrapLabel<AnyGenericTypeTag> label);
77+
void fillAnyFunctionType(const swift::AnyFunctionType& type, codeql::AnyFunctionType& entry);
78+
void fillBoundGenericType(const swift::BoundGenericType& type, codeql::BoundGenericType& entry);
79+
void fillAnyGenericType(const swift::AnyGenericType& type, codeql::AnyGenericType& entry);
8580

8681
template <typename T, typename... Args>
8782
auto createTypeEntry(const T& type, const Args&... args) {

swift/extractor/visitors/VisitorBase.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ class TypeVisitorBase : public swift::TypeVisitor<CrtpSubclass>, protected detai
116116
public:
117117
using VisitorBase::VisitorBase;
118118

119+
// TODO
120+
// swift does not provide const visitors, for the moment we const_cast and promise not to
121+
// change the entities. When all visitors have been turned to translators, we can ditch
122+
// swift::ASTVisitor and roll out our own const-correct TranslatorBase class
123+
template <typename E>
124+
void visit(const E* entity) {
125+
swift::TypeVisitor<CrtpSubclass>::visit(const_cast<E*>(entity));
126+
}
127+
119128
#define TYPE(CLASS, PARENT) DEFINE_VISIT(Type, CLASS, PARENT)
120129
#include "swift/AST/TypeNodes.def"
121130
};

swift/ql/lib/codeql/swift/generated/type/AnyFunctionType.qll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,21 @@ module Generated {
5252
final int getNumberOfParamTypes() { result = count(getAParamType()) }
5353

5454
/**
55-
* Gets the `index`th parameter label of this function type (0-based).
55+
* Gets the `index`th parameter label of this function type (0-based), if it exists.
5656
*/
5757
string getParamLabel(int index) {
5858
result = Synth::convertAnyFunctionTypeToRaw(this).(Raw::AnyFunctionType).getParamLabel(index)
5959
}
6060

6161
/**
62-
* Gets any of the parameter labels of this function type.
62+
* Holds if `getParamLabel(index)` exists.
6363
*/
64-
final string getAParamLabel() { result = getParamLabel(_) }
64+
final predicate hasParamLabel(int index) { exists(getParamLabel(index)) }
6565

6666
/**
67-
* Gets the number of parameter labels of this function type.
67+
* Gets any of the parameter labels of this function type.
6868
*/
69-
final int getNumberOfParamLabels() { result = count(getAParamLabel()) }
69+
final string getAParamLabel() { result = getParamLabel(_) }
7070

7171
/**
7272
* Holds if this type refers to a throwing function.

swift/ql/lib/codeql/swift/generated/type/DependentMemberType.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module Generated {
99
override string getAPrimaryQlClass() { result = "DependentMemberType" }
1010

1111
/**
12-
* Gets the basetype of this dependent member type.
12+
* Gets the base type of this dependent member type.
1313
*
1414
* This includes nodes from the "hidden" AST. It can be overridden in subclasses to change the
1515
* behavior of both the `Immediate` and non-`Immediate` versions.
@@ -22,7 +22,7 @@ module Generated {
2222
}
2323

2424
/**
25-
* Gets the basetype of this dependent member type.
25+
* Gets the base type of this dependent member type.
2626
*/
2727
final Type getBaseType() { result = getImmediateBaseType().resolve() }
2828

swift/ql/lib/codeql/swift/generated/type/TupleType.qll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,20 @@ module Generated {
3434
final int getNumberOfTypes() { result = count(getAType()) }
3535

3636
/**
37-
* Gets the `index`th name of this tuple type (0-based).
37+
* Gets the `index`th name of this tuple type (0-based), if it exists.
3838
*/
3939
string getName(int index) {
4040
result = Synth::convertTupleTypeToRaw(this).(Raw::TupleType).getName(index)
4141
}
4242

4343
/**
44-
* Gets any of the names of this tuple type.
44+
* Holds if `getName(index)` exists.
4545
*/
46-
final string getAName() { result = getName(_) }
46+
final predicate hasName(int index) { exists(getName(index)) }
4747

4848
/**
49-
* Gets the number of names of this tuple type.
49+
* Gets any of the names of this tuple type.
5050
*/
51-
final int getNumberOfNames() { result = count(getAName()) }
51+
final string getAName() { result = getName(_) }
5252
}
5353
}

swift/ql/lib/swift.dbscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1901,7 +1901,7 @@ any_generic_type_parents( //dir=type
19011901

19021902
dependent_member_types( //dir=type
19031903
unique int id: @dependent_member_type,
1904-
int baseType: @type ref,
1904+
int base_type: @type ref,
19051905
int associated_type_decl: @associated_type_decl ref
19061906
);
19071907

swift/schema.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ class TypeRepr(AstNode):
787787
class AnyFunctionType(Type):
788788
result: Type
789789
param_types: list[Type]
790-
param_labels: list[string]
790+
param_labels: list[optional[string]]
791791
is_throwing: predicate | doc("this type refers to a throwing function")
792792
is_async: predicate | doc("this type refers to an `async` function")
793793

@@ -803,7 +803,7 @@ class BuiltinType(Type):
803803
pass
804804

805805
class DependentMemberType(Type):
806-
baseType: Type
806+
base_type: Type
807807
associated_type_decl: AssociatedTypeDecl
808808

809809
class DynamicSelfType(Type):
@@ -853,7 +853,7 @@ class SugarType(Type):
853853

854854
class TupleType(Type):
855855
types: list[Type]
856-
names: list[string]
856+
names: list[optional[string]]
857857

858858
class TypeVariableType(Type):
859859
pass

0 commit comments

Comments
 (0)