Skip to content

Commit

Permalink
Merge pull request #1019 from hylo-lang/access-set-collection
Browse files Browse the repository at this point in the history
Make 'AccessEffectSet.Elements' conform to 'Collection'
  • Loading branch information
kyouko-taiga authored Sep 19, 2023
2 parents 3a2e32a + 8b52680 commit 4c9dca4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Sources/CodeGen/LLVM/Transpilation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ extension LLVM.Module {
let transpilation = declareFunction(ir.base.mangled(f), .init(from: parameters, in: &self))

configureAttributes(transpilation, transpiledFrom: f, of: m)
configureInputAttributes(transpilation.parameters.dropFirst(), transpiledFrom: f, in: m)
configureInputAttributes(transpilation.parameters.dropLast(), transpiledFrom: f, in: m)

return transpilation
}
Expand Down
45 changes: 25 additions & 20 deletions Sources/Core/AccessEffectSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public struct AccessEffectSet: OptionSet, Hashable {

/// The weakest capability in `self`, or `nil` if `self` is empty.
public var weakest: AccessEffect? {
var s = elements
return s.next()
elements.first
}

/// Returns the strongest capability in `self` including `k`.
Expand Down Expand Up @@ -74,33 +73,39 @@ public struct AccessEffectSet: OptionSet, Hashable {

extension AccessEffectSet {

/// A sequence with the elements of an access effect set.
public struct Elements: Sequence, IteratorProtocol {
/// A collection with the elements of an access effect set.
public struct Elements: Collection {

public typealias Index = UInt8

public typealias Element = AccessEffect

/// The contents of the set being iterated over.
private let base: AccessEffectSet

/// The next effect returned by the iterator.
private var position: UInt8

/// Creates an iterator over `s`.
public init(_ s: AccessEffectSet) {
self.base = s
self.position = s.rawValue & (~s.rawValue + 1)
if self.position == 0 {
self.position = 1 << 7
}
}

/// Returns the next element in the sequence.
public mutating func next() -> AccessEffect? {
if position == (1 << 7) { return nil }
defer {
repeat {
position = position << 1
} while (position != (1 << 7)) && ((position & base.rawValue) != position)
}
return .init(rawValue: position)
public var startIndex: UInt8 {
base.rawValue & (~base.rawValue + 1)
}

public var endIndex: UInt8 {
1 << 7
}

public func index(after position: UInt8) -> UInt8 {
var next = position
repeat {
next = next << 1
} while (next != endIndex) && ((next & base.rawValue) != next)
return next
}

public subscript(position: UInt8) -> AccessEffect {
AccessEffect(rawValue: position)!
}

}
Expand Down

0 comments on commit 4c9dca4

Please sign in to comment.