@@ -15,9 +15,23 @@ import ASTBridging
15
15
16
16
/// A Swift type.
17
17
/// 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
+
19
31
public let bridged : BridgedASTType
20
32
33
+ public var type : Type { self }
34
+
21
35
public init ? ( bridgedOrNil: BridgedASTType ) {
22
36
if bridgedOrNil. type == nil {
23
37
return nil
@@ -30,78 +44,77 @@ public struct Type: CustomStringConvertible, NoReflectionChildren {
30
44
}
31
45
32
46
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
+
45
48
public var instanceTypeOfMetatype : Type { Type ( bridged: bridged. getInstanceTypeOfMetatype ( ) ) }
46
-
49
+
47
50
public func subst( with substitutionMap: SubstitutionMap ) -> Type {
48
51
return Type ( bridged: bridged. subst ( substitutionMap. bridged) )
49
52
}
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) )
63
56
}
64
57
}
65
58
66
59
/// A Type that is statically known to be canonical.
67
60
/// 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 {
75
62
public let bridged : BridgedCanType
76
63
77
64
public init ( bridged: BridgedCanType ) { self . bridged = bridged }
78
65
79
66
public var type : Type { Type ( bridged: bridged. getType ( ) ) }
80
67
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 }
93
68
public var instanceTypeOfMetatype : CanonicalType { type. instanceTypeOfMetatype. canonical }
94
69
95
70
public func subst( with substitutionMap: SubstitutionMap ) -> CanonicalType {
96
71
return type. subst ( with: substitutionMap) . canonical
97
72
}
98
73
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) )
102
117
}
103
-
104
- public var canBeClass : TraitResult { bridged. canBeClass ( ) . result }
105
118
}
106
119
107
120
public struct TypeArray : RandomAccessCollection , CustomReflectable {
@@ -124,8 +137,8 @@ public struct TypeArray : RandomAccessCollection, CustomReflectable {
124
137
}
125
138
}
126
139
127
- extension BridgedCanType . TraitResult {
128
- var result : CanonicalType . TraitResult {
140
+ extension BridgedASTType . TraitResult {
141
+ var result : Type . TraitResult {
129
142
switch self {
130
143
case . IsNot: return . isNot
131
144
case . CanBe: return . canBe
@@ -136,6 +149,18 @@ extension BridgedCanType.TraitResult {
136
149
}
137
150
}
138
151
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
+
139
164
extension Type : Equatable {
140
165
public static func == ( lhs: Type , rhs: Type ) -> Bool {
141
166
lhs. bridged. type == rhs. bridged. type
0 commit comments