Skip to content

Commit 18e8ea3

Browse files
committed
Bridging: Implement bridges required for ongoing AutoDiff changes
In swiftlang#83926, part of the changes resolving swiftlang#68944 is submitted. The AutoDiff closure specialization optimizer pass relies on several AST-level bridges not implemented yet. This patch introduces these missing bridges.
1 parent 52a163f commit 18e8ea3

File tree

16 files changed

+433
-10
lines changed

16 files changed

+433
-10
lines changed

SwiftCompilerSources/Sources/AST/Declarations.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public class NominalTypeDecl: GenericTypeDecl {
5656
final public var valueTypeDestructor: DestructorDecl? {
5757
bridged.NominalType_getValueTypeDestructor().getAs(DestructorDecl.self)
5858
}
59+
60+
public var declaredInterfaceType : AST.`Type` {
61+
AST.`Type`(bridged: bridged.NominalType_getDeclaredInterfaceType())
62+
}
5963
}
6064

6165
final public class EnumDecl: NominalTypeDecl {
@@ -118,6 +122,8 @@ final public class MacroDecl: ValueDecl {}
118122

119123
final public class EnumElementDecl: ValueDecl {
120124
public var hasAssociatedValues: Bool { bridged.EnumElementDecl_hasAssociatedValues() }
125+
public var parameterList: BridgedParameterList { bridged.EnumElementDecl_getParameterList() }
126+
public var name: StringRef { StringRef(bridged: bridged.EnumElementDecl_getNameStr()) }
121127
}
122128

123129
final public class ExtensionDecl: Decl {}

SwiftCompilerSources/Sources/AST/GenericSignature.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,18 @@ public struct GenericSignature: CustomStringConvertible, NoReflectionChildren {
3535
}
3636

3737
public var isEmpty: Bool { bridged.impl == nil }
38+
39+
public var canonicalSignature: CanGenericSignature { CanGenericSignature(bridged: bridged.getCanonicalSignature()) }
40+
}
41+
42+
public struct CanGenericSignature {
43+
public let bridged: BridgedCanGenericSignature
44+
45+
public init(bridged: BridgedCanGenericSignature) {
46+
self.bridged = bridged
47+
}
48+
49+
public var isEmpty: Bool { bridged.impl == nil }
50+
51+
public var genericSignature: GenericSignature { GenericSignature(bridged: bridged.getGenericSignature()) }
3852
}

SwiftCompilerSources/Sources/AST/Type.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ public struct Type: TypeProperties, CustomStringConvertible, NoReflectionChildre
6666
public func subst(with substitutionMap: SubstitutionMap) -> Type {
6767
return Type(bridged: bridged.subst(substitutionMap.bridged))
6868
}
69+
70+
public func mapTypeOutOfContext() -> Type {
71+
return Type(bridged: bridged.mapTypeOutOfContext())
72+
}
73+
74+
public func getReducedType(sig: GenericSignature) -> CanonicalType {
75+
CanonicalType(bridged: bridged.getReducedType(sig.bridged))
76+
}
6977
}
7078

7179
/// A Type that is statically known to be canonical.
@@ -86,6 +94,10 @@ public struct CanonicalType: TypeProperties, CustomStringConvertible, NoReflecti
8694
public func subst(with substitutionMap: SubstitutionMap) -> CanonicalType {
8795
return rawType.subst(with: substitutionMap).canonical
8896
}
97+
98+
public func SILFunctionType_getSubstGenericSignature() -> CanGenericSignature {
99+
CanGenericSignature(bridged: bridged.SILFunctionType_getSubstGenericSignature())
100+
}
89101
}
90102

91103
/// Implements the common members of `AST.Type`, `AST.CanonicalType` and `SIL.Type`.

SwiftCompilerSources/Sources/SIL/ASTExtensions.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ extension CanonicalType {
3636
precondition(isBox)
3737
return BoxFieldsArray(boxType: self, function: function)
3838
}
39+
40+
public func loweredType(in function: Function) -> Type {
41+
function.bridged.getLoweredType(bridged).type
42+
}
3943
}
4044

4145
extension Decl {

SwiftCompilerSources/Sources/SIL/Context.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ extension Context {
5555
}
5656
}
5757

