@@ -30,7 +30,8 @@ public struct Type: TypeProperties, CustomStringConvertible, NoReflectionChildre
30
30
31
31
public let bridged : BridgedASTType
32
32
33
- public var type : Type { self }
33
+ // Needed to conform to TypeProperties
34
+ public var rawType : Type { self }
34
35
35
36
public init ? ( bridgedOrNil: BridgedASTType ) {
36
37
if bridgedOrNil. type == nil {
@@ -47,6 +48,17 @@ public struct Type: TypeProperties, CustomStringConvertible, NoReflectionChildre
47
48
48
49
public var instanceTypeOfMetatype : Type { Type ( bridged: bridged. getInstanceTypeOfMetatype ( ) ) }
49
50
51
+ public var superClassType : Type ? {
52
+ precondition ( isClass)
53
+ let bridgedSuperClassTy = bridged. getSuperClassType ( )
54
+ if bridgedSuperClassTy. type != nil {
55
+ return Type ( bridged: bridgedSuperClassTy)
56
+ }
57
+ return nil
58
+ }
59
+
60
+ public var builtinVectorElementType : Type { Type ( bridged: bridged. getBuiltinVectorElementType ( ) ) }
61
+
50
62
public func subst( with substitutionMap: SubstitutionMap ) -> Type {
51
63
return Type ( bridged: bridged. subst ( substitutionMap. bridged) )
52
64
}
@@ -63,49 +75,123 @@ public struct CanonicalType: TypeProperties, CustomStringConvertible, NoReflecti
63
75
64
76
public init ( bridged: BridgedCanType ) { self . bridged = bridged }
65
77
66
- public var type : Type { Type ( bridged: bridged. getType ( ) ) }
78
+ public var rawType : Type { Type ( bridged: bridged. getRawType ( ) ) }
79
+
80
+ public var instanceTypeOfMetatype : CanonicalType { rawType. instanceTypeOfMetatype. canonical }
81
+
82
+ public var superClassType : CanonicalType ? { rawType. superClassType? . canonical }
83
+
84
+ public var builtinVectorElementType : CanonicalType { rawType. builtinVectorElementType. canonical }
67
85
68
- public var instanceTypeOfMetatype : CanonicalType { type. instanceTypeOfMetatype. canonical }
69
-
70
86
public func subst( with substitutionMap: SubstitutionMap ) -> CanonicalType {
71
- return type . subst ( with: substitutionMap) . canonical
87
+ return rawType . subst ( with: substitutionMap) . canonical
72
88
}
73
89
74
90
public func subst( type: CanonicalType , with targetType: CanonicalType ) -> CanonicalType {
75
- return self . type . subst ( type: type. type , with: targetType. type ) . canonical
91
+ return self . rawType . subst ( type: type. rawType , with: targetType. rawType ) . canonical
76
92
}
77
93
}
78
94
79
- /// Contains common properties of AST.Type and AST.CanonicalType
95
+ /// Implements the common members of ` AST.Type`, ` AST.CanonicalType` and `SIL.Type`.
80
96
public protocol TypeProperties {
81
- var type : Type { get }
97
+ var rawType : Type { get }
82
98
}
83
99
84
100
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 isOptional : Bool { type. bridged. isOptional ( ) }
97
- public var isMetatypeType : Bool { type. bridged. isMetatypeType ( ) }
98
- public var isExistentialMetatypeType : Bool { type. bridged. isExistentialMetatypeType ( ) }
101
+ public var description : String { String ( taking: rawType. bridged. getDebugDescription ( ) ) }
102
+
103
+ //===--------------------------------------------------------------------===//
104
+ // Checks for different kinds of types
105
+ //===--------------------------------------------------------------------===//
106
+
107
+ public var isBuiltinInteger : Bool { rawType. bridged. isBuiltinInteger ( ) }
108
+
109
+ public func isBuiltinInteger( withFixedWidth width: Int ) -> Bool {
110
+ rawType. bridged. isBuiltinFixedWidthInteger ( width)
111
+ }
112
+
113
+ public var isBuiltinFloat : Bool { rawType. bridged. isBuiltinFloat ( ) }
114
+ public var isBuiltinVector : Bool { rawType. bridged. isBuiltinVector ( ) }
115
+
116
+ public var isClass : Bool {
117
+ if let nominal = nominal, nominal is ClassDecl {
118
+ return true
119
+ }
120
+ return false
121
+ }
122
+
123
+ public var isStruct : Bool {
124
+ if let nominal = nominal, nominal is StructDecl {
125
+ return true
126
+ }
127
+ return false
128
+ }
129
+
130
+ public var isEnum : Bool {
131
+ if let nominal = nominal, nominal is EnumDecl {
132
+ return true
133
+ }
134
+ return false
135
+ }
136
+
137
+ public var isTuple : Bool { rawType. bridged. isTuple ( ) }
138
+ public var isFunction : Bool { rawType. bridged. isFunction ( ) }
139
+ public var isExistentialArchetype : Bool { rawType. bridged. isExistentialArchetype ( ) }
140
+ public var isExistentialArchetypeWithError : Bool { rawType. bridged. isExistentialArchetypeWithError ( ) }
141
+ public var isExistential : Bool { rawType. bridged. isExistential ( ) }
142
+ public var isClassExistential : Bool { rawType. bridged. isClassExistential ( ) }
143
+ public var isUnownedStorageType : Bool { return rawType. bridged. isUnownedStorageType ( ) }
144
+ public var isMetatype : Bool { rawType. bridged. isMetatypeType ( ) }
145
+ public var isExistentialMetatype : Bool { rawType. bridged. isExistentialMetatypeType ( ) }
146
+ public var isDynamicSelf : Bool { rawType. bridged. isDynamicSelf ( ) }
147
+
148
+ /// True if this is the type which represents an integer literal used in a type position.
149
+ /// For example `N` in `struct T<let N: Int> {}`
150
+ public var isInteger : Bool { rawType. bridged. isInteger ( ) }
151
+
152
+ public var canBeClass : Type . TraitResult { rawType. bridged. canBeClass ( ) . result }
153
+
154
+ /// True if this the nominal type `Swift.Optional`.
155
+ public var isOptional : Bool { rawType. bridged. isOptional ( ) }
156
+
157
+ /// True if this type is a value type (struct/enum) that defines a `deinit`.
158
+ public var isValueTypeWithDeinit : Bool {
159
+ if let nominal = nominal, nominal. valueTypeDestructor != nil {
160
+ return true
161
+ }
162
+ return false
163
+ }
164
+
165
+ //===--------------------------------------------------------------------===//
166
+ // Type properties
167
+ //===--------------------------------------------------------------------===//
168
+
169
+ public var isLegalFormalType : Bool { rawType. bridged. isLegalFormalType ( ) }
170
+ public var hasArchetype : Bool { rawType. bridged. hasArchetype ( ) }
171
+ public var hasTypeParameter : Bool { rawType. bridged. hasTypeParameter ( ) }
172
+ public var hasLocalArchetype : Bool { rawType. bridged. hasLocalArchetype ( ) }
173
+ public var isEscapable : Bool { rawType. bridged. isEscapable ( ) }
174
+ public var isNoEscape : Bool { rawType. bridged. isNoEscape ( ) }
175
+
99
176
public var representationOfMetatype : AST . `Type` . MetatypeRepresentation {
100
- type . bridged. getRepresentationOfMetatype ( ) . representation
177
+ rawType . bridged. getRepresentationOfMetatype ( ) . representation
101
178
}
102
- public var invocationGenericSignatureOfFunctionType : GenericSignature {
103
- GenericSignature ( bridged: type. bridged. getInvocationGenericSignatureOfFunctionType ( ) )
179
+
180
+ /// Assumes this is a nominal type. Returns a substitution map that sends each
181
+ /// generic parameter of the declaration's generic signature to the corresponding
182
+ /// generic argument of this nominal type.
183
+ ///
184
+ /// Eg: Array<Int> ---> { Element := Int }
185
+ public var contextSubstitutionMap : SubstitutionMap {
186
+ SubstitutionMap ( bridged: rawType. bridged. getContextSubstitutionMap ( ) )
104
187
}
105
188
106
- public var canBeClass : Type . TraitResult { type. bridged. canBeClass ( ) . result }
189
+ // True if this type has generic parameters or it is in a context (e.g. an outer type) which has generic parameters.
190
+ public var isGenericAtAnyLevel : Bool { rawType. bridged. isGenericAtAnyLevel ( ) }
107
191
108
- public var anyNominal : NominalTypeDecl ? { type. bridged. getAnyNominal ( ) . getAs ( NominalTypeDecl . self) }
192
+ public var nominal : NominalTypeDecl ? {
193
+ rawType. bridged. getNominalOrBoundGenericNominal ( ) . getAs ( NominalTypeDecl . self)
194
+ }
109
195
110
196
/// Performas a global conformance lookup for this type for `protocol`.
111
197
/// It checks conditional requirements.
@@ -118,7 +204,7 @@ extension TypeProperties {
118
204
/// Returns an invalid conformance if the search failed, otherwise an
119
205
/// abstract, concrete or pack conformance, depending on the lookup type.
120
206
public func checkConformance( to protocol: ProtocolDecl ) -> Conformance {
121
- return Conformance ( bridged: type . bridged. checkConformance ( `protocol`. bridged) )
207
+ return Conformance ( bridged: rawType . bridged. checkConformance ( `protocol`. bridged) )
122
208
}
123
209
}
124
210
@@ -174,6 +260,6 @@ extension Type: Equatable {
174
260
175
261
extension CanonicalType : Equatable {
176
262
public static func == ( lhs: CanonicalType , rhs: CanonicalType ) -> Bool {
177
- lhs. type == rhs. type
263
+ lhs. rawType == rhs. rawType
178
264
}
179
265
}
0 commit comments