Skip to content

Commit 10085d6

Browse files
committed
Bridging: Implement bridges required for ongoing AutoDiff changes
In #83926, part of the changes resolving #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 10085d6

File tree

10 files changed

+331
-10
lines changed

10 files changed

+331
-10
lines changed

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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ public struct NominalFieldsArray : RandomAccessCollection, FormattedLikeArray {
266266
}
267267

268268
public struct EnumCase {
269+
public let enumElementDecl : EnumElementDecl
269270
public let payload: Type?
270271
public let index: Int
271272
}
@@ -288,7 +289,8 @@ public struct EnumCases : CollectionLikeSequence, IteratorProtocol {
288289
caseIterator = caseIterator.getNext()
289290
caseIndex += 1
290291
}
291-
return EnumCase(payload: enumType.bridged.getEnumCasePayload(caseIterator, function.bridged).typeOrNil,
292+
return EnumCase(enumElementDecl: enumType.bridged.getEnumElementDecl(caseIterator).getAs(EnumElementDecl.self),
293+
payload: enumType.bridged.getEnumCasePayload(caseIterator, function.bridged).typeOrNil,
292294
index: caseIndex)
293295
}
294296
return nil
@@ -304,6 +306,10 @@ public struct TupleElementArray : RandomAccessCollection, FormattedLikeArray {
304306
public subscript(_ index: Int) -> Type {
305307
type.bridged.getTupleElementType(index).type
306308
}
309+
310+
public func label(at index: Int) -> swift.Identifier {
311+
type.bridged.getTupleElementLabel(index)
312+
}
307313
}
308314

309315
public struct BoxFieldsArray : RandomAccessCollection, FormattedLikeArray {

include/swift/AST/ASTBridging.h

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,9 @@ class BridgedASTContext {
230230
BridgedAvailabilityMacroMap getAvailabilityMacroMap() const;
231231
};
232232

233+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedParamDecl
234+
BridgedParamDecl_cloneWithoutType(BridgedParamDecl pd);
235+
233236
#define IDENTIFIER_WITH_NAME(Name, _) \
234237
SWIFT_NAME("getter:BridgedASTContext.id_" #Name "(self:)") \
235238
BRIDGED_INLINE swift::Identifier BridgedASTContext_id_##Name( \
@@ -361,6 +364,7 @@ struct BridgedDeclObj {
361364
BRIDGED_INLINE swift::SourceLoc getLoc() const;
362365
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedDeclObj getModuleContext() const;
363366
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedDeclObj getParent() const;
367+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedDeclContext getDeclContext() const;
364368
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef Type_getName() const;
365369
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef Value_getUserFacingName() const;
366370
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE swift::SourceLoc Value_getNameLoc() const;
@@ -378,6 +382,10 @@ struct BridgedDeclObj {
378382
BRIDGED_INLINE bool AbstractFunction_isOverridden() const;
379383
BRIDGED_INLINE bool Destructor_isIsolated() const;
380384
BRIDGED_INLINE bool EnumElementDecl_hasAssociatedValues() const;
385+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedParameterList
386+
EnumElementDecl_getParameterList() const;
387+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef
388+
EnumElementDecl_getNameStr() const;
381389
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef AccessorDecl_getKindName() const;
382390
};
383391

@@ -431,6 +439,23 @@ class BridgedASTNode {
431439
#define ABSTRACT_DECL(Id, Parent) DECL(Id, Parent)
432440
#include "swift/AST/DeclNodes.def"
433441

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

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

655+
SWIFT_NAME("BridgedSourceFile.addTopLevelDecl(self:_:)")
656+
BRIDGED_INLINE void BridgedSourceFile_addTopLevelDecl(BridgedSourceFile sf,
657+
BridgedDecl decl);
658+
620659
SWIFT_NAME("BridgedPatternBindingInitializer.create(declContext:)")
621660
BridgedPatternBindingInitializer
622661
BridgedPatternBindingInitializer_create(BridgedDeclContext cDeclContext);
@@ -1388,6 +1427,10 @@ SWIFT_NAME("BridgedParamDecl.setTypeRepr(self:_:)")
13881427
BRIDGED_INLINE void BridgedParamDecl_setTypeRepr(BridgedParamDecl cDecl,
13891428
BridgedTypeRepr cType);
13901429

1430+
SWIFT_NAME("BridgedParamDecl.setInterfaceType(self:_:)")
1431+
BRIDGED_INLINE void BridgedParamDecl_setInterfaceType(BridgedParamDecl cDecl,
1432+
BridgedASTType cType);
1433+
13911434
/// The various spellings of ownership modifier that can be used in source.
13921435
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedParamSpecifier {
13931436
BridgedParamSpecifierDefault,
@@ -1406,8 +1449,18 @@ BRIDGED_INLINE void
14061449
BridgedParamDecl_setSpecifier(BridgedParamDecl cDecl,
14071450
BridgedParamSpecifier cSpecifier);
14081451

1409-
SWIFT_NAME("BridgedParamDecl.setImplicit(self:)")
1410-
BRIDGED_INLINE void BridgedParamDecl_setImplicit(BridgedParamDecl cDecl);
1452+
SWIFT_NAME("BridgedDecl.setImplicit(self:)")
1453+
BRIDGED_INLINE void BridgedDecl_setImplicit(BridgedDecl cDecl);
1454+
1455+
SWIFT_NAME("BridgedGenericContext.setGenericSignature(self:_:)")
1456+
BRIDGED_INLINE void
1457+
BridgedGenericContext_setGenericSignature(BridgedGenericContext cDecl,
1458+
BridgedGenericSignature cGenSig);
1459+
1460+
SWIFT_NAME("BridgedNominalTypeDecl.addMember(self:_:)")
1461+
BRIDGED_INLINE void
1462+
BridgedNominalTypeDecl_addMember(BridgedNominalTypeDecl cDecl,
1463+
BridgedDecl member);
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.setAccessPublic(self:)")
1731+
void BridgedValueDecl_setAccessPublic(BridgedValueDecl decl);
1732+
16651733
//===----------------------------------------------------------------------===//
16661734
// MARK: AbstractStorageDecl
16671735
//===----------------------------------------------------------------------===//
@@ -2913,6 +2981,10 @@ BridgedParameterList BridgedParameterList_createParsed(
29132981
BridgedASTContext cContext, swift::SourceLoc leftParenLoc,
29142982
BridgedArrayRef cParameters, swift::SourceLoc rightParenLoc);
29152983

2984+
SWIFT_NAME("BridgedParameterList.create(_:parameters:)")
2985+
BridgedParameterList BridgedParameterList_create(BridgedASTContext cContext,
2986+
BridgedArrayRef cParameters);
2987+
29162988
SWIFT_NAME("getter:BridgedParameterList.size(self:)")
29172989
size_t BridgedParameterList_size(BridgedParameterList cParameterList);
29182990

@@ -3017,6 +3089,7 @@ struct BridgedASTType {
30173089
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedGenericSignature getInvocationGenericSignatureOfFunctionType() const;
30183090
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType subst(BridgedSubstitutionMap substMap) const;
30193091
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedConformance checkConformance(BridgedDeclObj proto) const;
3092+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType mapTypeOutOfContext() const;
30203093
};
30213094

30223095
class BridgedCanType {
@@ -3084,13 +3157,23 @@ struct BridgedSubstitutionMap {
30843157
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTTypeArray getReplacementTypes() const;
30853158
};
30863159

3160+
struct BridgedCanGenericSignature;
3161+
30873162
struct BridgedGenericSignature {
30883163
const swift::GenericSignatureImpl * _Nullable impl;
30893164

30903165
BRIDGED_INLINE swift::GenericSignature unbridged() const;
30913166
BridgedOwnedString getDebugDescription() const;
30923167
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTTypeArray getGenericParams() const;
30933168
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType mapTypeIntoContext(BridgedASTType type) const;
3169+
BRIDGED_INLINE BridgedCanGenericSignature getCanonicalSignature() const;
3170+
};
3171+
3172+
struct BridgedCanGenericSignature {
3173+
const swift::GenericSignatureImpl *_Nullable impl;
3174+
3175+
BRIDGED_INLINE swift::CanGenericSignature unbridged() const;
3176+
BRIDGED_INLINE BridgedGenericSignature getGenericSignature() const;
30943177
};
30953178

30963179
struct BridgedFingerprint {

include/swift/AST/ASTBridgingImpl.h

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ BridgedStringRef BridgedASTContext_allocateCopyString(BridgedASTContext bridged,
9494
}
9595
#include "swift/AST/KnownIdentifiers.def"
9696

97+
BridgedParamDecl BridgedParamDecl_cloneWithoutType(BridgedParamDecl pd) {
98+
return swift::ParamDecl::cloneWithoutType(pd.unbridged()->getASTContext(),
99+
pd.unbridged());
100+
}
101+
97102
//===----------------------------------------------------------------------===//
98103
// MARK: BridgedDeclContext
99104
//===----------------------------------------------------------------------===//
@@ -137,6 +142,12 @@ bool BridgedSourceFile_isScriptMode(BridgedSourceFile sf) {
137142
return sf.unbridged()->isScriptMode();
138143
}
139144

145+
void BridgedSourceFile_addTopLevelDecl(BridgedSourceFile sf, BridgedDecl decl) {
146+
auto &file = sf.unbridged()->getOrCreateSynthesizedFile();
147+
file.addTopLevelDecl(decl.unbridged());
148+
file.getParentModule()->clearLookupCache();
149+
}
150+
140151
//===----------------------------------------------------------------------===//
141152
// MARK: BridgedDeclObj
142153
//===----------------------------------------------------------------------===//
@@ -153,6 +164,10 @@ OptionalBridgedDeclObj BridgedDeclObj::getParent() const {
153164
return {unbridged()->getDeclContext()->getAsDecl()};
154165
}
155166

167+
BridgedDeclContext BridgedDeclObj::getDeclContext() const {
168+
return {unbridged()->getDeclContext()};
169+
}
170+
156171
BridgedStringRef BridgedDeclObj::Type_getName() const {
157172
return getAs<swift::TypeDecl>()->getName().str();
158173
}
@@ -223,6 +238,14 @@ bool BridgedDeclObj::EnumElementDecl_hasAssociatedValues() const {
223238
return getAs<swift::EnumElementDecl>()->hasAssociatedValues();
224239
}
225240

241+
BridgedParameterList BridgedDeclObj::EnumElementDecl_getParameterList() const {
242+
return getAs<swift::EnumElementDecl>()->getParameterList();
243+
}
244+
245+
BridgedStringRef BridgedDeclObj::EnumElementDecl_getNameStr() const {
246+
return getAs<swift::EnumElementDecl>()->getNameStr();
247+
}
248+
226249
//===----------------------------------------------------------------------===//
227250
// MARK: BridgedASTNode
228251
//===----------------------------------------------------------------------===//
@@ -331,15 +354,30 @@ void BridgedParamDecl_setTypeRepr(BridgedParamDecl cDecl,
331354
cDecl.unbridged()->setTypeRepr(cType.unbridged());
332355
}
333356

357+
void BridgedParamDecl_setInterfaceType(BridgedParamDecl cDecl,
358+
BridgedASTType cType) {
359+
cDecl.unbridged()->setInterfaceType(cType.unbridged());
360+
}
361+
334362
void BridgedParamDecl_setSpecifier(BridgedParamDecl cDecl,
335363
BridgedParamSpecifier cSpecifier) {
336364
cDecl.unbridged()->setSpecifier(unbridge(cSpecifier));
337365
}
338366

339-
void BridgedParamDecl_setImplicit(BridgedParamDecl cDecl) {
367+
void BridgedDecl_setImplicit(BridgedDecl cDecl) {
340368
cDecl.unbridged()->setImplicit();
341369
}
342370

371+
void BridgedGenericContext_setGenericSignature(
372+
BridgedGenericContext cDecl, BridgedGenericSignature cGenSig) {
373+
cDecl.unbridged()->setGenericSignature(cGenSig.unbridged());
374+
}
375+
376+
void BridgedNominalTypeDecl_addMember(BridgedNominalTypeDecl cDecl,
377+
BridgedDecl member) {
378+
cDecl.unbridged()->addMember(member.unbridged());
379+
}
380+
343381
//===----------------------------------------------------------------------===//
344382
// MARK: BridgedSubscriptDecl
345383
//===----------------------------------------------------------------------===//
@@ -607,6 +645,10 @@ BridgedConformance BridgedASTType::checkConformance(BridgedDeclObj proto) const
607645
return swift::checkConformance(unbridged(), proto.getAs<swift::ProtocolDecl>(), /*allowMissing=*/ false);
608646
}
609647

648+
BridgedASTType BridgedASTType::mapTypeOutOfContext() const {
649+
return {unbridged()->mapTypeOutOfContext().getPointer()};
650+
}
651+
610652
static_assert((int)BridgedASTType::TraitResult::IsNot == (int)swift::TypeTraitResult::IsNot);
611653
static_assert((int)BridgedASTType::TraitResult::CanBe == (int)swift::TypeTraitResult::CanBe);
612654
static_assert((int)BridgedASTType::TraitResult::Is == (int)swift::TypeTraitResult::Is);
@@ -824,6 +866,20 @@ BridgedASTType BridgedGenericSignature::mapTypeIntoContext(BridgedASTType type)
824866
return {unbridged().getGenericEnvironment()->mapTypeIntoContext(type.unbridged()).getPointer()};
825867
}
826868

869+
BridgedCanGenericSignature
870+
BridgedGenericSignature::getCanonicalSignature() const {
871+
return BridgedCanGenericSignature{impl};
872+
}
873+
874+
swift::CanGenericSignature BridgedCanGenericSignature::unbridged() const {
875+
return swift::GenericSignature(impl).getCanonicalSignature();
876+
}
877+
878+
BridgedGenericSignature
879+
BridgedCanGenericSignature::getGenericSignature() const {
880+
return BridgedGenericSignature{impl};
881+
}
882+
827883
//===----------------------------------------------------------------------===//
828884
// MARK: BridgedFingerprint
829885
//===----------------------------------------------------------------------===//

include/swift/AST/ASTBridgingWrappers.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@
9595
// optional parameters.
9696
AST_BRIDGING_WRAPPER_NULLABLE(Decl)
9797
AST_BRIDGING_WRAPPER_NONNULL(DeclContext)
98+
AST_BRIDGING_WRAPPER_NONNULL(GenericContext)
9899
AST_BRIDGING_WRAPPER_NONNULL(SourceFile)
99100
AST_BRIDGING_WRAPPER_NULLABLE(Stmt)
100101
AST_BRIDGING_WRAPPER_NULLABLE(Expr)

0 commit comments

Comments
 (0)