58+
public func getTupleType(elements: [AST.`Type`]) -> AST.`Type` {
59+
let bridgedElements = elements.map { $0.bridged }
60+
return bridgedElements.withBridgedArrayRef {
61+
AST.`Type`(bridged: _bridged.getTupleType($0))
62+
}
63+
}
64+
65+
public func getTupleTypeWithLabels(elements: [AST.`Type`], labels: [swift.Identifier]) -> AST.`Type` {
66+
assert(elements.count == labels.count)
67+
return elements.withBridgedArrayRef{
68+
eltArr in labels.withBridgedArrayRef{labelsArr in
69+
AST.`Type`(bridged: _bridged.getTupleTypeWithLabels(eltArr, labelsArr))}}
70+
}
71+
5872
public var swiftArrayDecl: NominalTypeDecl {
5973
_bridged.getSwiftArrayDecl().getAs(NominalTypeDecl.self)
6074
}

SwiftCompilerSources/Sources/SIL/Function.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash
8888
return AST.`Type`(bridged: bridged.mapTypeIntoContext(type.bridged))
8989
}
9090

91+
public func mapTypeIntoContext(_ type: Type) -> Type {
92+
return Type(bridged: bridged.mapTypeIntoContext(type.bridged))
93+
}
94+
9195
/// Returns true if the function is a definition and not only an external declaration.
9296
///
9397
/// This is the case if the function contains a body, i.e. some basic blocks.

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public struct Type : TypeProperties, CustomStringConvertible, NoReflectionChildr
2727

2828
public var isAddress: Bool { bridged.isAddress() }
2929
public var isObject: Bool { !isAddress }
30+
public var category: ValueCategory { ValueCategory(bridged: bridged.getCategory()) }
3031

3132
public var addressType: Type { bridged.getAddressType().type }
3233
public var objectType: Type { bridged.getObjectType().type }
@@ -214,6 +215,12 @@ public struct Type : TypeProperties, CustomStringConvertible, NoReflectionChildr
214215
}
215216
return false
216217
}
218+
219+
public func mapTypeOutOfContext() -> Type { bridged.mapTypeOutOfContext().type }
220+
221+
public static func getPrimitiveType(canType: CanonicalType, silValueCategory: ValueCategory) -> Type {
222+
BridgedType.getPrimitiveType(canType: canType.bridged, silValueCategory: silValueCategory._bridged).type
223+
}
217224
}
218225

219226
extension Type: Equatable {
@@ -266,6 +273,7 @@ public struct NominalFieldsArray : RandomAccessCollection, FormattedLikeArray {
266273
}
267274

268275
public struct EnumCase {
276+
public let enumElementDecl : EnumElementDecl
269277
public let payload: Type?
270278
public let index: Int
271279
}
@@ -288,7 +296,8 @@ public struct EnumCases : CollectionLikeSequence, IteratorProtocol {
288296
caseIterator = caseIterator.getNext()
289297
caseIndex += 1
290298
}
291-
return EnumCase(payload: enumType.bridged.getEnumCasePayload(caseIterator, function.bridged).typeOrNil,
299+
return EnumCase(enumElementDecl: enumType.bridged.getEnumElementDecl(caseIterator).getAs(EnumElementDecl.self),
300+
payload: enumType.bridged.getEnumCasePayload(caseIterator, function.bridged).typeOrNil,
292301
index: caseIndex)
293302
}
294303
return nil
@@ -304,6 +313,10 @@ public struct TupleElementArray : RandomAccessCollection, FormattedLikeArray {
304313
public subscript(_ index: Int) -> Type {
305314
type.bridged.getTupleElementType(index).type
306315
}
316+
317+
public func label(at index: Int) -> swift.Identifier {
318+
type.bridged.getTupleElementLabel(index)
319+
}
307320
}
308321

309322
public struct BoxFieldsArray : RandomAccessCollection, FormattedLikeArray {

SwiftCompilerSources/Sources/SIL/Value.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,27 @@ public protocol Value : AnyObject, CustomStringConvertible {
4141
var isLexical: Bool { get }
4242
}
4343

44+
public enum ValueCategory {
45+
case address
46+
case object
47+
48+
public init(bridged: BridgedValueCategory) {
49+
switch bridged {
50+
case .Address: self = .address
51+
case .Object: self = .object
52+
default:
53+
fatalError("unsupported value category")
54+
}
55+
}
56+
57+
public var _bridged: BridgedValueCategory {
58+
switch self {
59+
case .address: return BridgedValueCategory.Address
60+
case .object: return BridgedValueCategory.Object
61+
}
62+
}
63+
}
64+
4465
public enum Ownership {
4566
/// A Value with `unowned` ownership kind is an independent value that
4667
/// has a lifetime that is only guaranteed to last until the next program

0 commit comments

Comments
 (0)