Skip to content

Commit 807a589

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 807a589

File tree

15 files changed

+430
-10
lines changed

15 files changed

+430
-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/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

include/swift/AST/ASTBridging.h

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class BridgedCanType;
8484
class BridgedASTContext;
8585
struct BridgedSubstitutionMap;
8686
struct BridgedGenericSignature;
87+
struct BridgedCanGenericSignature;
8788
struct BridgedConformance;
8889
class BridgedParameterList;
8990

@@ -369,6 +370,7 @@ struct BridgedDeclObj {
369370
BRIDGED_INLINE bool AbstractStorage_isConst() const;
370371
BRIDGED_INLINE bool GenericType_isGenericAtAnyLevel() const;
371372
BRIDGED_INLINE bool NominalType_isGlobalActor() const;
373+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType NominalType_getDeclaredInterfaceType() const;
372374
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedDeclObj NominalType_getValueTypeDestructor() const;
373375
BRIDGED_INLINE bool Enum_hasRawType() const;
374376
BRIDGED_INLINE bool Struct_hasUnreferenceableStorage() const;
@@ -378,6 +380,10 @@ struct BridgedDeclObj {
378380
BRIDGED_INLINE bool AbstractFunction_isOverridden() const;
379381
BRIDGED_INLINE bool Destructor_isIsolated() const;
380382
BRIDGED_INLINE bool EnumElementDecl_hasAssociatedValues() const;
383+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedParameterList
384+
EnumElementDecl_getParameterList() const;
385+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef
386+
EnumElementDecl_getNameStr() const;
381387
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef AccessorDecl_getKindName() const;
382388
};
383389

@@ -431,6 +437,23 @@ class BridgedASTNode {
431437
#define ABSTRACT_DECL(Id, Parent) DECL(Id, Parent)
432438
#include "swift/AST/DeclNodes.def"
433439

440+
// Declare `.asValueDecl` on each BridgedXXXDecl type that's also a
441+
// ValueDecl.
442+
#define DECL(Id, Parent)
443+
#define VALUE_DECL(Id, Parent) \
444+
SWIFT_NAME("getter:Bridged" #Id "Decl.asValueDecl(self:)") \
445+
BridgedValueDecl Bridged##Id##Decl_asValueDecl(Bridged##Id##Decl decl);
446+
#include "swift/AST/DeclNodes.def"
447+
448+
// Declare `.asNominalTypeDecl` on each BridgedXXXDecl type that's also a
449+
// NominalTypeDecl.
450+
#define DECL(Id, Parent)
451+
#define NOMINAL_TYPE_DECL(Id, Parent) \
452+
SWIFT_NAME("getter:Bridged" #Id "Decl.asNominalTypeDecl(self:)") \
453+
BridgedNominalTypeDecl Bridged##Id##Decl_asNominalTypeDecl( \
454+
Bridged##Id##Decl decl);
455+
#include "swift/AST/DeclNodes.def"
456+
434457
// Declare `.asDeclContext` on each BridgedXXXDecl type that's also a
435458
// DeclContext.
436459
#define DECL(Id, Parent)
@@ -440,6 +463,16 @@ class BridgedASTNode {
440463
#define ABSTRACT_CONTEXT_DECL(Id, Parent) CONTEXT_DECL(Id, Parent)
441464
#include "swift/AST/DeclNodes.def"
442465

466+
// Declare `.asGenericContext` on each BridgedXXXDecl type that's also a
467+
// GenericContext.
468+
#define DECL(Id, Parent)
469+
#define GENERIC_DECL(Id, Parent) \
470+
SWIFT_NAME("getter:Bridged" #Id "Decl.asGenericContext(self:)") \
471+
BridgedGenericContext Bridged##Id##Decl_asGenericContext( \
472+
Bridged##Id##Decl decl);
473+
#define ITERABLE_GENERIC_DECL(Id, Parent) GENERIC_DECL(Id, Parent)
474+
#include "swift/AST/DeclNodes.def"
475+
443476
// Declare `.asStmt` on each BridgedXXXStmt type, which upcasts a wrapper for
444477
// a Stmt subclass to a BridgedStmt.
445478
#define STMT(Id, Parent) \
@@ -617,6 +650,10 @@ BridgedDeclContext_getParentSourceFile(BridgedDeclContext dc);
617650
SWIFT_NAME("getter:BridgedSourceFile.isScriptMode(self:)")
618651
BRIDGED_INLINE bool BridgedSourceFile_isScriptMode(BridgedSourceFile sf);
619652

653+
SWIFT_NAME("BridgedSourceFile.addTopLevelDecl(self:_:)")
654+
BRIDGED_INLINE void BridgedSourceFile_addTopLevelDecl(BridgedSourceFile sf,
655+
BridgedDecl decl);
656+
620657
SWIFT_NAME("BridgedPatternBindingInitializer.create(declContext:)")
621658
BridgedPatternBindingInitializer
622659
BridgedPatternBindingInitializer_create(BridgedDeclContext cDeclContext);
@@ -1337,6 +1374,9 @@ SWIFT_NAME("BridgedDecl.forEachDeclToHoist(self:_:)")
13371374
void BridgedDecl_forEachDeclToHoist(BridgedDecl decl,
13381375
BridgedSwiftClosure closure);
13391376

1377+
SWIFT_NAME("BridgedDecl.getDeclContext(self:)")
1378+
BridgedDeclContext BridgedDecl_getDeclContext(BridgedDecl decl);
1379+
13401380
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedStaticSpelling {
13411381
BridgedStaticSpellingNone,
13421382
BridgedStaticSpellingStatic,
@@ -1384,10 +1424,18 @@ BridgedParamDecl BridgedParamDecl_createParsed(
13841424
swift::SourceLoc paramNameLoc, BridgedNullableExpr defaultValue,
13851425
BridgedNullableDefaultArgumentInitializer cDefaultArgumentInitContext);
13861426

1427+
SWIFT_NAME("BridgedParamDecl.cloneWithoutType(self:)")
1428+
BRIDGED_INLINE BridgedParamDecl
1429+
BridgedParamDecl_cloneWithoutType(BridgedParamDecl cDecl);
1430+
13871431
SWIFT_NAME("BridgedParamDecl.setTypeRepr(self:_:)")
13881432
BRIDGED_INLINE void BridgedParamDecl_setTypeRepr(BridgedParamDecl cDecl,
13891433
BridgedTypeRepr cType);
13901434

1435+
SWIFT_NAME("BridgedParamDecl.setInterfaceType(self:_:)")
1436+
BRIDGED_INLINE void BridgedParamDecl_setInterfaceType(BridgedParamDecl cDecl,
1437+
BridgedASTType cType);
1438+
13911439
/// The various spellings of ownership modifier that can be used in source.
13921440
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedParamSpecifier {
13931441
BridgedParamSpecifierDefault,
@@ -1406,8 +1454,13 @@ BRIDGED_INLINE void
14061454
BridgedParamDecl_setSpecifier(BridgedParamDecl cDecl,
14071455
BridgedParamSpecifier cSpecifier);
14081456

1409-
SWIFT_NAME("BridgedParamDecl.setImplicit(self:)")
1410-
BRIDGED_INLINE void BridgedParamDecl_setImplicit(BridgedParamDecl cDecl);
1457+
SWIFT_NAME("BridgedDecl.setImplicit(self:)")
1458+
BRIDGED_INLINE void BridgedDecl_setImplicit(BridgedDecl cDecl);
1459+
1460+
SWIFT_NAME("BridgedGenericContext.setGenericSignature(self:_:)")
1461+
BRIDGED_INLINE void
1462+
BridgedGenericContext_setGenericSignature(BridgedGenericContext cDecl,
1463+
BridgedGenericSignature cGenSig);
14111464

14121465
SWIFT_NAME("BridgedConstructorDecl.setParsedBody(self:_:)")
14131466
void BridgedConstructorDecl_setParsedBody(BridgedConstructorDecl decl,
@@ -1471,14 +1524,20 @@ void BridgedExtensionDecl_setParsedMembers(BridgedExtensionDecl decl,
14711524
SWIFT_NAME(
14721525
"BridgedEnumDecl.createParsed(_:declContext:enumKeywordLoc:name:nameLoc:"
14731526
"genericParamList:inheritedTypes:genericWhereClause:braceRange:)")
1474-
BridgedNominalTypeDecl BridgedEnumDecl_createParsed(
1527+
BridgedEnumDecl BridgedEnumDecl_createParsed(
14751528
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
14761529
swift::SourceLoc enumKeywordLoc, swift::Identifier name,
14771530
swift::SourceLoc nameLoc, BridgedNullableGenericParamList genericParamList,
14781531
BridgedArrayRef cInheritedTypes,
14791532
BridgedNullableTrailingWhereClause genericWhereClause,
14801533
swift::SourceRange braceRange);
14811534

1535+
SWIFT_NAME("BridgedEnumDecl.create(_:declContext:name:genericParamList:)")
1536+
BridgedEnumDecl
1537+
BridgedEnumDecl_create(BridgedASTContext cContext,
1538+
BridgedDeclContext cDeclContext, BridgedStringRef name,
1539+
BridgedNullableGenericParamList genericParamList);
1540+
14821541
SWIFT_NAME(
14831542
"BridgedEnumCaseDecl.createParsed(declContext:caseKeywordLoc:elements:)")
14841543
BridgedEnumCaseDecl
@@ -1494,6 +1553,12 @@ BridgedEnumElementDecl BridgedEnumElementDecl_createParsed(
14941553
BridgedNullableParameterList parameterList, swift::SourceLoc equalsLoc,
14951554
BridgedNullableExpr opaqueRawValue);
14961555

1556+
SWIFT_NAME("BridgedEnumElementDecl.create(_:declContext:name:"
1557+
"parameterList:)")
1558+
BridgedEnumElementDecl BridgedEnumElementDecl_create(
1559+
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
1560+
BridgedStringRef name, BridgedParameterList parameterList);
1561+
14971562
SWIFT_NAME("BridgedStructDecl.createParsed(_:declContext:structKeywordLoc:name:"
14981563
"nameLoc:genericParamList:inheritedTypes:genericWhereClause:"
14991564
"braceRange:)")
@@ -1662,6 +1727,9 @@ void BridgedTopLevelCodeDecl_dump(BridgedTopLevelCodeDecl decl);
16621727
SWIFT_NAME("BridgedDecl.dump(self:)")
16631728
void BridgedDecl_dump(BridgedDecl decl);
16641729

1730+
SWIFT_NAME("BridgedValueDecl.setAccess(self:_:)")
1731+
void BridgedValueDecl_setAccess(BridgedValueDecl decl, swift::AccessLevel accessLevel);
1732+
16651733
//===----------------------------------------------------------------------===//
16661734
// MARK: AbstractStorageDecl
16671735
//===----------------------------------------------------------------------===//
@@ -1711,6 +1779,11 @@ SWIFT_NAME("BridgedNominalTypeDecl.getSourceLocation(self:)")
17111779
BRIDGED_INLINE swift::SourceLoc
17121780
BridgedNominalTypeDecl_getSourceLocation(BridgedNominalTypeDecl decl);
17131781

1782+
SWIFT_NAME("BridgedNominalTypeDecl.addMember(self:_:)")
1783+
BRIDGED_INLINE void
1784+
BridgedNominalTypeDecl_addMember(BridgedNominalTypeDecl cDecl,
1785+
BridgedDecl member);
1786+
17141787
//===----------------------------------------------------------------------===//
17151788
// MARK: SubscriptDecl
17161789
//===----------------------------------------------------------------------===//
@@ -2913,6 +2986,10 @@ BridgedParameterList BridgedParameterList_createParsed(
29132986
BridgedASTContext cContext, swift::SourceLoc leftParenLoc,
29142987
BridgedArrayRef cParameters, swift::SourceLoc rightParenLoc);
29152988

2989+
SWIFT_NAME("BridgedParameterList.create(_:parameters:)")
2990+
BridgedParameterList BridgedParameterList_create(BridgedASTContext cContext,
2991+
BridgedArrayRef cParameters);
2992+
29162993
SWIFT_NAME("getter:BridgedParameterList.size(self:)")
29172994
size_t BridgedParameterList_size(BridgedParameterList cParameterList);
29182995

@@ -3017,6 +3094,8 @@ struct BridgedASTType {
30173094
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedGenericSignature getInvocationGenericSignatureOfFunctionType() const;
30183095
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType subst(BridgedSubstitutionMap substMap) const;
30193096
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedConformance checkConformance(BridgedDeclObj proto) const;
3097+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType mapTypeOutOfContext() const;
3098+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType getReducedType(BridgedGenericSignature sig) const;
30203099
};
30213100

30223101
class BridgedCanType {
@@ -3027,6 +3106,7 @@ class BridgedCanType {
30273106
BRIDGED_INLINE BridgedCanType(swift::CanType ty);
30283107
BRIDGED_INLINE swift::CanType unbridged() const;
30293108
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType getRawType() const;
3109+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanGenericSignature SILFunctionType_getSubstGenericSignature() const;
30303110
};
30313111

30323112
struct BridgedASTTypeArray {
@@ -3091,6 +3171,14 @@ struct BridgedGenericSignature {
30913171
BridgedOwnedString getDebugDescription() const;
30923172
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTTypeArray getGenericParams() const;
30933173
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType mapTypeIntoContext(BridgedASTType type) const;
3174+
BRIDGED_INLINE BridgedCanGenericSignature getCanonicalSignature() const;
3175+
};
3176+
3177+
struct BridgedCanGenericSignature {
3178+
const swift::GenericSignatureImpl *_Nullable impl;
3179+
3180+
BRIDGED_INLINE swift::CanGenericSignature unbridged() const;
3181+
BRIDGED_INLINE BridgedGenericSignature getGenericSignature() const;
30943182
};
30953183

30963184
struct BridgedFingerprint {

0 commit comments

Comments
 (0)