Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1961338rwl; Sat, 8 Apr 2023 05:27:02 -0700 (PDT) X-Google-Smtp-Source: AKy350a58EJXQi290M8AHNbSjosJJ+rAnukG9fmmWbOLC0oOYLc63262qJQzMmuGT5o0hL2ek2Yw X-Received: by 2002:a17:90b:3ec3:b0:23d:816:faa9 with SMTP id rm3-20020a17090b3ec300b0023d0816faa9mr6917954pjb.12.1680956822206; Sat, 08 Apr 2023 05:27:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680956822; cv=none; d=google.com; s=arc-20160816; b=vBlftx7Pw9PyC6xAF2PapMF471Sg+lz+PnuuKdQ+MNqHmF1EH55zkOGxqUyi3rpoMu ye8wiNUME0fjzFiddf72Jqe2nNqX0NAzoqHBaagwpy67a4R2DF9iuzB1BQYjvLNKVMgq OnIcrDI2a8WJQcYFkAcE21S4Yi7xQpf/MuE6AA2BsraVDSbdkccj6KjyE5MK7mgSqHsf c8+vbvEiVjdBEJmE5E4CyyQ6z7MYW801EGg0tK23wHlWmi8hIWbz9NBM08fr6fo17iXp j2PUiuhsXkpncaJ2AM5HBfIAobPscqpObIXCxEc0/bXY6/QBXwOYV00Lijcb1ZmFMqc0 QWug== 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=K9JbIFZF8PegFhJuUFtlmmuNCJL0YgKAbcJnn1iYflI=; b=ujxMT8xFHxBr8lb+kr4k0/fQXTE9C8chPT/SlvNUYqxGWRNrzdWaF5a4ko6QU1cin7 U8UWqOWqAD1wU7KT2kTXl6t+3VW53vpvjxOkdVjgpYikNgbKmnZJmCRJC3yNRD0nwlMo onDpDiHvk4NkyhHsUYF2o9m66R/fyHGregdFdGJmezxCr1vy9BVj7seUGi89sEnScIl2 4kJBqPgS/amUKh1+3Pc1EAoAsL23zPMCRXFixFXrlYXmmx3ozhhxWTJ68M/TkUFboYLz qpngPUhy3mpv1874q1e4zH1/rIa4riUjYux3mmgyYr9WnLpIzvlX49VSMt4YIXq2Ufvw 0ENw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=xyxNIwI5; 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 q4-20020a17090a938400b0024496072b17si5979671pjo.133.2023.04.08.05.26.48; Sat, 08 Apr 2023 05:27:02 -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=xyxNIwI5; 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 S230195AbjDHMZi (ORCPT + 99 others); Sat, 8 Apr 2023 08:25:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60240 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229914AbjDHMZg (ORCPT ); Sat, 8 Apr 2023 08:25:36 -0400 Received: from mail-40133.protonmail.ch (mail-40133.protonmail.ch [185.70.40.133]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 62B49FF0E for ; Sat, 8 Apr 2023 05:25:35 -0700 (PDT) Date: Sat, 08 Apr 2023 12:25:23 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1680956733; x=1681215933; bh=K9JbIFZF8PegFhJuUFtlmmuNCJL0YgKAbcJnn1iYflI=; 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=xyxNIwI5rbLIDZRXv6V8QWw/emXcOZO2V0U++S4GLVil8TNtAseaDnoeznUnGmurX RQHjT5gNMUSagRlWnmJWeoyIfxzaSHrhbSph+8Sh5rCoF2GWB69LDHqOdzq4PKpd// MX4dRnj23UDTjI7PsNOf2PPnvq08qepMmmiuQU3fX0E/6oGVIalymfvUrE44V47P6G bDgG3c7e7djLmNm+qypzrWcxlvi7+B7NI8jH4bDMuuqxLi3El19b4wYGqLO/OsGMyF pV9ms3oQnFKdXNVlrychje6q0MaT6hMBnfuRA912x3lFBCX2zdC+z0CYodga3UA/qG WAE8i05K/bp5g== To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Alice Ryhl , Andreas Hindborg From: Benno Lossin Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Benno Lossin , Andreas Hindborg , Alice Ryhl Subject: [PATCH v7 02/15] rust: macros: add `quote!` macro Message-ID: <20230408122429.1103522-3-y86-dev@protonmail.com> In-Reply-To: <20230408122429.1103522-1-y86-dev@protonmail.com> References: <20230408122429.1103522-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 Reviewed-by: Andreas Hindborg Reviewed-by: Alice Ryhl --- 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..94a6277182ee --- /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 ::std::vec::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); + 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); + } + quote_spanned!(@proc $v $span $($tt)*); + }; + (@proc $v:ident $span:ident ( $($inner:tt)* ) $($tt:tt)*) =3D> { + let mut tokens =3D ::std::vec::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 ::std::vec::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 ::std::vec::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(st= ringify!($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