forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathType.swift
313 lines (252 loc) · 10.3 KB
/
Type.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
//===--- Type.swift - Value type ------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Basic
import AST
import SILBridging
/// A Swift type that has been lowered to a SIL representation type.
/// A `SIL.Type` is basically an `AST.CanonicalType` with the distinction between "object" and "address" type
/// (`*T` is the type of an address pointing at T).
/// Note that not all `CanonicalType`s can be represented as a `SIL.Type`.
public struct Type : CustomStringConvertible, NoReflectionChildren {
public let bridged: BridgedType
public var isAddress: Bool { bridged.isAddress() }
public var isObject: Bool { !isAddress }
public var addressType: Type { bridged.getAddressType().type }
public var objectType: Type { bridged.getObjectType().type }
public var astType: CanonicalType { CanonicalType(bridged: bridged.getCanType()) }
public func isTrivial(in function: Function) -> Bool {
return bridged.isTrivial(function.bridged)
}
/// Returns true if the type is a trivial type and is and does not contain a Builtin.RawPointer.
public func isTrivialNonPointer(in function: Function) -> Bool {
return !bridged.isNonTrivialOrContainsRawPointer(function.bridged)
}
/// True if this type is a value type (struct/enum) that requires deinitialization beyond
/// destruction of its members.
public var isValueTypeWithDeinit: Bool { bridged.isValueTypeWithDeinit() }
public func isLoadable(in function: Function) -> Bool {
return bridged.isLoadable(function.bridged)
}
public func isReferenceCounted(in function: Function) -> Bool {
return bridged.isReferenceCounted(function.bridged)
}
public var isUnownedStorageType: Bool {
return bridged.isUnownedStorageType()
}
public var hasArchetype: Bool { bridged.hasArchetype() }
public var isClass: Bool { bridged.isClassOrBoundGenericClass() }
public var isStruct: Bool { bridged.isStructOrBoundGenericStruct() }
public var isTuple: Bool { bridged.isTuple() }
public var isEnum: Bool { bridged.isEnumOrBoundGenericEnum() }
public var isFunction: Bool { bridged.isFunction() }
public var isMetatype: Bool { bridged.isMetatype() }
public var isClassExistential: Bool { bridged.isClassExistential() }
public var isNoEscapeFunction: Bool { bridged.isNoEscapeFunction() }
public var containsNoEscapeFunction: Bool { bridged.containsNoEscapeFunction() }
public var isThickFunction: Bool { bridged.isThickFunction() }
public var isAsyncFunction: Bool { bridged.isAsyncFunction() }
public var canBeClass: CanonicalType.TraitResult { astType.canBeClass }
public var isMoveOnly: Bool { bridged.isMoveOnly() }
// Note that invalid types are not considered Escapable. This makes it difficult to make any assumptions about
// nonescapable types.
public func isEscapable(in function: Function) -> Bool {
bridged.isEscapable(function.bridged)
}
public func mayEscape(in function: Function) -> Bool {
!isNoEscapeFunction && isEscapable(in: function)
}
/// Can only be used if the type is in fact a nominal type (`isNominal` is true).
public var nominal: NominalTypeDecl? {
bridged.getNominalOrBoundGenericNominal().getAs(NominalTypeDecl.self)
}
public var superClassType: Type? {
precondition(isClass)
return bridged.getSuperClassType().typeOrNil
}
public var contextSubstitutionMap: SubstitutionMap {
SubstitutionMap(bridged: bridged.getContextSubstitutionMap())
}
public var isGenericAtAnyLevel: Bool { bridged.isGenericAtAnyLevel() }
public var isOrContainsObjectiveCClass: Bool { bridged.isOrContainsObjectiveCClass() }
public var isBuiltinInteger: Bool { bridged.isBuiltinInteger() }
public var isBuiltinFloat: Bool { bridged.isBuiltinFloat() }
public var isBuiltinVector: Bool { bridged.isBuiltinVector() }
public var builtinVectorElementType: Type { bridged.getBuiltinVectorElementType().type }
public func isBuiltinInteger(withFixedWidth width: Int) -> Bool {
bridged.isBuiltinFixedWidthInteger(width)
}
public func isExactSuperclass(of type: Type) -> Bool {
bridged.isExactSuperclassOf(type.bridged)
}
public var isVoid: Bool {
bridged.isVoid()
}
public func isEmpty(in function: Function) -> Bool {
bridged.isEmpty(function.bridged)
}
public var tupleElements: TupleElementArray { TupleElementArray(type: self) }
public func getLoweredType(in function: Function) -> Type {
function.bridged.getLoweredType(self.bridged).type
}
/// Can only be used if the type is in fact a nominal type (`isNominal` is true).
/// Returns nil if the nominal is a resilient type because in this case the complete list
/// of fields is not known.
public func getNominalFields(in function: Function) -> NominalFieldsArray? {
if nominal!.isResilient(in: function) {
return nil
}
return NominalFieldsArray(type: self, function: function)
}
/// Can only be used if the type is in fact an enum type.
/// Returns nil if the enum is a resilient type because in this case the complete list
/// of cases is not known.
public func getEnumCases(in function: Function) -> EnumCases? {
if nominal!.isResilient(in: function) {
return nil
}
return EnumCases(enumType: self, function: function)
}
public typealias MetatypeRepresentation = BridgedType.MetatypeRepresentation
public func loweredInstanceTypeOfMetatype(in function: Function) -> Type {
bridged.getLoweredInstanceTypeOfMetatype(function.bridged).type
}
public var isDynamicSelfMetatype: Bool {
bridged.isDynamicSelfMetatype()
}
public func representationOfMetatype(in function: Function) -> MetatypeRepresentation {
bridged.getRepresentationOfMetatype(function.bridged)
}
public var isCalleeConsumedFunction: Bool { bridged.isCalleeConsumedFunction() }
public var isMarkedAsImmortal: Bool { bridged.isMarkedAsImmortal() }
public func getIndexOfEnumCase(withName name: String) -> Int? {
let idx = name._withBridgedStringRef {
bridged.getCaseIdxOfEnumType($0)
}
return idx >= 0 ? idx : nil
}
/// Returns true if this is a struct, enum or tuple and `otherType` is contained in this type - or is the same type.
public func aggregateIsOrContains(_ otherType: Type, in function: Function) -> Bool {
if self == otherType {
return true
}
if isStruct {
guard let fields = getNominalFields(in: function) else {
return true
}
return fields.contains { $0.aggregateIsOrContains(otherType, in: function) }
}
if isTuple {
return tupleElements.contains { $0.aggregateIsOrContains(otherType, in: function) }
}
if isEnum {
guard let cases = getEnumCases(in: function) else {
return true
}
return cases.contains { $0.payload?.aggregateIsOrContains(otherType, in: function) ?? false }
}
return false
}
// compiling bridged.getFunctionTypeWithNoEscape crashes the 5.10 Windows compiler
#if !os(Windows)
// TODO: https://github.com/apple/swift/issues/73253
public func getFunctionType(withNoEscape: Bool) -> Type {
bridged.getFunctionTypeWithNoEscape(withNoEscape).type
}
#endif
public var description: String {
String(taking: bridged.getDebugDescription())
}
}
extension Value {
public var isEscapable: Bool {
type.objectType.isEscapable(in: parentFunction)
}
public var mayEscape: Bool {
type.objectType.mayEscape(in: parentFunction)
}
}
extension Type: Equatable {
public static func ==(lhs: Type, rhs: Type) -> Bool {
lhs.bridged.opaqueValue == rhs.bridged.opaqueValue
}
}
public struct TypeArray : RandomAccessCollection, CustomReflectable {
public let bridged: BridgedSILTypeArray
public var startIndex: Int { return 0 }
public var endIndex: Int { return bridged.getCount() }
public init(bridged: BridgedSILTypeArray) {
self.bridged = bridged
}
public subscript(_ index: Int) -> Type {
bridged.getAt(index).type
}
public var customMirror: Mirror {
let c: [Mirror.Child] = map { (label: nil, value: $0) }
return Mirror(self, children: c)
}
}
public struct NominalFieldsArray : RandomAccessCollection, FormattedLikeArray {
fileprivate let type: Type
fileprivate let function: Function
public var startIndex: Int { return 0 }
public var endIndex: Int { Int(type.bridged.getNumNominalFields()) }
public subscript(_ index: Int) -> Type {
type.bridged.getFieldType(index, function.bridged).type
}
public func getIndexOfField(withName name: String) -> Int? {
let idx = name._withBridgedStringRef {
type.bridged.getFieldIdxOfNominalType($0)
}
return idx >= 0 ? idx : nil
}
public func getNameOfField(withIndex idx: Int) -> StringRef {
StringRef(bridged: type.bridged.getFieldName(idx))
}
}
public struct EnumCase {
public let payload: Type?
public let index: Int
}
public struct EnumCases : CollectionLikeSequence, IteratorProtocol {
fileprivate let enumType: Type
fileprivate let function: Function
private var caseIterator: BridgedType.EnumElementIterator
private var caseIndex = 0
fileprivate init(enumType: Type, function: Function) {
self.enumType = enumType
self.function = function
self.caseIterator = enumType.bridged.getFirstEnumCaseIterator()
}
public mutating func next() -> EnumCase? {
if !enumType.bridged.isEndCaseIterator(caseIterator) {
defer {
caseIterator = caseIterator.getNext()
caseIndex += 1
}
return EnumCase(payload: enumType.bridged.getEnumCasePayload(caseIterator, function.bridged).typeOrNil,
index: caseIndex)
}
return nil
}
}
public struct TupleElementArray : RandomAccessCollection, FormattedLikeArray {
fileprivate let type: Type
public var startIndex: Int { return 0 }
public var endIndex: Int { Int(type.bridged.getNumTupleElements()) }
public subscript(_ index: Int) -> Type {
type.bridged.getTupleElementType(index).type
}
}
extension BridgedType {
public var type: Type { Type(bridged: self) }
var typeOrNil: Type? { isNull() ? nil : type }
}