Rust for Linux List
 help / color / mirror / Atom feed
From: Boming Kong <michaelkongboming@gmail.com>
To: rust-for-linux@vger.kernel.org
Cc: "Boming Kong" <michaelkongboming@gmail.com>,
	"Alexandre Courbot" <acourbot@nvidia.com>,
	"Yury Norov" <yury.norov@gmail.com>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Boqun Feng" <boqun@kernel.org>, "Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Benno Lossin" <lossin@kernel.org>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Daniel Almeida" <daniel.almeida@collabora.com>,
	"Tamir Duberstein" <tamird@kernel.org>,
	"Onur Özkan" <work@onurozkan.dev>,
	driver-core@lists.linux.dev
Subject: [RFC PATCH 2/2] rust: io: register: implemented overlap flag inside register!
Date: Mon, 30 Nov 2026 03:13:58 -0500	[thread overview]
Message-ID: <20261130081359.158313-3-michaelkongboming@gmail.com> (raw)
In-Reply-To: <20261130081359.158313-1-michaelkongboming@gmail.com>

Added overlap flag inside register parser so it can generate bitfields!
with overlap=false to trigger bitfields check. register! default use
overlap=false but user can still change it back if overlapping is
intended.

Signed-off-by: Boming Kong <michaelkongboming@gmail.com>
---
 rust/macros/io/register.rs | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)

diff --git a/rust/macros/io/register.rs b/rust/macros/io/register.rs
index 420c3ad052..4422095787 100644
--- a/rust/macros/io/register.rs
+++ b/rust/macros/io/register.rs
@@ -23,6 +23,7 @@
     Error,
     Expr,
     Ident,
+    LitBool,
     Path,
     Result,
     Token,
@@ -32,6 +33,7 @@
 
 mod kw {
     syn::custom_keyword!(base);
+    syn::custom_keyword!(overlap);
     syn::custom_keyword!(stride);
 }
 
@@ -67,7 +69,7 @@ struct Reg {
     ty: Type,
     array: Option<RegArrayDef>,
     offset: RegOffset,
-    bitfield: Option<(Type, Group)>,
+    bitfield: Option<(Type, Group, bool)>,
 }
 
 impl Parse for Reg {
@@ -77,7 +79,7 @@ fn parse(input: syn::parse::ParseStream<'_>) -> Result<Self> {
         let name = input.parse()?;
 
         let lh = input.lookahead1();
-        let (unique, ty, bitfield_storage) = if lh.peek(Token![:]) {
+        let (unique, ty, bitfield_storage, overlap) = if lh.peek(Token![:]) {
             let _: Token![:] = input.parse()?;
 
             let mut attrs = input.call(Attribute::parse_outer)?;
@@ -89,14 +91,22 @@ fn parse(input: syn::parse::ParseStream<'_>) -> Result<Self> {
                 Err(Error::new_spanned(&attrs[0], "unexpected attributes"))?
             }
 
-            (unique, input.parse()?, None)
+            (unique, input.parse()?, None, false)
         } else if lh.peek(token::Paren) {
             let content;
             parenthesized!(content in input);
             let bitfield_storage = Some(content.parse()?);
+            let is_overlap = if content.peek(Token![,]) {
+                let _: Token![,] = content.parse()?;
+                let _: kw::overlap = content.parse()?;
+                let _: Token![=] = content.parse()?;
+                content.parse::<LitBool>()?.value
+            } else {
+                false
+            };
 
             // For bitfields, bitfield macro will generate a type with the same name as `name`.
-            (true, parse_quote!(#name), bitfield_storage)
+            (true, parse_quote!(#name), bitfield_storage, is_overlap)
         } else {
             Err(lh.error())?
         };
@@ -151,7 +161,7 @@ fn parse(input: syn::parse::ParseStream<'_>) -> Result<Self> {
             } else {
                 Err(lh.error())?
             };
-            Some((storage, args))
+            Some((storage, args, overlap))
         } else {
             let _: Token![;] = input.parse()?;
             None
@@ -230,13 +240,13 @@ pub(crate) fn register(def: RegDef) -> Result<TokenStream> {
             }
         };
 
-        if let Some((storage, args)) = &bitfield {
+        if let Some((storage, args, is_overlap)) = &bitfield {
             outputs.extend(quote_spanned!(span =>
                 ::kernel::bitfield!(
                     // `#[allow(non_camel_case_types)]` is added since register names typically use
                     // `SCREAMING_CASE`.
                     #[allow(non_camel_case_types)]
-                    #(#attrs)* #vis struct #name(#storage) #args
+                    #(#attrs)* #vis struct #name(#storage, overlap=#is_overlap) #args
                 );
             ));
         }
-- 
2.56.1


  parent reply	other threads:[~2026-11-30  8:14 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  8:13 [RFC PATCH 0/2] rust: Add compile-time overlap checking for bitfield! and register! Boming Kong
2026-11-30  8:13 ` [RFC PATCH 1/2] rust: bitfield: added optional overlap argument for bitfield! Boming Kong
2026-11-30  8:13 ` Boming Kong [this message]
2026-11-30 10:34 ` [RFC PATCH 0/2] rust: Add compile-time overlap checking for bitfield! and register! Miguel Ojeda
2026-11-30 17:31   ` Boming Kong
2026-12-02  6:23   ` Alexandre Courbot
2026-12-02  7:33     ` Boming Kong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261130081359.158313-3-michaelkongboming@gmail.com \
    --to=michaelkongboming@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=acourbot@nvidia.com \
    --cc=aliceryhl@google.com \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun@kernel.org \
    --cc=dakr@kernel.org \
    --cc=daniel.almeida@collabora.com \
    --cc=driver-core@lists.linux.dev \
    --cc=gary@garyguo.net \
    --cc=lossin@kernel.org \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tamird@kernel.org \
    --cc=tmgross@umich.edu \
    --cc=work@onurozkan.dev \
    --cc=yury.norov@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox