Skip to content

Commit 810064b

Browse files
committed
Cleanup and additions to AST and SIL Type/CanonicalType
* factor out common methods of AST Type/CanonicalType into a `TypeProperties` protocol. * add more APIs to AST Type/CanoncialType. * move `MetatypeRepresentation` from SIL.Type to AST.Type and implement it with a swift enum. * let `Builder.createMetatype` get a CanonicalType as instance type, because the instance type must not be a lowered type.
1 parent 35097b5 commit 810064b

File tree

13 files changed

+160
-122
lines changed

13 files changed

+160
-122
lines changed

SwiftCompilerSources/Sources/AST/Type.swift

Lines changed: 78 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,23 @@ import ASTBridging
1515

1616
/// A Swift type.
1717
/// It is not necessarily canoncial, e.g. typealiases are not resolved.
18-
public struct Type: CustomStringConvertible, NoReflectionChildren {
18+
public struct Type: TypeProperties, CustomStringConvertible, NoReflectionChildren {
19+
public enum TraitResult {
20+
case isNot
21+
case canBe
22+
case `is`
23+
}
24+
25+
public enum MetatypeRepresentation {
26+
case thin
27+
case thick
28+
case objC
29+
};
30+
1931
public let bridged: BridgedASTType
2032

33+
public var type: Type { self }
34+
2135
public init?(bridgedOrNil: BridgedASTType) {
2236
if bridgedOrNil.type == nil {
2337
return nil
@@ -30,78 +44,77 @@ public struct Type: CustomStringConvertible, NoReflectionChildren {
3044
}
3145

3246
public var canonical: CanonicalType { CanonicalType(bridged: bridged.getCanonicalType()) }
33-
public var description: String { String(taking: bridged.getDebugDescription()) }
34-
35-
public var hasTypeParameter: Bool { bridged.hasTypeParameter() }
36-
public var hasOpenedExistential: Bool { bridged.hasOpenedExistential() }
37-
public var isOpenedExistentialWithError: Bool { bridged.isOpenedExistentialWithError() }
38-
public var isEscapable: Bool { bridged.isEscapable() }
39-
public var isNoEscape: Bool { bridged.isNoEscape() }
40-
public var isInteger: Bool { bridged.isInteger() }
41-
public var isMetatypeType: Bool { bridged.isMetatypeType() }
42-
public var isExistentialMetatypeType: Bool { bridged.isExistentialMetatypeType() }
43-
44-
public var anyNominal: NominalTypeDecl? { bridged.getAnyNominal().getAs(NominalTypeDecl.self) }
47+
4548
public var instanceTypeOfMetatype: Type { Type(bridged: bridged.getInstanceTypeOfMetatype()) }
46-
49+
4750
public func subst(with substitutionMap: SubstitutionMap) -> Type {
4851
return Type(bridged: bridged.subst(substitutionMap.bridged))
4952
}
50-
51-
/// Performas a global conformance lookup for this type for `protocol`.
52-
/// It checks conditional requirements.
53-
///
54-
/// This type must be a contextualized type. It must not contain type parameters.
55-
///
56-
/// The resulting conformance reference does not include "missing" conformances, which are synthesized for
57-
/// some protocols as an error recovery mechanism.
58-
///
59-
/// Returns an invalid conformance if the search failed, otherwise an
60-
/// abstract, concrete or pack conformance, depending on the lookup type.
61-
public func checkConformance(to protocol: ProtocolDecl) -> Conformance {
62-
return Conformance(bridged: bridged.checkConformance(`protocol`.bridged))
53+
54+
public func subst(type: Type, with targetType: Type) -> Type {
55+
return Type(bridged: bridged.subst(type.bridged, targetType.bridged))
6356
}
6457
}
6558

6659
/// A Type that is statically known to be canonical.
6760
/// For example, typealiases are resolved.
68-
public struct CanonicalType: CustomStringConvertible, NoReflectionChildren {
69-
public enum TraitResult {
70-
case isNot
71-
case canBe
72-
case `is`
73-
}
74-
61+
public struct CanonicalType: TypeProperties, CustomStringConvertible, NoReflectionChildren {
7562
public let bridged: BridgedCanType
7663

7764
public init(bridged: BridgedCanType) { self.bridged = bridged }
7865

7966
public var type: Type { Type(bridged: bridged.getType()) }
8067

81-
public var description: String { type.description }
82-
83-
public var hasTypeParameter: Bool { type.hasTypeParameter }
84-
public var hasOpenedExistential: Bool { type.hasOpenedExistential }
85-
public var isOpenedExistentialWithError: Bool { type.isOpenedExistentialWithError }
86-
public var isEscapable: Bool { type.isEscapable }
87-
public var isNoEscape: Bool { type.isNoEscape }
88-
public var isInteger: Bool { type.isInteger }
89-
public var isMetatypeType: Bool { type.isMetatypeType }
90-
public var isExistentialMetatypeType: Bool { type.isExistentialMetatypeType }
91-
92-
public var anyNominal: NominalTypeDecl? { type.anyNominal }
9368
public var instanceTypeOfMetatype: CanonicalType { type.instanceTypeOfMetatype.canonical }
9469

9570
public func subst(with substitutionMap: SubstitutionMap) -> CanonicalType {
9671
return type.subst(with: substitutionMap).canonical
9772
}
9873

99-
// See `type.checkConformance`
100-
public func checkConformance(to proto: ProtocolDecl) -> Conformance {
101-
return type.checkConformance(to: proto)
74+
public func subst(type: CanonicalType, with targetType: CanonicalType) -> CanonicalType {
75+
return self.type.subst(type: type.type, with: targetType.type).canonical
76+
}
77+
}
78+
79+
/// Contains common properties of AST.Type and AST.CanonicalType
80+
public protocol TypeProperties {
81+
var type: Type { get }
82+
}
83+
84+
extension TypeProperties {
85+
public var description: String { String(taking: type.bridged.getDebugDescription()) }
86+
87+
public var hasTypeParameter: Bool { type.bridged.hasTypeParameter() }
88+
public var hasLocalArchetype: Bool { type.bridged.hasLocalArchetype() }
89+
public var isExistentialArchetype: Bool { type.bridged.isExistentialArchetype() }
90+
public var isExistentialArchetypeWithError: Bool { type.bridged.isExistentialArchetypeWithError() }
91+
public var isExistential: Bool { type.bridged.isExistential() }
92+
public var isEscapable: Bool { type.bridged.isEscapable() }
93+
public var isNoEscape: Bool { type.bridged.isNoEscape() }
94+
public var isInteger: Bool { type.bridged.isInteger() }
95+
public var isMetatypeType: Bool { type.bridged.isMetatypeType() }
96+
public var isExistentialMetatypeType: Bool { type.bridged.isExistentialMetatypeType() }
97+
public var representationOfMetatype: AST.`Type`.MetatypeRepresentation {
98+
type.bridged.getRepresentationOfMetatype().representation
99+
}
100+
101+
public var canBeClass: Type.TraitResult { type.bridged.canBeClass().result }
102+
103+
public var anyNominal: NominalTypeDecl? { type.bridged.getAnyNominal().getAs(NominalTypeDecl.self) }
104+
105+
/// Performas a global conformance lookup for this type for `protocol`.
106+
/// It checks conditional requirements.
107+
///
108+
/// This type must be a contextualized type. It must not contain type parameters.
109+
///
110+
/// The resulting conformance reference does not include "missing" conformances, which are synthesized for
111+
/// some protocols as an error recovery mechanism.
112+
///
113+
/// Returns an invalid conformance if the search failed, otherwise an
114+
/// abstract, concrete or pack conformance, depending on the lookup type.
115+
public func checkConformance(to protocol: ProtocolDecl) -> Conformance {
116+
return Conformance(bridged: type.bridged.checkConformance(`protocol`.bridged))
102117
}
103-
104-
public var canBeClass: TraitResult { bridged.canBeClass().result }
105118
}
106119

107120
public struct TypeArray : RandomAccessCollection, CustomReflectable {
@@ -124,8 +137,8 @@ public struct TypeArray : RandomAccessCollection, CustomReflectable {
124137
}
125138
}
126139

127-
extension BridgedCanType.TraitResult {
128-
var result: CanonicalType.TraitResult {
140+
extension BridgedASTType.TraitResult {
141+
var result: Type.TraitResult {
129142
switch self {
130143
case .IsNot: return .isNot
131144
case .CanBe: return .canBe
@@ -136,6 +149,18 @@ extension BridgedCanType.TraitResult {
136149
}
137150
}
138151

152+
extension BridgedASTType.MetatypeRepresentation {
153+
var representation: Type.MetatypeRepresentation {
154+
switch self {
155+
case .Thin: return .thin
156+
case .Thick: return .thick
157+
case .ObjC: return .objC
158+
default:
159+
fatalError("wrong type MetatypeRepresentation enum case")
160+
}
161+
}
162+
}
163+
139164
extension Type: Equatable {
140165
public static func ==(lhs: Type, rhs: Type) -> Bool {
141166
lhs.bridged.type == rhs.bridged.type

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyBuiltin.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,13 @@ private extension BuiltinInst {
193193
return
194194
}
195195

196-
guard type.representationOfMetatype(in: parentFunction) == .Thick else {
196+
guard type.astType.representationOfMetatype == .thick else {
197197
return
198198
}
199199

200-
let instanceType = type.loweredInstanceTypeOfMetatype(in: parentFunction)
201200
let builder = Builder(before: self, context)
202-
let newMetatype = builder.createMetatype(of: instanceType, representation: .Thin)
201+
let newMetatype = builder.createMetatype(ofInstanceType: type.astType.instanceTypeOfMetatype,
202+
representation: .thin)
203203
operands[argument].set(to: newMetatype, context)
204204
}
205205

SwiftCompilerSources/Sources/Optimizer/Utilities/FunctionSignatureTransforms.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ private func removeMetatypArguments(in specializedFunction: Function, _ context:
148148
if funcArg.type.isRemovableMetatype(in: specializedFunction) {
149149
// Rematerialize the metatype value in the entry block.
150150
let builder = Builder(atBeginOf: entryBlock, context)
151-
let instanceType = funcArg.type.loweredInstanceTypeOfMetatype(in: specializedFunction)
152-
let metatype = builder.createMetatype(of: instanceType, representation: .Thick)
151+
let instanceType = funcArg.type.astType.instanceTypeOfMetatype
152+
let metatype = builder.createMetatype(ofInstanceType: instanceType, representation: .thick)
153153
funcArg.uses.replaceAll(with: metatype, context)
154154
entryBlock.eraseArgument(at: funcArgIdx, context)
155155
} else {
@@ -232,7 +232,7 @@ private func replace(apply: FullApplySite, to specializedCallee: Function, _ con
232232
private extension Type {
233233
func isRemovableMetatype(in function: Function) -> Bool {
234234
if isMetatype {
235-
if representationOfMetatype(in: function) == .Thick {
235+
if astType.representationOfMetatype == .thick {
236236
let instanceTy = loweredInstanceTypeOfMetatype(in: function)
237237
// For structs and enums we know the metatype statically.
238238
return instanceTy.isStruct || instanceTy.isEnum

SwiftCompilerSources/Sources/SIL/Builder.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -549,8 +549,17 @@ public struct Builder {
549549
return notifyNew(initExistential.getAs(InitExistentialMetatypeInst.self))
550550
}
551551

552-
public func createMetatype(of type: Type, representation: Type.MetatypeRepresentation) -> MetatypeInst {
553-
let metatype = bridged.createMetatype(type.bridged, representation)
552+
public func createMetatype(
553+
ofInstanceType instanceType: CanonicalType,
554+
representation: AST.`Type`.MetatypeRepresentation
555+
) -> MetatypeInst {
556+
let bridgedRep: BridgedASTType.MetatypeRepresentation
557+
switch representation {
558+
case .thin: bridgedRep = .Thin
559+
case .thick: bridgedRep = .Thick
560+
case .objC: bridgedRep = .ObjC
561+
}
562+
let metatype = bridged.createMetatype(instanceType.bridged, bridgedRep)
554563
return notifyNew(metatype.getAs(MetatypeInst.self))
555564
}
556565

SwiftCompilerSources/Sources/SIL/FunctionConvention.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public struct ResultInfo : CustomStringConvertible {
122122
public var isSILIndirect: Bool {
123123
switch convention {
124124
case .indirect:
125-
return hasLoweredAddresses || type.isOpenedExistentialWithError()
125+
return hasLoweredAddresses || type.isExistentialArchetypeWithError()
126126
case .pack:
127127
return true
128128
case .owned, .unowned, .unownedInnerPointer, .autoreleased:
@@ -178,7 +178,7 @@ public struct ParameterInfo : CustomStringConvertible {
178178
public var isSILIndirect: Bool {
179179
switch convention {
180180
case .indirectIn, .indirectInGuaranteed:
181-
return hasLoweredAddresses || type.isOpenedExistentialWithError
181+
return hasLoweredAddresses || type.isExistentialArchetypeWithError
182182
case .indirectInout, .indirectInoutAliasable, .indirectInCXX:
183183
return true
184184
case .directOwned, .directUnowned, .directGuaranteed:

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public struct Type : CustomStringConvertible, NoReflectionChildren {
6868
public var isThickFunction: Bool { bridged.isThickFunction() }
6969
public var isAsyncFunction: Bool { bridged.isAsyncFunction() }
7070

71-
public var canBeClass: CanonicalType.TraitResult { astType.canBeClass }
71+
public var canBeClass: AST.`Type`.TraitResult { astType.canBeClass }
7272

7373
public var isMoveOnly: Bool { bridged.isMoveOnly() }
7474

@@ -148,8 +148,6 @@ public struct Type : CustomStringConvertible, NoReflectionChildren {
148148
return EnumCases(enumType: self, function: function)
149149
}
150150

151-
public typealias MetatypeRepresentation = BridgedType.MetatypeRepresentation
152-
153151
public func loweredInstanceTypeOfMetatype(in function: Function) -> Type {
154152
bridged.getLoweredInstanceTypeOfMetatype(function.bridged).type
155153
}
@@ -158,10 +156,6 @@ public struct Type : CustomStringConvertible, NoReflectionChildren {
158156
bridged.isDynamicSelfMetatype()
159157
}
160158

161-
public func representationOfMetatype(in function: Function) -> MetatypeRepresentation {
162-
bridged.getRepresentationOfMetatype(function.bridged)
163-
}
164-
165159
public var isCalleeConsumedFunction: Bool { bridged.isCalleeConsumedFunction() }
166160

167161
public var isMarkedAsImmortal: Bool { bridged.isMarkedAsImmortal() }

include/swift/AST/ASTBridging.h

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2984,39 +2984,49 @@ enum ENUM_EXTENSIBILITY_ATTR(open) BridgedMacroDefinitionKind : size_t {
29842984
};
29852985

29862986
struct BridgedASTType {
2987+
enum class TraitResult {
2988+
IsNot,
2989+
CanBe,
2990+
Is
2991+
};
2992+
2993+
enum class MetatypeRepresentation {
2994+
Thin,
2995+
Thick,
2996+
ObjC
2997+
};
2998+
29872999
swift::TypeBase * _Nullable type;
29883000

29893001
BRIDGED_INLINE swift::Type unbridged() const;
29903002
BRIDGED_INLINE BridgedOwnedString getDebugDescription() const;
29913003
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType getCanonicalType() const;
29923004
BRIDGED_INLINE bool hasTypeParameter() const;
2993-
BRIDGED_INLINE bool hasOpenedExistential() const;
2994-
BRIDGED_INLINE bool isOpenedExistentialWithError() const;
3005+
BRIDGED_INLINE bool hasLocalArchetype() const;
3006+
BRIDGED_INLINE bool isExistentialArchetype() const;
3007+
BRIDGED_INLINE bool isExistentialArchetypeWithError() const;
3008+
BRIDGED_INLINE bool isExistential() const;
29953009
BRIDGED_INLINE bool isEscapable() const;
29963010
BRIDGED_INLINE bool isNoEscape() const;
29973011
BRIDGED_INLINE bool isInteger() const;
29983012
BRIDGED_INLINE bool isMetatypeType() const;
29993013
BRIDGED_INLINE bool isExistentialMetatypeType() const;
3014+
BRIDGED_INLINE TraitResult canBeClass() const;
30003015
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedDeclObj getAnyNominal() const;
30013016
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType getInstanceTypeOfMetatype() const;
3017+
BRIDGED_INLINE MetatypeRepresentation getRepresentationOfMetatype() const;
30023018
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType subst(BridgedSubstitutionMap substMap) const;
3019+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType subst(BridgedASTType fromType, BridgedASTType toType) const;
30033020
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedConformance checkConformance(BridgedDeclObj proto) const;
30043021
};
30053022

30063023
class BridgedCanType {
30073024
swift::TypeBase * _Nullable type;
30083025

30093026
public:
3010-
enum class TraitResult {
3011-
IsNot,
3012-
CanBe,
3013-
Is
3014-
};
3015-
30163027
BRIDGED_INLINE BridgedCanType(swift::CanType ty);
30173028
BRIDGED_INLINE swift::CanType unbridged() const;
30183029
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType getType() const;
3019-
BRIDGED_INLINE TraitResult canBeClass() const;
30203030
};
30213031

30223032
struct BridgedASTTypeArray {

0 commit comments

Comments
 (0)