/
Reflection.swift
509 lines (426 loc) · 15.2 KB
/
Reflection.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
501
502
503
504
505
506
507
508
509
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
/// Customizes the result of `reflect(x)`, where `x` is a conforming
/// type.
public protocol Reflectable {
// The runtime has inappropriate knowledge of this protocol and how its
// witness tables are laid out. Changing this protocol requires a
// corresponding change to Reflection.cpp.
/// Returns a mirror that reflects `self`.
func getMirror() -> MirrorType
}
/// A unique identifier for a class instance or metatype. This can be used by
/// reflection clients to recognize cycles in the object graph.
///
/// In Swift, only class instances and metatypes have unique identities. There
/// is no notion of identity for structs, enums, functions, or tuples.
public struct ObjectIdentifier : Hashable, Comparable {
let value: Builtin.RawPointer
/// Convert to a `UInt` that captures the full value of `self`.
///
/// Axiom: `a.uintValue == b.uintValue` iff `a == b`
public var uintValue: UInt {
return UInt(Builtin.ptrtoint_Word(value))
}
// FIXME: Better hashing algorithm
/// The hash value.
///
/// **Axiom:** `x == y` implies `x.hashValue == y.hashValue`
///
/// **Note:** the hash value is not guaranteed to be stable across
/// different invocations of the same program. Do not persist the
/// hash value across program runs.
public var hashValue: Int {
return Int(Builtin.ptrtoint_Word(value))
}
/// Construct an instance that uniquely identifies the class instance `x`.
public init(_ x: AnyObject) {
self.value = unsafeBitCast(x, Builtin.RawPointer.self)
}
/// Construct an instance that uniquely identifies the metatype `x`.
public init(_ x: Any.Type) {
self.value = unsafeBitCast(x, Builtin.RawPointer.self)
}
}
public func <(lhs: ObjectIdentifier, rhs: ObjectIdentifier) -> Bool {
return lhs.uintValue < rhs.uintValue
}
public func ==(x: ObjectIdentifier, y: ObjectIdentifier) -> Bool {
return Bool(Builtin.cmp_eq_RawPointer(x.value, y.value))
}
/// The sum of types that can be used as a quick look representation.
public enum QuickLookObject {
//
// This type must be binary-compatible with the 'QuickLookObject' struct in
// stdlib/Runtime/Reflection.mm, and 'QuickLookObject?' must be binary
// compatible with 'OptionalQuickLookObject' from the same.
//
// NB: This type is somewhat carefully laid out to *suppress* enum layout
// optimization so that it is easier to manufacture in the C++ runtime
// implementation.
/// Plain text.
case Text(String)
/// An integer numeric value.
case Int(Int64)
/// An unsigned integer numeric value.
case UInt(UInt64)
/// A single precision floating-point numeric value.
case Float(Float32)
/// A double precision floating-point numeric value.
case Double(Float64)
// FIXME: Uses an Any to avoid coupling a particular Cocoa type.
/// An image.
case Image(Any)
// FIXME: Uses an Any to avoid coupling a particular Cocoa type.
/// A sound.
case Sound(Any)
// FIXME: Uses an Any to avoid coupling a particular Cocoa type.
/// A color.
case Color(Any)
// FIXME: Uses an Any to avoid coupling a particular Cocoa type.
/// A bezier path.
case BezierPath(Any)
// FIXME: Uses an Any to avoid coupling a particular Cocoa type.
/// An attributed string.
case AttributedString(Any)
/// A rectangle
/// Uses explicit coordinates to avoid coupling a particular Cocoa type.
case Rectangle(Float64,Float64,Float64,Float64)
/// A point
/// Uses explicit coordinates to avoid coupling a particular Cocoa type.
case Point(Float64,Float64)
/// A size
/// Uses explicit coordinates to avoid coupling a particular Cocoa type.
case Size(Float64,Float64)
/// A logical value
case Logical(Bool)
/// A range
/// Uses explicit values to avoid coupling a particular Cocoa type.
case Range(UInt64, UInt64)
/// A GUI view
/// Uses an Any to avoid coupling a particular Cocoa type.
case View(Any)
/// A graphical sprite
/// Uses an Any to avoid coupling a particular Cocoa type.
case Sprite(Any)
/// A Uniform Resource Locator
case URL(String)
/// Raw data that has already been encoded in a format the IDE understands.
case _Raw([UInt8], String)
}
/// How children of this value should be presented in the IDE.
public enum MirrorDisposition {
/// As a struct.
case Struct
/// As a class.
case Class
/// As an enum.
case Enum
/// As a tuple.
case Tuple
/// As a miscellaneous aggregate with a fixed set of children.
case Aggregate
/// As a container that is accessed by index.
case IndexContainer
/// As a container that is accessed by key.
case KeyContainer
/// As a container that represents membership of its values.
case MembershipContainer
/// As a miscellaneous container with a variable number of children.
case Container
/// An Optional which can have either zero or one children.
case Optional
/// An Objective-C object imported in Swift
case ObjCObject
}
/// The type returned by `reflect(x)`; supplies an API for runtime
/// reflection on `x`
public protocol MirrorType {
/// The instance being reflected
var value: Any { get }
/// Identical to `value.dynamicType`
var valueType: Any.Type { get }
/// A unique identifier for `value` if it is a class instance; `nil`
/// otherwise.
var objectIdentifier: ObjectIdentifier? { get }
/// The count of `value`\ 's logical children
var count: Int { get }
/// Get a name and mirror for the `i`\ th logical child.
subscript(i: Int) -> (String, MirrorType) { get }
/// A string description of `value`.
var summary: String { get }
/// A rich representation of `value` for an IDE, or `nil` if none is supplied.
var quickLookObject: QuickLookObject? { get }
/// How `value` should be presented in an IDE.
var disposition: MirrorDisposition { get }
}
/// An entry point that can be called from C++ code to get the summary string
/// for an arbitrary object. The memory pointed to by "out" is initialized with
/// the summary string.
@asmname("swift_getSummary")
public // COMPILER_INTRINSIC
func _getSummary<T>(out: UnsafeMutablePointer<String>, x: T) {
out.initialize(reflect(x).summary)
}
/// Produce a mirror for any value. If the value's type conforms to Reflectable,
/// invoke its getMirror() method; otherwise, fall back to an implementation
/// in the runtime that structurally reflects values of any type.
@asmname("swift_reflectAny")public func reflect<T>(x: T) -> MirrorType
/// Unsafely produce a mirror for a value in memory whose lifetime is
/// guaranteed by holding a strong reference to a heap object.
/// This lets containers with heap storage vend mirrors for their elements
/// without unnecessary copying of the underlying value.
@asmname("swift_unsafeReflectAny")func unsafeReflect<T>(
owner: Builtin.NativeObject,
ptr: UnsafeMutablePointer<T>
) -> MirrorType
/// Dump an object's contents using its mirror to the specified output stream.
public func dump<T, TargetStream : OutputStreamType>(
x: T, inout targetStream: TargetStream,
name: String? = nil, indent: Int = 0,
maxDepth: Int = .max, maxItems: Int = .max
) -> T {
var maxItemCounter = maxItems
var visitedItems = [ObjectIdentifier : Int]()
_dumpWithMirror(
reflect(x), name, indent, maxDepth, &maxItemCounter, &visitedItems,
&targetStream)
return x
}
/// Dump an object's contents using its mirror to standard output.
public func dump<T>(x: T, name: String? = nil, indent: Int = 0,
maxDepth: Int = .max, maxItems: Int = .max) -> T {
var stdoutStream = _Stdout()
return dump(
x, &stdoutStream, name: name, indent: indent, maxDepth: maxDepth,
maxItems: maxItems)
}
/// Dump an object's contents using a mirror. User code should use dump().
func _dumpWithMirror<TargetStream : OutputStreamType>(
mirror: MirrorType, name: String?, indent: Int, maxDepth: Int,
inout maxItemCounter: Int,
inout visitedItems: [ObjectIdentifier : Int],
inout targetStream: TargetStream
) {
if maxItemCounter <= 0 { return }
--maxItemCounter
for _ in 0..<indent { print(" ", &targetStream) }
let count = mirror.count
let bullet = count == 0 ? "-"
: maxDepth <= 0 ? "▹" : "▿"
print("\(bullet) ", &targetStream)
if let nam = name {
print("\(nam): ", &targetStream)
}
print(mirror.summary, &targetStream)
if let id = mirror.objectIdentifier {
if let previous = visitedItems[id] {
println(" #\(previous)", &targetStream)
return
}
let identifier = visitedItems.count
visitedItems[id] = identifier
print(" #\(identifier)", &targetStream)
}
println("", &targetStream)
if maxDepth <= 0 { return }
for i in 0..<count {
if maxItemCounter <= 0 {
for _ in 0..<(indent+4) { print(" ", &targetStream) }
let remainder = count - i
print("(\(remainder)", &targetStream)
if i > 0 { print(" more", &targetStream) }
if remainder == 1 {
println(" child)", &targetStream)
} else {
println(" children)", &targetStream)
}
return
}
let (name, child) = mirror[i]
_dumpWithMirror(child, name, indent + 2, maxDepth - 1,
&maxItemCounter, &visitedItems, &targetStream)
}
}
// -- MirrorType implementations for basic data types
/// A mirror for a value that is represented as a simple value with no
/// children.
internal struct _LeafMirror<T>: MirrorType {
let _value: T
let summaryFunction: T -> String
let quickLookFunction: T -> QuickLookObject?
init(_ value: T, _ summaryFunction: T -> String,
_ quickLookFunction: T -> QuickLookObject?) {
self._value = value
self.summaryFunction = summaryFunction
self.quickLookFunction = quickLookFunction
}
var value: Any { return _value }
var valueType: Any.Type { return value.dynamicType }
var objectIdentifier: ObjectIdentifier? { return nil }
var count: Int { return 0 }
subscript(i: Int) -> (String, MirrorType) {
_preconditionFailure("no children")
}
var summary: String { return summaryFunction(_value) }
var quickLookObject: QuickLookObject? { return quickLookFunction(_value) }
var disposition: MirrorDisposition { return .Aggregate }
}
// -- Implementation details for the runtime's MirrorType implementation
@asmname("swift_MagicMirrorData_summary")
func _swift_MagicMirrorData_summaryImpl(
metadata: Any.Type, result: UnsafeMutablePointer<String>
)
public struct _MagicMirrorData {
let owner: Builtin.NativeObject
let ptr: Builtin.RawPointer
let metadata: Any.Type
var value: Any {
@asmname("swift_MagicMirrorData_value")get
}
var valueType: Any.Type {
@asmname("swift_MagicMirrorData_valueType")get
}
public var objcValue: Any {
@asmname("swift_MagicMirrorData_objcValue")get
}
public var objcValueType: Any.Type {
@asmname("swift_MagicMirrorData_objcValueType")get
}
var summary: String {
var (_, result) = _withUninitializedString {
_swift_MagicMirrorData_summaryImpl(self.metadata, $0)
}
return result
}
public func _loadValue<T>() -> T {
return Builtin.load(ptr) as T
}
}
struct _OpaqueMirror: MirrorType {
let data: _MagicMirrorData
var value: Any { return data.value }
var valueType: Any.Type { return data.valueType }
var objectIdentifier: ObjectIdentifier? { return nil }
var count: Int { return 0 }
subscript(i: Int) -> (String, MirrorType) {
_preconditionFailure("no children")
}
var summary: String { return data.summary }
var quickLookObject: QuickLookObject? { return nil }
var disposition: MirrorDisposition { return .Aggregate }
}
internal struct _TupleMirror: MirrorType {
let data: _MagicMirrorData
var value: Any { return data.value }
var valueType: Any.Type { return data.valueType }
var objectIdentifier: ObjectIdentifier? { return nil }
var count: Int {
@asmname("swift_TupleMirror_count")get
}
subscript(i: Int) -> (String, MirrorType) {
@asmname("swift_TupleMirror_subscript")get
}
var summary: String { return "(\(count) elements)" }
var quickLookObject: QuickLookObject? { return nil }
var disposition: MirrorDisposition { return .Tuple }
}
struct _StructMirror: MirrorType {
let data: _MagicMirrorData
var value: Any { return data.value }
var valueType: Any.Type { return data.valueType }
var objectIdentifier: ObjectIdentifier? { return nil }
var count: Int {
@asmname("swift_StructMirror_count")get
}
subscript(i: Int) -> (String, MirrorType) {
@asmname("swift_StructMirror_subscript")get
}
var summary: String {
return _stdlib_getDemangledTypeName(value)
}
var quickLookObject: QuickLookObject? { return nil }
var disposition: MirrorDisposition { return .Struct }
}
@asmname("swift_ClassMirror_count")
func _getClassCount(_MagicMirrorData) -> Int
@asmname("swift_ClassMirror_subscript")
func _getClassChild(Int, _MagicMirrorData) -> (String, MirrorType)
#if _runtime(_ObjC)
@asmname("swift_ClassMirror_quickLookObject")public
func _getClassQuickLookObject(data: _MagicMirrorData) -> QuickLookObject?
#endif
struct _ClassMirror: MirrorType {
let data: _MagicMirrorData
var value: Any { return data.value }
var valueType: Any.Type { return data.valueType }
var objectIdentifier: ObjectIdentifier? {
return data._loadValue() as ObjectIdentifier
}
var count: Int {
return _getClassCount(data)
}
subscript(i: Int) -> (String, MirrorType) {
return _getClassChild(i, data)
}
var summary: String {
return _stdlib_getDemangledTypeName(value)
}
var quickLookObject: QuickLookObject? {
#if _runtime(_ObjC)
return _getClassQuickLookObject(data)
#else
return .None
#endif
}
var disposition: MirrorDisposition { return .Class }
}
struct _ClassSuperMirror: MirrorType {
let data: _MagicMirrorData
var value: Any { return data.value }
var valueType: Any.Type { return data.valueType }
// Suppress the value identifier for super mirrors.
var objectIdentifier: ObjectIdentifier? {
return nil
}
var count: Int {
return _getClassCount(data)
}
subscript(i: Int) -> (String, MirrorType) {
return _getClassChild(i, data)
}
var summary: String {
return _stdlib_getDemangledTypeName(value)
}
var quickLookObject: QuickLookObject? { return nil }
var disposition: MirrorDisposition { return .Class }
}
struct _MetatypeMirror: MirrorType {
let data: _MagicMirrorData
var value: Any { return data.value }
var valueType: Any.Type { return data.valueType }
var objectIdentifier: ObjectIdentifier? {
return data._loadValue() as ObjectIdentifier
}
var count: Int {
return 0
}
subscript(i: Int) -> (String, MirrorType) {
_preconditionFailure("no children")
}
var summary: String {
return _typeName(data._loadValue() as Any.Type)
}
var quickLookObject: QuickLookObject? { return nil }
// Special disposition for types?
var disposition: MirrorDisposition { return .Aggregate }
}