~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/rust/macros/vtable.rs

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /rust/macros/vtable.rs (Architecture mips) and /rust/macros/vtable.rs (Architecture sparc64)


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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php