mirror of
https://github.com/vosen/ZLUDA.git
synced 2025-10-05 15:49:25 +00:00
Api traits test code (#487)
Add initial templated API test support. This needs to be improved to use an attribute macro, but that will require some major surgery :(
This commit is contained in:
parent
9d4f1699d0
commit
4752fcdcf2
11 changed files with 115 additions and 4 deletions
|
@ -10,7 +10,7 @@ use syn::punctuated::Punctuated;
|
|||
use syn::visit_mut::VisitMut;
|
||||
use syn::{
|
||||
bracketed, parse_macro_input, File, ForeignItem, ForeignItemFn, Ident, Item, Path, Signature,
|
||||
Token,
|
||||
Token, token
|
||||
};
|
||||
|
||||
const CUDA_RS: &'static str = include_str! {"cuda.rs"};
|
||||
|
@ -308,3 +308,40 @@ fn join(
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
#[proc_macro]
|
||||
pub fn generate_api_macro(input: TokenStream) -> TokenStream {
|
||||
let input = parse_macro_input!(input as ApiMacroInput);
|
||||
let ApiMacroInput(_, trait_name, _, type_name, _, macro_name) = input;
|
||||
let expanded = quote! {
|
||||
struct #type_name;
|
||||
macro_rules! #macro_name {
|
||||
($($abi:literal fn $fn_name:ident( $( $arg_id:ident : $arg_type:ty ),* ) -> $ret_type:ty;)*) => {
|
||||
impl #trait_name for #type_name {
|
||||
fn new() -> Self { Self }
|
||||
$(
|
||||
#[inline(always)]
|
||||
fn $fn_name(&self, $( $arg_id : $arg_type ),* ) -> $ret_type {
|
||||
unsafe { super::$fn_name( $( $arg_id ),* ) }
|
||||
}
|
||||
)*
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
TokenStream::from(expanded)
|
||||
}
|
||||
#[allow(dead_code)]
|
||||
struct ApiMacroInput(token::Impl, Path, token::For, Path, token::Use, Ident);
|
||||
impl syn::parse::Parse for ApiMacroInput {
|
||||
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
|
||||
Ok(ApiMacroInput(
|
||||
input.parse()?, // impl
|
||||
input.parse()?, // trait
|
||||
input.parse()?, // for
|
||||
input.parse()?, // type
|
||||
input.parse()?, // using
|
||||
input.parse()? // macro
|
||||
))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue