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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
use prelude::*;
use super::common::*;
use std::iter;
use std::collections::BTreeMap;
use idents;
use utils;
use tyhandlers::{Direction, ModelTypeSystem};
use model;
use methodinfo::ComMethodInfo;
extern crate proc_macro;
#[derive(Default)]
struct InterfaceOutput {
iid_arms : Vec<TokenStream>,
method_impls : BTreeMap< String, MethodImpl >,
}
struct MethodImpl {
info : ComMethodInfo,
impls : BTreeMap< ModelTypeSystem, TokenStream >,
}
impl MethodImpl {
pub fn new( mi : ComMethodInfo ) -> Self {
MethodImpl {
info: mi,
impls : Default::default(),
}
}
}
pub fn expand_com_interface(
attr_tokens: TokenStreamNightly,
item_tokens: TokenStreamNightly,
) -> Result<TokenStreamNightly, model::ParseError>
{
let mut output = vec![];
let itf = model::ComInterface::parse(
&lib_name(),
attr_tokens.into(),
&item_tokens.to_string() )?;
let itf_ident = itf.name();
let mut itf_output = InterfaceOutput::default();
for ( &ts, itf_variant ) in itf.variants() {
process_itf_variant(
&itf, ts, itf_variant,
&mut output, &mut itf_output );
}
if itf.item_type() == utils::InterfaceType::Trait {
let mut impls = vec![];
for ( _, method ) in itf_output.method_impls.iter() {
let mut impl_branches = vec![];
for ( ts, method_ts_impl ) in method.impls.iter() {
let ts_tokens = ts.as_typesystem_tokens();
impl_branches.push( quote!(
if let Some( comptr ) = ComItf::maybe_ptr( self, #ts_tokens ) {
#method_ts_impl
}
) );
}
let impl_args = method.info.args.iter().map( |ca| {
let name = &ca.name;
let ty = &ca.ty;
quote!( #name : #ty )
} );
let unsafety = if method.info.is_unsafe { quote!( unsafe ) } else { quote!() };
let self_arg = &method.info.rust_self_arg;
let method_rust_ident = &method.info.display_name;
let return_ty = &method.info.rust_return_ty;
impls.push( quote!(
#unsafety fn #method_rust_ident(
#self_arg, #( #impl_args ),*
) -> #return_ty {
#[allow(unused_imports)]
use ::intercom::ComInto;
#[allow(unused_imports)]
use ::intercom::ErrorValue;
#( #impl_branches )*
< #return_ty as ::intercom::ErrorValue >::from_com_error(
::intercom::ComError::E_POINTER.into() )
}
) );
}
let unsafety = if itf.is_unsafe() { quote!( unsafe ) } else { quote!() };
output.push( quote!(
#unsafety impl #itf_ident for ::intercom::ComItf< #itf_ident > {
#( #impls )*
}
) );
}
let iid_arms = itf_output.iid_arms;
let ( deref_impl, deref_ret ) = if itf.item_type() == utils::InterfaceType::Trait {
(
quote!( com_itf ),
quote!( &( #itf_ident + 'static ) )
)
} else {
(
quote!(
let some_iunk : &::intercom::ComItf<::intercom::IUnknown> = com_itf.as_ref();
let iunknown_iid = ::intercom::IUnknown::iid(
::intercom::TypeSystem::Automation )
.expect( "IUnknown must have Automation IID" );
let primary_iunk = some_iunk.query_interface( iunknown_iid )
.expect( "All types must implement IUnknown" );
let combox : *mut ::intercom::ComBox< #itf_ident > =
primary_iunk as *mut ::intercom::ComBox< #itf_ident >;
unsafe {
::intercom::ComBox::release( combox );
use std::ops::Deref;
(*combox).deref()
}
),
quote!( & #itf_ident )
)
};
output.push( quote!(
impl ::intercom::ComInterface for #itf_ident {
#[doc = "Returns the IID of the requested interface."]
fn iid( ts : ::intercom::TypeSystem ) -> Option< &'static ::intercom::IID > {
match ts {
#( #iid_arms ),*
}
}
fn deref(
com_itf : &::intercom::ComItf< #itf_ident >
) -> #deref_ret {
#deref_impl
}
}
) );
Ok( tokens_to_tokenstream( item_tokens, output ) )
}
fn process_itf_variant(
itf : &model::ComInterface,
ts : ModelTypeSystem,
itf_variant : &model::ComInterfaceVariant,
output : &mut Vec<TokenStream>,
itf_output : &mut InterfaceOutput,
) {
let itf_ident = itf.name();
let visibility = itf.visibility();
let iid_ident = idents::iid( itf_variant.unique_name() );
let vtable_ident = idents::vtable_struct( itf_variant.unique_name() );
let iid_tokens = utils::get_guid_tokens( itf_variant.iid() );
let iid_doc = format!( "`{}` interface ID.", itf_ident );
output.push( quote!(
#[doc = #iid_doc]
#[allow(non_upper_case_globals)]
#visibility const #iid_ident : ::intercom::IID = #iid_tokens;
) );
let ts_match = ts.as_typesystem_tokens();
itf_output.iid_arms.push( quote!( #ts_match => Some( & #iid_ident ) ) );
let mut vtbl_fields = vec![];
if let Some( ref base ) = *itf.base_interface() {
let vtbl = match base.to_string().as_ref() {
"IUnknown" => quote!( ::intercom::IUnknownVtbl ),
_ => { let vtbl = idents::vtable_struct( &base ); quote!( #vtbl ) }
};
vtbl_fields.push( quote!( pub __base : #vtbl ) );
}
let calling_convention = get_calling_convetion();
for method_info in itf_variant.methods() {
let method_ident = &method_info.unique_name;
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 ret_ty = method_info.returnhandler.com_ty();
vtbl_fields.push( quote!(
pub #method_ident :
unsafe extern #calling_convention fn( #( #args ),* ) -> #ret_ty
) );
let method_name = method_info.display_name.to_string();
if ! itf_output.method_impls.contains_key( &method_name ) {
itf_output.method_impls.insert(
method_name.clone(),
MethodImpl::new( method_info.clone() ) );
}
let method_impl = &mut itf_output.method_impls.get_mut( &method_name )
.expect( "We just ensured this exists three lines up... ;_;" );
method_impl.impls.insert(
itf_variant.type_system(),
rust_to_com_delegate( itf_variant, method_info, &vtable_ident ) );
}
output.push( quote!(
#[allow(non_camel_case_types)]
#[repr(C)]
#[doc(hidden)]
#visibility struct #vtable_ident { #( #vtbl_fields, )* }
) );
}
fn rust_to_com_delegate(
itf_variant : &model::ComInterfaceVariant,
method_info : &ComMethodInfo,
vtable_ident : &Ident,
) -> TokenStream {
let out_arg_declarations = method_info.returnhandler.com_out_args()
.iter()
.map( |ca| {
let ident = &ca.name;
let ty = &ca.handler.com_ty( Direction::Retval );
let default = ca.handler.default_value();
quote!( let mut #ident : #ty = #default; )
} ).collect::<Vec<_>>();
let ( temporaries, params ) : ( Vec<_>, Vec<_> ) = method_info.raw_com_args()
.into_iter()
.map( |com_arg| {
let name = com_arg.name;
match com_arg.dir {
Direction::In => {
let param = com_arg.handler.rust_to_com( &name, Direction::In );
( param.temporary, param.value )
},
Direction::Out | Direction::Retval
=> ( None, quote!( &mut #name ) ),
}
} )
.unzip();
let params = iter::once( quote!( comptr.ptr ) ).chain( params );
let return_ident = Ident::new( "__result", Span::call_site() );
let return_statement = method_info
.returnhandler
.com_to_rust_return( &return_ident );
let method_ident = &method_info.unique_name;
let return_ty = &method_info.rust_return_ty;
let iid_tokens = utils::get_guid_tokens( itf_variant.iid() );
quote!(
let vtbl = comptr.ptr as *const *const #vtable_ident;
#( #temporaries )*
#[allow(unused_unsafe)]
let result : Result< #return_ty, ::intercom::ComError > = ( || unsafe {
#( #out_arg_declarations )*
let #return_ident = ((**vtbl).#method_ident)( #( #params ),* );
let INTERCOM_iid = #iid_tokens;
Ok( { #return_statement } )
} )();
return match result {
Ok( v ) => v,
Err( err ) => < #return_ty as ::intercom::ErrorValue >::from_com_error( err ),
};
)
}