Skip to content

Commit 80ee583

Browse files
committed
wip: internalize more
1 parent daf88d7 commit 80ee583

File tree

4 files changed

+35
-30
lines changed

4 files changed

+35
-30
lines changed

stdlib/public/SDK/Foundation/Foundation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ extension Array : _ObjectiveCBridgeable {
414414

415415
@_semantics("convertToObjectiveC")
416416
public func _bridgeToObjectiveC() -> NSArray {
417-
return unsafeBitCast(self._buffer._asCocoaArray(), to: NSArray.self)
417+
return unsafeBitCast(self._bridgeToObjectiveCImpl(), to: NSArray.self)
418418
}
419419

420420
public static func _forceBridgeFromObjectiveC(

stdlib/public/core/ArrayBuffer.swift

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ internal struct _ArrayBuffer<Element> : _ArrayBufferProtocol {
4646

4747
/// The spare bits that are set when a native array needs deferred
4848
/// element type checking.
49-
var deferredTypeCheckMask: Int { return 1 }
49+
internal var deferredTypeCheckMask: Int { return 1 }
5050

5151
/// Returns an `_ArrayBuffer<U>` containing the same elements,
5252
/// deferring checking each element's `U`-ness until it is accessed.
@@ -68,7 +68,7 @@ internal struct _ArrayBuffer<Element> : _ArrayBufferProtocol {
6868
native: _native._storage, bits: deferredTypeCheckMask))
6969
}
7070

71-
var needsElementTypeCheck: Bool {
71+
internal var needsElementTypeCheck: Bool {
7272
// NSArray's need an element typecheck when the element type isn't AnyObject
7373
return !_isNativeTypeChecked && !(AnyObject.self is Element.Type)
7474
}
@@ -89,12 +89,12 @@ extension _ArrayBuffer {
8989
}
9090

9191
/// `true`, if the array is native and does not need a deferred type check.
92-
var arrayPropertyIsNativeTypeChecked: Bool {
92+
internal var arrayPropertyIsNativeTypeChecked: Bool {
9393
return _isNativeTypeChecked
9494
}
9595

9696
/// Returns `true` iff this buffer's storage is uniquely-referenced.
97-
mutating func isUniquelyReferenced() -> Bool {
97+
internal mutating func isUniquelyReferenced() -> Bool {
9898
if !_isClassOrObjCExistential(Element.self) {
9999
return _storage.isUniquelyReferenced_native_noSpareBits()
100100
}
@@ -103,7 +103,7 @@ extension _ArrayBuffer {
103103

104104
/// Returns `true` iff this buffer's storage is either
105105
/// uniquely-referenced or pinned.
106-
mutating func isUniquelyReferencedOrPinned() -> Bool {
106+
internal mutating func isUniquelyReferencedOrPinned() -> Bool {
107107
if !_isClassOrObjCExistential(Element.self) {
108108
return _storage.isUniquelyReferencedOrPinned_native_noSpareBits()
109109
}
@@ -114,7 +114,7 @@ extension _ArrayBuffer {
114114
///
115115
/// - Precondition: `_isBridgedToObjectiveC(Element.self)`.
116116
/// O(1) if the element type is bridged verbatim, O(N) otherwise.
117-
public func _asCocoaArray() -> _NSArrayCore {
117+
internal func _asCocoaArray() -> _NSArrayCore {
118118
_sanityCheck(
119119
_isBridgedToObjectiveC(Element.self),
120120
"Array element type is not bridged to Objective-C")
@@ -175,7 +175,7 @@ extension _ArrayBuffer {
175175
}
176176
}
177177

178-
func _typeCheck(_ subRange: Range<Int>) {
178+
internal func _typeCheck(_ subRange: Range<Int>) {
179179
if !_isClassOrObjCExistential(Element.self) {
180180
return
181181
}
@@ -194,7 +194,7 @@ extension _ArrayBuffer {
194194
/// memory starting at `target`. Return a pointer "past the end" of the
195195
/// just-initialized memory.
196196
@discardableResult
197-
public func _copyContents(
197+
internal func _copyContents(
198198
subRange bounds: Range<Int>,
199199
initializing target: UnsafeMutablePointer<Element>
200200
) -> UnsafeMutablePointer<Element> {
@@ -274,17 +274,17 @@ extension _ArrayBuffer {
274274
/// A pointer to the first element.
275275
///
276276
/// - Precondition: The elements are known to be stored contiguously.
277-
public var firstElementAddress: UnsafeMutablePointer<Element> {
277+
internal var firstElementAddress: UnsafeMutablePointer<Element> {
278278
_sanityCheck(_isNative, "must be a native buffer")
279279
return _native.firstElementAddress
280280
}
281281

282-
public var firstElementAddressIfContiguous: UnsafeMutablePointer<Element>? {
282+
internal var firstElementAddressIfContiguous: UnsafeMutablePointer<Element>? {
283283
return _fastPath(_isNative) ? firstElementAddress : nil
284284
}
285285

286286
/// The number of elements the buffer stores.
287-
public var count: Int {
287+
internal var count: Int {
288288
@inline(__always)
289289
get {
290290
return _fastPath(_isNative) ? _native.count : _nonNative.count
@@ -332,13 +332,13 @@ extension _ArrayBuffer {
332332
}
333333

334334
/// The number of elements the buffer can store without reallocation.
335-
public var capacity: Int {
335+
internal var capacity: Int {
336336
return _fastPath(_isNative) ? _native.capacity : _nonNative.count
337337
}
338338

339339
@_versioned
340340
@inline(__always)
341-
func getElement(_ i: Int, wasNativeTypeChecked: Bool) -> Element {
341+
internal func getElement(_ i: Int, wasNativeTypeChecked: Bool) -> Element {
342342
if _fastPath(wasNativeTypeChecked) {
343343
return _nativeTypeChecked[i]
344344
}
@@ -347,7 +347,7 @@ extension _ArrayBuffer {
347347

348348
@_versioned
349349
@inline(never)
350-
func _getElementSlowPath(_ i: Int) -> AnyObject {
350+
internal func _getElementSlowPath(_ i: Int) -> AnyObject {
351351
_sanityCheck(
352352
_isClassOrObjCExistential(Element.self),
353353
"Only single reference elements can be indexed here.")
@@ -373,7 +373,7 @@ extension _ArrayBuffer {
373373
}
374374

375375
/// Get or set the value of the ith element.
376-
public subscript(i: Int) -> Element {
376+
internal subscript(i: Int) -> Element {
377377
get {
378378
return getElement(i, wasNativeTypeChecked: _isNativeTypeChecked)
379379
}
@@ -395,7 +395,7 @@ extension _ArrayBuffer {
395395
/// Call `body(p)`, where `p` is an `UnsafeBufferPointer` over the
396396
/// underlying contiguous storage. If no such storage exists, it is
397397
/// created on-demand.
398-
public func withUnsafeBufferPointer<R>(
398+
internal func withUnsafeBufferPointer<R>(
399399
_ body: @noescape (UnsafeBufferPointer<Element>) throws -> R
400400
) rethrows -> R {
401401
if _fastPath(_isNative) {
@@ -410,7 +410,7 @@ extension _ArrayBuffer {
410410
/// over the underlying contiguous storage.
411411
///
412412
/// - Precondition: Such contiguous storage exists or the buffer is empty.
413-
public mutating func withUnsafeMutableBufferPointer<R>(
413+
internal mutating func withUnsafeMutableBufferPointer<R>(
414414
_ body: @noescape (UnsafeMutableBufferPointer<Element>) throws -> R
415415
) rethrows -> R {
416416
_sanityCheck(
@@ -423,22 +423,22 @@ extension _ArrayBuffer {
423423
}
424424

425425
/// An object that keeps the elements stored in this buffer alive.
426-
public var owner: AnyObject {
426+
internal var owner: AnyObject {
427427
return _fastPath(_isNative) ? _native._storage : _nonNative
428428
}
429429

430430
/// An object that keeps the elements stored in this buffer alive.
431431
///
432432
/// - Precondition: This buffer is backed by a `_ContiguousArrayBuffer`.
433-
public var nativeOwner: AnyObject {
433+
internal var nativeOwner: AnyObject {
434434
_sanityCheck(_isNative, "Expect a native array")
435435
return _native._storage
436436
}
437437

438438
/// A value that identifies the storage used by the buffer. Two
439439
/// buffers address the same elements when they have the same
440440
/// identity and count.
441-
public var identity: UnsafePointer<Void> {
441+
internal var identity: UnsafePointer<Void> {
442442
if _isNative {
443443
return _native.identity
444444
}
@@ -451,7 +451,7 @@ extension _ArrayBuffer {
451451
/// The position of the first element in a non-empty collection.
452452
///
453453
/// In an empty collection, `startIndex == endIndex`.
454-
public var startIndex: Int {
454+
internal var startIndex: Int {
455455
return 0
456456
}
457457

@@ -460,18 +460,18 @@ extension _ArrayBuffer {
460460
/// `endIndex` is not a valid argument to `subscript`, and is always
461461
/// reachable from `startIndex` by zero or more applications of
462462
/// `index(after:)`.
463-
public var endIndex: Int {
463+
internal var endIndex: Int {
464464
return count
465465
}
466466

467-
public typealias Indices = CountableRange<Int>
467+
internal typealias Indices = CountableRange<Int>
468468

469469
//===--- private --------------------------------------------------------===//
470470
internal typealias Storage = _ContiguousArrayStorage<Element>
471471
internal typealias NativeBuffer = _ContiguousArrayBuffer<Element>
472472

473473
@_versioned
474-
var _isNative: Bool {
474+
internal var _isNative: Bool {
475475
if !_isClassOrObjCExistential(Element.self) {
476476
return true
477477
} else {
@@ -480,7 +480,7 @@ extension _ArrayBuffer {
480480
}
481481

482482
/// `true`, if the array is native and does not need a deferred type check.
483-
var _isNativeTypeChecked: Bool {
483+
internal var _isNativeTypeChecked: Bool {
484484
if !_isClassOrObjCExistential(Element.self) {
485485
return true
486486
} else {
@@ -492,7 +492,7 @@ extension _ArrayBuffer {
492492
///
493493
/// - Precondition: `_isNative`.
494494
@_versioned
495-
var _native: NativeBuffer {
495+
internal var _native: NativeBuffer {
496496
return NativeBuffer(
497497
_isClassOrObjCExistential(Element.self)
498498
? _storage.nativeInstance : _storage.nativeInstance_noSpareBits)
@@ -502,12 +502,12 @@ extension _ArrayBuffer {
502502
///
503503
/// - Precondition: `_isNativeTypeChecked`.
504504
@_versioned
505-
var _nativeTypeChecked: NativeBuffer {
505+
internal var _nativeTypeChecked: NativeBuffer {
506506
return NativeBuffer(_storage.nativeInstance_noSpareBits)
507507
}
508508

509509
@_versioned
510-
var _nonNative: _NSArrayCore {
510+
internal var _nonNative: _NSArrayCore {
511511
@inline(__always)
512512
get {
513513
_sanityCheck(_isClassOrObjCExistential(Element.self))

stdlib/public/core/Arrays.swift.gyb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,6 +2096,11 @@ public func != <Element : Equatable>(
20962096

20972097
#if _runtime(_ObjC)
20982098
extension Array {
2099+
public // @SPI(Foundation)
2100+
func _bridgeToObjectiveCImpl() -> AnyObject {
2101+
return _buffer._asCocoaArray()
2102+
}
2103+
20992104
/// Tries to downcast the source `NSArray` as our native buffer type.
21002105
/// If it succeeds, creates a new `Array` around it and returns that.
21012106
/// Returns `nil` otherwise.

test/Interpreter/SDK/objc_fast_enumeration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ for x: AnyObject in s_m {
9191
// CHECK: 2
9292
// CHECK: 1
9393
var a2 = [3, 2, 1]
94-
var nsa2 = (a2._buffer._asCocoaArray() as AnyObject) as! NSArray
94+
var nsa2: NSArray = a2._bridgeToObjectiveC()
9595
for x: AnyObject in nsa2 {
9696
print(x.description!)
9797
}

0 commit comments

Comments
 (0)