Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions Sources/MMIO/MMIOMacros.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,20 @@ public macro RegisterBlock(offset: Int, stride: Int, count: Int) =
public macro Register(bitWidth: Int) =
#externalMacro(module: "MMIOMacros", type: "RegisterMacro")

// Note: Since the 'Reserved' macro shares an implementation with the other
// bitfield macros, it can also handle the `as:` parameter found on their
// external macro declarations. However, this parameter will never be used by
// expansion for reserved bitfields, so it is omitted to avoid programmer use.
@attached(accessor)
public macro Reserved<Range>(bits: Range...) =
public macro Reserved<Range, Value>(
bits: Range..., as: Value.Type = Never.self
) =
#externalMacro(module: "MMIOMacros", type: "ReservedMacro")
where Range: RangeExpression, Range.Bound: BinaryInteger
where
Range: RangeExpression, Range.Bound: BinaryInteger, Value: BitFieldProjectable

@attached(accessor)
public macro Reserved(bits: UnboundedRange) =
public macro Reserved<Value>(
bits: UnboundedRange, as: Value.Type = Never.self
) =
#externalMacro(module: "MMIOMacros", type: "ReservedMacro")
where Value: BitFieldProjectable

@attached(accessor)
public macro ReadWrite<Range, Value>(
Expand Down
3 changes: 3 additions & 0 deletions Sources/MMIOMacros/Macros/BitFieldMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public struct ReservedMacro: BitFieldMacro {
var bitRanges: [BitRange]
var bitRangeExpressions: [ExprSyntax] { self.$bitRanges }

@Argument(label: "as")
var projectedType: BitFieldTypeProjection?

mutating func update(
Expand All @@ -82,6 +83,8 @@ public struct ReservedMacro: BitFieldMacro {
switch label {
case "bits":
try self._bitRanges.update(from: expression, in: context)
case "as":
try self._projectedType.update(from: expression, in: context)
default:
fatalError()
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/MMIOMacrosTests/Macros/RegisterMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ struct RegisterMacroTests {
column: 3,
highlights: ["var v1: Int"],
fixIts: [
.init(message: "Insert '@Reserved(bits:)' macro"),
.init(message: "Insert '@Reserved(bits:as:)' macro"),
.init(message: "Insert '@ReadWrite(bits:as:)' macro"),
.init(message: "Insert '@ReadOnly(bits:as:)' macro"),
.init(message: "Insert '@WriteOnly(bits:as:)' macro"),
Expand All @@ -119,7 +119,7 @@ struct RegisterMacroTests {
column: 3,
highlights: ["@OtherAttribute var v2: Int"],
fixIts: [
.init(message: "Insert '@Reserved(bits:)' macro"),
.init(message: "Insert '@Reserved(bits:as:)' macro"),
.init(message: "Insert '@ReadWrite(bits:as:)' macro"),
.init(message: "Insert '@ReadOnly(bits:as:)' macro"),
.init(message: "Insert '@WriteOnly(bits:as:)' macro"),
Expand All @@ -132,7 +132,7 @@ struct RegisterMacroTests {
column: 3,
highlights: ["var v3: Int { willSet {} }"],
fixIts: [
.init(message: "Insert '@Reserved(bits:)' macro"),
.init(message: "Insert '@Reserved(bits:as:)' macro"),
.init(message: "Insert '@ReadWrite(bits:as:)' macro"),
.init(message: "Insert '@ReadOnly(bits:as:)' macro"),
.init(message: "Insert '@WriteOnly(bits:as:)' macro"),
Expand Down
1 change: 1 addition & 0 deletions Tests/MMIOTests/Assertions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//
//===----------------------------------------------------------------------===//

import MMIOUtilities
import Testing

@testable import MMIO
Expand Down
Loading