Skip to content

Commit 821b8af

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 821b8af

File tree

14 files changed

+409
-10
lines changed

14 files changed

+409
-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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ 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+
}
6973
}
7074

7175
/// A Type that is statically known to be canonical.

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: 8 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 }
@@ -266,6 +267,7 @@ public struct NominalFieldsArray : RandomAccessCollection, FormattedLikeArray {
266267
}
267268

268269
public struct EnumCase {
270+
public let enumElementDecl : EnumElementDecl
269271
public let payload: Type?
270272
public let index: Int
271273
}
@@ -288,7 +290,8 @@ public struct EnumCases : CollectionLikeSequence, IteratorProtocol {
288290
caseIterator = caseIterator.getNext()
289291
caseIndex += 1
290292
}
291-
return EnumCase(payload: enumType.bridged.getEnumCasePayload(caseIterator, function.bridged).typeOrNil,
293+
return EnumCase(enumElementDecl: enumType.bridged.getEnumElementDecl(caseIterator).getAs(EnumElementDecl.self),
294+
payload: enumType.bridged.getEnumCasePayload(caseIterator, function.bridged).typeOrNil,
292295
index: caseIndex)
293296
}
294297
return nil
@@ -304,6 +307,10 @@ public struct TupleElementArray : RandomAccessCollection, FormattedLikeArray {
304307
public subscript(_ index: Int) -> Type {
305308
type.bridged.getTupleElementType(index).type
306309
}
310+
311+
public func label(at index: Int) -> swift.Identifier {
312+
type.bridged.getTupleElementLabel(index)
313+
}
307314
}
308315

