1b44becc5SGary Guo // SPDX-License-Identifier: GPL-2.0
2b44becc5SGary Guo
3b44becc5SGary Guo use proc_macro::{Delimiter, Group, TokenStream, TokenTree};
4b44becc5SGary Guo use std::collections::HashSet;
5b44becc5SGary Guo use std::fmt::Write;
6b44becc5SGary Guo
vtable(_attr: TokenStream, ts: TokenStream) -> TokenStream7b44becc5SGary Guo pub(crate) fn vtable(_attr: TokenStream, ts: TokenStream) -> TokenStream {
8b44becc5SGary Guo let mut tokens: Vec<_> = ts.into_iter().collect();
9b44becc5SGary Guo
10b44becc5SGary Guo // Scan for the `trait` or `impl` keyword.
11b44becc5SGary Guo let is_trait = tokens
12b44becc5SGary Guo .iter()
13b44becc5SGary Guo .find_map(|token| match token {
14b44becc5SGary Guo TokenTree::Ident(ident) => match ident.to_string().as_str() {
15b44becc5SGary Guo "trait" => Some(true),
16b44becc5SGary Guo "impl" => Some(false),
17b44becc5SGary Guo _ => None,
18b44becc5SGary Guo },
19b44becc5SGary Guo _ => None,
20b44becc5SGary Guo })
21b44becc5SGary Guo .expect("#[vtable] attribute should only be applied to trait or impl block");
22b44becc5SGary Guo
23b44becc5SGary Guo // Retrieve the main body. The main body should be the last token tree.
24b44becc5SGary Guo let body = match tokens.pop() {
25b44becc5SGary Guo Some(TokenTree::Group(group)) if group.delimiter() == Delimiter::Brace => group,
26b44becc5SGary Guo _ => panic!("cannot locate main body of trait or impl block"),
27b44becc5SGary Guo };
28b44becc5SGary Guo
29b44becc5SGary Guo let mut body_it = body.stream().into_iter();
30b44becc5SGary Guo let mut functions = Vec::new();
31b44becc5SGary Guo let mut consts = HashSet::new();
32b44becc5SGary Guo while let Some(token) = body_it.next() {
33b44becc5SGary Guo match token {
34b44becc5SGary Guo TokenTree::Ident(ident) if ident.to_string() == "fn" => {
35b44becc5SGary Guo let fn_name = match body_it.next() {
36b44becc5SGary Guo Some(TokenTree::Ident(ident)) => ident.to_string(),
37b44becc5SGary Guo // Possibly we've encountered a fn pointer type instead.
38b44becc5SGary Guo _ => continue,
39b44becc5SGary Guo };
40b44becc5SGary Guo functions.push(fn_name);
41b44becc5SGary Guo }
42b44becc5SGary Guo TokenTree::Ident(ident) if ident.to_string() == "const" => {
43b44becc5SGary Guo let const_name = match body_it.next() {
44b44becc5SGary Guo Some(TokenTree::Ident(ident)) => ident.to_string(),
45b44becc5SGary Guo // Possibly we've encountered an inline const block instead.
46b44becc5SGary Guo _ => continue,
47b44becc5SGary Guo };
48b44becc5SGary Guo consts.insert(const_name);
49b44becc5SGary Guo }
50b44becc5SGary Guo _ => (),
51b44becc5SGary Guo }
52b44becc5SGary Guo }
53b44becc5SGary Guo
54b44becc5SGary Guo let mut const_items;
55b44becc5SGary Guo if is_trait {
56b44becc5SGary Guo const_items = "
57b44becc5SGary Guo /// A marker to prevent implementors from forgetting to use [`#[vtable]`](vtable)
58b44becc5SGary Guo /// attribute when implementing this trait.
59b44becc5SGary Guo const USE_VTABLE_ATTR: ();
60b44becc5SGary Guo "
61b44becc5SGary Guo .to_owned();
62b44becc5SGary Guo
63b44becc5SGary Guo for f in functions {
64b44becc5SGary Guo let gen_const_name = format!("HAS_{}", f.to_uppercase());
65b44becc5SGary Guo // Skip if it's declared already -- this allows user override.
66b44becc5SGary Guo if consts.contains(&gen_const_name) {
67b44becc5SGary Guo continue;
68b44becc5SGary Guo }
69b44becc5SGary Guo // We don't know on the implementation-site whether a method is required or provided
70b44becc5SGary Guo // so we have to generate a const for all methods.
71b44becc5SGary Guo write!(
72b44becc5SGary Guo const_items,
73b44becc5SGary Guo "/// Indicates if the `{f}` method is overridden by the implementor.
74b44becc5SGary Guo const {gen_const_name}: bool = false;",
75b44becc5SGary Guo )
76b44becc5SGary Guo .unwrap();
77*3fa7187eSQingsong Chen consts.insert(gen_const_name);
78b44becc5SGary Guo }
79b44becc5SGary Guo } else {
80b44becc5SGary Guo const_items = "const USE_VTABLE_ATTR: () = ();".to_owned();
81b44becc5SGary Guo
82b44becc5SGary Guo for f in functions {
83b44becc5SGary Guo let gen_const_name = format!("HAS_{}", f.to_uppercase());
84b44becc5SGary Guo if consts.contains(&gen_const_name) {
85b44becc5SGary Guo continue;
86b44becc5SGary Guo }
87b44becc5SGary Guo write!(const_items, "const {gen_const_name}: bool = true;").unwrap();
88b44becc5SGary Guo }
89b44becc5SGary Guo }
90b44becc5SGary Guo
91b44becc5SGary Guo let new_body = vec![const_items.parse().unwrap(), body.stream()]
92b44becc5SGary Guo .into_iter()
93b44becc5SGary Guo .collect();
94b44becc5SGary Guo tokens.push(TokenTree::Group(Group::new(Delimiter::Brace, new_body)));
95b44becc5SGary Guo tokens.into_iter().collect()
96b44becc5SGary Guo }
97