File: lifetime_dependence_span_lifetime_attr.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (179 lines) | stat: -rw-r--r-- 6,098 bytes parent folder | download
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
// RUN: %target-swift-frontend %s -emit-sil \
// RUN:   -enable-experimental-feature LifetimeDependence | %FileCheck %s

// REQUIRES: swift_in_compiler
// REQUIRES: swift_feature_LifetimeDependence

// 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 SpanIndex<Element> {
  let _rawValue: UnsafeRawPointer

  internal init(rawValue: UnsafeRawPointer) {
    _rawValue = rawValue
  }

  var isAligned: Bool {
    (Int(bitPattern: _rawValue) & (MemoryLayout<Element>.alignment-1)) == 0
  }
}

extension SpanIndex: Equatable {}

extension SpanIndex: Hashable {}

extension SpanIndex: Strideable {
  public typealias Stride = Int

  public func distance(to other: SpanIndex) -> 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) -> SpanIndex {
    .init(rawValue: _rawValue.advanced(by: n &* MemoryLayout<Element>.stride))
  }
}

extension SpanIndex: Comparable {
  public static func <(lhs: SpanIndex, rhs: SpanIndex) -> Bool {
    lhs._rawValue < rhs._rawValue
  }
}

public struct Span<Element> : ~Escapable {
  let start: SpanIndex<Element>
  public let count: Int
  private var baseAddress: UnsafeRawPointer { start._rawValue }
// CHECK-LABEL: sil @$s025lifetime_dependence_span_A5_attr4SpanV11baseAddress5count9dependsOnACyxGSV_Siqd__htcRi_d__Ri0_d__lufC : $@convention(method) <Element><Owner where Owner : ~Copyable, Owner : ~Escapable> (UnsafeRawPointer, Int, @in_guaranteed Owner, @thin Span<Element>.Type) -> @lifetime(copy 2) @owned Span<Element> {
  @lifetime(owner)
  public init<Owner: ~Copyable & ~Escapable>(
      baseAddress: UnsafeRawPointer,
      count: Int,
      dependsOn owner: borrowing Owner
    ) {
      self.init(
        start: .init(rawValue: baseAddress), count: count, dependsOn: owner
      )
  }
// CHECK-LABEL: sil hidden @$s025lifetime_dependence_span_A5_attr4SpanV5start5count9dependsOnACyxGAA0E5IndexVyxG_Siqd__htcRi_d__Ri0_d__lufC : $@convention(method) <Element><Owner where Owner : ~Copyable, Owner : ~Escapable> (SpanIndex<Element>, Int, @in_guaranteed Owner, @thin Span<Element>.Type) -> @lifetime(copy 2) @owned Span<Element> {
  @lifetime(owner)
  init<Owner: ~Copyable & ~Escapable>(
    start index: SpanIndex<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: SpanIndex<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
  }
}

extension Span {
  public typealias Index = SpanIndex<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-LABEL: sil @$s025lifetime_dependence_span_A5_attr4SpanVyACyxGAA9FakeRangeVyAA0E5IndexVyxGGcig : $@convention(method) <Element> (FakeRange<SpanIndex<Element>>, @guaranteed Span<Element>) -> @lifetime(copy 1) @owned Span<Element> {
  public subscript(bounds: FakeRange<SpanIndex<Element>>) -> Self {
  @lifetime(self)
    get {
      Span(
        start: bounds.lowerBound,
        count: bounds.upperBound.distance(to:bounds.lowerBound) / MemoryLayout<Element>.stride
      )
    }
  }

// CHECK-LABEL: sil @$s025lifetime_dependence_span_A5_attr4SpanV6prefix4upToACyxGAA0E5IndexVyxG_tF : $@convention(method) <Element> (SpanIndex<Element>, @guaranteed Span<Element>) -> @lifetime(copy 1) @owned Span<Element> {
  @lifetime(self)
  borrowing public func prefix(upTo index: SpanIndex<Element>) -> Self {
    index == startIndex
    ? Self(start: start, count: 0, dependsOn: copy self)
    : prefix(through: index.advanced(by: -1))
  }

// CHECK-LABEL: sil @$s025lifetime_dependence_span_A5_attr4SpanV6prefix7throughACyxGAA0E5IndexVyxG_tF : $@convention(method) <Element> (SpanIndex<Element>, @guaranteed Span<Element>) -> @lifetime(copy 1) @owned Span<Element> {
  @lifetime(self)
  borrowing public func prefix(through index: Index) -> Self {
    let nc = distance(from: startIndex, to: index) &+ 1
    return Self(start: start, count: nc, dependsOn: copy self)
  }

// CHECK-LABEL: sil @$s025lifetime_dependence_span_A5_attr4SpanV6prefixyACyxGSiF : $@convention(method) <Element> (Int, @owned Span<Element>) -> @lifetime(copy 1) @owned Span<Element> {
  @lifetime(self)
  consuming public func prefix(_ maxLength: Int) -> 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: Span<Element> {
    @lifetime(borrow self)
    borrowing _read {
      yield Span(
        baseAddress: _baseAddressIfContiguous!, count: count, dependsOn: self
      )
    }
  }
}

public func array_view_element(a: ContiguousArray<Int> , i: SpanIndex<Int>) -> Int {
  a.view[i]
}

public func array_view_slice_element(a: ContiguousArray<Int> , sliceIdx: FakeRange<SpanIndex<Int>>, Idx: SpanIndex<Int>) -> Int {
  a.view[sliceIdx][Idx]
}