@@ -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,47 +44,81 @@ 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 ( ) ) }
3447
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 ( ) ) }
4049
4150 public func subst( with substitutionMap: SubstitutionMap ) -> Type {
4251 return Type ( bridged: bridged. subst ( substitutionMap. bridged) )
4352 }
53+
54+ public func subst( type: Type , with targetType: Type ) -> Type {
55+ return Type ( bridged: bridged. subst ( type. bridged, targetType. bridged) )
56+ }
4457}
4558
4659/// A Type that is statically known to be canonical.
4760/// 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 {
5562 public let bridged : BridgedCanType
5663
5764 public init ( bridged: BridgedCanType ) { self . bridged = bridged }
5865
5966 public var type : Type { Type ( bridged: bridged. getType ( ) ) }
6067
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+
6970 public func subst( with substitutionMap: SubstitutionMap ) -> CanonicalType {
7071 return type. subst ( with: substitutionMap) . canonical
7172 }
7273
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+ }
74122}
75123
76124public struct TypeArray : RandomAccessCollection , CustomReflectable {
@@ -93,8 +141,8 @@ public struct TypeArray : RandomAccessCollection, CustomReflectable {
93141 }
94142}
95143
96- extension BridgedCanType . TraitResult {
97- var result : CanonicalType . TraitResult {
144+ extension BridgedASTType . TraitResult {
145+ var result : Type . TraitResult {
98146 switch self {
99147 case . IsNot: return . isNot
100148 case . CanBe: return . canBe
@@ -104,3 +152,27 @@ extension BridgedCanType.TraitResult {
104152 }
105153 }
106154}
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