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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
use prelude::*;
use super::common::*;
use std::iter;
use tyhandlers::{Direction};
use idents;
use model;
extern crate proc_macro;
use self::proc_macro::TokenStream;
pub fn expand_com_impl(
_attr_tokens: &TokenStream,
item_tokens: TokenStream,
) -> Result<TokenStream, model::ParseError>
{
let mut output = vec![] ;
let imp = model::ComImpl::parse( &item_tokens.to_string() )?;
let struct_ident = imp.struct_name();
let itf_ident = imp.interface_name();
for ( _, impl_variant ) in imp.variants() {
let itf_unique_ident = impl_variant.interface_unique_name();
let vtable_struct_ident = idents::vtable_struct( &itf_unique_ident );
let vtable_instance_ident = idents::vtable_instance( &struct_ident, &itf_unique_ident );
let vtable_offset = idents::vtable_offset( &struct_ident, &itf_unique_ident );
let calling_convetion = get_calling_convetion();
let query_interface_ident = idents::method_impl(
&struct_ident, &itf_unique_ident, "query_interface" );
output.push( quote!(
#[allow(non_snake_case)]
#[doc(hidden)]
unsafe extern #calling_convetion fn #query_interface_ident(
self_vtable : ::intercom::RawComPtr,
riid : ::intercom::REFIID,
out : *mut ::intercom::RawComPtr
) -> ::intercom::raw::HRESULT
{
::intercom::ComBox::< #struct_ident >::query_interface(
&mut *(( self_vtable as usize - #vtable_offset() ) as *mut _ ),
riid,
out )
}
) );
let add_ref_ident = idents::method_impl(
&struct_ident, &itf_unique_ident, "add_ref" );
output.push( quote!(
#[allow(non_snake_case)]
#[allow(dead_code)]
#[doc(hidden)]
unsafe extern #calling_convetion fn #add_ref_ident(
self_vtable : ::intercom::RawComPtr
) -> u32 {
::intercom::ComBox::< #struct_ident >::add_ref(
&mut *(( self_vtable as usize - #vtable_offset() ) as *mut _ ) )
}
) );
let release_ident = idents::method_impl(
&struct_ident, &itf_unique_ident, "release" );
output.push( quote!(
#[allow(non_snake_case)]
#[allow(dead_code)]
#[doc(hidden)]
unsafe extern #calling_convetion fn #release_ident(
self_vtable : ::intercom::RawComPtr
) -> u32 {
::intercom::ComBox::< #struct_ident >::release_ptr(
( self_vtable as usize - #vtable_offset() ) as *mut _ )
}
) );
let mut vtable_fields = vec![
quote!(
__base : ::intercom::IUnknownVtbl {
query_interface_Automation : #query_interface_ident,
add_ref_Automation : #add_ref_ident,
release_Automation : #release_ident,
},
) ];
for method_info in impl_variant.methods() {
let method_ident = &method_info.unique_name;
let method_rust_ident = &method_info.display_name;
let method_impl_ident = idents::method_impl(
&struct_ident, &itf_unique_ident, &method_ident.to_string() );
let in_out_args = method_info.raw_com_args()
.into_iter()
.map( |com_arg| {
let name = &com_arg.name;
let com_ty = &com_arg.handler.com_ty( com_arg.dir );
let dir = match com_arg.dir {
Direction::In => quote!(),
Direction::Out | Direction::Retval => quote!( *mut )
};
quote!( #name : #dir #com_ty )
} );
let self_arg = quote!( self_vtable : ::intercom::RawComPtr );
let args = iter::once( self_arg ).chain( in_out_args );
let ( in_temporaries, in_params ) : ( Vec<_>, Vec<_> ) = method_info.args
.iter()
.map( |ca| {
let conversion = ca.handler.com_to_rust( &ca.name, Direction::In );
( conversion.temporary, conversion.value )
} )
.unzip();
let return_ident = Ident::new( "__result", Span::call_site() );
let return_statement = method_info
.returnhandler
.rust_to_com_return( &return_ident );
let ret_ty = method_info.returnhandler.com_ty();
let self_struct_stmt = if method_info.is_const {
quote!( let self_struct : &#itf_ident = &**self_combox )
} else {
quote!( let self_struct : &mut #itf_ident = &mut **self_combox )
};
output.push( quote!(
#[allow(non_snake_case)]
#[allow(dead_code)]
#[doc(hidden)]
unsafe extern #calling_convetion fn #method_impl_ident(
#( #args ),*
) -> #ret_ty {
let result : Result< #ret_ty, ::intercom::ComError > = ( || {
let self_combox = ( self_vtable as usize - #vtable_offset() )
as *mut ::intercom::ComBox< #struct_ident >;
#( #in_temporaries )*
#self_struct_stmt;
let #return_ident = self_struct.#method_rust_ident( #( #in_params ),* );
Ok( { #return_statement } )
} )();
use ::intercom::ErrorValue;
match result {
Ok( v ) => v,
Err( err ) => < #ret_ty as ErrorValue >::from_error(
::intercom::store_error( err ) ),
}
}
) );
vtable_fields.push( quote!( #method_ident : #method_impl_ident, ) );
}
output.push( quote!(
#[allow(non_upper_case_globals)]
const #vtable_instance_ident : #vtable_struct_ident
= #vtable_struct_ident { #( #vtable_fields )* };
) );
}
output.push( quote!(
impl ::intercom::HasInterface< #itf_ident > for #struct_ident {}
) );
Ok( tokens_to_tokenstream( item_tokens, output ) )
}