forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBridgeObjectiveC.swift
497 lines (441 loc) · 16.2 KB
/
BridgeObjectiveC.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
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#if _runtime(_ObjC)
/// A Swift Array or Dictionary of types conforming to
/// `_ObjectiveCBridgeable` can be passed to Objective-C as an NSArray or
/// NSDictionary, respectively. The elements of the resulting NSArray
/// or NSDictionary will be the result of calling `_bridgeToObjectiveC`
/// on each elmeent of the source container.
public protocol _ObjectiveCBridgeable {
typealias _ObjectiveCType : AnyObject
/// Return true iff instances of `Self` can be converted to
/// Objective-C. Even if this method returns `true`, A given
/// instance of `Self._ObjectiveCType` may, or may not, convert
/// successfully to `Self`; for example, an `NSArray` will only
/// convert successfully to `[String]` if it contains only
/// `NSString`s.
@warn_unused_result
static func _isBridgedToObjectiveC() -> Bool
// _getObjectiveCType is a workaround: right now protocol witness
// tables don't include associated types, so we can not find
// '_ObjectiveCType.self' from them.
/// Must return `_ObjectiveCType.self`.
@warn_unused_result
static func _getObjectiveCType() -> Any.Type
/// Convert `self` to Objective-C.
@warn_unused_result
func _bridgeToObjectiveC() -> _ObjectiveCType
/// Bridge from an Objective-C object of the bridged class type to a
/// value of the Self type.
///
/// This bridging operation is used for forced downcasting (e.g.,
/// via as), and may defer complete checking until later. For
/// example, when bridging from `NSArray` to `Array<Element>`, we can defer
/// the checking for the individual elements of the array.
///
/// - parameter result: The location where the result is written. The optional
/// will always contain a value.
static func _forceBridgeFromObjectiveC(
source: _ObjectiveCType,
inout result: Self?
)
/// Try to bridge from an Objective-C object of the bridged class
/// type to a value of the Self type.
///
/// This conditional bridging operation is used for conditional
/// downcasting (e.g., via as?) and therefore must perform a
/// complete conversion to the value type; it cannot defer checking
/// to a later time.
///
/// - parameter result: The location where the result is written.
///
/// - Returns: `true` if bridging succeeded, `false` otherwise. This redundant
/// information is provided for the convenience of the runtime's `dynamic_cast`
/// implementation, so that it need not look into the optional representation
/// to determine success.
static func _conditionallyBridgeFromObjectiveC(
source: _ObjectiveCType,
inout result: Self?
) -> Bool
}
//===--- Bridging for metatypes -------------------------------------------===//
/// A stand-in for a value of metatype type.
///
/// The language and runtime do not yet support protocol conformances for
/// structural types like metatypes. However, we can use a struct that contains
/// a metatype, make it conform to to _ObjectiveCBridgeable, and its witness table
/// will be ABI-compatible with one that directly provided conformance to the
/// metatype type itself.
public struct _BridgeableMetatype: _ObjectiveCBridgeable {
internal var value: AnyObject.Type
public typealias _ObjectiveCType = AnyObject
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
public static func _getObjectiveCType() -> Any.Type {
return AnyObject.self
}
public func _bridgeToObjectiveC() -> AnyObject {
return value
}
public static func _forceBridgeFromObjectiveC(
source: AnyObject,
inout result: _BridgeableMetatype?
) {
result = _BridgeableMetatype(value: source as! AnyObject.Type)
}
public static func _conditionallyBridgeFromObjectiveC(
source: AnyObject,
inout result: _BridgeableMetatype?
) -> Bool {
if let type = source as? AnyObject.Type {
result = _BridgeableMetatype(value: type)
return true
}
result = nil
return false
}
}
//===--- Bridging facilities written in Objective-C -----------------------===//
// Functions that must discover and possibly use an arbitrary type's
// conformance to a given protocol. See ../runtime/Metadata.cpp for
// implementations.
//===----------------------------------------------------------------------===//
/// Attempt to convert `x` to its Objective-C representation.
///
/// - If `T` is a class type, it is alaways bridged verbatim, the function
/// returns `x`;
///
/// - otherwise, `T` conforms to `_ObjectiveCBridgeable`:
/// + if `T._isBridgedToObjectiveC()` returns `false`, then the
/// result is empty;
/// + otherwise, returns the result of `x._bridgeToObjectiveC()`;
///
/// - otherwise, the result is empty.
@warn_unused_result
public func _bridgeToObjectiveC<T>(x: T) -> AnyObject? {
if _fastPath(_isClassOrObjCExistential(T.self)) {
return unsafeBitCast(x, AnyObject.self)
}
return _bridgeNonVerbatimToObjectiveC(x)
}
@warn_unused_result
public func _bridgeToObjectiveCUnconditional<T>(x: T) -> AnyObject {
let optResult: AnyObject? = _bridgeToObjectiveC(x)
_precondition(optResult != nil,
"value failed to bridge from Swift type to a Objective-C type")
return optResult!
}
/// Same as `_bridgeToObjectiveCUnconditional`, but autoreleases the
/// return value if `T` is bridged non-verbatim.
@warn_unused_result
func _bridgeToObjectiveCUnconditionalAutorelease<T>(x: T) -> AnyObject
{
if _fastPath(_isClassOrObjCExistential(T.self)) {
return unsafeBitCast(x, AnyObject.self)
}
guard let bridged = _bridgeNonVerbatimToObjectiveC(x) else {
_preconditionFailure(
"Dictionary key failed to bridge from Swift type to a Objective-C type")
}
_autorelease(bridged)
return bridged
}
@warn_unused_result
@_silgen_name("swift_bridgeNonVerbatimToObjectiveC")
func _bridgeNonVerbatimToObjectiveC<T>(x: T) -> AnyObject?
/// Convert `x` from its Objective-C representation to its Swift
/// representation.
///
/// - If `T` is a class type:
/// - if the dynamic type of `x` is `T` or a subclass of it, it is bridged
/// verbatim, the function returns `x`;
/// - otherwise, if `T` conforms to `_ObjectiveCBridgeable`:
/// + if the dynamic type of `x` is not `T._getObjectiveCType()`
/// or a subclass of it, trap;
/// + otherwise, returns the result of `T._forceBridgeFromObjectiveC(x)`;
/// - otherwise, trap.
@warn_unused_result
public func _forceBridgeFromObjectiveC<T>(x: AnyObject, _: T.Type) -> T {
if _fastPath(_isClassOrObjCExistential(T.self)) {
return x as! T
}
var result: T?
_bridgeNonVerbatimFromObjectiveC(x, T.self, &result)
return result!
}
/// Convert `x` from its Objective-C representation to its Swift
/// representation.
@warn_unused_result
@_silgen_name("_forceBridgeFromObjectiveC_bridgeable")
public func _forceBridgeFromObjectiveC_bridgeable<T:_ObjectiveCBridgeable>(x: T._ObjectiveCType, _: T.Type) -> T {
var result: T?
T._forceBridgeFromObjectiveC(x, result: &result)
return result!
}
/// Attempt to convert `x` from its Objective-C representation to its Swift
/// representation.
///
/// - If `T` is a class type:
/// - if the dynamic type of `x` is `T` or a subclass of it, it is bridged
/// verbatim, the function returns `x`;
/// - otherwise, if `T` conforms to `_ObjectiveCBridgeable`:
/// + if `T._isBridgedToObjectiveC()` returns `false`, then the result is
/// empty;
/// + otherwise, if the dynamic type of `x` is not `T._getObjectiveCType()`
/// or a subclass of it, the result is empty;
/// + otherwise, returns the result of
/// `T._conditionallyBridgeFromObjectiveC(x)`;
/// - otherwise, the result is empty.
@warn_unused_result
public func _conditionallyBridgeFromObjectiveC<T>(
x: AnyObject,
_: T.Type
) -> T? {
if _fastPath(_isClassOrObjCExistential(T.self)) {
return x as? T
}
var result: T?
_bridgeNonVerbatimFromObjectiveCConditional(x, T.self, &result)
return result
}
/// Attempt to convert `x` from its Objective-C representation to its Swift
/// representation.
@warn_unused_result
@_silgen_name("_conditionallyBridgeFromObjectiveC_bridgeable")
public func _conditionallyBridgeFromObjectiveC_bridgeable<T:_ObjectiveCBridgeable>(
x: T._ObjectiveCType,
_: T.Type
) -> T? {
var result: T?
T._conditionallyBridgeFromObjectiveC (x, result: &result)
return result
}
@_silgen_name("swift_bridgeNonVerbatimFromObjectiveC")
func _bridgeNonVerbatimFromObjectiveC<T>(
x: AnyObject,
_ nativeType: T.Type,
inout _ result: T?
)
/// Runtime optional to conditionall perform a bridge from an object to a value
/// type.
///
/// - parameter result: Will be set to the resulting value if bridging succeeds, and
/// unchanged otherwise.
///
/// - Returns: `true` to indicate success, `false` to indicate failure.
@_silgen_name("swift_bridgeNonVerbatimFromObjectiveCConditional")
func _bridgeNonVerbatimFromObjectiveCConditional<T>(
x: AnyObject,
_ nativeType: T.Type,
inout _ result: T?
) -> Bool
/// Determines if values of a given type can be converted to an Objective-C
/// representation.
///
/// - If `T` is a class type, returns `true`;
/// - otherwise, if `T` conforms to `_ObjectiveCBridgeable`, returns
/// `T._isBridgedToObjectiveC()`.
@warn_unused_result
public func _isBridgedToObjectiveC<T>(_: T.Type) -> Bool {
if _fastPath(_isClassOrObjCExistential(T.self)) {
return true
}
return _isBridgedNonVerbatimToObjectiveC(T.self)
}
@warn_unused_result
@_silgen_name("swift_isBridgedNonVerbatimToObjectiveC")
func _isBridgedNonVerbatimToObjectiveC<T>(_: T.Type) -> Bool
/// A type that's bridged "verbatim" does not conform to
/// `_ObjectiveCBridgeable`, and can have its bits reinterpreted as an
/// `AnyObject`. When this function returns true, the storage of an
/// `Array<T>` can be `unsafeBitCast` as an array of `AnyObject`.
@warn_unused_result
public func _isBridgedVerbatimToObjectiveC<T>(_: T.Type) -> Bool {
return _isClassOrObjCExistential(T.self)
}
/// Retrieve the Objective-C type to which the given type is bridged.
@warn_unused_result
public func _getBridgedObjectiveCType<T>(_: T.Type) -> Any.Type? {
if _fastPath(_isClassOrObjCExistential(T.self)) {
return T.self
}
return _getBridgedNonVerbatimObjectiveCType(T.self)
}
@warn_unused_result
@_silgen_name("swift_getBridgedNonVerbatimObjectiveCType")
func _getBridgedNonVerbatimObjectiveCType<T>(_: T.Type) -> Any.Type?
// -- Pointer argument bridging
@_transparent
internal var _nilNativeObject: AnyObject? {
return nil
}
/// A mutable pointer-to-ObjC-pointer argument.
///
/// This type has implicit conversions to allow passing any of the following
/// to a C or ObjC API:
///
/// - `nil`, which gets passed as a null pointer,
/// - an inout argument of the referenced type, which gets passed as a pointer
/// to a writeback temporary with autoreleasing ownership semantics,
/// - an `UnsafeMutablePointer<Memory>`, which is passed as-is.
///
/// Passing pointers to mutable arrays of ObjC class pointers is not
/// directly supported. Unlike `UnsafeMutablePointer<Memory>`,
/// `AutoreleasingUnsafeMutablePointer<Memory>` must reference storage that
/// does not own a reference count to the referenced
/// value. UnsafeMutablePointer's operations, by contrast, assume that
/// the referenced storage owns values loaded from or stored to it.
///
/// This type does not carry an owner pointer unlike the other C*Pointer types
/// because it only needs to reference the results of inout conversions, which
/// already have writeback-scoped lifetime.
public struct AutoreleasingUnsafeMutablePointer<Memory /* TODO : class */>
: Equatable, NilLiteralConvertible, _PointerType {
@available(*, unavailable, renamed="Memory")
public typealias T = Memory
public let _rawValue: Builtin.RawPointer
@_transparent
public // COMPILER_INTRINSIC
init(_ _rawValue: Builtin.RawPointer) {
self._rawValue = _rawValue
}
@_transparent
var _isNull : Bool {
return UnsafeMutablePointer<Memory>(self)._isNull
}
/// Access the underlying raw memory, getting and
/// setting values.
public var memory: Memory {
/// Retrieve the value the pointer points to.
@_transparent get {
_debugPrecondition(!_isNull)
// We can do a strong load normally.
return UnsafeMutablePointer<Memory>(self).memory
}
/// Set the value the pointer points to, copying over the previous value.
///
/// AutoreleasingUnsafeMutablePointers are assumed to reference a
/// value with __autoreleasing ownership semantics, like 'NSFoo**'
/// in ARC. This autoreleases the argument before trivially
/// storing it to the referenced memory.
@_transparent nonmutating set {
_debugPrecondition(!_isNull)
// Autorelease the object reference.
typealias OptionalAnyObject = AnyObject?
Builtin.retain(unsafeBitCast(newValue, OptionalAnyObject.self))
Builtin.autorelease(unsafeBitCast(newValue, OptionalAnyObject.self))
// Trivially assign it as a COpaquePointer; the pointer references an
// autoreleasing slot, so retains/releases of the original value are
// unneeded.
let p = UnsafeMutablePointer<COpaquePointer>(
UnsafeMutablePointer<Memory>(self))
p.memory = unsafeBitCast(newValue, COpaquePointer.self)
}
}
/// Access the `i`th element of the raw array pointed to by
/// `self`.
///
/// - Requires: `self != nil`.
public subscript(i: Int) -> Memory {
@_transparent
get {
_debugPrecondition(!_isNull)
// We can do a strong load normally.
return (UnsafePointer<Memory>(self) + i).memory
}
}
/// Create an instance initialized with `nil`.
@_transparent public
init(nilLiteral: ()) {
_rawValue = _nilRawPointer
}
/// Initialize to a null pointer.
@_transparent public
init() {
self._rawValue = _nilRawPointer
}
/// Explicit construction from an UnsafeMutablePointer.
///
/// This is inherently unsafe; UnsafeMutablePointer assumes the
/// referenced memory has +1 strong ownership semantics, whereas
/// AutoreleasingUnsafeMutablePointer implies +0 semantics.
@_transparent public
init<U>(_ ptr: UnsafeMutablePointer<U>) {
self._rawValue = ptr._rawValue
}
/// Explicit construction from a UnsafePointer.
///
/// This is inherently unsafe because UnsafePointers do not imply
/// mutability.
@_transparent
init<U>(_ ptr: UnsafePointer<U>) {
self._rawValue = ptr._rawValue
}
}
extension AutoreleasingUnsafeMutablePointer : CustomDebugStringConvertible {
/// A textual representation of `self`, suitable for debugging.
public var debugDescription: String {
return _rawPointerToString(_rawValue)
}
}
@_transparent
@warn_unused_result
public func == <Memory> (
lhs: AutoreleasingUnsafeMutablePointer<Memory>,
rhs: AutoreleasingUnsafeMutablePointer<Memory>
) -> Bool {
return Bool(Builtin.cmp_eq_RawPointer(lhs._rawValue, rhs._rawValue))
}
internal struct _CocoaFastEnumerationStackBuf {
// Clang uses 16 pointers. So do we.
var item0: Builtin.RawPointer
var item1: Builtin.RawPointer
var item2: Builtin.RawPointer
var item3: Builtin.RawPointer
var item4: Builtin.RawPointer
var item5: Builtin.RawPointer
var item6: Builtin.RawPointer
var item7: Builtin.RawPointer
var item8: Builtin.RawPointer
var item9: Builtin.RawPointer
var item10: Builtin.RawPointer
var item11: Builtin.RawPointer
var item12: Builtin.RawPointer
var item13: Builtin.RawPointer
var item14: Builtin.RawPointer
var item15: Builtin.RawPointer
@_transparent
var length: Int {
return 16
}
init() {
item0 = _nilRawPointer
item1 = item0
item2 = item0
item3 = item0
item4 = item0
item5 = item0
item6 = item0
item7 = item0
item8 = item0
item9 = item0
item10 = item0
item11 = item0
item12 = item0
item13 = item0
item14 = item0
item15 = item0
_sanityCheck(sizeofValue(self) >= sizeof(Builtin.RawPointer.self) * length)
}
}
#endif