forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 436
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a single `#[vtable]` macro to replace the current boilerplating of `ToUse`, `USE_NONE`, `declare_file_operations` required for declaring and implementing traits that maps to Linux's pure vtables and contains optional methods. Signed-off-by: Gary Guo <[email protected]>
- Loading branch information
Showing
9 changed files
with
159 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
|
||
use proc_macro::{Delimiter, Group, TokenStream, TokenTree}; | ||
use std::fmt::Write; | ||
|
||
pub fn vtable(_attr: TokenStream, ts: TokenStream) -> TokenStream { | ||
let mut it = ts.into_iter(); | ||
|
||
let mut tokens = Vec::new(); | ||
|
||
// Scan for the `trait` or `impl` keyword | ||
let is_trait = loop { | ||
let token = it.next().expect("unexpected end"); | ||
let keyword = match &token { | ||
TokenTree::Ident(ident) => match ident.to_string().as_str() { | ||
"trait" => Some(true), | ||
"impl" => Some(false), | ||
_ => None, | ||
}, | ||
_ => None, | ||
}; | ||
tokens.push(token); | ||
if let Some(v) = keyword { | ||
break v; | ||
} | ||
}; | ||
|
||
// Scan for the main body | ||
// FIXME: `{}` is allowed within type as well, e.g. `impl Foo for Bar<{0}>`. | ||
// but these are very rare and we don't want to have generics parsing code. | ||
let body = loop { | ||
match it.next() { | ||
Some(TokenTree::Group(group)) if group.delimiter() == Delimiter::Brace => { | ||
break group; | ||
} | ||
Some(token) => tokens.push(token), | ||
None => panic!("unexpected end"), | ||
} | ||
}; | ||
|
||
assert!(it.next().is_none(), "expected end"); | ||
|
||
let mut body_it = body.stream().into_iter(); | ||
let mut functions = Vec::new(); | ||
while let Some(token) = body_it.next() { | ||
match token { | ||
TokenTree::Ident(ident) if ident.to_string() == "fn" => { | ||
let fn_name = match body_it.next() { | ||
Some(TokenTree::Ident(ident)) => ident.to_string(), | ||
_ => panic!("expected identifier after `fn`"), | ||
}; | ||
functions.push(fn_name); | ||
} | ||
_ => (), | ||
} | ||
} | ||
|
||
let mut const_items; | ||
if is_trait { | ||
const_items = "/// A marker to prevent implementors from forgetting to use [`#[vtable]`](vtable) attribute when implementing this trait. | ||
const USE_VTABLE_ATTR: ();".to_owned(); | ||
|
||
for f in functions { | ||
write!( | ||
const_items, | ||
"/// Indicates if the `{}` method is overriden by the implementor. | ||
const HAS_{}: bool = false;", | ||
f, | ||
f.to_uppercase() | ||
) | ||
.unwrap(); | ||
} | ||
} else { | ||
const_items = "const USE_VTABLE_ATTR: () = ();".to_owned(); | ||
|
||
for f in functions { | ||
write!(const_items, "const HAS_{}: bool = true;", f.to_uppercase()).unwrap(); | ||
} | ||
} | ||
|
||
let new_body = vec![const_items.parse().unwrap(), body.stream()] | ||
.into_iter() | ||
.collect(); | ||
tokens.push(TokenTree::Group(Group::new(Delimiter::Brace, new_body))); | ||
tokens.into_iter().collect() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.