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
|
// RUN: %target-swift-frontend %s -emit-sil \
// RUN: -enable-experimental-feature NonescapableTypes
// REQUIRES: asserts
// REQUIRES: swift_in_compiler
// TODO: Use real Range
public struct FakeRange<Bound> {
public let lowerBound: Bound
public let upperBound: Bound
}
// TODO: Use real Optional
public enum FakeOptional<T> {
case none
case some(T)
}
public struct BufferViewIndex<Element> {
let _rawValue: UnsafeRawPointer
internal init(rawValue: UnsafeRawPointer) {
_rawValue = rawValue
}
var isAligned: Bool {
(Int(bitPattern: _rawValue) & (MemoryLayout<Element>.alignment-1)) == 0
}
}
extension BufferViewIndex: Equatable {}
extension BufferViewIndex: Hashable {}
extension BufferViewIndex: Strideable {
public typealias Stride = Int
public func distance(to other: BufferViewIndex) -> Int {
let bytes = _rawValue.distance(to: other._rawValue)
let (q, r) = bytes.quotientAndRemainder(dividingBy: MemoryLayout<Element>.stride)
precondition(r == 0)
return q
}
public func advanced(by n: Int) -> BufferViewIndex {
.init(rawValue: _rawValue.advanced(by: n &* MemoryLayout<Element>.stride))
}
}
extension BufferViewIndex: Comparable {
public static func <(lhs: BufferViewIndex, rhs: BufferViewIndex) -> Bool {
lhs._rawValue < rhs._rawValue
}
}
public struct BufferView<Element> : ~Escapable {
let start: BufferViewIndex<Element>
public let count: Int
private var baseAddress: UnsafeRawPointer { start._rawValue }
// TODO: Enable diagnostics once this initializer's store to temporary is handled
// CHECK: sil @$s31lifetime_dependence_scope_fixup10BufferViewV11baseAddress5count9dependsOnACyxGSVYls_Siqd__htclufC : $@convention(method) <Element><Owner> (UnsafeRawPointer, Int, @in_guaranteed Owner, @thin BufferView<Element>.Type) -> _scope(1) @owned BufferView<Element> {
public init<Owner: ~Copyable & ~Escapable>(
baseAddress: UnsafeRawPointer,
count: Int,
dependsOn owner: borrowing Owner
) {
self.init(
start: .init(rawValue: baseAddress), count: count, dependsOn: owner
)
}
// CHECK: sil hidden @$s31lifetime_dependence_scope_fixup10BufferViewV5start5count9dependsOnACyxGAA0eF5IndexVyxGYls_Siqd__htclufC : $@convention(method) <Element><Owner> (BufferViewIndex<Element>, Int, @in_guaranteed Owner, @thin BufferView<Element>.Type) -> _scope(1) @owned BufferView<Element> {
init<Owner: ~Copyable & ~Escapable>(
start index: BufferViewIndex<Element>,
count: Int,
dependsOn owner: borrowing Owner
) {
precondition(count >= 0, "Count must not be negative")
if !_isPOD(Element.self) {
precondition(
index.isAligned,
"baseAddress must be properly aligned for \(Element.self)"
)
}
self.start = index
self.count = count
}
@_unsafeNonescapableResult
init(start index: BufferViewIndex<Element>,
count: Int) {
precondition(count >= 0, "Count must not be negative")
if !_isPOD(Element.self) {
precondition(
index.isAligned,
"baseAddress must be properly aligned for \(Element.self)"
)
}
self.start = index
self.count = count
}
}
// TODO: extend Collection, BidirectionalCollection, RandomAccessCollection {
extension BufferView {
public typealias Index = BufferViewIndex<Element>
public typealias SubSequence = Self
public var startIndex: Index { start }
public var endIndex: Index { start.advanced(by: count) }
@inlinable @inline(__always)
public func distance(from start: Index, to end: Index) -> Int {
start.distance(to: end)
}
public subscript(position: Index) -> Element {
get {
if _isPOD(Element.self) {
return position._rawValue.loadUnaligned(as: Element.self)
}
else {
return position._rawValue.load(as: Element.self)
}
}
}
// CHECK: sil @$s31lifetime_dependence_scope_fixup10BufferViewVyACyxGAA9FakeRangeVyAA0eF5IndexVyxGGcig : $@convention(method) <Element> (FakeRange<BufferViewIndex<Element>>, @guaranteed BufferView<Element>) -> _scope(0) @owned BufferView<Element> {
public subscript(bounds: FakeRange<BufferViewIndex<Element>>) -> Self {
get {
BufferView(
start: bounds.lowerBound,
count: bounds.upperBound.distance(to:bounds.lowerBound) / MemoryLayout<Element>.stride
)
}
}
borrowing public func prefix(upTo index: BufferViewIndex<Element>) -> dependsOn(self) Self {
index == startIndex
? Self(start: start, count: 0, dependsOn: copy self)
: prefix(through: index.advanced(by: -1))
}
borrowing public func prefix(through index: Index) -> dependsOn(self) Self {
let nc = distance(from: startIndex, to: index) &+ 1
return Self(start: start, count: nc, dependsOn: copy self)
}
consuming public func prefix(_ maxLength: Int) -> dependsOn(self) Self {
precondition(maxLength >= 0, "Can't have a prefix of negative length.")
let nc = maxLength < count ? maxLength : count
return Self(start: start, count: nc, dependsOn: self)
}
}
extension ContiguousArray {
public var view: BufferView<Element> {
borrowing _read {
yield BufferView(
baseAddress: _baseAddressIfContiguous!, count: count, dependsOn: self
)
}
}
}
public func array_view_element(a: ContiguousArray<Int> , i: BufferViewIndex<Int>) -> Int {
a.view[i]
}
public func array_view_slice_element(a: ContiguousArray<Int> , sliceIdx: FakeRange<BufferViewIndex<Int>>, Idx: BufferViewIndex<Int>) -> Int {
a.view[sliceIdx][Idx]
}
|