-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathForEachField.swift
500 lines (425 loc) · 12.6 KB
/
ForEachField.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2020 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
//
//===----------------------------------------------------------------------===//
// RUN: %target-run-simple-swift
// RUN: %target-run-simple-swift(-Xfrontend -disable-reflection-names -DNO_FIELD_NAMES)
// REQUIRES: executable_test
// REQUIRES: reflection
// Only run these tests with a just-built stdlib.
// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: back_deployment_runtime
@_spi(Reflection) import Swift
import StdlibUnittest
struct TestStruct {
var int = 0
var double = 0.0
var bool = false
}
struct GenericStruct<T> {
var int = 0
var first: T
var second: T
}
enum TestEnum {
case one
case two
case three(TestStruct)
}
class BaseClass {
var superInt = 0
init() {}
}
class TestClass: BaseClass {
var int = 0
var double = 0.0
var bool = false
override init() {}
}
class TestSubclass: TestClass {
var strings: [String] = []
override init() {}
}
class GenericClass<T, U>: BaseClass {
var first: T
var second: U
init(_ t: T, _ u: U) {
self.first = t
self.second = u
}
}
class GenericSubclass<V, W>: GenericClass<V, Bool> {
var third: W
init(_ v: V, _ w: W) {
self.third = w
super.init(v, false)
}
}
class OwnershipTestClass: BaseClass {
weak var test1: TestClass?
unowned var test2: TestClass
unowned(unsafe) var test3: TestClass
init(_ t: TestClass) {
self.test1 = t
self.test2 = t
self.test3 = t
}
}
struct SimilarToNSPoint {
var x: Double
var y: Double
}
struct SimilarToNSSize {
var width: Double
var height: Double
}
struct SimilarToNSRect {
var origin: SimilarToNSPoint
var size: SimilarToNSSize
}
struct ContainsObject {
var obj: TestClass
}
struct LetKeyPaths {
let int : Int
let double: Double
}
protocol TestExistential {}
struct KeyPathTypes {
weak var weakObj: TestClass?
unowned var unownedObj: TestClass
var obj: TestClass
var tuple: (Int, Int, Int)
var structField: Int
var function: (Int) -> (Int)
var optionalFunction: (Int) -> (Int)?
var enumField: TestEnum
var existential: TestExistential
var existentialMetatype: Any.Type
var metatype: Int.Type
}
#if _runtime(_ObjC)
import Foundation
class NSObjectSubclass: NSObject {
var point: (Double, Double)
init(x: Double, y: Double) {
self.point = (x, y)
}
}
class EmptyNSObject: NSObject {}
#endif
@available(SwiftStdlib 5.2, *)
func checkFields<T>(
of type: T.Type,
options: _EachFieldOptions = [],
fields: [String: (Int, Any.Type)]
) {
var count = 0
_forEachField(of: T.self, options: options) {
charPtr, offset, type, kind in
count += 1
let fieldName = String(cString: charPtr)
if fieldName == "" {
#if NO_FIELD_NAMES
expectTrue(fields.values.contains{ $0 == offset && $1 == type })
#else
expectTrue(false, "Empty field name")
#endif
return true
}
guard let (checkOffset, checkType) = fields[fieldName] else {
expectTrue(false, "Unexpected field '\(fieldName)'")
return true
}
expectEqual(checkOffset, offset)
expectEqual(checkType, type)
return true
}
expectEqual(fields.count, count)
}
@available(SwiftStdlib 5.5, *)
func checkFieldsWithKeyPath<T>(
of type: T.Type,
options: _EachFieldOptions = [],
fields: [String: PartialKeyPath<T>]
) {
var count = 0
_forEachFieldWithKeyPath(of: T.self, options: options) {
charPtr, keyPath in
count += 1
let fieldName = String(cString: charPtr)
if fieldName == "" {
#if NO_FIELD_NAMES
expectTrue(fields.values.contains{ $0 == keyPath })
#else
expectTrue(false, "Empty field name")
#endif
return true
}
guard let checkKeyPath = fields[fieldName] else {
expectTrue(false, "Unexpected field '\(fieldName)'")
return true
}
expectTrue(checkKeyPath == keyPath)
return true
}
expectEqual(fields.count, count)
}
protocol ExistentialProtocol {}
extension TestStruct: ExistentialProtocol {}
extension GenericStruct: ExistentialProtocol {}
extension GenericSubclass: ExistentialProtocol {}
@available(SwiftStdlib 5.2, *)
extension ExistentialProtocol {
static func doCheckFields(
options: _EachFieldOptions = [],
fields: [String: (Int, Any.Type)]
) {
checkFields(of: Self.self, options: options, fields: fields)
}
}
@available(SwiftStdlib 5.2, *)
func checkFieldsAsExistential(
of type: ExistentialProtocol.Type,
options: _EachFieldOptions = [],
fields: [String: (Int, Any.Type)]
) {
type.doCheckFields(options: options, fields: fields)
}
@available(SwiftStdlib 5.2, *)
func _withTypeEncodingCallback(encoding: inout String, name: UnsafePointer<CChar>, offset: Int, type: Any.Type, kind: _MetadataKind) -> Bool {
if type == Bool.self {
encoding += "B"
return true
} else if type == Int.self {
if MemoryLayout<Int>.size == MemoryLayout<Int64>.size {
encoding += "q"
} else if MemoryLayout<Int>.size == MemoryLayout<Int32>.size {
encoding += "l"
} else {
return false
}
return true
} else if type == Double.self {
encoding += "d"
return true
}
switch kind {
case .struct:
encoding += "{"
defer { encoding += "}" }
_forEachField(of: type) { name, offset, type, kind in
_withTypeEncodingCallback(encoding: &encoding, name: name, offset: offset, type: type, kind: kind)
}
case .class:
encoding += "@"
default:
break
}
return true
}
@available(SwiftStdlib 5.2, *)
func getTypeEncoding<T>(_ type: T.Type) -> String? {
var encoding = ""
_ = _forEachField(of: type) { name, offset, type, kind in
_withTypeEncodingCallback(encoding: &encoding, name: name, offset: offset, type: type, kind: kind)
}
return "{\(encoding)}"
}
//===----------------------------------------------------------------------===//
var tests = TestSuite("ForEachField")
if #available(SwiftStdlib 5.2, *) {
tests.test("TestTuple") {
checkFields(
of: (Int, Bool).self,
fields: [".0": (0, Int.self), ".1": (MemoryLayout<Int>.stride, Bool.self)])
checkFields(
of: (a: Int, b: Bool).self,
fields: ["a": (0, Int.self), "b": (MemoryLayout<Int>.stride, Bool.self)])
}
tests.test("TestEnum") {
checkFields(of: TestEnum.self, fields: [:])
}
tests.test("TestStruct") {
checkFields(
of: TestStruct.self,
fields: [
"int": (0, Int.self),
"double": (MemoryLayout<Double>.stride, Double.self),
"bool": (MemoryLayout<Double>.stride * 2, Bool.self),
])
checkFieldsAsExistential(
of: TestStruct.self,
fields: [
"int": (0, Int.self),
"double": (MemoryLayout<Double>.stride, Double.self),
"bool": (MemoryLayout<Double>.stride * 2, Bool.self),
])
// Applying to struct type with .classType option fails
expectFalse(_forEachField(of: TestStruct.self, options: .classType) {
_, _, _, _ in true
})
}
if #available(SwiftStdlib 5.5, *) {
tests.test("StructKeyPath") {
checkFieldsWithKeyPath(
of: TestStruct.self,
fields: [
"int": \TestStruct.int,
"double": \TestStruct.double,
"bool": \TestStruct.bool,
])
}
tests.test("LetKeyPaths") {
checkFieldsWithKeyPath(
of: LetKeyPaths.self,
fields: [
"int": \LetKeyPaths.int,
"double": \LetKeyPaths.double,
])
}
tests.test("KeyPathTypes") {
checkFieldsWithKeyPath(
of: KeyPathTypes.self,
options: .ignoreUnknown,
fields: [
"obj": \KeyPathTypes.obj,
"tuple": \KeyPathTypes.tuple,
"structField": \KeyPathTypes.structField,
"enumField": \KeyPathTypes.enumField,
"existential": \KeyPathTypes.existential,
"existentialMetatype": \KeyPathTypes.existentialMetatype,
])
}
tests.test("TupleKeyPath") {
typealias TestTuple = (Int, Int, TestClass, TestStruct)
checkFieldsWithKeyPath(
of: TestTuple.self,
fields: [
".0": \TestTuple.0,
".1": \TestTuple.1,
".2": \TestTuple.2,
".3": \TestTuple.3,
])
}
}
func checkGenericStruct<T>(_: T.Type) {
let firstOffset = max(MemoryLayout<Int>.stride, MemoryLayout<T>.alignment)
checkFields(
of: GenericStruct<T>.self,
fields: [
"int": (0, Int.self),
"first": (firstOffset, T.self),
"second": (firstOffset + MemoryLayout<T>.stride, T.self),
])
checkFieldsAsExistential(
of: GenericStruct<T>.self,
fields: [
"int": (0, Int.self),
"first": (firstOffset, T.self),
"second": (firstOffset + MemoryLayout<T>.stride, T.self),
])
}
tests.test("GenericStruct") {
checkGenericStruct(Bool.self)
checkGenericStruct(TestStruct.self)
checkGenericStruct((TestStruct, TestClass, Int, Int).self)
}
tests.test("TestClass") {
let classOffset = MemoryLayout<Int>.stride * 2
let doubleOffset = classOffset
+ max(MemoryLayout<Int>.stride * 2, MemoryLayout<Double>.stride)
checkFields(
of: TestClass.self, options: .classType,
fields: [
"superInt": (classOffset, Int.self),
"int": (classOffset + MemoryLayout<Int>.stride, Int.self),
"double": (doubleOffset, Double.self),
"bool": (doubleOffset + MemoryLayout<Double>.stride, Bool.self),
])
checkFields(
of: TestSubclass.self, options: .classType,
fields: [
"superInt": (classOffset, Int.self),
"int": (classOffset + MemoryLayout<Int>.stride, Int.self),
"double": (doubleOffset, Double.self),
"bool": (doubleOffset + MemoryLayout<Double>.stride, Bool.self),
"strings": (doubleOffset + MemoryLayout<Double>.stride + MemoryLayout<Array<String>>.stride, Array<String>.self),
])
let firstOffset = classOffset
+ max(MemoryLayout<Int>.stride, MemoryLayout<TestStruct>.alignment)
checkFields(
of: GenericSubclass<TestStruct, TestStruct>.self, options: .classType,
fields: [
"superInt": (classOffset, Int.self),
"first": (firstOffset, TestStruct.self),
"second": (firstOffset + MemoryLayout<TestStruct>.size, Bool.self),
"third": (firstOffset + MemoryLayout<TestStruct>.stride, TestStruct.self),
])
checkFields(
of: GenericSubclass<Int, Never>.self, options: .classType,
fields: [
"superInt": (classOffset, Int.self),
"first": (classOffset + MemoryLayout<Int>.stride, Int.self),
"second": (classOffset + MemoryLayout<Int>.stride * 2, Bool.self),
"third": (0, Never.self),
])
checkFieldsAsExistential(
of: GenericSubclass<TestStruct, TestStruct>.self, options: .classType,
fields: [
"superInt": (classOffset, Int.self),
"first": (firstOffset, TestStruct.self),
"second": (firstOffset + MemoryLayout<TestStruct>.size, Bool.self),
"third": (firstOffset + MemoryLayout<TestStruct>.stride, TestStruct.self),
])
// Applying to class type without .classType option fails
expectFalse(_forEachField(of: TestClass.self) {
_, _, _, _ in true
})
}
tests.test("OwnershipTestClass") {
let classOffset = MemoryLayout<Int>.stride * 2
checkFields(
of: OwnershipTestClass.self, options: .classType,
fields: [
"superInt": (classOffset, Int.self),
"test1": (classOffset + MemoryLayout<Int>.stride, Optional<TestClass>.self),
"test2": (classOffset + MemoryLayout<Int>.stride * 2, TestClass.self),
"test3": (classOffset + MemoryLayout<Int>.stride * 3, TestClass.self),
])
}
#if _runtime(_ObjC)
tests.test("NSObjectSubclass") {
expectTrue(_forEachField(of: NSObjectSubclass.self, options: .classType) {
charPtr, _, type, _ in
let fieldName = String(cString: charPtr)
#if NO_FIELD_NAMES
return type == (Double, Double).self
&& fieldName == ""
#else
return type == (Double, Double).self
&& fieldName == "point"
#endif
})
expectTrue(_forEachField(of: EmptyNSObject.self, options: .classType) {
_, _, _, _ in true
})
}
#endif
tests.test("withTypeEncoding") {
expectEqual("{@}", getTypeEncoding(ContainsObject.self))
expectEqual("{{dd}{dd}}", getTypeEncoding(SimilarToNSRect.self))
let testEncoding = getTypeEncoding(TestStruct.self)
expectTrue("{qdB}" == testEncoding || "{ldB}" == testEncoding)
}
runAllTests()
} else {
runNoTests()
}