Swift: extract DynamicSelfType

This commit is contained in:
Paolo Tranquilli
2022-06-24 16:49:58 +02:00
parent 4281605ba7
commit 346110e8dd
9 changed files with 36 additions and 5 deletions

View File

@@ -93,6 +93,7 @@ DependentMemberType:
DynamicSelfType:
_extends: Type
static_self_type: Type
ErrorType:
_extends: Type

View File

@@ -253,11 +253,18 @@ void TypeVisitor::fillArchetypeType(const swift::ArchetypeType& type, ArchetypeT
entry.superclass = dispatcher_.fetchOptionalLabel(type.getSuperclass());
fillType(type, entry);
}
codeql::ExistentialType TypeVisitor::translateExistentialType(const swift::ExistentialType& type) {
codeql::ExistentialType entry{dispatcher_.assignNewLabel(type)};
entry.constraint = dispatcher_.fetchLabel(type.getConstraintType());
fillType(type, entry);
return entry;
}
codeql::DynamicSelfType TypeVisitor::translateDynamicSelfType(const swift::DynamicSelfType& type) {
codeql::DynamicSelfType entry{dispatcher_.assignNewLabel(type)};
entry.static_self_type = dispatcher_.fetchLabel(type.getSelfType());
fillType(type, entry);
return entry;
}
} // namespace codeql

View File

@@ -39,6 +39,7 @@ class TypeVisitor : public TypeVisitorBase<TypeVisitor> {
const swift::PrimaryArchetypeType& type);
codeql::NestedArchetypeType translateNestedArchetypeType(const swift::NestedArchetypeType& type);
codeql::ExistentialType translateExistentialType(const swift::ExistentialType& type);
codeql::DynamicSelfType translateDynamicSelfType(const swift::DynamicSelfType& type);
private:
void fillType(const swift::TypeBase& type, codeql::Type& entry);

View File

@@ -3,4 +3,11 @@ import codeql.swift.elements.type.Type
class DynamicSelfTypeBase extends @dynamic_self_type, Type {
override string getAPrimaryQlClass() { result = "DynamicSelfType" }
Type getStaticSelfType() {
exists(Type x |
dynamic_self_types(this, x) and
result = x.resolve()
)
}
}

View File

@@ -244,7 +244,8 @@ dependent_member_types( //dir=type
);
dynamic_self_types( //dir=type
unique int id: @dynamic_self_type
unique int id: @dynamic_self_type,
int static_self_type: @type ref
);
error_types( //dir=type

View File

@@ -0,0 +1 @@
| Self | getDiagnosticsName: | Self | getCanonicalType: | Self | getStaticSelfType: | X |

View File

@@ -0,0 +1,13 @@
// generated by codegen/codegen.py
import codeql.swift.elements
import TestUtils
from DynamicSelfType x, string getDiagnosticsName, Type getCanonicalType, Type getStaticSelfType
where
toBeTested(x) and
not x.isUnknown() and
getDiagnosticsName = x.getDiagnosticsName() and
getCanonicalType = x.getCanonicalType() and
getStaticSelfType = x.getStaticSelfType()
select x, "getDiagnosticsName:", getDiagnosticsName, "getCanonicalType:", getCanonicalType,
"getStaticSelfType:", getStaticSelfType

View File

@@ -1,4 +0,0 @@
// generated by codegen/codegen.py
After a swift source file is added in this directory and codegen/codegen.py is run again, test queries
will appear and this file will be deleted

View File

@@ -0,0 +1,4 @@
class X {
class func create() -> Self { return Self() }
required init() {}
}