Skip to content

Commit 8299674

Browse files
committed
[embedded] Simplify AnyObject/Builtin.NativeObject split in arrays by using a typealias
1 parent 9f9e906 commit 8299674

File tree

8 files changed

+47
-140
lines changed

8 files changed

+47
-140
lines changed

stdlib/public/core/Array.swift

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -476,17 +476,11 @@ extension Array: _ArrayProtocol {
476476
return _getCapacity()
477477
}
478478

479-
/// An object that guarantees the lifetime of this array's elements.
480479
#if $Embedded
481-
public // @testable
482-
var _owner: Builtin.NativeObject? {
483-
@inlinable // FIXME(inline-always)
484-
@inline(__always)
485-
get {
486-
return _buffer.owner
487-
}
488-
}
489-
#else
480+
public typealias AnyObject = Builtin.NativeObject
481+
#endif
482+
483+
/// An object that guarantees the lifetime of this array's elements.
490484
@inlinable
491485
public // @testable
492486
var _owner: AnyObject? {
@@ -496,7 +490,6 @@ extension Array: _ArrayProtocol {
496490
return _buffer.owner
497491
}
498492
}
499-
#endif
500493

501494
/// If the elements are stored contiguously, a pointer to the first
502495
/// element. Otherwise, `nil`.
@@ -1492,7 +1485,6 @@ extension Array: CustomStringConvertible, CustomDebugStringConvertible {
14921485
}
14931486

