From patchwork Mon Apr 24 08:11:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benno Lossin X-Patchwork-Id: 86902 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp2588844vqo; Mon, 24 Apr 2023 01:33:58 -0700 (PDT) X-Google-Smtp-Source: AKy350a70lDvTsFSoNpdl4NBPSJmj2oe4zu2SeB8fqOTBJ8or/NviYAzndYIzmOdq42LCoSz6Ib3 X-Received: by 2002:a05:6a20:7491:b0:f5:ae09:ce02 with SMTP id p17-20020a056a20749100b000f5ae09ce02mr1009789pzd.18.1682325237836; Mon, 24 Apr 2023 01:33:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1682325237; cv=none; d=google.com; s=arc-20160816; b=hguZdvzHlSEsq6XzUmo6TFWaqh1wJ7U7u0DDBSQBYgTcztHjMsWK8VZEcjl84RgeLU LRyz9peYoBJV3x2zzE9vOrD8hTwA8TgqICSHlDHQkDs1gZVQbIPb1u38JvNU/qejPtvn umP+YzmVVypJeTG5D4TF5WwRI4z1ksZfF5NW2j0K+TySlBhkqn5fxhjgipanjqj5QBwA QQVPe9cYLbTbJSMQNui5tTbPbM6Vm/uE7qIYCCUj0IJp1qHaka37nOglwJrfjGiD23l3 rdV1IHXR8/Q/YJYRKIgSZDABmNGi8YSjDykQcAM2i0kjEjy4+sUxMH5fMOMfY6MaMeWc 4hzQ== 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=RUNMyd4H4XD6TO4X8sc2wnYhEAx+HVkNXO8MNMYE5N8=; b=k3xO7A2EjknVvz/CEBMo46ABsbHhfXVU8VUQZeL8eJ3h+QrxlS96AzfubYyzYFZWeH zrPaKSjYCbzJZpWzcVxVUVKD1s9R8BPxvjM8qBNaP6kDFyIyKbKiugFnUnSrZo4S/v54 q7SamLqGJJ3qLpDxgxUXZXxrh2xCb911yTXIXnp4ixd8KXirB5dM2SbhpyNKNeOJ0QOi UEVvdC1cZa7nomRhJZtZy9VRusrPrVnTFrYuuqFpD4LaARPZFo+8fTJsT0gxxvnI+Vak //J2lVBpb3ul2ZNyXBHVaT0H8Ad8i8bE60QYR6QrEQYTs+NgjII1U5I9kux2yCV4actM JoLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@proton.me header.s=protonmail header.b=Q62kl+6T; 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=proton.me Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r20-20020a6560d4000000b00524b0097b6csi10287365pgv.816.2023.04.24.01.33.46; Mon, 24 Apr 2023 01:33:57 -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=@proton.me header.s=protonmail header.b=Q62kl+6T; 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=proton.me Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231574AbjDXIL5 (ORCPT + 99 others); Mon, 24 Apr 2023 04:11:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49532 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231550AbjDXILs (ORCPT ); Mon, 24 Apr 2023 04:11:48 -0400 Received: from mail-40131.protonmail.ch (mail-40131.protonmail.ch [185.70.40.131]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7CD04E58; Mon, 24 Apr 2023 01:11:45 -0700 (PDT) Date: Mon, 24 Apr 2023 08:11:38 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=protonmail; t=1682323903; x=1682583103; bh=RUNMyd4H4XD6TO4X8sc2wnYhEAx+HVkNXO8MNMYE5N8=; 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=Q62kl+6TuKHTrT+CMX/qBxhc7l80lq1xvUnqqeAJLDaR56S1C1rKHaKRs7oAEU8iA jU7ek97cmOruPkIYkPNyxVGjFshqBnwqhE/l/MMBvCPqjFra20lV5vlrVz1KbSlSsk Eo9uZmHoagw7g9q7tF768d/OB0N8khZ4zsK7omdd1tJNFym5LUuld+PWX9f4X/jCjq b39K9dw5o3uJplQPD2udnLf7cCcMX+c2Ro/ltxGySqM6F+MTRXsjqjLtkGIv2JHLbA XBF54msZfql8B4iFkuFau8m6wNTyzokrWpunuVgQXDrIfQ+AewfsxI/Hqy9VvNRoqx asVt3nwX5BjMA== To: Miguel Ojeda , Alex Gaynor , Wedson Almeida Filho , Boqun Feng , Gary Guo , =?utf-8?q?Bj=C3=B6rn_Roy_Baron?= From: Benno Lossin Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org, patches@lists.linux.dev, Benno Lossin Subject: [PATCH 2/4] rust: macros: refactor generics parsing of `#[pin_data]` into its own function Message-ID: <20230424081112.99890-2-benno.lossin@proton.me> In-Reply-To: <20230424081112.99890-1-benno.lossin@proton.me> References: <20230424081112.99890-1-benno.lossin@proton.me> Feedback-ID: 71780778: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,SPF_HELO_PASS,SPF_PASS, T_SCC_BODY_TEXT_LINE 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?1764045868955340708?= X-GMAIL-MSGID: =?utf-8?q?1764045868955340708?= Other macros might also want to parse generics. Additionally this makes the code easier to read, as the next commit will introduce more code in `#[pin_data]`. Also add more comments to explain how parsing generics work. Signed-off-by: Benno Lossin Reviewed-by: Martin Rodriguez Reboredo Reviewed-by: Alice Ryhl Reviewed-by: Gary Guo --- rust/macros/helpers.rs | 86 ++++++++++++++++++++++++++++++++++++++++- rust/macros/pin_data.rs | 70 +++++---------------------------- 2 files changed, 94 insertions(+), 62 deletions(-) -- 2.40.0 diff --git a/rust/macros/helpers.rs b/rust/macros/helpers.rs index b2bdd4d8c958..afb0f2e3a36a 100644 --- a/rust/macros/helpers.rs +++ b/rust/macros/helpers.rs @@ -1,6 +1,6 @@ // SPDX-License-Identifier: GPL-2.0 -use proc_macro::{token_stream, Group, TokenTree}; +use proc_macro::{token_stream, Group, Punct, Spacing, TokenStream, TokenTree}; pub(crate) fn try_ident(it: &mut token_stream::IntoIter) -> Option { if let Some(TokenTree::Ident(ident)) = it.next() { @@ -69,3 +69,87 @@ pub(crate) fn expect_end(it: &mut token_stream::IntoIter) { panic!("Expected end"); } } + +pub(crate) struct Generics { + pub(crate) impl_generics: Vec, + pub(crate) ty_generics: Vec, +} + +/// Parses the given `TokenStream` into `Generics` and the rest. +/// +/// The generics are not present in the rest, but a where clause might remain. +pub(crate) fn parse_generics(input: TokenStream) -> (Generics, Vec) { + // `impl_generics`, the declared generics with their bounds. + let mut impl_generics = vec![]; + // Only the names of the generics, without any bounds. + let mut ty_generics = vec![]; + // Tokens not related to the generics e.g. the `where` token and definition. + let mut rest = vec![]; + // The current level of `<`. + let mut nesting = 0; + let mut toks = input.into_iter(); + // If we are at the beginning of a generic parameter. + let mut at_start = true; + for tt in &mut toks { + match tt.clone() { + TokenTree::Punct(p) if p.as_char() == '<' => { + if nesting >= 1 { + // This is inside of the generics and part of some bound. + impl_generics.push(tt); + } + nesting += 1; + } + TokenTree::Punct(p) if p.as_char() == '>' => { + // This is a parsing error, so we just end it here. + if nesting == 0 { + break; + } else { + nesting -= 1; + if nesting >= 1 { + // We are still inside of the generics and part of some bound. + impl_generics.push(tt); + } + if nesting == 0 { + break; + } + } + } + tt => { + if nesting == 1 { + // Here depending on the token, it might be a generic variable name. + match &tt { + // Ignore const. + TokenTree::Ident(i) if i.to_string() == "const" => {} + TokenTree::Ident(_) if at_start => { + ty_generics.push(tt.clone()); + // We also already push the `,` token, this makes it easier to append + // generics. + ty_generics.push(TokenTree::Punct(Punct::new(',', Spacing::Alone))); + at_start = false; + } + TokenTree::Punct(p) if p.as_char() == ',' => at_start = true, + // Lifetimes begin with `'`. + TokenTree::Punct(p) if p.as_char() == '\'' && at_start => { + ty_generics.push(tt.clone()); + } + _ => {} + } + } + if nesting >= 1 { + impl_generics.push(tt); + } else if nesting == 0 { + // If we haven't entered the generics yet, we still want to keep these tokens. + rest.push(tt); + } + } + } + } + rest.extend(toks); + ( + Generics { + impl_generics, + ty_generics, + }, + rest, + ) +} diff --git a/rust/macros/pin_data.rs b/rust/macros/pin_data.rs index 954149d77181..c593b05d9e8c 100644 --- a/rust/macros/pin_data.rs +++ b/rust/macros/pin_data.rs @@ -1,71 +1,19 @@ // SPDX-License-Identifier: Apache-2.0 OR MIT -use proc_macro::{Punct, Spacing, TokenStream, TokenTree}; +use crate::helpers::{parse_generics, Generics}; +use proc_macro::TokenStream; pub(crate) fn pin_data(args: TokenStream, input: TokenStream) -> TokenStream { // This proc-macro only does some pre-parsing and then delegates the actual parsing to // `kernel::__pin_data!`. - // - // In here we only collect the generics, since parsing them in declarative macros is very - // elaborate. We also do not need to analyse their structure, we only need to collect them. - // `impl_generics`, the declared generics with their bounds. - let mut impl_generics = vec![]; - // Only the names of the generics, without any bounds. - let mut ty_generics = vec![]; - // Tokens not related to the generics e.g. the `impl` token. - let mut rest = vec![]; - // The current level of `<`. - let mut nesting = 0; - let mut toks = input.into_iter(); - // If we are at the beginning of a generic parameter. - let mut at_start = true; - for tt in &mut toks { - match tt.clone() { - TokenTree::Punct(p) if p.as_char() == '<' => { - if nesting >= 1 { - impl_generics.push(tt); - } - nesting += 1; - } - TokenTree::Punct(p) if p.as_char() == '>' => { - if nesting == 0 { - break; - } else { - nesting -= 1; - if nesting >= 1 { - impl_generics.push(tt); - } - if nesting == 0 { - break; - } - } - } - tt => { - if nesting == 1 { - match &tt { - TokenTree::Ident(i) if i.to_string() == "const" => {} - TokenTree::Ident(_) if at_start => { - ty_generics.push(tt.clone()); - ty_generics.push(TokenTree::Punct(Punct::new(',', Spacing::Alone))); - at_start = false; - } - TokenTree::Punct(p) if p.as_char() == ',' => at_start = true, - TokenTree::Punct(p) if p.as_char() == '\'' && at_start => { - ty_generics.push(tt.clone()); - } - _ => {} - } - } - if nesting >= 1 { - impl_generics.push(tt); - } else if nesting == 0 { - rest.push(tt); - } - } - } - } - rest.extend(toks); + let ( + Generics { + impl_generics, + ty_generics, + }, + mut rest, + ) = parse_generics(input); // This should be the body of the struct `{...}`. let last = rest.pop(); quote!(::kernel::__pin_data! {