Skip to content

Commit b73676e

Browse files
authored
Bridging: Implement several optional- and enum-related bridges (#85756)
In #85757, part of the changes resolving #68944 is submitted. Most bridges required for #85757 were previously implemented in #84648. After #82653 got merged, we have demand for several new bridges in order to properly support optimizing derivatives of throwing functions via AutoDiff Closure Specialization pass. This patch implements: - **AST:** * `var optionalObjectType: Type` property of `Type` struct * `var optionalType: Type` property of `Type` struct - **SIL:** * `let name: StringRef` property of `EnumCase` struct * `func createOptionalSome(operand: Value, type: Type) -> EnumInst` method of `Builder` * `func createOptionalNone(type: Type) -> EnumInst` method of `Builder`
1 parent 12cb3f9 commit b73676e

File tree

5 files changed

+38
-1
lines changed

5 files changed

+38
-1
lines changed

SwiftCompilerSources/Sources/AST/Type.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ public struct Type: TypeProperties, CustomStringConvertible, NoReflectionChildre
6363

6464
public var builtinVectorElementType: Type { Type(bridged: bridged.getBuiltinVectorElementType()) }
6565

66+
public var optionalObjectType: Type {
67+
assert(self.isOptional)
68+
return genericArgumentsOfBoundGenericType[0]
69+
}
70+
71+
public var optionalType: Type {
72+
return Type(bridged: bridged.getOptionalType())
73+
}
74+
6675
public func subst(with substitutionMap: SubstitutionMap) -> Type {
6776
return Type(bridged: bridged.subst(substitutionMap.bridged))
6877
}
@@ -92,6 +101,10 @@ public struct Type: TypeProperties, CustomStringConvertible, NoReflectionChildre
92101
public var kindOfGenericTypeParameter: GenericTypeParameterKind {
93102
bridged.GenericTypeParam_getParamKind()
94103
}
104+
105+
public var genericArgumentsOfBoundGenericType: TypeArray {
106+
TypeArray(bridged: bridged.BoundGenericType_getGenericArgs())
107+
}
95108
}
96109

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

SwiftCompilerSources/Sources/SIL/Builder.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,17 @@ public struct Builder {
573573
return notifyNew(enumInst.getAs(EnumInst.self))
574574
}
575575

576+
static let optionalNoneCaseIndex = 0
577+
static let optionalSomeCaseIndex = 1
578+
579+
public func createOptionalNone(type: Type) -> EnumInst {
580+
return createEnum(caseIndex: Self.optionalNoneCaseIndex, payload: nil, enumType: type)
581+
}
582+
583+
public func createOptionalSome(operand: Value, type: Type) -> EnumInst {
584+
return createEnum(caseIndex: Self.optionalSomeCaseIndex, payload: operand, enumType: type)
585+
}
586+
576587
public func createThinToThickFunction(thinFunction: Value, resultType: Type) -> ThinToThickFunctionInst {
577588
let tttf = bridged.createThinToThickFunction(thinFunction.bridged, resultType.bridged)
578589
return notifyNew(tttf.getAs(ThinToThickFunctionInst.self))

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ public struct EnumCase {
291291
public let enumElementDecl : EnumElementDecl
292292
public let payload: Type?
293293
public let index: Int
294+
public var name: StringRef { enumElementDecl.name }
294295
}
295296

296297
public struct EnumCases : CollectionLikeSequence, IteratorProtocol {
@@ -329,7 +330,7 @@ public struct EnumCases : CollectionLikeSequence, IteratorProtocol {
329330
if currentIndex == index && !enumType.bridged.isEndCaseIterator(iterator) {
330331
return EnumCase(enumElementDecl: enumType.bridged.getEnumElementDecl(iterator).getAs(EnumElementDecl.self),
331332
payload: enumType.bridged.getEnumCasePayload(iterator, function.bridged).typeOrNil,
332-
index: caseIndex)
333+
index: index)
333334
}
334335
return nil
335336
}

include/swift/AST/ASTBridging.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ enum class RequirementReprKind : unsigned;
8383
}
8484

8585
struct BridgedASTType;
86+
struct BridgedASTTypeArray;
8687
class BridgedCanType;
8788
class BridgedASTContext;
8889
class BridgedLangOptions;
@@ -3046,6 +3047,7 @@ struct BridgedASTType {
30463047
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType getBuiltinVectorElementType() const;
30473048
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType getBuiltinFixedArrayElementType() const;
30483049
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType getBuiltinFixedArraySizeType() const;
3050+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType getOptionalType() const;
30493051
BRIDGED_INLINE bool isBuiltinFixedWidthInteger(SwiftInt width) const;
30503052
BRIDGED_INLINE bool isOptional() const;
30513053
BRIDGED_INLINE bool isBuiltinType() const;
@@ -3079,6 +3081,8 @@ struct BridgedASTType {
30793081
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedConformance checkConformance(BridgedDeclObj proto) const;
30803082
BRIDGED_INLINE bool containsSILPackExpansionType() const;
30813083
BRIDGED_INLINE bool isSILPackElementAddress() const;
3084+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTTypeArray
3085+
BoundGenericType_getGenericArgs() const;
30823086
};
30833087

30843088
class BridgedCanType {

include/swift/AST/ASTBridgingImpl.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,10 @@ BridgedCanType BridgedASTType::getBuiltinFixedArraySizeType() const {
639639
return unbridged()->castTo<swift::BuiltinFixedArrayType>()->getSize();
640640
}
641641

642+
BridgedASTType BridgedASTType::getOptionalType() const {
643+
return {swift::OptionalType::get(unbridged())};
644+
}
645+
642646
bool BridgedASTType::isBuiltinFixedWidthInteger(SwiftInt width) const {
643647
if (auto *intTy = unbridged()->getAs<swift::BuiltinIntegerType>())
644648
return intTy->isFixedWidth((unsigned)width);
@@ -782,6 +786,10 @@ BridgedASTType::GenericTypeParam_getParamKind() const {
782786
return llvm::cast<swift::GenericTypeParamType>(type)->getParamKind();
783787
}
784788

789+
BridgedASTTypeArray BridgedASTType::BoundGenericType_getGenericArgs() const {
790+
return {llvm::cast<swift::BoundGenericType>(type)->getGenericArgs()};
791+
}
792+
785793
static_assert((int)BridgedASTType::TraitResult::IsNot == (int)swift::TypeTraitResult::IsNot);
786794
static_assert((int)BridgedASTType::TraitResult::CanBe == (int)swift::TypeTraitResult::CanBe);
787795
static_assert((int)BridgedASTType::TraitResult::Is == (int)swift::TypeTraitResult::Is);

0 commit comments

Comments
 (0)