14941487
extension Array {
1495-
#if !$Embedded
14961488
@usableFromInline @_transparent
14971489
@_unavailableInEmbedded
14981490
internal func _cPointerArgs() -> (AnyObject?, UnsafeRawPointer?) {
@@ -1503,18 +1495,6 @@ extension Array {
15031495
let n = ContiguousArray(self._buffer)._buffer
15041496
return (n.owner, UnsafeRawPointer(n.firstElementAddress))
15051497
}
1506-
#else
1507-
@usableFromInline @_transparent
1508-
@_unavailableInEmbedded
1509-
internal func _cPointerArgs() -> (Builtin.NativeObject?, UnsafeRawPointer?) {
1510-
let p = _baseAddressIfContiguous
1511-
if _fastPath(p != nil || isEmpty) {
1512-
return (_owner, UnsafeRawPointer(p))
1513-
}
1514-
let n = ContiguousArray(self._buffer)._buffer
1515-
return (n.owner, UnsafeRawPointer(n.firstElementAddress))
1516-
}
1517-
#endif
15181498
}
15191499

15201500
extension Array {

stdlib/public/core/ArrayBufferProtocol.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ where Indices == Range<Int> {
9797
/// The number of elements the buffer can store without reallocation.
9898
var capacity: Int { get }
9999

100-
/// An object that keeps the elements stored in this buffer alive.
101100
#if $Embedded
102-
var owner: Builtin.NativeObject { get }
103-
#else
104-
var owner: AnyObject { get }
101+
typealias AnyObject = Builtin.NativeObject
105102
#endif
106103

104+
/// An object that keeps the elements stored in this buffer alive.
105+
var owner: AnyObject { get }
106+
107107
/// A pointer to the first element.
108108
///
109109
/// - Precondition: The elements are known to be stored contiguously.

stdlib/public/core/ArraySlice.swift

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,18 +1152,6 @@ extension ArraySlice: CustomStringConvertible, CustomDebugStringConvertible {
11521152
}
11531153

11541154
extension ArraySlice {
1155-
#if $Embedded
1156-
@usableFromInline @_transparent
1157-
@_unavailableInEmbedded
1158-
internal func _cPointerArgs() -> (Builtin.NativeObject?, UnsafeRawPointer?) {
1159-
let p = _baseAddressIfContiguous
1160-
if _fastPath(p != nil || isEmpty) {
1161-
return (_owner, UnsafeRawPointer(p))
1162-
}
1163-
let n = ContiguousArray(self._buffer)._buffer
1164-
return (n.owner, UnsafeRawPointer(n.firstElementAddress))
1165-
}
1166-
#else
11671155
@usableFromInline @_transparent
11681156
internal func _cPointerArgs() -> (AnyObject?, UnsafeRawPointer?) {
11691157
let p = _baseAddressIfContiguous
@@ -1173,7 +1161,6 @@ extension ArraySlice {
11731161
let n = ContiguousArray(self._buffer)._buffer
11741162
return (n.owner, UnsafeRawPointer(n.firstElementAddress))
11751163
}
1176-
#endif
11771164
}
11781165

11791166
extension ArraySlice {

stdlib/public/core/ArrayType.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ where Indices == Range<Int> {
1717
/// The number of elements the Array can store without reallocation.
1818
var capacity: Int { get }
1919

20+
#if $Embedded
21+
typealias AnyObject = Builtin.NativeObject
22+
#endif
23+
2024
/// An object that guarantees the lifetime of this array's elements.
21-
#if !$Embedded
2225
var _owner: AnyObject? { get }
23-
#else
24-
var _owner: Builtin.NativeObject? { get }
25-
#endif
2626

2727
/// If the elements are stored contiguously, a pointer to the first
2828
/// element. Otherwise, `nil`.

stdlib/public/core/ContiguousArray.swift

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -147,20 +147,16 @@ extension ContiguousArray: _ArrayProtocol {
147147
return _getCapacity()
148148
}
149149

150+
#if $Embedded
151+
public typealias AnyObject = Builtin.NativeObject
152+
#endif
153+
150154
/// An object that guarantees the lifetime of this array's elements.
151-
#if !$Embedded
152155
@inlinable
153156
public // @testable
154157
var _owner: AnyObject? {
155158
return _buffer.owner
156159
}
157-
#else
158-
@inlinable
159-
public // @testable
160-
var _owner: Builtin.NativeObject? {
161-
return _buffer.owner
162-
}
163-
#endif
164160

165161
/// If the elements are stored contiguously, a pointer to the first
166162
/// element. Otherwise, `nil`.
@@ -1060,18 +1056,6 @@ extension ContiguousArray: CustomStringConvertible, CustomDebugStringConvertible
10601056
}
10611057

10621058
extension ContiguousArray {
1063-
#if $Embedded
1064-
@usableFromInline @_transparent
1065-
@_unavailableInEmbedded
1066-
internal func _cPointerArgs() -> (Builtin.NativeObject?, UnsafeRawPointer?) {
1067-
let p = _baseAddressIfContiguous
1068-
if _fastPath(p != nil || isEmpty) {
1069-
return (_owner, UnsafeRawPointer(p))
1070-
}
1071-
let n = ContiguousArray(self._buffer)._buffer
1072-
return (n.owner, UnsafeRawPointer(n.firstElementAddress))
1073-
}
1074-
#else
10751059
@usableFromInline @_transparent
10761060
internal func _cPointerArgs() -> (AnyObject?, UnsafeRawPointer?) {
10771061
let p = _baseAddressIfContiguous
@@ -1081,7 +1065,6 @@ extension ContiguousArray {
10811065
let n = ContiguousArray(self._buffer)._buffer
10821066
return (n.owner, UnsafeRawPointer(n.firstElementAddress))
10831067
}
1084-
#endif
10851068
}
10861069

10871070
extension ContiguousArray {

stdlib/public/core/ContiguousArrayBuffer.swift

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -844,31 +844,25 @@ internal struct _ContiguousArrayBuffer<Element>: _ArrayBufferProtocol {
844844
}
845845
#endif
846846

847-
#if !$Embedded
848-
/// An object that keeps the elements stored in this buffer alive.
849-
@inlinable
850-
internal var owner: AnyObject {
851-
return _storage
852-
}
847+
#if $Embedded
848+
public typealias AnyObject = Builtin.NativeObject
849+
#endif
853850

854851
/// An object that keeps the elements stored in this buffer alive.
855852
@inlinable
856-
internal var nativeOwner: AnyObject {
853+
internal var owner: AnyObject {
854+
#if !$Embedded
857855
return _storage
858-
}
859-
#else
860-
/// An object that keeps the elements stored in this buffer alive.
861-
@inlinable
862-
internal var owner: Builtin.NativeObject {
856+
#else
863857
return Builtin.castToNativeObject(_storage)
858+
#endif
864859
}
865860

866861
/// An object that keeps the elements stored in this buffer alive.
867862
@inlinable
868-
internal var nativeOwner: Builtin.NativeObject {
869-
return Builtin.castToNativeObject(_storage)
863+
internal var nativeOwner: AnyObject {
864+
return owner
870865
}
871-
#endif
872866

873867
/// A value that identifies the storage used by the buffer.
874868
///

stdlib/public/core/Pointer.swift

Lines changed: 22 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -415,33 +415,13 @@ func _convertInOutToPointerArgument<
415415
return ToPointer(from)
416416
}
417417

418+
#if !$Embedded
419+
418420
/// Derive a pointer argument from a value array parameter.
419421
///
420422
/// This always produces a non-null pointer, even if the array doesn't have any
421423
/// storage.
422-
#if $Embedded
423424
@_transparent
424-
@_unavailableInEmbedded
425-
public // COMPILER_INTRINSIC
426-
func _convertConstArrayToPointerArgument<
427-
FromElement,
428-
ToPointer: _Pointer
429-
>(_ arr: [FromElement]) -> (Builtin.NativeObject?, ToPointer) {
430-
let (owner, opaquePointer) = arr._cPointerArgs()
431-
432-
let validPointer: ToPointer
433-
if let addr = opaquePointer {
434-
validPointer = ToPointer(addr._rawValue)
435-
} else {
436-
let lastAlignedValue = ~(MemoryLayout<FromElement>.alignment - 1)
437-
let lastAlignedPointer = UnsafeRawPointer(bitPattern: lastAlignedValue)!
438-
validPointer = ToPointer(lastAlignedPointer._rawValue)
439-
}
440-
return (owner, validPointer)
441-
}
442-
#else
443-
@_transparent
444-
@_unavailableInEmbedded
445425
public // COMPILER_INTRINSIC
446426
func _convertConstArrayToPointerArgument<
447427
FromElement,
@@ -459,31 +439,11 @@ func _convertConstArrayToPointerArgument<
459439
}
460440
return (owner, validPointer)
461441
}
462-
#endif
463442

464443
/// Derive a pointer argument from an inout array parameter.
465444
///
466445
/// This always produces a non-null pointer, even if the array's length is 0.
467-
#if $Embedded
468-
@_transparent
469-
@_unavailableInEmbedded
470-
public // COMPILER_INTRINSIC
471-
func _convertMutableArrayToPointerArgument<
472-
FromElement,
473-
ToPointer: _Pointer
474-
>(_ a: inout [FromElement]) -> (Builtin.NativeObject?, ToPointer) {
475-
// TODO: Putting a canary at the end of the array in checked builds might
476-
// be a good idea
477-
478-
// Call reserve to force contiguous storage.
479-
a.reserveCapacity(0)
480-
_debugPrecondition(a._baseAddressIfContiguous != nil || a.isEmpty)
481-
482-
return _convertConstArrayToPointerArgument(a)
483-
}
484-
#else
485446
@_transparent
486-
@_unavailableInEmbedded
487447
public // COMPILER_INTRINSIC
488448
func _convertMutableArrayToPointerArgument<
489449
FromElement,
@@ -498,27 +458,35 @@ func _convertMutableArrayToPointerArgument<
498458

499459
return _convertConstArrayToPointerArgument(a)
500460
}
501-
#endif
502461

503462
/// Derive a UTF-8 pointer argument from a value string parameter.
504-
#if $Embedded
505463
@_transparent
506-
@_unavailableInEmbedded
507464
public // COMPILER_INTRINSIC
508465
func _convertConstStringToUTF8PointerArgument<
509466
ToPointer: _Pointer
510-
>(_ str: String) -> (Builtin.NativeObject?, ToPointer) {
467+
>(_ str: String) -> (AnyObject?, ToPointer) {
511468
let utf8 = Array(str.utf8CString)
512469
return _convertConstArrayToPointerArgument(utf8)
513470
}
471+
514472
#else
515-
@_transparent
516-
@_unavailableInEmbedded
517-
public // COMPILER_INTRINSIC
518-
func _convertConstStringToUTF8PointerArgument<
519-
ToPointer: _Pointer
520-
>(_ str: String) -> (AnyObject?, ToPointer) {
521-
let utf8 = Array(str.utf8CString)
522-
return _convertConstArrayToPointerArgument(utf8)
473+
474+
@_unavailableInEmbedded public
475+
func _convertConstArrayToPointerArgument<FromElement,ToPointer: _Pointer>(
476+
_ arr: [FromElement]) -> (Builtin.NativeObject?, ToPointer) {
477+
fatalError("unreachable in embedded Swift (marked as unavailable)")
478+
}
479+
480+
@_unavailableInEmbedded public
481+
func _convertMutableArrayToPointerArgument<FromElement, ToPointer: _Pointer>(
482+
_ a: inout [FromElement]) -> (Builtin.NativeObject?, ToPointer) {
483+
fatalError("unreachable in embedded Swift (marked as unavailable)")
523484
}
485+
486+
@_unavailableInEmbedded public
487+
func _convertConstStringToUTF8PointerArgument<ToPointer: _Pointer>(
488+
_ str: String) -> (Builtin.NativeObject?, ToPointer) {
489+
fatalError("unreachable in embedded Swift (marked as unavailable)")
490+
}
491+
524492
#endif

stdlib/public/core/SliceBuffer.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,9 @@ internal struct _SliceBuffer<Element>
2626
@usableFromInline
2727
internal typealias NativeBuffer = _ContiguousArrayBuffer<Element>
2828

29-
#if $Embedded
3029
/// An object that keeps the elements stored in this buffer alive.
3130
@usableFromInline
32-
internal var owner: Builtin.NativeObject
33-
#else
34-
@usableFromInline
3531
internal var owner: AnyObject
36-
#endif
3732

3833
@usableFromInline
3934
internal let subscriptBaseAddress: UnsafeMutablePointer<Element>

0 commit comments

Comments
 (0)