From patchwork Mon Mar 13 01:23:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: y86-dev X-Patchwork-Id: 68612 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp956390wrd; Sun, 12 Mar 2023 18:44:35 -0700 (PDT) X-Google-Smtp-Source: AK7set+t4J2WvxSr55Jun5TVMuEVyyd+lgoVecRlMbn2s3UXu72eTbFqgqWY62j9MAV/2g21MU2I X-Received: by 2002:a17:902:d50c:b0:199:4d25:6a4d with SMTP id b12-20020a170902d50c00b001994d256a4dmr9473532plg.10.1678671875161; Sun, 12 Mar 2023 18:44:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678671875; cv=none; d=google.com; s=arc-20160816; b=cdXzEzPA1q4sqPcEEd1ezBCyp/A9/KkBH1PQxQryB6wJmEB/BGQfrSmX2US33e4Rw8 yf6SrP8uQL2Pqa4N8VkKj5kiXnH0Ewd1yZ9QjJcYqkg9gqAlN0CpgLLVS+U3cSMRlF9/ YngrOcZ8IaMXZCONEpagkJMwxzDAT8Dx39PY0A55u63tdfT/6BZuC7NcSjiERknWhB/c z8ifcXLYM2KkO/CQ43athhfCPh4025gBkaCMlMOENrAjetCt56Dk8Wrpi/r6pE6jox8p b7tDImvyiBefXr1BQfNChs509YJSmLZbniVAiyzohbHW1rtbnyv/JVov+8935AQGxU2x /lHA== 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:message-id:subject:cc:from:to:dkim-signature:date; bh=XdinrPtR68VBR2Oe7yoV/KX84VkXWkmiSOLxXszvESk=; b=ocxbyxSsOVJy+BwRobpe5aZXvXKUgArvnGMIzjarQ/J6dZ/9BSueDlbbasrM3xjnNQ OQgUNHN3WKcE2IsJnU3oMtFaj5lLQb/Vc2e/4BBE3N+FiwhggyImSM7GRkScTwsG8Xuo 9bV2jhHTES7a9S9M0zX1vg3fJ3msfs/VOc+IiaTg+lARqd38OPrhMJipVjBp1yUq14wY AfDlKu02hd2hCmc9+mrmn+sXjtPYK4vKojbv+gYhCZqwYZUWpSjCRIs+8lBySva/v/N0 j24psf6YXks+MF7XmnuJp2xSyjRIDWqjOIfq/6RiMXQuGPg8Rn4Ydx/CEpnErlHOTzum zoHw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@protonmail.com header.s=protonmail3 header.b=biOaiB8S; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h8-20020a170902ac8800b0019aaac6d603si1641069plr.318.2023.03.12.18.44.19; Sun, 12 Mar 2023 18:44:35 -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=biOaiB8S; 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 S229863AbjCMBXe (ORCPT + 99 others); Sun, 12 Mar 2023 21:23:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56126 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229473AbjCMBXd (ORCPT ); Sun, 12 Mar 2023 21:23:33 -0400 Received: from mail-4318.protonmail.ch (mail-4318.protonmail.ch [185.70.43.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 611D327D79; Sun, 12 Mar 2023 18:23:30 -0700 (PDT) Date: Mon, 13 Mar 2023 01:23:18 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1678670608; x=1678929808; bh=XdinrPtR68VBR2Oe7yoV/KX84VkXWkmiSOLxXszvESk=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=biOaiB8SrAcoEGLq2xURoFLR4bbe2u0lT5ad924UQqbgNhkMmOEuErl3nlJU2qnN0 FYkoC19X0aMQ6UKrQh6rIz68CdOXi5ivzldTOgp4cvsG1rRSHv1H1iqAcDYNHpSr/d /cZiRO/WaDmxzRNbWjZ4msXoOd3WhEHcoQvm/wi40j6dbndSdX32djfmNOac9tK6p7 MEy/vs9rtmnAgrfbupV/+pN7PmYDNbE9FMvc0P4QqWoKTiboDTMuRyDoYhdlCa4Z/h NU0rxSJM3n0GVGQlroRGXMxDSApKu6JzBwF/NezkCiLXJ6UFOHbiLCFbjjOFRURTuj w9Xq9gfCo717Q== To: "ojeda@kernel.org" , "alex.gaynor@gmail.com" , "wedsonaf@gmail.com" , "boqun.feng@gmail.com" , Gary Guo , "bjorn3_gh@protonmail.com" From: y86-dev Cc: "rust-for-linux@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "patches@lists.linux.dev" Subject: [PATCH v1 1/3] rust: macros: add `quote!` macro Message-ID: Feedback-ID: 40624463:user:proton MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,SPF_HELO_PASS, SPF_PASS autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760215040135657231?= X-GMAIL-MSGID: =?utf-8?q?1760215040135657231?= 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 Acked-by: Boqun Feng --- rust/macros/lib.rs | 2 + rust/macros/quote.rs | 125 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 127 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..55b663d665ec --- /dev/null +++ b/rust/macros/quote.rs @@ -0,0 +1,125 @@ +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()); + } +} + +macro_rules! quote_spanned { + ($span:expr => $($tt:tt)*) => {{ + let mut tokens = Vec::new(); + let span = $span; + quote_spanned!(@proc tokens span $($tt)*); + proc_macro::TokenStream::from_iter(tokens) + }}; + (@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()); + } + 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)*); + }; +} + +macro_rules! quote { + ($($tt:tt)*) => { + quote_spanned!(proc_macro::Span::mixed_site() => $($tt)*) + } +}