309316
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: 90 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ struct BridgedDeclObj {
369369
BRIDGED_INLINE bool AbstractStorage_isConst() const;
370370
BRIDGED_INLINE bool GenericType_isGenericAtAnyLevel() const;
371371
BRIDGED_INLINE bool NominalType_isGlobalActor() const;
372+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType NominalType_getDeclaredInterfaceType() const;
372373
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedDeclObj NominalType_getValueTypeDestructor() const;
373374
BRIDGED_INLINE bool Enum_hasRawType() const;
374375
BRIDGED_INLINE bool Struct_hasUnreferenceableStorage() const;
@@ -378,6 +379,10 @@ struct BridgedDeclObj {
378379
BRIDGED_INLINE bool AbstractFunction_isOverridden() const;
379380
BRIDGED_INLINE bool Destructor_isIsolated() const;
380381
BRIDGED_INLINE bool EnumElementDecl_hasAssociatedValues() const;
382+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedParameterList
383+
EnumElementDecl_getParameterList() const;
384+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef
385+
EnumElementDecl_getNameStr() const;
381386
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef AccessorDecl_getKindName() const;
382387
};
383388

@@ -431,6 +436,23 @@ class BridgedASTNode {
431436
#define ABSTRACT_DECL(Id, Parent) DECL(Id, Parent)
432437
#include "swift/AST/DeclNodes.def"
433438

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

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

652+
SWIFT_NAME("BridgedSourceFile.addTopLevelDecl(self:_:)")
653+
BRIDGED_INLINE void BridgedSourceFile_addTopLevelDecl(BridgedSourceFile sf,
654+
BridgedDecl decl);
655+
620656
SWIFT_NAME("BridgedPatternBindingInitializer.create(declContext:)")
621657
BridgedPatternBindingInitializer
622658
BridgedPatternBindingInitializer_create(BridgedDeclContext cDeclContext);
@@ -1337,6 +1373,9 @@ SWIFT_NAME("BridgedDecl.forEachDeclToHoist(self:_:)")
13371373
void BridgedDecl_forEachDeclToHoist(BridgedDecl decl,
13381374
BridgedSwiftClosure closure);
13391375

1376+
SWIFT_NAME("BridgedDecl.getDeclContext(self:)")
1377+
BridgedDeclContext BridgedDecl_getDeclContext(BridgedDecl decl);
1378+
13401379
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedStaticSpelling {
13411380
BridgedStaticSpellingNone,
13421381
BridgedStaticSpellingStatic,
@@ -1384,10 +1423,18 @@ BridgedParamDecl BridgedParamDecl_createParsed(
13841423
swift::SourceLoc paramNameLoc, BridgedNullableExpr defaultValue,
13851424
BridgedNullableDefaultArgumentInitializer cDefaultArgumentInitContext);
13861425

1426+
SWIFT_NAME("BridgedParamDecl.cloneWithoutType(self:)")
1427+
BRIDGED_INLINE BridgedParamDecl
1428+
BridgedParamDecl_cloneWithoutType(BridgedParamDecl cDecl);
1429+
13871430
SWIFT_NAME("BridgedParamDecl.setTypeRepr(self:_:)")
13881431
BRIDGED_INLINE void BridgedParamDecl_setTypeRepr(BridgedParamDecl cDecl,
13891432
BridgedTypeRepr cType);
13901433

1434+
SWIFT_NAME("BridgedParamDecl.setInterfaceType(self:_:)")
1435+
BRIDGED_INLINE void BridgedParamDecl_setInterfaceType(BridgedParamDecl cDecl,
1436+
BridgedASTType cType);
1437+
13911438
/// The various spellings of ownership modifier that can be used in source.
13921439
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedParamSpecifier {
13931440
BridgedParamSpecifierDefault,
@@ -1406,8 +1453,13 @@ BRIDGED_INLINE void
14061453
BridgedParamDecl_setSpecifier(BridgedParamDecl cDecl,
14071454
BridgedParamSpecifier cSpecifier);
14081455

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

14121464
SWIFT_NAME("BridgedConstructorDecl.setParsedBody(self:_:)")
14131465
void BridgedConstructorDecl_setParsedBody(BridgedConstructorDecl decl,
@@ -1471,14 +1523,20 @@ void BridgedExtensionDecl_setParsedMembers(BridgedExtensionDecl decl,
14711523
SWIFT_NAME(
14721524
"BridgedEnumDecl.createParsed(_:declContext:enumKeywordLoc:name:nameLoc:"
14731525
"genericParamList:inheritedTypes:genericWhereClause:braceRange:)")
1474-
BridgedNominalTypeDecl BridgedEnumDecl_createParsed(
1526+
BridgedEnumDecl BridgedEnumDecl_createParsed(
14751527
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
14761528
swift::SourceLoc enumKeywordLoc, swift::Identifier name,
14771529
swift::SourceLoc nameLoc, BridgedNullableGenericParamList genericParamList,
14781530
BridgedArrayRef cInheritedTypes,
14791531
BridgedNullableTrailingWhereClause genericWhereClause,
14801532
swift::SourceRange braceRange);
14811533

1534+
SWIFT_NAME("BridgedEnumDecl.create(_:declContext:name:genericParamList:)")
1535+
BridgedEnumDecl
1536+
BridgedEnumDecl_create(BridgedASTContext cContext,
1537+
BridgedDeclContext cDeclContext, BridgedStringRef name,
1538+
BridgedNullableGenericParamList genericParamList);
1539+
14821540
SWIFT_NAME(
14831541
"BridgedEnumCaseDecl.createParsed(declContext:caseKeywordLoc:elements:)")
14841542
BridgedEnumCaseDecl
@@ -1494,6 +1552,12 @@ BridgedEnumElementDecl BridgedEnumElementDecl_createParsed(
14941552
BridgedNullableParameterList parameterList, swift::SourceLoc equalsLoc,
14951553
BridgedNullableExpr opaqueRawValue);
14961554

1555+
SWIFT_NAME("BridgedEnumElementDecl.create(_:declContext:name:"
1556+
"parameterList:)")
1557+
BridgedEnumElementDecl BridgedEnumElementDecl_create(
1558+
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
1559+
BridgedStringRef name, BridgedParameterList parameterList);
1560+
14971561
SWIFT_NAME("BridgedStructDecl.createParsed(_:declContext:structKeywordLoc:name:"
14981562
"nameLoc:genericParamList:inheritedTypes:genericWhereClause:"
14991563
"braceRange:)")
@@ -1662,6 +1726,9 @@ void BridgedTopLevelCodeDecl_dump(BridgedTopLevelCodeDecl decl);
16621726
SWIFT_NAME("BridgedDecl.dump(self:)")
16631727
void BridgedDecl_dump(BridgedDecl decl);
16641728

1729+
SWIFT_NAME("BridgedValueDecl.setAccess(self:_:)")
1730+
void BridgedValueDecl_setAccess(BridgedValueDecl decl, swift::AccessLevel accessLevel);
1731+
16651732
//===----------------------------------------------------------------------===//
16661733
// MARK: AbstractStorageDecl
16671734
//===----------------------------------------------------------------------===//
@@ -1711,6 +1778,11 @@ SWIFT_NAME("BridgedNominalTypeDecl.getSourceLocation(self:)")
17111778
BRIDGED_INLINE swift::SourceLoc
17121779
BridgedNominalTypeDecl_getSourceLocation(BridgedNominalTypeDecl decl);
17131780

1781+
SWIFT_NAME("BridgedNominalTypeDecl.addMember(self:_:)")
1782+
BRIDGED_INLINE void
1783+
BridgedNominalTypeDecl_addMember(BridgedNominalTypeDecl cDecl,
1784+
BridgedDecl member);
1785+
17141786
//===----------------------------------------------------------------------===//
17151787
// MARK: SubscriptDecl
17161788
//===----------------------------------------------------------------------===//
@@ -2913,6 +2985,10 @@ BridgedParameterList BridgedParameterList_createParsed(
29132985
BridgedASTContext cContext, swift::SourceLoc leftParenLoc,
29142986
BridgedArrayRef cParameters, swift::SourceLoc rightParenLoc);
29152987

2988+
SWIFT_NAME("BridgedParameterList.create(_:parameters:)")
2989+
BridgedParameterList BridgedParameterList_create(BridgedASTContext cContext,
2990+
BridgedArrayRef cParameters);
2991+
29162992
SWIFT_NAME("getter:BridgedParameterList.size(self:)")
29172993
size_t BridgedParameterList_size(BridgedParameterList cParameterList);
29182994

@@ -3017,6 +3093,7 @@ struct BridgedASTType {
30173093
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedGenericSignature getInvocationGenericSignatureOfFunctionType() const;
30183094
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType subst(BridgedSubstitutionMap substMap) const;
30193095
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedConformance checkConformance(BridgedDeclObj proto) const;
3096+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType mapTypeOutOfContext() const;
30203097
};
30213098

30223099
class BridgedCanType {
@@ -3084,13 +3161,23 @@ struct BridgedSubstitutionMap {
30843161
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTTypeArray getReplacementTypes() const;
30853162
};
30863163

3164+
struct BridgedCanGenericSignature;
3165+
30873166
struct BridgedGenericSignature {
30883167
const swift::GenericSignatureImpl * _Nullable impl;
30893168

30903169
BRIDGED_INLINE swift::GenericSignature unbridged() const;
30913170
BridgedOwnedString getDebugDescription() const;
30923171
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTTypeArray getGenericParams() const;
30933172
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType mapTypeIntoContext(BridgedASTType type) const;
3173+
BRIDGED_INLINE BridgedCanGenericSignature getCanonicalSignature() const;
3174+
};
3175+
3176+
struct BridgedCanGenericSignature {
3177+
const swift::GenericSignatureImpl *_Nullable impl;
3178+
3179+
BRIDGED_INLINE swift::CanGenericSignature unbridged() const;
3180+
BRIDGED_INLINE BridgedGenericSignature getGenericSignature() const;
30943181
};
30953182

30963183
struct BridgedFingerprint {

0 commit comments

Comments
 (0)