From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E09A4C2FD for ; Thu, 30 Mar 2023 12:03:06 +0000 (UTC) Received: by mail-wm1-f41.google.com with SMTP id r19-20020a05600c459300b003eb3e2a5e7bso11598825wmo.0 for ; Thu, 30 Mar 2023 05:03:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=metaspace-dk.20210112.gappssmtp.com; s=20210112; t=1680177784; h=mime-version:message-id:in-reply-to:date:subject:cc:to:from :user-agent:references:from:to:cc:subject:date:message-id:reply-to; bh=aDhiTImEy8g6luxO+RzLJfBSTXKM3IhPiycrVlIdxhI=; b=f+PugFily1kZ+Fvc1V52KTsyl/9YhHJAmNobFbz60ZeToSkwFYRXhsR5hevIQvsOBw iAoEJPQLTkkjAArcl0wmdXFWVXq6ea23bWTks2k3S85wPiKRphWT9J1iLIljSJBjyBdV Mw5R00uP7gIQ5+B99dgkSUKwLx3n3sq6M67DhRvWj7tfv1f9xQSGwdDQAM+9girnCzNL 2xd1ZHIMpgkJHEizNy1XG/RpzYz3rPh5RTReDA1tLmy/nk3FEOEvuC7P/+uZcQKS+iix 46ftS8llwdWlj0OEFSagJb15jC0/6ayycoqGKDF1sLeASyEd5iZi4pk5D72aAhv7LEDv 1BnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680177784; h=mime-version:message-id:in-reply-to:date:subject:cc:to:from :user-agent:references:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=aDhiTImEy8g6luxO+RzLJfBSTXKM3IhPiycrVlIdxhI=; b=rftuyXxQpOjsFZuwJiNx9jKmJXEvPE4jZnXWXciVZX/aQujvzjeOEkE5CgMEhh5xGh 3toOv0zcWFfBlNJxqxAMLDP39sOEuyRtdYeAzjbpmhLaZwRbyetfVbYlHH0LT9Fy2kOV sVbiJQ+um0pb/Xnu3Pchl17MbpDrBgxGffZrs4XM8hKGcvaF75cSSC18/ulAIE4U0fWt ZngH0TgbxuO+N2r1PmV3ZRrgSehOxtpbF1PYtWhRp62s+rE/YhQw47NrYBNOiwpnF5zV 0XZMlh2kj8aUql6/eCzwPlMxg9KemQa51cxZqKQKVjWAf3yM6MqdlSaBFpCSsnOA371E 6JMA== X-Gm-Message-State: AO0yUKUtnDu7I42yL5p9U7nv8ZX9uv1qfpVNd8CpgD054jkcDx0WOL+C F4HE8MgiMMr+Xu4Map7zvN1Cxw== X-Google-Smtp-Source: AK7set97KAJhfM8PBq4MPaXeNclKkaCf8DiLP2D62aeASwc61kH8n6I0i86fuHqdrnK+cHoD77xg7g== X-Received: by 2002:a1c:7206:0:b0:3ed:2352:eebd with SMTP id n6-20020a1c7206000000b003ed2352eebdmr18179525wmc.11.1680177784507; Thu, 30 Mar 2023 05:03:04 -0700 (PDT) Received: from localhost ([165.225.194.221]) by smtp.gmail.com with ESMTPSA id a13-20020a05600c224d00b003edef091b17sm5670855wmm.37.2023.03.30.05.03.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Mar 2023 05:03:04 -0700 (PDT) References: <20230329223239.138757-1-y86-dev@protonmail.com> <20230329223239.138757-2-y86-dev@protonmail.com> <877cuy5ur2.fsf@metaspace.dk> User-agent: mu4e 1.9.18; emacs 28.2.50 From: Andreas Hindborg To: Andreas Hindborg Cc: y86-dev@protonmail.com, Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj?= =?utf-8?Q?=C3=B6rn?= Roy Baron , Alice Ryhl , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: Re: [PATCH v3 01/13] rust: macros: add `quote!` macro Date: Thu, 30 Mar 2023 14:01:21 +0200 In-reply-to: <877cuy5ur2.fsf@metaspace.dk> Message-ID: <87355m5tvs.fsf@metaspace.dk> Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain Andreas Hindborg writes: > y86-dev@protonmail.com writes: > >> From: Gary Guo >> >> Add the `quote!` macro for creating `TokenStream`s directly via the >> given Rust tokens. It also supports repetitions using iterators. >> >> It will be used by the pin-init API proc-macros to generate code. >> >> Signed-off-by: Gary Guo >> Signed-off-by: Benno Lossin >> --- >> rust/macros/lib.rs | 2 + >> rust/macros/quote.rs | 145 +++++++++++++++++++++++++++++++++++++++++++ >> 2 files changed, 147 insertions(+) >> create mode 100644 rust/macros/quote.rs >> >> diff --git a/rust/macros/lib.rs b/rust/macros/lib.rs >> index c1d385e345b9..82b520f024dd 100644 >> --- a/rust/macros/lib.rs >> +++ b/rust/macros/lib.rs >> @@ -2,6 +2,8 @@ >> >> //! Crate for all kernel procedural macros. >> >> +#[macro_use] >> +mod quote; >> mod concat_idents; >> mod helpers; >> mod module; >> diff --git a/rust/macros/quote.rs b/rust/macros/quote.rs >> new file mode 100644 >> index 000000000000..7efb009c25a9 >> --- /dev/null >> +++ b/rust/macros/quote.rs >> @@ -0,0 +1,145 @@ >> +// SPDX-License-Identifier: Apache-2.0 OR MIT >> + >> +use proc_macro::{TokenStream, TokenTree}; >> + >> +pub(crate) trait ToTokens { >> + fn to_tokens(&self, tokens: &mut TokenStream); >> +} >> + >> +impl ToTokens for Option { >> + fn to_tokens(&self, tokens: &mut TokenStream) { >> + if let Some(v) = self { >> + v.to_tokens(tokens); >> + } >> + } >> +} >> + >> +impl ToTokens for proc_macro::Group { >> + fn to_tokens(&self, tokens: &mut TokenStream) { >> + tokens.extend([TokenTree::from(self.clone())]); >> + } >> +} >> + >> +impl ToTokens for TokenTree { >> + fn to_tokens(&self, tokens: &mut TokenStream) { >> + tokens.extend([self.clone()]); >> + } >> +} >> + >> +impl ToTokens for TokenStream { >> + fn to_tokens(&self, tokens: &mut TokenStream) { >> + tokens.extend(self.clone()); >> + } >> +} >> + >> +/// Converts tokens into [`proc_macro::TokenStream`] and performs variable interpolations with >> +/// the given span. >> +/// >> +/// This is a similar to the >> +/// [`quote_spanned!`](https://docs.rs/quote/latest/quote/macro.quote_spanned.html) macro from the >> +/// `quote` crate but provides only just enough functionality needed by the current `macros` crate. >> +#[allow(unused_macros)] >> +macro_rules! quote_spanned { >> + ($span:expr => $($tt:tt)*) => { >> + #[allow(clippy::vec_init_then_push)] >> + { >> + let mut tokens = Vec::new(); >> + let span = $span; >> + quote_spanned!(@proc tokens span $($tt)*); >> + proc_macro::TokenStream::from_iter(tokens) > > Should this be `::alloc::vec::Vec` and > `::proc_macro::TokenStream::from_iter` ? > > Best regards, > Andreas > >> + }}; >> + (@proc $v:ident $span:ident) => {}; >> + (@proc $v:ident $span:ident #$id:ident $($tt:tt)*) => { >> + let mut ts = proc_macro::TokenStream::new(); >> + crate::quote::ToTokens::to_tokens(&$id, &mut ts); >> + $v.extend(ts.into_iter()); >> + quote_spanned!(@proc $v $span $($tt)*); >> + }; >> + (@proc $v:ident $span:ident #(#$id:ident)* $($tt:tt)*) => { >> + for token in $id { >> + let mut ts = proc_macro::TokenStream::new(); >> + crate::quote::ToTokens::to_tokens(&token, &mut ts); >> + $v.extend(ts.into_iter()); Also, could this be without `into_iter()`: `$v.extend(ts)`? BR Andreas >> + } >> + quote_spanned!(@proc $v $span $($tt)*); >> + }; >> + (@proc $v:ident $span:ident ( $($inner:tt)* ) $($tt:tt)*) => { >> + let mut tokens = Vec::new(); >> + quote_spanned!(@proc tokens $span $($inner)*); >> + $v.push(proc_macro::TokenTree::Group(proc_macro::Group::new( >> + proc_macro::Delimiter::Parenthesis, >> + proc_macro::TokenStream::from_iter(tokens) >> + ))); >> + quote_spanned!(@proc $v $span $($tt)*); >> + }; >> + (@proc $v:ident $span:ident [ $($inner:tt)* ] $($tt:tt)*) => { >> + let mut tokens = Vec::new(); >> + quote_spanned!(@proc tokens $span $($inner)*); >> + $v.push(proc_macro::TokenTree::Group(proc_macro::Group::new( >> + proc_macro::Delimiter::Bracket, >> + proc_macro::TokenStream::from_iter(tokens) >> + ))); >> + quote_spanned!(@proc $v $span $($tt)*); >> + }; >> + (@proc $v:ident $span:ident { $($inner:tt)* } $($tt:tt)*) => { >> + let mut tokens = Vec::new(); >> + quote_spanned!(@proc tokens $span $($inner)*); >> + $v.push(proc_macro::TokenTree::Group(proc_macro::Group::new( >> + proc_macro::Delimiter::Brace, >> + proc_macro::TokenStream::from_iter(tokens) >> + ))); >> + quote_spanned!(@proc $v $span $($tt)*); >> + }; >> + (@proc $v:ident $span:ident :: $($tt:tt)*) => { >> + $v.push( >> + proc_macro::TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Joint)) >> + ); >> + $v.push( >> + proc_macro::TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Alone)) >> + ); >> + quote_spanned!(@proc $v $span $($tt)*); >> + }; >> + (@proc $v:ident $span:ident : $($tt:tt)*) => { >> + $v.push( >> + proc_macro::TokenTree::Punct(proc_macro::Punct::new(':', proc_macro::Spacing::Alone)) >> + ); >> + quote_spanned!(@proc $v $span $($tt)*); >> + }; >> + (@proc $v:ident $span:ident , $($tt:tt)*) => { >> + $v.push( >> + proc_macro::TokenTree::Punct(proc_macro::Punct::new(',', proc_macro::Spacing::Alone)) >> + ); >> + quote_spanned!(@proc $v $span $($tt)*); >> + }; >> + (@proc $v:ident $span:ident @ $($tt:tt)*) => { >> + $v.push( >> + proc_macro::TokenTree::Punct(proc_macro::Punct::new('@', proc_macro::Spacing::Alone)) >> + ); >> + quote_spanned!(@proc $v $span $($tt)*); >> + }; >> + (@proc $v:ident $span:ident ! $($tt:tt)*) => { >> + $v.push( >> + proc_macro::TokenTree::Punct(proc_macro::Punct::new('!', proc_macro::Spacing::Alone)) >> + ); >> + quote_spanned!(@proc $v $span $($tt)*); >> + }; >> + (@proc $v:ident $span:ident $id:ident $($tt:tt)*) => { >> + $v.push(proc_macro::TokenTree::Ident(proc_macro::Ident::new(stringify!($id), $span))); >> + quote_spanned!(@proc $v $span $($tt)*); >> + }; >> +} >> + >> +/// Converts tokens into [`proc_macro::TokenStream`] and performs variable interpolations with >> +/// mixed site span ([`Span::mixed_site()`]). >> +/// >> +/// This is a similar to the [`quote!`](https://docs.rs/quote/latest/quote/macro.quote.html) macro >> +/// from the `quote` crate but provides only just enough functionality needed by the current >> +/// `macros` crate. >> +/// >> +/// [`Span::mixed_site()`]: https://doc.rust-lang.org/proc_macro/struct.Span.html#method.mixed_site >> +#[allow(unused_macros)] >> +macro_rules! quote { >> + ($($tt:tt)*) => { >> + quote_spanned!(proc_macro::Span::mixed_site() => $($tt)*) >> + } >> +}