Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1280489rwl; Wed, 29 Mar 2023 15:35:25 -0700 (PDT) X-Google-Smtp-Source: AK7set9B9AZ+/3EqCRjRE6+GsGIqum6Pzk/3dFBLyJ1tBVJlJk6VPJJ+iehrkTPA90gZgzVvfdcT X-Received: by 2002:a05:6a20:66a9:b0:d9:43a5:8cd0 with SMTP id o41-20020a056a2066a900b000d943a58cd0mr19442291pzh.14.1680129325023; Wed, 29 Mar 2023 15:35:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680129325; cv=none; d=google.com; s=arc-20160816; b=Dk5pkzINHZ/7KatOZ/YQP/pvFXJXA+nSvZOWZscIzIEgVIWbe0bG4WaMR6BXNo7P/U ebi7FPCM6xuQmywdGp73Oyjg7bMlFmjPeeV7oghyP6X9q8hwJ+KH2zKSFXvuMnzW+gpc Y3tXRQwj+UO9xcsjjERtv5De/haDL9O3QWRMjN4qvtHzPzjURp57WNpOaIOGpVjT9K+K ++uN+l7Ow9AOQ5VnMYbez7atXGiJxU6eosAMbFA8y1jJwd7S4AwVDOgMvepXOh5fJcke p0KscTKtcxbqsLyM0QpwJ0I960zv3HOON5dej0NPbFCICvhSjhAXNV4/okz39nhyRZ6Z lxOw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :feedback-id:references:in-reply-to:message-id:subject:cc:from:to :dkim-signature:date; bh=34FzFldeZSN4gNaY0AVW74p21n+Hrhm8D3FHnB95c1U=; b=BA9p8xo98eAkJ3sbZEGcyJ5gfTt/iwU/vi+yepwFxSsyqy6QSGpbqSDDffxCHm8Mwe /vomKMB4sB11d2ARDR3kaO1FYkIJiADwoR8Ziijhqev46omU4DfeEm5S/MxGruhIXCnS y3tIqEnNRNkgqsAhodE80cE8gmdIRZnYhvfaCheb99sN6raZKWOIWKguBejUQL0/F3xE nRwJZVUar6x0zCSdpvT92mrwA+yVAwLG0HMW7bL/4GhHTMEmgPAlqpCjJ2qG7sR10GCR tjcpm26gpXGLL1KLTHx0rObj6eNeAtELKY3KprsooS4DICvl8ZN0AleaTatwdSjEitfS BJUg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=mtLGHzBm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=protonmail.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v63-20020a638942000000b005030006a2desi32405601pgd.182.2023.03.29.15.35.12; Wed, 29 Mar 2023 15:35:24 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=mtLGHzBm; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=protonmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229774AbjC2WdE (ORCPT + 99 others); Wed, 29 Mar 2023 18:33:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229546AbjC2WdD (ORCPT ); Wed, 29 Mar 2023 18:33:03 -0400 Received: from mail-4322.protonmail.ch (mail-4322.protonmail.ch [185.70.43.22]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A5FF64C28 for ; Wed, 29 Mar 2023 15:33:01 -0700 (PDT) Date: Wed, 29 Mar 2023 22:32:53 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1680129178; x=1680388378; bh=34FzFldeZSN4gNaY0AVW74p21n+Hrhm8D3FHnB95c1U=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=mtLGHzBmBCAfgefvQMgn2z52q0bezoGPU4oAKrZnIM/MzloWQIYI08G3700TgZxut NaOdSv4sft48kNhPqpzw8RSCHJvz+D/hvZsQWvw8rLCB5yZXyLUFGI3wa+8fd0LHKE Mqe/KtJ4Czbd6VSUAfliJA5tF6wjLr2UCqhUsZXH3BLTy89jfuzT/muw8RBkIFbAMk Le6yvFQnTVVIM21a37rc8DXk9aFOuRzp26pY+wGXhF94P8qGJVNifuY+1z1qZiWdsK 286jaUzv+Ih5G2jakB/8v5CEAnnF8sy3XkFgPwwuz2FaDW2eI1u0ys73jzHFVs0TOg gqq8dKNJf3sYA== To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Alice Ryhl From: y86-dev@protonmail.com Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev Subject: [PATCH v3 01/13] rust: macros: add `quote!` macro Message-ID: <20230329223239.138757-2-y86-dev@protonmail.com> In-Reply-To: <20230329223239.138757-1-y86-dev@protonmail.com> References: <20230329223239.138757-1-y86-dev@protonmail.com> Feedback-ID: 40624463:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,RCVD_IN_MSPIKE_H2, SPF_HELO_PASS,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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) =3D 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_span= ned.html) macro from the +/// `quote` crate but provides only just enough functionality needed by th= e current `macros` crate. +#[allow(unused_macros)] +macro_rules! quote_spanned { + ($span:expr =3D> $($tt:tt)*) =3D> { + #[allow(clippy::vec_init_then_push)] + { + let mut tokens =3D Vec::new(); + let span =3D $span; + quote_spanned!(@proc tokens span $($tt)*); + proc_macro::TokenStream::from_iter(tokens) + }}; + (@proc $v:ident $span:ident) =3D> {}; + (@proc $v:ident $span:ident #$id:ident $($tt:tt)*) =3D> { + let mut ts =3D 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)*) =3D> { + for token in $id { + let mut ts =3D proc_macro::TokenStream::new(); + crate::quote::ToTokens::to_tokens(&token, &mut ts); + $v.extend(ts.into_iter()); + } + quote_spanned!(@proc $v $span $($tt)*); + }; + (@proc $v:ident $span:ident ( $($inner:tt)* ) $($tt:tt)*) =3D> { + let mut tokens =3D 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)*) =3D> { + let mut tokens =3D 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)*) =3D> { + let mut tokens =3D 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)*) =3D> { + $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)*) =3D> { + $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)*) =3D> { + $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)*) =3D> { + $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)*) =3D> { + $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)*) =3D> { + $v.push(proc_macro::TokenTree::Ident(proc_macro::Ident::new(string= ify!($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 nee= ded by the current +/// `macros` crate. +/// +/// [`Span::mixed_site()`]: https://doc.rust-lang.org/proc_macro/struct.Sp= an.html#method.mixed_site +#[allow(unused_macros)] +macro_rules! quote { + ($($tt:tt)*) =3D> { + quote_spanned!(proc_macro::Span::mixed_site() =3D> $($tt)*) + } +} -- 2.39.2