Skip to content

Commit 1f4332a

Browse files
committed
SIL: add AST.Type.loweredType and CanonicalType.loweredType
1 parent 50bf66e commit 1f4332a

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

SwiftCompilerSources/Sources/SIL/ASTExtensions.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,29 @@
1313
import AST
1414
import SILBridging
1515

16+
extension AST.`Type` {
17+
// See `CanonicalType.loweredType(in:)`.
18+
public func loweredType(in function: Function, maximallyAbstracted: Bool = false) -> Type {
19+
function.bridged.getLoweredType(bridged, maximallyAbstracted).type.objectType
20+
}
21+
}
22+
1623
extension CanonicalType {
1724
// This can yield nil if the AST type is not a lowered type.
1825
// For example, if the AST type is a `AnyFunctionType` for which the lowered type would be a `SILFunctionType`.
1926
public var silType: Type? {
2027
BridgedType.createSILType(bridged).typeOrNil
2128
}
29+
30+
// Lowers the AST type to a SIL type - in a specific function.
31+
// In contrast to `silType` this always succeeds. Still, it's not allowed to do this for certain AST types
32+
// which are not present in SIL, like an `InOut` or LValue types.
33+
//
34+
// If `maximallyAbstracted` is true, the lowering is done with a completely opaque abstraction pattern
35+
// (see AbstractionPattern for details).
36+
public func loweredType(in function: Function, maximallyAbstracted: Bool = false) -> Type {
37+
type.loweredType(in: function, maximallyAbstracted: maximallyAbstracted)
38+
}
2239
}
2340

2441
extension Decl {

include/swift/SIL/SILBridging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ struct BridgedFunction {
528528
BRIDGED_INLINE void setNeedStackProtection(bool needSP) const;
529529
BRIDGED_INLINE void setIsPerformanceConstraint(bool isPerfConstraint) const;
530530
BRIDGED_INLINE bool isResilientNominalDecl(BridgedDeclObj decl) const;
531-
BRIDGED_INLINE BridgedType getLoweredType(BridgedASTType type) const;
531+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getLoweredType(BridgedASTType type, bool maximallyAbstracted) const;
532532
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedType getLoweredType(BridgedType type) const;
533533
BRIDGED_INLINE BridgedLinkage getLinkage() const;
534534
BRIDGED_INLINE void setLinkage(BridgedLinkage linkage) const;

include/swift/SIL/SILBridgingImpl.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,10 @@ bool BridgedFunction::isResilientNominalDecl(BridgedDeclObj decl) const {
912912
getFunction()->getResilienceExpansion());
913913
}
914914

915-
BridgedType BridgedFunction::getLoweredType(BridgedASTType type) const {
915+
BridgedType BridgedFunction::getLoweredType(BridgedASTType type, bool maximallyAbstracted) const {
916+
if (maximallyAbstracted) {
917+
return BridgedType(getFunction()->getLoweredType(swift::Lowering::AbstractionPattern::getOpaque(), type.type));
918+
}
916919
return BridgedType(getFunction()->getLoweredType(type.type));
917920
}
918921

0 commit comments

Comments
 (0)