@@ -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,47 +44,81 @@ 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
47
35
- public var hasTypeParameter : Bool { bridged. hasTypeParameter ( ) }
36
- public var isOpenedExistentialWithError : Bool { bridged. isOpenedExistentialWithError ( ) }
37
- public var isEscapable : Bool { bridged. isEscapable ( ) }
38
- public var isNoEscape : Bool { bridged. isNoEscape ( ) }
39
- public var isInteger : Bool { bridged. isInteger ( ) }
48
+ public var instanceTypeOfMetatype : Type { Type ( bridged: bridged. getInstanceTypeOfMetatype ( ) ) }
40
49
41
50
public func subst( with substitutionMap: SubstitutionMap ) -> Type {
42
51
return Type ( bridged: bridged. subst ( substitutionMap. bridged) )
43
52
}
53
+
54
+ public func subst( type: Type , with targetType: Type ) -> Type {
55
+ return Type ( bridged: bridged. subst ( type. bridged, targetType. bridged) )
56
+ }
44
57
}
45
58
46
59
/// A Type that is statically known to be canonical.
47
60
/// For example, typealiases are resolved.
48
- public struct CanonicalType : CustomStringConvertible , NoReflectionChildren {
49
- public enum TraitResult {
50
- case isNot
51
- case canBe
52
- case `is`
53
- }
54
-
61
+ public struct CanonicalType : TypeProperties , CustomStringConvertible , NoReflectionChildren {
55
62
public let bridged : BridgedCanType
56
63
57
64
public init ( bridged: BridgedCanType ) { self . bridged = bridged }
58
65
59
66
public var type : Type { Type ( bridged: bridged. getType ( ) ) }
60
67
61
- public var description : String { type. description }
62
-
63
- public var hasTypeParameter : Bool { type. hasTypeParameter }
64
- public var isOpenedExistentialWithError : Bool { type. isOpenedExistentialWithError }
65
- public var isEscapable : Bool { type. isEscapable }
66
- public var isNoEscape : Bool { type. isNoEscape }
67
- public var isInteger : Bool { type. isInteger }
68
-
68
+ public var instanceTypeOfMetatype : CanonicalType { type. instanceTypeOfMetatype. canonical }
69
+
69
70
public func subst( with substitutionMap: SubstitutionMap ) -> CanonicalType {
70
71
return type. subst ( with: substitutionMap) . canonical
71
72
}
72
73
73
- public var canBeClass : TraitResult { bridged. canBeClass ( ) . result }
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 isLegalFormalType : Bool { type. bridged. isLegalFormalType ( ) }
88
+ public var hasTypeParameter : Bool { type. bridged. hasTypeParameter ( ) }
89
+ public var hasLocalArchetype : Bool { type. bridged. hasLocalArchetype ( ) }
90
+ public var isExistentialArchetype : Bool { type. bridged. isExistentialArchetype ( ) }
91
+ public var isExistentialArchetypeWithError : Bool { type. bridged. isExistentialArchetypeWithError ( ) }
92
+ public var isExistential : Bool { type. bridged. isExistential ( ) }
93
+ public var isEscapable : Bool { type. bridged. isEscapable ( ) }
94
+ public var isNoEscape : Bool { type. bridged. isNoEscape ( ) }
95
+ public var isInteger : Bool { type. bridged. isInteger ( ) }
96
+ public var isMetatypeType : Bool { type. bridged. isMetatypeType ( ) }
97
+ public var isExistentialMetatypeType : Bool { type. bridged. isExistentialMetatypeType ( ) }
98
+ public var representationOfMetatype : AST . `Type` . MetatypeRepresentation {
99
+ type. bridged. getRepresentationOfMetatype ( ) . representation
100
+ }
101
+ public var invocationGenericSignatureOfFunctionType : GenericSignature {
102
+ GenericSignature ( bridged: type. bridged. getInvocationGenericSignatureOfFunctionType ( ) )
103
+ }
104
+
105
+ public var canBeClass : Type . TraitResult { type. bridged. canBeClass ( ) . result }
106
+
107
+ public var anyNominal : NominalTypeDecl ? { type. bridged. getAnyNominal ( ) . getAs ( NominalTypeDecl . self) }
108
+
109
+ /// Performas a global conformance lookup for this type for `protocol`.
110
+ /// It checks conditional requirements.
111
+ ///
112
+ /// This type must be a contextualized type. It must not contain type parameters.
113
+ ///
114
+ /// The resulting conformance reference does not include "missing" conformances, which are synthesized for
115
+ /// some protocols as an error recovery mechanism.
116
+ ///
117
+ /// Returns an invalid conformance if the search failed, otherwise an
118
+ /// abstract, concrete or pack conformance, depending on the lookup type.
119
+ public func checkConformance( to protocol: ProtocolDecl ) -> Conformance {
120
+ return Conformance ( bridged: type. bridged. checkConformance ( `protocol`. bridged) )
121
+ }
74
122
}
75
123
76
124
public struct TypeArray : RandomAccessCollection , CustomReflectable {
@@ -93,8 +141,8 @@ public struct TypeArray : RandomAccessCollection, CustomReflectable {
93
141
}
94
142
}
95
143
96
- extension BridgedCanType . TraitResult {
97
- var result : CanonicalType . TraitResult {
144
+ extension BridgedASTType . TraitResult {
145
+ var result : Type . TraitResult {
98
146
switch self {
99
147
case . IsNot: return . isNot
100
148
case . CanBe: return . canBe
@@ -104,3 +152,27 @@ extension BridgedCanType.TraitResult {
104
152
}
105
153
}
106
154
}
155
+
156
+ extension BridgedASTType . MetatypeRepresentation {
157
+ var representation : Type . MetatypeRepresentation {
158
+ switch self {
159
+ case . Thin: return . thin
160
+ case . Thick: return . thick
161
+ case . ObjC: return . objC
162
+ default :
163
+ fatalError ( " wrong type MetatypeRepresentation enum case " )
164
+ }
165
+ }
166
+ }
167
+
168
+ extension Type : Equatable {
169
+ public static func == ( lhs: Type , rhs: Type ) -> Bool {
170
+ lhs. bridged. type == rhs. bridged. type
171
+ }
172
+ }
173
+
174
+ extension CanonicalType : Equatable {
175
+ public static func == ( lhs: CanonicalType , rhs: CanonicalType ) -> Bool {
176
+ lhs. type == rhs. type
177
+ }
178
+ }
0 commit comments