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`
This commit is contained in:
Daniil Kovalev
2025-12-04 11:26:44 +03:00
committed by GitHub
parent 12cb3f9a9a
commit b73676ee68
5 changed files with 38 additions and 1 deletions

View File

@@ -63,6 +63,15 @@ public struct Type: TypeProperties, CustomStringConvertible, NoReflectionChildre
public var builtinVectorElementType: Type { Type(bridged: bridged.getBuiltinVectorElementType()) }
public var optionalObjectType: Type {
assert(self.isOptional)
return genericArgumentsOfBoundGenericType[0]
}
public var optionalType: Type {
return Type(bridged: bridged.getOptionalType())
}
public func subst(with substitutionMap: SubstitutionMap) -> Type {
return Type(bridged: bridged.subst(substitutionMap.bridged))
}
@@ -92,6 +101,10 @@ public struct Type: TypeProperties, CustomStringConvertible, NoReflectionChildre
public var kindOfGenericTypeParameter: GenericTypeParameterKind {
bridged.GenericTypeParam_getParamKind()
}
public var genericArgumentsOfBoundGenericType: TypeArray {
TypeArray(bridged: bridged.BoundGenericType_getGenericArgs())
}
}
/// A Type that is statically known to be canonical.

View File

@@ -573,6 +573,17 @@ public struct Builder {
return notifyNew(enumInst.getAs(EnumInst.self))
}
static let optionalNoneCaseIndex = 0
static let optionalSomeCaseIndex = 1
public func createOptionalNone(type: Type) -> EnumInst {
return createEnum(caseIndex: Self.optionalNoneCaseIndex, payload: nil, enumType: type)
}
public func createOptionalSome(operand: Value, type: Type) -> EnumInst {
return createEnum(caseIndex: Self.optionalSomeCaseIndex, payload: operand, enumType: type)
}
public func createThinToThickFunction(thinFunction: Value, resultType: Type) -> ThinToThickFunctionInst {
let tttf = bridged.createThinToThickFunction(thinFunction.bridged, resultType.bridged)
return notifyNew(tttf.getAs(ThinToThickFunctionInst.self))

View File

@@ -291,6 +291,7 @@ public struct EnumCase {
public let enumElementDecl : EnumElementDecl
public let payload: Type?
public let index: Int
public var name: StringRef { enumElementDecl.name }
}
public struct EnumCases : CollectionLikeSequence, IteratorProtocol {
@@ -329,7 +330,7 @@ public struct EnumCases : CollectionLikeSequence, IteratorProtocol {
if currentIndex == index && !enumType.bridged.isEndCaseIterator(iterator) {
return EnumCase(enumElementDecl: enumType.bridged.getEnumElementDecl(iterator).getAs(EnumElementDecl.self),
payload: enumType.bridged.getEnumCasePayload(iterator, function.bridged).typeOrNil,
index: caseIndex)
index: index)
}
return nil
}

View File

@@ -83,6 +83,7 @@ enum class RequirementReprKind : unsigned;
}
struct BridgedASTType;
struct BridgedASTTypeArray;
class BridgedCanType;
class BridgedASTContext;
class BridgedLangOptions;
@@ -3046,6 +3047,7 @@ struct BridgedASTType {
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType getBuiltinVectorElementType() const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType getBuiltinFixedArrayElementType() const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType getBuiltinFixedArraySizeType() const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType getOptionalType() const;
BRIDGED_INLINE bool isBuiltinFixedWidthInteger(SwiftInt width) const;
BRIDGED_INLINE bool isOptional() const;
BRIDGED_INLINE bool isBuiltinType() const;
@@ -3079,6 +3081,8 @@ struct BridgedASTType {
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedConformance checkConformance(BridgedDeclObj proto) const;
BRIDGED_INLINE bool containsSILPackExpansionType() const;
BRIDGED_INLINE bool isSILPackElementAddress() const;
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTTypeArray
BoundGenericType_getGenericArgs() const;
};
class BridgedCanType {

View File

@@ -639,6 +639,10 @@ BridgedCanType BridgedASTType::getBuiltinFixedArraySizeType() const {
return unbridged()->castTo<swift::BuiltinFixedArrayType>()->getSize();
}
BridgedASTType BridgedASTType::getOptionalType() const {
return {swift::OptionalType::get(unbridged())};
}
bool BridgedASTType::isBuiltinFixedWidthInteger(SwiftInt width) const {
if (auto *intTy = unbridged()->getAs<swift::BuiltinIntegerType>())
return intTy->isFixedWidth((unsigned)width);
@@ -782,6 +786,10 @@ BridgedASTType::GenericTypeParam_getParamKind() const {
return llvm::cast<swift::GenericTypeParamType>(type)->getParamKind();
}
BridgedASTTypeArray BridgedASTType::BoundGenericType_getGenericArgs() const {
return {llvm::cast<swift::BoundGenericType>(type)->getGenericArgs()};
}
static_assert((int)BridgedASTType::TraitResult::IsNot == (int)swift::TypeTraitResult::IsNot);
static_assert((int)BridgedASTType::TraitResult::CanBe == (int)swift::TypeTraitResult::CanBe);
static_assert((int)BridgedASTType::TraitResult::Is == (int)swift::TypeTraitResult::Is);