Skip to content

Commit 37ba33c

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 37ba33c

File tree

14 files changed

+395
-10
lines changed

14 files changed

+395
-10
lines changed

SwiftCompilerSources/Sources/AST/Declarations.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ final public class MacroDecl: ValueDecl {}
118118

119119
final public class EnumElementDecl: ValueDecl {
120120
public var hasAssociatedValues: Bool { bridged.EnumElementDecl_hasAssociatedValues() }
121+
public var parameterList: BridgedParameterList { bridged.EnumElementDecl_getParameterList() }
122+
public var name: StringRef { StringRef(bridged: bridged.EnumElementDecl_getNameStr()) }
121123
}
122124

123125
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: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,10 @@ struct BridgedDeclObj {
378378
BRIDGED_INLINE bool AbstractFunction_isOverridden() const;
379379
BRIDGED_INLINE bool Destructor_isIsolated() const;
380380
BRIDGED_INLINE bool EnumElementDecl_hasAssociatedValues() const;
381+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedParameterList
382+
EnumElementDecl_getParameterList() const;
383+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef
384+
EnumElementDecl_getNameStr() const;
381385
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedStringRef AccessorDecl_getKindName() const;
382386
};
383387

@@ -431,6 +435,23 @@ class BridgedASTNode {
431435
#define ABSTRACT_DECL(Id, Parent) DECL(Id, Parent)
432436
#include "swift/AST/DeclNodes.def"
433437

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

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

651+
SWIFT_NAME("BridgedSourceFile.addTopLevelDecl(self:_:)")
652+
BRIDGED_INLINE void BridgedSourceFile_addTopLevelDecl(BridgedSourceFile sf,
653+
BridgedDecl decl);
654+
620655
SWIFT_NAME("BridgedPatternBindingInitializer.create(declContext:)")
621656
BridgedPatternBindingInitializer
622657
BridgedPatternBindingInitializer_create(BridgedDeclContext cDeclContext);
@@ -1337,6 +1372,9 @@ SWIFT_NAME("BridgedDecl.forEachDeclToHoist(self:_:)")
13371372
void BridgedDecl_forEachDeclToHoist(BridgedDecl decl,
13381373
BridgedSwiftClosure closure);
13391374

1375+
SWIFT_NAME("BridgedDecl.getDeclContext(self:)")
1376+
BridgedDeclContext BridgedDecl_getDeclContext(BridgedDecl decl);
1377+
13401378
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedStaticSpelling {
13411379
BridgedStaticSpellingNone,
13421380
BridgedStaticSpellingStatic,
@@ -1384,10 +1422,18 @@ BridgedParamDecl BridgedParamDecl_createParsed(
13841422
swift::SourceLoc paramNameLoc, BridgedNullableExpr defaultValue,
13851423
BridgedNullableDefaultArgumentInitializer cDefaultArgumentInitContext);
13861424

1425+
SWIFT_NAME("BridgedParamDecl.cloneWithoutType(self:)")
1426+
BRIDGED_INLINE BridgedParamDecl
1427+
BridgedParamDecl_cloneWithoutType(BridgedParamDecl cDecl);
1428+
13871429
SWIFT_NAME("BridgedParamDecl.setTypeRepr(self:_:)")
13881430
BRIDGED_INLINE void BridgedParamDecl_setTypeRepr(BridgedParamDecl cDecl,
13891431
BridgedTypeRepr cType);
13901432

1433+
SWIFT_NAME("BridgedParamDecl.setInterfaceType(self:_:)")
1434+
BRIDGED_INLINE void BridgedParamDecl_setInterfaceType(BridgedParamDecl cDecl,
1435+
BridgedASTType cType);
1436+
13911437
/// The various spellings of ownership modifier that can be used in source.
13921438
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedParamSpecifier {
13931439
BridgedParamSpecifierDefault,
@@ -1406,8 +1452,18 @@ BRIDGED_INLINE void
14061452
BridgedParamDecl_setSpecifier(BridgedParamDecl cDecl,
14071453
BridgedParamSpecifier cSpecifier);
14081454

1409-
SWIFT_NAME("BridgedParamDecl.setImplicit(self:)")
1410-
BRIDGED_INLINE void BridgedParamDecl_setImplicit(BridgedParamDecl cDecl);
1455+
SWIFT_NAME("BridgedDecl.setImplicit(self:)")
1456+
BRIDGED_INLINE void BridgedDecl_setImplicit(BridgedDecl cDecl);
1457+
1458+
SWIFT_NAME("BridgedGenericContext.setGenericSignature(self:_:)")
1459+
BRIDGED_INLINE void
1460+
BridgedGenericContext_setGenericSignature(BridgedGenericContext cDecl,
1461+
BridgedGenericSignature cGenSig);
1462+
1463+
SWIFT_NAME("BridgedNominalTypeDecl.addMember(self:_:)")
1464+
BRIDGED_INLINE void
1465+
BridgedNominalTypeDecl_addMember(BridgedNominalTypeDecl cDecl,
1466+
BridgedDecl member);
14111467

14121468
SWIFT_NAME("BridgedConstructorDecl.setParsedBody(self:_:)")
14131469
void BridgedConstructorDecl_setParsedBody(BridgedConstructorDecl decl,
@@ -1471,14 +1527,20 @@ void BridgedExtensionDecl_setParsedMembers(BridgedExtensionDecl decl,
14711527
SWIFT_NAME(
14721528
"BridgedEnumDecl.createParsed(_:declContext:enumKeywordLoc:name:nameLoc:"
14731529
"genericParamList:inheritedTypes:genericWhereClause:braceRange:)")
1474-
BridgedNominalTypeDecl BridgedEnumDecl_createParsed(
1530+
BridgedEnumDecl BridgedEnumDecl_createParsed(
14751531
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
14761532
swift::SourceLoc enumKeywordLoc, swift::Identifier name,
14771533
swift::SourceLoc nameLoc, BridgedNullableGenericParamList genericParamList,
14781534
BridgedArrayRef cInheritedTypes,
14791535
BridgedNullableTrailingWhereClause genericWhereClause,
14801536
swift::SourceRange braceRange);
14811537

1538+
SWIFT_NAME("BridgedEnumDecl.create(_:declContext:name:genericParamList:)")
1539+
BridgedEnumDecl
1540+
BridgedEnumDecl_create(BridgedASTContext cContext,
1541+
BridgedDeclContext cDeclContext, BridgedStringRef name,
1542+
BridgedNullableGenericParamList genericParamList);
1543+
14821544
SWIFT_NAME(
14831545
"BridgedEnumCaseDecl.createParsed(declContext:caseKeywordLoc:elements:)")
14841546
BridgedEnumCaseDecl
@@ -1494,6 +1556,12 @@ BridgedEnumElementDecl BridgedEnumElementDecl_createParsed(
14941556
BridgedNullableParameterList parameterList, swift::SourceLoc equalsLoc,
14951557
BridgedNullableExpr opaqueRawValue);
14961558

1559+
SWIFT_NAME("BridgedEnumElementDecl.create(_:declContext:name:"
1560+
"parameterList:)")
1561+
BridgedEnumElementDecl BridgedEnumElementDecl_create(
1562+
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
1563+
BridgedStringRef name, BridgedParameterList parameterList);
1564+
14971565
SWIFT_NAME("BridgedStructDecl.createParsed(_:declContext:structKeywordLoc:name:"
14981566
"nameLoc:genericParamList:inheritedTypes:genericWhereClause:"
14991567
"braceRange:)")
@@ -1662,6 +1730,9 @@ void BridgedTopLevelCodeDecl_dump(BridgedTopLevelCodeDecl decl);
16621730
SWIFT_NAME("BridgedDecl.dump(self:)")
16631731
void BridgedDecl_dump(BridgedDecl decl);
16641732

1733+
SWIFT_NAME("BridgedValueDecl.setAccess(self:_:)")
1734+
void BridgedValueDecl_setAccess(BridgedValueDecl decl, swift::AccessLevel accessLevel);
1735+
16651736
//===----------------------------------------------------------------------===//
16661737
// MARK: AbstractStorageDecl
16671738
//===----------------------------------------------------------------------===//
@@ -2913,6 +2984,10 @@ BridgedParameterList BridgedParameterList_createParsed(
29132984
BridgedASTContext cContext, swift::SourceLoc leftParenLoc,
29142985
BridgedArrayRef cParameters, swift::SourceLoc rightParenLoc);
29152986

2987+
SWIFT_NAME("BridgedParameterList.create(_:parameters:)")
2988+
BridgedParameterList BridgedParameterList_create(BridgedASTContext cContext,
2989+
BridgedArrayRef cParameters);
2990+
29162991
SWIFT_NAME("getter:BridgedParameterList.size(self:)")
29172992
size_t BridgedParameterList_size(BridgedParameterList cParameterList);
29182993

@@ -3017,6 +3092,7 @@ struct BridgedASTType {
30173092
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedGenericSignature getInvocationGenericSignatureOfFunctionType() const;
30183093
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType subst(BridgedSubstitutionMap substMap) const;
30193094
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedConformance checkConformance(BridgedDeclObj proto) const;
3095+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType mapTypeOutOfContext() const;
30203096
};
30213097

30223098
class BridgedCanType {
@@ -3084,13 +3160,23 @@ struct BridgedSubstitutionMap {
30843160
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTTypeArray getReplacementTypes() const;
30853161
};
30863162

3163+
struct BridgedCanGenericSignature;
3164+
30873165
struct BridgedGenericSignature {
30883166
const swift::GenericSignatureImpl * _Nullable impl;
30893167

30903168
BRIDGED_INLINE swift::GenericSignature unbridged() const;
30913169
BridgedOwnedString getDebugDescription() const;
30923170
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTTypeArray getGenericParams() const;
30933171
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType mapTypeIntoContext(BridgedASTType type) const;
3172+
BRIDGED_INLINE BridgedCanGenericSignature getCanonicalSignature() const;
3173+
};
3174+
3175+
struct BridgedCanGenericSignature {
3176+
const swift::GenericSignatureImpl *_Nullable impl;
3177+
3178+
BRIDGED_INLINE swift::CanGenericSignature unbridged() const;
3179+
BRIDGED_INLINE BridgedGenericSignature getGenericSignature() const;
30943180
};
30953181

30963182
struct BridgedFingerprint {

0 commit comments

Comments
 (0)