1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use crate::*;
use squote::{quote, TokenStream};

// TODO: move winmd into gen crate to improve inlining and simplify
#[derive(Debug)]
pub struct Function {
    pub name: TypeName,
    pub signature: Signature,
}

impl Function {
    pub fn new(name: TypeName, method: &winmd::MethodDef) -> Self {
        let signature = Signature::new(method, &[], &name.namespace);
        Self { name, signature }
    }

    pub fn gen(&self) -> TokenStream {
        let name = self.signature.method.name();
        let name = format_ident(name);

        let params = self.signature.params.iter().map(|t| {
            let name = format_ident(&t.name);
            let tokens = t.gen_field();
            quote! { #name: #tokens }
        });

        let return_type = if let Some(t) = &self.signature.return_type {
            let tokens = t.gen_field();
            quote! { -> #tokens }
        } else {
            TokenStream::new()
        };

        let mut link = self.signature.method.impl_map().unwrap().scope().name();

        // TODO: workaround for https://github.com/microsoft/windows-rs/issues/463
        if link.contains("-ms-win-") || link == "D3DCOMPILER_47" {
            link = "onecoreuap";
        }

        quote! {
            #[link(name = #link)]
            extern "system" {
                pub fn #name(#(#params),*) #return_type;
            }
        }
    }

    pub fn dependencies(&self) -> Vec<winmd::TypeDef> {
        self.signature.dependencies()
    }
}