Received: by 2002:a05:7412:419a:b0:f3:1519:9f41 with SMTP id i26csp1832293rdh; Sat, 25 Nov 2023 04:51:48 -0800 (PST) X-Google-Smtp-Source: AGHT+IGmvJr5fuZ7mjjd54P8F3GK+sgO5JDF0nz+ELtBzOCO7O23P1/Ng2akuZLgKG4xIDtVeZdv X-Received: by 2002:a17:902:e746:b0:1cf:b989:9660 with SMTP id p6-20020a170902e74600b001cfb9899660mr621321plf.68.1700916708320; Sat, 25 Nov 2023 04:51:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1700916708; cv=none; d=google.com; s=arc-20160816; b=lZpg+iWfGHgldLnIjjtFpC9q/71CMrziaKKLyw6mSM23fQSL5y0EMV/Gs8shOtjWxU ThezW0PPkYcNjeFHYdq0tGa9H9gXlAdQKv1avVQ4jee09Dqi0XAhGjb3jMIVInn34yGy H6rre5CRPWK7KaZwRyiF4HXUpL8HtvpY9vJOP+NmUuR0p2HEez7nYleUIj7S6URu/OsC qEvin8P7iVIU/43QjPgzJG1UsOTFEgVR0IbL58unInKdTXIt0VHHm0dAfERam8BbO9LK 0icK9TZnfKAiEqNJC83rw2FyU+C09L1jsvNNSlVRCQ9ePLRKsWz9ErlU3BfaGQaIU8bp 5l9g== 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:date:dkim-signature; bh=bzZvCYsbJgfCiGy9vIa+JkIEYDyc8iF8244zXEcpiHg=; fh=8/UpcEQQNzv8moBPlaSWYgYEdx/WlUKNBRVLxHLt8X4=; b=arjhn4gRHJt2L6zn6ObCrW+NSULwOAniWlOv401ODgQbZk2FJrWGHVNZRecuFvkJxX +1PprNxCgeiPcM3+oKmo18VJendDYabvERvbSVzFZvQiy8kWAy3bw3IDMlZEwmUyxx5v +kqPnAne02U8PebycYnpLjNPuP/dWeoG7Bg6+xY1ikGweNJa37ElaauiGvDbZiUtSn81 87HNwRgwXpBZv5jBMeK77Qg9HVTjwEuEtZH5vBBrZYAYeMVSXAbyQJLaogxLMYPMOqeS kcYzJsO7o9G1dkhjDtkyFS/jxYgcU2Zrq8imJNwM+aABiFn7mKr7WwVpHUf7DUTuffLm StuA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=qh2uj6xbbvbxrnxhxti6oxthfi.protonmail header.b=GVmy2iMx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id a17-20020a170902b59100b001cc2ef72ab2si5285647pls.68.2023.11.25.04.51.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Nov 2023 04:51:48 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@proton.me header.s=qh2uj6xbbvbxrnxhxti6oxthfi.protonmail header.b=GVmy2iMx; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=proton.me Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id C0940803EE1E; Sat, 25 Nov 2023 04:51:45 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.11 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232017AbjKYMvE (ORCPT + 99 others); Sat, 25 Nov 2023 07:51:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51574 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229483AbjKYMvD (ORCPT ); Sat, 25 Nov 2023 07:51:03 -0500 Received: from mail-4322.protonmail.ch (mail-4322.protonmail.ch [185.70.43.22]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9964FA5; Sat, 25 Nov 2023 04:51:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=qh2uj6xbbvbxrnxhxti6oxthfi.protonmail; t=1700916665; x=1701175865; bh=bzZvCYsbJgfCiGy9vIa+JkIEYDyc8iF8244zXEcpiHg=; h=Date:To:From:Cc:Subject:Message-ID:Feedback-ID:From:To:Cc:Date: Subject:Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=GVmy2iMxJHq9iElHjyYdipNjbfCH53bjqnSfPZ2DLKxnWqkx31YLNeJnqfZyS8J8m NVhahLyT9M5SMThAzuqRfmfP2bDZOqEjk+2Y2GFixw6EQEybLb0MXr0BA6VqEFcvx8 4u3Dd3lW4ytsSuumAoaqYPv1SKcos0yRzUSX0rImOl6uMqG/rN5E6UtJxfK56Q1VaG HivdhnsNJYhlc2lt23xlSeZ70Eequ9oFb56lIckCeLyyae/EtHCBRSJux+ZluYi/9F RLKBfXNg2/XADzqevnu5OKYDqY++zNTndl3KOVeiQ+6HFysYoaDlPnNLmI2F7MEO5C Zju/8HHaNdDTQ== Date: Sat, 25 Nov 2023 12:50:49 +0000 To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?Q?Bj=C3=B6rn_Roy_Baron?= , Benno Lossin , Andreas Hindborg , Alice Ryhl , Martin Rodriguez Reboredo , Asahi Lina From: Benno Lossin Cc: Sumera Priyadarsini , rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/3] rust: macros: `parse_generics` add `decl_generics` Message-ID: <20231125125024.1235933-1-benno.lossin@proton.me> Feedback-ID: 71780778:user:proton MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Sat, 25 Nov 2023 04:51:45 -0800 (PST) When parsing generics of a type definition, default values can be specified. This syntax is however only available on type definitions and not e.g. impl blocks. This patch adds the `decl_generics` which can only be used on type defintions, since they contain the default values for generic parameters. This patch also changes how `impl_generics` are made up, as these should be used with `impl<$impl_generics>`, they will omit the default values. Signed-off-by: Benno Lossin --- rust/macros/helpers.rs | 91 +++++++++++++++++++++++++++-------------- rust/macros/pin_data.rs | 1 + rust/macros/zeroable.rs | 1 + 3 files changed, 63 insertions(+), 30 deletions(-) diff --git a/rust/macros/helpers.rs b/rust/macros/helpers.rs index afb0f2e3a36a..36fecdd998d0 100644 --- a/rust/macros/helpers.rs +++ b/rust/macros/helpers.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 =20 -use proc_macro::{token_stream, Group, Punct, Spacing, TokenStream, TokenTr= ee}; +use proc_macro::{token_stream, Group, TokenStream, TokenTree}; =20 pub(crate) fn try_ident(it: &mut token_stream::IntoIter) -> Option= { if let Some(TokenTree::Ident(ident)) =3D it.next() { @@ -72,6 +72,7 @@ pub(crate) fn expect_end(it: &mut token_stream::IntoIter)= { =20 pub(crate) struct Generics { pub(crate) impl_generics: Vec, + pub(crate) decl_generics: Vec, pub(crate) ty_generics: Vec, } =20 @@ -81,6 +82,8 @@ pub(crate) struct Generics { pub(crate) fn parse_generics(input: TokenStream) -> (Generics, Vec) { // `impl_generics`, the declared generics with their bounds. let mut impl_generics =3D vec![]; + // The generics with bounds and default values. + let mut decl_generics =3D vec![]; // Only the names of the generics, without any bounds. let mut ty_generics =3D vec![]; // Tokens not related to the generics e.g. the `where` token and defin= ition. @@ -90,10 +93,17 @@ pub(crate) fn parse_generics(input: TokenStream) -> (Ge= nerics, Vec) { let mut toks =3D input.into_iter(); // If we are at the beginning of a generic parameter. let mut at_start =3D true; - for tt in &mut toks { + let mut skip_until_comma =3D false; + while let Some(tt) =3D toks.next() { + if nesting =3D=3D 1 && matches!(&tt, TokenTree::Punct(p) if p.as_c= har() =3D=3D '>') { + // Found the end of the generics. + break; + } else if nesting >=3D 1 { + decl_generics.push(tt.clone()); + } match tt.clone() { TokenTree::Punct(p) if p.as_char() =3D=3D '<' =3D> { - if nesting >=3D 1 { + if nesting >=3D 1 && !skip_until_comma { // This is inside of the generics and part of some bou= nd. impl_generics.push(tt); } @@ -105,49 +115,70 @@ pub(crate) fn parse_generics(input: TokenStream) -> (= Generics, Vec) { break; } else { nesting -=3D 1; - if nesting >=3D 1 { + if nesting >=3D 1 && !skip_until_comma { // We are still inside of the generics and part of= some bound. impl_generics.push(tt); } - if nesting =3D=3D 0 { - break; - } } } - tt =3D> { + TokenTree::Punct(p) if skip_until_comma && p.as_char() =3D=3D = ',' =3D> { if nesting =3D=3D 1 { - // Here depending on the token, it might be a generic = variable name. - match &tt { - // Ignore const. - TokenTree::Ident(i) if i.to_string() =3D=3D "const= " =3D> {} - TokenTree::Ident(_) if at_start =3D> { - ty_generics.push(tt.clone()); - // We also already push the `,` token, this ma= kes it easier to append - // generics. - ty_generics.push(TokenTree::Punct(Punct::new('= ,', Spacing::Alone))); - at_start =3D false; - } - TokenTree::Punct(p) if p.as_char() =3D=3D ',' =3D>= at_start =3D true, - // Lifetimes begin with `'`. - TokenTree::Punct(p) if p.as_char() =3D=3D '\'' && = at_start =3D> { - ty_generics.push(tt.clone()); - } - _ =3D> {} - } + impl_generics.push(TokenTree::Punct(p.clone())); + ty_generics.push(TokenTree::Punct(p)); + skip_until_comma =3D false; } - if nesting >=3D 1 { - impl_generics.push(tt); - } else if nesting =3D=3D 0 { + } + tt if !skip_until_comma =3D> { + match nesting { // If we haven't entered the generics yet, we still wa= nt to keep these tokens. - rest.push(tt); + 0 =3D> rest.push(tt), + 1 =3D> { + // Here depending on the token, it might be a gene= ric variable name. + match tt { + TokenTree::Ident(i) if at_start && i.to_string= () =3D=3D "const" =3D> { + let Some(name) =3D toks.next() else { + // Parsing error. + break; + }; + impl_generics.push(TokenTree::Ident(i)); + impl_generics.push(name.clone()); + ty_generics.push(name.clone()); + decl_generics.push(name); + at_start =3D false; + } + tt @ TokenTree::Ident(_) if at_start =3D> { + impl_generics.push(tt.clone()); + ty_generics.push(tt); + at_start =3D false; + } + TokenTree::Punct(p) if p.as_char() =3D=3D ',' = =3D> { + impl_generics.push(TokenTree::Punct(p.clon= e())); + ty_generics.push(TokenTree::Punct(p)); + at_start =3D true; + } + // Lifetimes begin with `'`. + TokenTree::Punct(p) if p.as_char() =3D=3D '\''= && at_start =3D> { + ty_generics.push(TokenTree::Punct(p.clone(= ))); + impl_generics.push(TokenTree::Punct(p)); + } + // Generics can have default values, we skip t= hese. + TokenTree::Punct(p) if p.as_char() =3D=3D '=3D= ' =3D> { + skip_until_comma =3D true; + } + tt =3D> impl_generics.push(tt), + } + } + _ =3D> impl_generics.push(tt), } } + _ =3D> {} } } rest.extend(toks); ( Generics { impl_generics, + decl_generics, ty_generics, }, rest, diff --git a/rust/macros/pin_data.rs b/rust/macros/pin_data.rs index 6d58cfda9872..022e68e9720d 100644 --- a/rust/macros/pin_data.rs +++ b/rust/macros/pin_data.rs @@ -10,6 +10,7 @@ pub(crate) fn pin_data(args: TokenStream, input: TokenStr= eam) -> TokenStream { let ( Generics { impl_generics, + decl_generics: _, ty_generics, }, rest, diff --git a/rust/macros/zeroable.rs b/rust/macros/zeroable.rs index 0d605c46ab3b..cfee2cec18d5 100644 --- a/rust/macros/zeroable.rs +++ b/rust/macros/zeroable.rs @@ -7,6 +7,7 @@ pub(crate) fn derive(input: TokenStream) -> TokenStream { let ( Generics { impl_generics, + decl_generics: _, ty_generics, }, mut rest, base-commit: 98b1cc82c4affc16f5598d4fa14b1858671b2263 --=20 2.40.1