forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReflection.swift
223 lines (188 loc) · 5.94 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
// RUN: rm -rf %t && mkdir %t
// RUN: %target-build-swift -parse-stdlib %s -module-name Reflection -o %t/a.out
// RUN: %S/timeout.sh 360 %target-run %t/a.out | FileCheck %s
// REQUIRES: executable_test
// FIXME: timeout wrapper is necessary because the ASan test runs for hours
//
// DO NOT add more tests to this file. Add them to test/1_stdlib/Runtime.swift.
//
import Swift
// A more interesting struct type.
struct Complex<T> {
let real, imag: T
}
// CHECK-LABEL: Complex:
print("Complex:")
// CHECK-NEXT: Reflection.Complex<Swift.Double>
// CHECK-NEXT: real: 1.5
// CHECK-NEXT: imag: 0.75
dump(Complex<Double>(real: 1.5, imag: 0.75))
// CHECK-NEXT: Reflection.Complex<Swift.Double>
// CHECK-NEXT: real: -1.5
// CHECK-NEXT: imag: -0.75
dump(Complex<Double>(real: -1.5, imag: -0.75))
// CHECK-NEXT: Reflection.Complex<Swift.Int>
// CHECK-NEXT: real: 22
// CHECK-NEXT: imag: 44
dump(Complex<Int>(real: 22, imag: 44))
// CHECK-NEXT: Reflection.Complex<Swift.String>
// CHECK-NEXT: real: is this the real life?
// CHECK-NEXT: imag: is it just fantasy?
dump(Complex<String>(real: "is this the real life?",
imag: "is it just fantasy?"))
// Test destructuring of a pure Swift class hierarchy.
class Good {
let x: UInt = 11
let y: String = "222"
}
class Better : Good {
let z: Double = 333.5
}
class Best : Better {
let w: String = "4444"
}
// CHECK-LABEL: Root class:
// CHECK-NEXT: Reflection.Good #0
// CHECK-NEXT: x: 11
// CHECK-NEXT: y: 222
print("Root class:")
dump(Good())
// CHECK-LABEL: Subclass:
// CHECK-NEXT: Reflection.Best #0
// CHECK-NEXT: super: Reflection.Better
// CHECK-NEXT: super: Reflection.Good
// CHECK-NEXT: x: 11
// CHECK-NEXT: y: 222
// CHECK-NEXT: z: 333.5
// CHECK-NEXT: w: 4444
print("Subclass:")
dump(Best())
// Test protocol types, which reflect as their dynamic types.
// CHECK-LABEL: Any int:
// CHECK-NEXT: 1
print("Any int:")
var any: Any = 1
dump(any)
// CHECK-LABEL: Any class:
// CHECK-NEXT: Reflection.Best #0
// CHECK-NEXT: super: Reflection.Better
// CHECK-NEXT: super: Reflection.Good
// CHECK-NEXT: x: 11
// CHECK-NEXT: y: 222
// CHECK-NEXT: z: 333.5
// CHECK-NEXT: w: 4444
print("Any class:")
any = Best()
dump(any)
// CHECK-LABEL: second verse
// CHECK-NEXT: Reflection.Best #0
print("second verse same as the first:")
dump(any)
// CHECK-LABEL: Any double:
// CHECK-NEXT: 2.5
print("Any double:")
any = 2.5
dump(any)
// CHECK-LABEL: Character:
// CHECK-NEXT: a
print("Character:")
print(_reflect(Character("a")).summary)
let range = 3...9
// CHECK-NEXT: 3..<10
print(_reflect(range).summary)
// CHECK-NEXT: startIndex=3
print("startIndex=\(_reflect(range)[0].1.summary)")
protocol Fooable {}
extension Int : Fooable {}
extension Double : Fooable {}
// CHECK-LABEL: Fooable int:
// CHECK-NEXT: 1
print("Fooable int:")
var fooable: Fooable = 1
dump(fooable)
// CHECK-LABEL: Fooable double:
// CHECK-NEXT: 2.5
print("Fooable double:")
fooable = 2.5
dump(fooable)
protocol Barrable : class {}
extension Best: Barrable {}
// CHECK-LABEL: Barrable class:
// CHECK-NEXT: Reflection.Best #0
// CHECK-NEXT: super: Reflection.Better
// CHECK-NEXT: super: Reflection.Good
// CHECK-NEXT: x: 11
// CHECK-NEXT: y: 222
// CHECK-NEXT: z: 333.5
// CHECK-NEXT: w: 4444
print("Barrable class:")
var barrable: Barrable = Best()
dump(barrable)
// CHECK-LABEL: second verse
// CHECK-NEXT: Reflection.Best #0
print("second verse same as the first:")
dump(barrable)
// With _Reflectable protocols we extract the witness table from the container.
// CHECK-LABEL: _Reflectable int:
// CHECK-NEXT: 1
print("_Reflectable int:")
var reflectable: _Reflectable = 1
dump(reflectable)
// CHECK-NEXT: Logical: true
switch _reflect(true).quickLookObject {
case .None: print("no quicklook")
case .Some(let ql):
switch ql {
case .Logical(let x): print("Logical: \(x)")
default: print("wrong quicklook type")
}
}
// CHECK-NEXT: Hello world
print( _reflect(Optional<String>("Hello world")).summary )
// CHECK-NEXT: nil
print( _reflect(Optional<String>()).summary )
let intArray = [1,2,3,4,5]
let intArrayMirror = _reflect(intArray)
// CHECK-NEXT: 5 elements
print(intArrayMirror.summary)
// CHECK-NEXT: [0]: 1
print("\(intArrayMirror[0].0): \(intArrayMirror[0].1.summary)")
// CHECK-NEXT: [4]: 5
print("\(intArrayMirror[4].0): \(intArrayMirror[4].1.summary)")
var justSomeFunction = { (x:Int)->Int in return x + 1 }
// CHECK-NEXT: (Function)
print(_reflect(justSomeFunction).summary)
// CHECK-NEXT: Swift.String
print(_reflect(String.self).summary)
// CHECK-NEXT: CollectionOfOne(Howdy Swift!)
// CHECK-NEXT: - element: Howdy Swift!
dump(CollectionOfOne("Howdy Swift!"))
// CHECK-NEXT: EmptyCollection
var emptyCollectionOfInt: EmptyCollection<Int> = EmptyCollection()
print(_reflect(emptyCollectionOfInt).summary)
// CHECK-NEXT: .One
print(_reflect(Bit.One).summary)
// CHECK-NEXT: ▿
// CHECK-NEXT: from: 1.0
// CHECK-NEXT: through: 12.15
// CHECK-NEXT: by: 3.14
dump(1.0.stride(through: 12.15, by: 3.14))
// CHECK-NEXT: UnsafeMutablePointer(nil)
var nilUnsafeMutablePointerString: UnsafeMutablePointer<String> = nil
print(_reflect(nilUnsafeMutablePointerString).summary)
// CHECK-NEXT: UnsafeMutablePointer(0x123456)
var randomUnsafeMutablePointerString = UnsafeMutablePointer<String>(
bitPattern: 0x123456)
print(_reflect(randomUnsafeMutablePointerString).summary)
// CHECK-NEXT: Hello panda
var sanePointerString = UnsafeMutablePointer<String>.alloc(1)
sanePointerString.initialize("Hello panda")
print(_reflect(sanePointerString.memory).summary)
sanePointerString.destroy()
sanePointerString.dealloc(1)
// Don't crash on types with opaque metadata. rdar://problem/19791252
var rawPointer = unsafeBitCast(0 as Int, Builtin.RawPointer.self)
dump(rawPointer)
// CHECK: - (Opaque Value)
// CHECK-LABEL: and now our song is done
print("and now our song is done")