Path: blob/master/rust/pin-init/internal/src/pin_data.rs
29281 views
// SPDX-License-Identifier: Apache-2.0 OR MIT12#[cfg(not(kernel))]3use proc_macro2 as proc_macro;45use crate::helpers::{parse_generics, Generics};6use proc_macro::{Group, Punct, Spacing, TokenStream, TokenTree};78pub(crate) fn pin_data(args: TokenStream, input: TokenStream) -> TokenStream {9// This proc-macro only does some pre-parsing and then delegates the actual parsing to10// `pin_init::__pin_data!`.1112let (13Generics {14impl_generics,15decl_generics,16ty_generics,17},18rest,19) = parse_generics(input);20// The struct definition might contain the `Self` type. Since `__pin_data!` will define a new21// type with the same generics and bounds, this poses a problem, since `Self` will refer to the22// new type as opposed to this struct definition. Therefore we have to replace `Self` with the23// concrete name.2425// Errors that occur when replacing `Self` with `struct_name`.26let mut errs = TokenStream::new();27// The name of the struct with ty_generics.28let struct_name = rest29.iter()30.skip_while(|tt| !matches!(tt, TokenTree::Ident(i) if i.to_string() == "struct"))31.nth(1)32.and_then(|tt| match tt {33TokenTree::Ident(_) => {34let tt = tt.clone();35let mut res = vec![tt];36if !ty_generics.is_empty() {37// We add this, so it is maximally compatible with e.g. `Self::CONST` which38// will be replaced by `StructName::<$generics>::CONST`.39res.push(TokenTree::Punct(Punct::new(':', Spacing::Joint)));40res.push(TokenTree::Punct(Punct::new(':', Spacing::Alone)));41res.push(TokenTree::Punct(Punct::new('<', Spacing::Alone)));42res.extend(ty_generics.iter().cloned());43res.push(TokenTree::Punct(Punct::new('>', Spacing::Alone)));44}45Some(res)46}47_ => None,48})49.unwrap_or_else(|| {50// If we did not find the name of the struct then we will use `Self` as the replacement51// and add a compile error to ensure it does not compile.52errs.extend(53"::core::compile_error!(\"Could not locate type name.\");"54.parse::<TokenStream>()55.unwrap(),56);57"Self".parse::<TokenStream>().unwrap().into_iter().collect()58});59let impl_generics = impl_generics60.into_iter()61.flat_map(|tt| replace_self_and_deny_type_defs(&struct_name, tt, &mut errs))62.collect::<Vec<_>>();63let mut rest = rest64.into_iter()65.flat_map(|tt| {66// We ignore top level `struct` tokens, since they would emit a compile error.67if matches!(&tt, TokenTree::Ident(i) if i.to_string() == "struct") {68vec![tt]69} else {70replace_self_and_deny_type_defs(&struct_name, tt, &mut errs)71}72})73.collect::<Vec<_>>();74// This should be the body of the struct `{...}`.75let last = rest.pop();76let mut quoted = quote!(::pin_init::__pin_data! {77parse_input:78@args(#args),79@sig(#(#rest)*),80@impl_generics(#(#impl_generics)*),81@ty_generics(#(#ty_generics)*),82@decl_generics(#(#decl_generics)*),83@body(#last),84});85quoted.extend(errs);86quoted87}8889/// Replaces `Self` with `struct_name` and errors on `enum`, `trait`, `struct` `union` and `impl`90/// keywords.91///92/// The error is appended to `errs` to allow normal parsing to continue.93fn replace_self_and_deny_type_defs(94struct_name: &Vec<TokenTree>,95tt: TokenTree,96errs: &mut TokenStream,97) -> Vec<TokenTree> {98match tt {99TokenTree::Ident(ref i)100if i.to_string() == "enum"101|| i.to_string() == "trait"102|| i.to_string() == "struct"103|| i.to_string() == "union"104|| i.to_string() == "impl" =>105{106errs.extend(107format!(108"::core::compile_error!(\"Cannot use `{i}` inside of struct definition with \109`#[pin_data]`.\");"110)111.parse::<TokenStream>()112.unwrap()113.into_iter()114.map(|mut tok| {115tok.set_span(tt.span());116tok117}),118);119vec![tt]120}121TokenTree::Ident(i) if i.to_string() == "Self" => struct_name.clone(),122TokenTree::Literal(_) | TokenTree::Punct(_) | TokenTree::Ident(_) => vec![tt],123TokenTree::Group(g) => vec![TokenTree::Group(Group::new(124g.delimiter(),125g.stream()126.into_iter()127.flat_map(|tt| replace_self_and_deny_type_defs(struct_name, tt, errs))128.collect(),129))],130}131}132133134