Skip to content

Commit 3fb7032

Browse files
committed
SIL Type: add Type.aggregateIsOrContains
1 parent 8cb2caa commit 3fb7032

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,29 @@ public struct Type : CustomStringConvertible, NoReflectionChildren {
154154
return idx >= 0 ? idx : nil
155155
}
156156

157+
/// Returns true if this is a struct, enum or tuple and `otherType` is contained in this type - or is the same type.
158+
public func aggregateIsOrContains(_ otherType: Type, in function: Function) -> Bool {
159+
if self == otherType {
160+
return true
161+
}
162+
if isStruct {
163+
guard let fields = getNominalFields(in: function) else {
164+
return true
165+
}
166+
return fields.contains { $0.aggregateIsOrContains(otherType, in: function) }
167+
}
168+
if isTuple {
169+
return tupleElements.contains { $0.aggregateIsOrContains(otherType, in: function) }
170+
}
171+
if isEnum {
172+
guard let cases = getEnumCases(in: function) else {
173+
return true
174+
}
175+
return cases.contains { $0.payload?.aggregateIsOrContains(otherType, in: function) ?? false }
176+
}
177+
return false
178+
}
179+
157180
// compiling bridged.getFunctionTypeWithNoEscape crashes the 5.10 Windows compiler
158181
#if !os(Windows)
159182
// TODO: https://github.com/apple/swift/issues/73253

0 commit comments

Comments
 (0)