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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use proc_macro2::TokenStream as HelperTokenStream;
use quote::quote;
use std::collections::HashMap;
use syn::{Fields, Ident, ItemStruct};
pub fn generate(
aggr_map: &HashMap<Ident, Vec<Ident>>,
base_interface_idents: &[Ident],
struct_item: &ItemStruct,
) -> HelperTokenStream {
let struct_ident = &struct_item.ident;
let vis = &struct_item.vis;
let base_fields = gen_base_fields(base_interface_idents);
let ref_count_field = gen_ref_count_field();
let user_fields = gen_user_fields(struct_item);
let aggregate_fields = gen_aggregate_fields(aggr_map);
quote!(
#[repr(C)]
#vis struct #struct_ident {
#base_fields
#ref_count_field
#aggregate_fields
#user_fields
}
)
}
pub fn gen_base_fields(base_interface_idents: &[Ident]) -> HelperTokenStream {
let bases_interface_idents = base_interface_idents.iter().map(|base| {
let field_ident = crate::utils::vptr_field_ident(&base);
quote!(#field_ident: *const <dyn #base as com::ComInterface>::VTable)
});
quote!(#(#bases_interface_idents,)*)
}
pub fn gen_ref_count_field() -> HelperTokenStream {
let ref_count_ident = crate::utils::ref_count_ident();
quote!(#ref_count_ident: std::cell::Cell<u32>,)
}
pub fn gen_aggregate_fields(aggr_map: &HashMap<Ident, Vec<Ident>>) -> HelperTokenStream {
let aggregates = aggr_map.iter().map(|(aggr_field_ident, _)| {
quote!(
#aggr_field_ident: *mut *const <dyn com::interfaces::iunknown::IUnknown as com::ComInterface>::VTable
)
});
quote!(#(#aggregates,)*)
}
pub fn gen_user_fields(struct_item: &ItemStruct) -> HelperTokenStream {
let fields = match &struct_item.fields {
Fields::Named(f) => &f.named,
_ => panic!("Found non Named fields in struct."),
};
quote!(#fields)
}