intercom-common 0.4.0

See 'intercom'
Documentation
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
use super::common::*;
use crate::prelude::*;

use std::collections::BTreeMap;
use std::iter;

use crate::idents;
use crate::methodinfo::ComMethodInfo;
use crate::model;
use crate::tyhandlers::{Direction, ModelTypeSystem};
use crate::utils;

use syn::spanned::Spanned;

extern crate proc_macro;

/// Interface level output.
#[derive(Default)]
struct InterfaceOutput
{
    iid_arms: Vec<TokenStream>,
    method_impls: BTreeMap<String, MethodImpl>,
}

struct MethodImpl
{
    /// _Some_ method info.
    ///
    /// This should not be depended upon for anything type system specific.
    info: ComMethodInfo,

    /// Type system specific implementation for the method.
    impls: BTreeMap<ModelTypeSystem, TokenStream>,
}

impl MethodImpl
{
    pub fn new(mi: ComMethodInfo) -> Self
    {
        MethodImpl {
            info: mi,
            impls: Default::default(),
        }
    }
}

/// Expands the `com_interface` attribute.
///
/// The attribute expansion results in the following items:
///
/// - Global IID for the interface.
/// - Virtual table struct definition for the interface.
/// - Implementation for the delegating methods when calling the COM interface
///   from Rust.
pub fn expand_com_interface(
    attr_tokens: TokenStreamNightly,
    item_tokens: TokenStreamNightly,
) -> Result<TokenStreamNightly, model::ParseError>
{
    // Parse the attribute.
    let mut output = vec![];
    let itf =
        model::ComInterface::from_ast(&lib_name(), attr_tokens.into(), item_tokens.clone().into())?;
    let itf_path = &itf.path;
    let itf_name = itf.ident.to_string();
    let itf_ref = &itf.itf_ref;

    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 method_rust_ident = &method.info.name;
            let method_name = method_rust_ident.to_string();
            let mut impl_branches = vec![];
            for (ts, method_ts_impl) in method.impls.iter() {
                let ts_tokens = ts.as_typesystem_type(method.info.signature_span);
                let ts_name = format!("{:?}", ts);
                impl_branches.push(quote_spanned!(method.info.signature_span =>
                    if let Some( comptr ) = intercom::ComItf::ptr::<#ts_tokens>( self ) {
                        intercom::logging::trace(|l| l(module_path!(), format_args!(
                            "[{:p}, with {:p}] Calling {}::{}, type system: {}",
                            self, comptr.ptr, #itf_name, #method_name, #ts_name)));

                        #method_ts_impl
                    }
                ));
            }

            // Format the method arguments into tokens.
            let impl_args = method.info.args.iter().map(|ca| {
                let name = &ca.name;
                let ty = &ca.ty;
                quote_spanned!(ca.span => #name : #ty )
            });

            let unsafety = if method.info.is_unsafe {
                quote_spanned!(method.info.signature_span => unsafe)
            } else {
                quote!()
            };
            let self_arg = &method.info.rust_self_arg;
            let return_ty = &method.info.rust_return_ty;

            // Rust to COM implementation.
            impls.push(quote_spanned!(method.info.signature_span =>
                #unsafety fn #method_rust_ident(
                    #self_arg, #( #impl_args ),*
                ) -> #return_ty {

                    intercom::logging::trace(|l| l(module_path!(), format_args!(
                        "[{:p}] Calling {}::{}", self, #itf_name, #method_name)));

                    #[allow(unused_imports)]
                    use intercom::ErrorValue;

                    // Try the available type systems.
                    #( #impl_branches )*

                    // The ComItf invariant states it has at least one pointer
                    // available so we should never get here.
                    //
                    // Also since this is Rust-to-COM call we are allowed to
                    // panic here.
                    unreachable!();
                }
            ));
        }

        let unsafety = if itf.is_unsafe {
            quote_spanned!(itf.span => unsafe)
        } else {
            quote!()
        };
        output.push(quote_spanned!(itf.span =>
            #[allow(clippy::all)]
            #[allow(unused_braces)]
            #unsafety impl<I: intercom::attributes::ComInterface + #itf_path + ?Sized> #itf_path for intercom::ComItf<I> {
                #( #impls )*
            }
        ));
    }

    // Implement the ComInterface for the trait.
    let iid_arms = itf_output.iid_arms;
    let (deref_impl, deref_ret) = if itf.item_type == utils::InterfaceType::Trait {
        (
            quote_spanned!(itf.span => com_itf),
            quote_spanned!(itf.span => &( dyn #itf_path + 'static ) ),
        )
    } else {
        // Note this is _extremely_ dangerous.
        //
        // Essentially we are assuming here that every #itf_path pointer represents
        // a ComBox structure that we have created. This will fail the moment
        // the user code implements #itf_path interface on their own and passes
        // that back to us.
        //
        // There's no real way to get this to work and we might want to just remove
        // the possibility to do 'implicit' interfaces by just impling the struct.
        (
            quote_spanned!(itf.span =>
                let some_iunk : &intercom::ComItf<dyn intercom::interfaces::RawIUnknown> = com_itf.as_raw_iunknown();
                let iunknown_iid = <dyn intercom::IUnknown>::iid(
                        intercom::type_system::TypeSystemName::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::ComBoxData< #itf_path > =
                        primary_iunk as *mut intercom::ComBoxData< #itf_path >;
                unsafe {

                    // We are already holding a reference to the 'self', which should
                    // keep this alive. We don't need to maintain a lifetime of the
                    // queried interface.
                    intercom::ComBoxData::release( combox );

                    // Deref.
                    use std::ops::Deref;
                    (*combox).deref()
                }
            ),
            quote_spanned!(itf.span => & #itf_path ),
        )
    };

    output.push(quote_spanned!(itf.span =>
        impl intercom::attributes::ComInterface for #itf_ref {

            type TSelf = #itf_ref;

            #[doc = "Returns the IID of the requested interface."]
            fn iid_ts<TS: intercom::type_system::TypeSystem>() -> &'static intercom::IID
                where Self: intercom::attributes::ComInterfaceVariant<TS>
            {
                <Self as intercom::attributes::ComInterfaceVariant<TS>>::iid()
            }

            fn iid(
                ts : intercom::type_system::TypeSystemName
            ) -> Option< &'static intercom::IID >
            {
                match ts {
                    #( #iid_arms ),*
                }
            }

            fn deref(
                com_itf : &intercom::ComItf<#itf_ref>
            ) -> #deref_ret {
                #deref_impl
            }
        }
    ));

    // Implement type info for the interface.
    output.push(quote_spanned!(itf.span =>

        impl intercom::type_system::ForeignType for #itf_ref {

            /// The name of the type.
            fn type_name() -> &'static str { stringify!( #itf_path )  }
        }

    ));

    // Create runtime type info.
    output.push(create_get_typeinfo_function(&itf));

    Ok(tokens_to_tokenstream(item_tokens, output))
}

/// Processes the interface type system variant.
///
/// # Arguments
///
/// * `itf` - Interface details.
/// * `ts` - Type system the variant represents.
/// * `itf_variant` - Interface variant details.
/// * `output` - Direct output emitted for each interface variant.
/// * `itf_output` - Interface variant data for the interface level output.
fn process_itf_variant(
    itf: &model::ComInterface,
    ts: ModelTypeSystem,
    itf_variant: &model::ComInterfaceVariant,
    output: &mut Vec<TokenStream>,
    itf_output: &mut InterfaceOutput,
)
{
    let itf_path = &itf.path;
    let itf_ident = &itf.ident;
    let visibility = &itf.visibility;
    let ts_value_tokens = ts.as_typesystem_tokens(itf.span);
    let ts_type_tokens = ts.as_typesystem_type(itf.span);
    let itf_ref = &itf.itf_ref;
    let vtable_path = itf.vtable(ts);
    let attr_cominterfacevariant = quote_spanned!(itf_ident.span() =>
        intercom::attributes::ComInterfaceVariant<#ts_type_tokens>);
    let attr_cominterfacevtablefor = quote_spanned!(itf_ident.span() =>
        intercom::attributes::ComInterfaceVTableFor<I, S, #ts_type_tokens>);

    // Construct the iid(ts) match arm for this type system.
    itf_output
        .iid_arms
        .push(quote_spanned!(itf.span => #ts_value_tokens => Some( <Self as #attr_cominterfacevariant>::iid() ) ));

    // Create a vector for the virtual table fields and insert the base
    // interface virtual table in it if required.
    let mut vtbl_fields = vec![];
    let mut vtbl_values = vec![];
    if let Some(ref base) = itf.base_interface {
        vtbl_values.push(quote_spanned!(itf.span =>
                __base : <dyn #base as #attr_cominterfacevtablefor>::VTABLE));
        vtbl_fields.push(quote_spanned!(itf.span =>
                pub __base : <dyn #base as #attr_cominterfacevariant>::VTable));
    }

    // Gather all the trait methods for the remaining vtable fields.
    for method_info in &itf_variant.methods {
        // Create the vtable field and add it to the vector of fields.
        let (vtbl_field, vtbl_value) = format_method_vtable_entries(itf, method_info, ts);
        vtbl_fields.push(vtbl_field);
        vtbl_values.push(vtbl_value);

        // Ensure the MethodImpl exists for the method.
        // These are shared by type systems so only one type system needs to
        // add them here.
        let method_name = method_info.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, itf_variant, method_info),
        );

        output.push(create_virtual_method(itf, method_info, ts));
    }

    // Create the vtable. We've already gathered all the vtable method
    // pointer fields so defining the struct is simple enough.
    let itf_bound = match itf.item_type {
        utils::InterfaceType::Struct => quote!(),
        utils::InterfaceType::Trait if itf.implemented_by.is_some() => quote!(),
        utils::InterfaceType::Trait => quote!(+ #itf_ident),
    };
    if itf.vtable_of.is_none() {
        output.push(quote_spanned!(itf.span =>
            #[allow(non_camel_case_types)]
            #[allow(non_snake_case)]
            #[allow(clippy::all)]
            #[repr(C)]
            #[doc(hidden)]
            #[derive(Clone, Copy)]
            #visibility struct #vtable_path { #( #vtbl_fields, )* }

            #[allow(unused)]
            impl<I, S> intercom::attributes::ComInterfaceVTableFor<I, S, #ts_type_tokens> for #itf_ref
            where I: ?Sized,
                  S: intercom::attributes::ComClassInterface<I, #ts_type_tokens> + intercom::attributes::ComClass #itf_bound,
            {
                const VTABLE: #vtable_path = #vtable_path {
                    #( #vtbl_values, )*
                };
            }
        ));
    }

    let iid_tokens = utils::get_guid_tokens(&itf_variant.iid, itf.span);
    output.push(quote_spanned!(itf_path.span() =>
        #[allow(non_camel_case_types)]
        #[allow(non_snake_case)]
        #[allow(clippy::all)]
        #[doc(hidden)]
        impl #attr_cominterfacevariant for #itf_ref {
            type VTable = #vtable_path;
            fn iid() -> &'static intercom::IID {
                & #iid_tokens
            }
        }
    ));
}

/// Creates the functions responsible for delegating calls from Rust to COM
/// interfaces.
///
/// # Arguments
///
/// * `itf` - Interface details.
/// * `itf_variant` - Interface variant details.
/// * `method_info` - Method to delegate.
fn rust_to_com_delegate(
    itf: &model::ComInterface,
    itf_variant: &model::ComInterfaceVariant,
    method_info: &ComMethodInfo,
) -> TokenStream
{
    // The COM out-arguments that mirror the Rust return value will
    // require temporary variables during the COM call. Format their
    // declarations.
    let infallible = method_info.returnhandler.is_infallible();
    let out_arg_declarations = method_info
        .returnhandler
        .com_out_args()
        .iter()
        .map(|ca| {
            let ident = &ca.name;
            let ty = &ca.handler.com_ty(ca.span);
            let default = ca.handler.default_value();
            quote_spanned!(ca.span => let mut #ident : #ty = #default; )
        })
        .collect::<Vec<_>>();

    // Format the in and out parameters for the COM call.
    let params: Vec<_> = method_info
        .raw_com_args()
        .into_iter()
        .map(|com_arg| {
            let name = com_arg.name;
            match com_arg.dir {
                Direction::In => {
                    com_arg
                        .handler
                        .rust_to_com(&name, com_arg.span, Direction::In, infallible)
                }
                Direction::Out | Direction::Retval => quote_spanned!(com_arg.span => &mut #name ),
            }
        })
        .collect();

    // Combine the parameters into the final parameter list.
    // This includes the 'this' pointer and both the IN and OUT
    // parameters.
    let params =
        iter::once(quote_spanned!(method_info.rust_self_arg.span() => comptr.ptr.as_ptr()))
            .chain(params);

    // Create the return statement.
    let return_ident = Ident::new("__result", Span::call_site());
    let return_statement = method_info.returnhandler.com_to_rust_return(&return_ident);

    // Resolve some of the fields needed for quote.
    let method_ident = &method_info.name;
    let return_ty = &method_info.rust_return_ty;
    let iid_tokens = utils::get_guid_tokens(&itf_variant.iid, method_info.signature_span);
    let itf_ref = &itf.itf_ref;
    let ts_type = itf_variant.type_system.as_typesystem_type(itf.span);

    // Construct the final method.
    if infallible {
        quote_spanned!(method_info.signature_span =>
            #[allow(unused_imports)]
            let vtbl = comptr.ptr.as_ptr() as *const *const <#itf_ref as
                intercom::attributes::ComInterfaceVariant<#ts_type>>::VTable;

            #[allow(unused_unsafe)]  // The fn itself _might_ be unsafe.
            unsafe {
                #( #out_arg_declarations )*
                let #return_ident = ((**vtbl).#method_ident)( #( #params ),* );

                let __intercom_iid = #iid_tokens;
                #[allow(unused_braces)]
                return { #return_statement };
            }
        )
    } else {
        quote_spanned!(method_info.signature_span =>
            #[allow(unused_imports)]
            let vtbl = comptr.ptr.as_ptr() as *const *const <#itf_ref as
                intercom::attributes::ComInterfaceVariant<#ts_type>>::VTable;

            // Use an IIFE to act as a try/catch block. The various template
            // substitutions might end up using ?'s for error handling. The IIFE allows
            // us to handle the results here immediately.
            #[allow(unused_unsafe)]  // The fn itself _might_ be unsafe.
            let __intercom_result : Result< #return_ty, intercom::ComError > = ( || unsafe {
                #( #out_arg_declarations )*
                let #return_ident = ((**vtbl).#method_ident)( #( #params ),* );

                let __intercom_iid = #iid_tokens;
                #[allow(unused_braces)]
                Ok( { #return_statement } )
            } )();

            return match __intercom_result {
                Ok( v ) => v,
                Err( err ) => < #return_ty as intercom::ErrorValue >::from_error( err ),
            };
        )
    }
}

fn format_method_vtable_entries(
    itf: &model::ComInterface,
    method_info: &ComMethodInfo,
    ts: ModelTypeSystem,
) -> (TokenStream, TokenStream)
{
    let itf_ident = &itf.ident;
    let method_ident = &method_info.name;
    let method_impl_ident = idents::com_to_rust_method_impl(itf_ident, method_ident, ts);
    let ret_ty = method_info.returnhandler.com_ty();
    let generics = match itf.item_type {
        utils::InterfaceType::Struct => quote!(),
        utils::InterfaceType::Trait => quote!(::<I, S>),
    };
    let params = method_info.get_parameters_tokenstream();

    (
        quote_spanned!(method_info.signature_span =>
            pub #method_ident : unsafe extern "system" fn(#params) -> #ret_ty),
        quote_spanned!(method_info.signature_span =>
            #method_ident: #method_impl_ident #generics),
    )
}

fn create_virtual_method(
    itf: &model::ComInterface,
    method_info: &ComMethodInfo,
    ts: ModelTypeSystem,
) -> TokenStream
{
    let itf_ident = &itf.ident;
    let method_ident = &method_info.name;
    let method_name = method_ident.to_string();
    let method_impl_ident = idents::com_to_rust_method_impl(itf_ident, method_ident, ts);
    let infallible = method_info.returnhandler.is_infallible();
    let itf_ident = &itf.ident;
    let itf_path = &itf.path;
    let ts_type_tokens = ts.as_typesystem_type(itf.span);
    let params = method_info.get_parameters_tokenstream();
    let attr_comclassinterface = quote!(intercom::attributes::ComClassInterface);

    // Format the in and out parameters for the Rust call.
    let in_args: Vec<_> = method_info
        .args
        .iter()
        .map(|ca| {
            ca.handler
                .com_to_rust(&ca.name, ca.span, Direction::In, infallible)
        })
        .collect();

    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();

    // Figure out how to get the self struct reference.
    let self_struct_expr = if itf.implemented_by.is_some() {
        quote!(&*self_combox)
    } else if method_info.is_const {
        quote!(&**self_combox)
    } else {
        quote!(&mut **self_combox)
    };

    // The implemented_by option affects the actual method implementation
    // as well as the interface bounds. Interfaces implemented manually
    // do not require the interface as a bound.
    let (required_itf, call) = match &itf.implemented_by {
        Some(path) => (
            quote!(),
            quote!(#path::#method_ident(self_struct, #( #in_args ),*)),
        ),
        None => (
            quote!(+ #itf_ident),
            quote!(self_struct.#method_ident( #( #in_args ),* )),
        ),
    };

    // Since "+ Struct" is an invalid bound and wouldn't really make sense
    // anywya, we won't use generic parameters on struct impl based implicit
    // interfaces.
    let (generics, bounds, s_ref, i_ref) = match itf.item_type {
        utils::InterfaceType::Struct => (quote!(), quote!(), quote!(#itf_path), quote!(#itf_path)),
        utils::InterfaceType::Trait => (
            quote!(<I, S>),
            quote!(
                where I: ?Sized,
                      S: #attr_comclassinterface<I, #ts_type_tokens> + intercom::attributes::ComClass #required_itf
            ),
            quote!(S),
            quote!(I),
        ),
    };

    // Format the payload depending on whether the method is infallible or not.
    let payload = if infallible {
        quote!(
            let self_struct = #self_struct_expr;
            let #return_ident = #call;

            intercom::logging::trace(|l| l(module_path!(), format_args!(
                "[{:p}, through {:p}] Serving {}::{}, OK",
                self_combox, self_vtable, std::any::type_name::<#s_ref>(), #method_name)));

            #return_statement
        )
    } else {
        // Fallible methods require an error-catching closure and error handling.
        quote!(
            let result : Result< #ret_ty, intercom::ComError > = ( || {
                let self_struct = #self_struct_expr;
                let #return_ident = #call;
                Ok( { #return_statement } )
            } )();

            match result {
                Ok( v ) => {
                    intercom::logging::trace(|l| l(module_path!(), format_args!(
                        "[{:p}, through {:p}] Serving {}::{}, OK",
                        self_combox, self_vtable,
                        std::any::type_name::<#s_ref>(), #method_name)));
                    v
                },
                Err( err ) => {
                    intercom::logging::trace(|l| l(module_path!(), format_args!(
                        "[{:p}, through {:p}] Serving {}::{}, ERROR",
                        self_combox, self_vtable,
                        std::any::type_name::<#s_ref>(), #method_name)));
                    <#ret_ty as intercom::ErrorValue>::from_error(
                        intercom::store_error(err))
                },
            }
        )
    };

    quote!(
        #[allow(non_snake_case)]
        #[allow(dead_code)]
        #[doc(hidden)]
        unsafe extern "system" fn #method_impl_ident #generics(
            #params
        ) -> #ret_ty
        #bounds
        {
            // Acquire the reference to the ComBoxData. For this we need
            // to offset the current 'self_vtable' vtable pointer.
            let offset = <#s_ref as #attr_comclassinterface<#i_ref, #ts_type_tokens>>::offset();
            let self_combox = ( self_vtable as usize - offset )
                    as *mut intercom::ComBoxData<#s_ref>;

            intercom::logging::trace(|l| l(module_path!(), format_args!(
                "[{:p}, through {:p}] Serving {}::{}",
                self_combox, self_vtable,
                std::any::type_name::<#s_ref>(), #method_name)));

            #payload
        }
    )
}

fn create_get_typeinfo_function(itf: &model::ComInterface) -> TokenStream
{
    let itf_name = itf.ident.to_string();
    let itf_ref = &itf.itf_ref;
    let mut variant_tokens = vec![];
    for (ts, variant) in &itf.variants {
        variant_tokens.push(create_typeinfo_for_variant(itf, *ts, variant));
    }
    let is_impl_interface = itf.item_type == utils::InterfaceType::Struct;

    quote_spanned!(itf.span =>
        #[allow(non_snake_case)]
        #[allow(dead_code)]
        impl intercom::attributes::ComInterfaceTypeInfo for #itf_ref
        {
            fn gather_type_info() -> Vec<intercom::typelib::TypeInfo>
            {
                let variants = vec![ #( #variant_tokens ),* ];

                vec![ intercom::typelib::TypeInfo::Interface(
                    intercom::ComBox::new( intercom::typelib::Interface {
                        name: #itf_name.into(),
                        variants,
                        options: intercom::typelib::InterfaceOptions {
                            class_impl_interface: #is_impl_interface,
                            ..Default::default()
                        }
                    })
                ) ]
            }
        }
    )
}

fn create_typeinfo_for_variant(
    itf: &model::ComInterface,
    ts: ModelTypeSystem,
    itf_variant: &model::ComInterfaceVariant,
) -> TokenStream
{
    let ts_tokens = ts.as_typesystem_tokens(itf.span);
    let ts_type = ts.as_typesystem_type(itf.span);
    let iid_tokens = utils::get_guid_tokens(&itf_variant.iid, itf.span);
    let methods = itf_variant.methods.iter().map( |m| {
        let method_name = m.name.to_string();
        let return_type = match &m.return_type {
            Some(rt) => quote_spanned!(m.signature_span =>
                intercom::typelib::Arg {
                    name: "".into(),
                    ty: <
                        <#rt as intercom::type_system::ExternType<#ts_type>>::ForeignType
                        as intercom::type_system::ForeignType>::type_name().into(),
                    indirection_level: <
                        <#rt as intercom::type_system::ExternType<#ts_type>>::ForeignType
                        as intercom::type_system::ForeignType>::indirection_level(),
                    direction: intercom::typelib::Direction::Return,
                }),
            None => quote_spanned!(m.signature_span => intercom::typelib::Arg {
                name: "".into(),
                ty: "void".into(),
                indirection_level: 0,
                direction: intercom::typelib::Direction::Return,
            } ),
        };

        let params = m.raw_com_args().into_iter().map(|arg| {
            let com_ty = arg.handler.com_ty(arg.span);
            let arg_name = arg.name.to_string();
            let dir_ident = Ident::new(match arg.dir {
                Direction::In => "In",
                Direction::Out => "Out",
                Direction::Retval => "Retval"
            }, arg.span);

            quote_spanned!(arg.span => intercom::typelib::Arg {
                name: #arg_name.into(),
                ty: <#com_ty as intercom::type_system::ForeignType>::type_name().into(),
                indirection_level: <#com_ty as intercom::type_system::ForeignType>::indirection_level(),
                direction: intercom::typelib::Direction::#dir_ident,
            })
        }).collect::<Vec<_>>();

        quote_spanned!(m.signature_span =>
            intercom::ComBox::new(intercom::typelib::Method {
                name: #method_name.into(),
                return_type: #return_type,
                parameters: vec![ #( #params ),* ],
            })
        )
    }).collect::<Vec<_>>();

    quote_spanned!(itf.span =>
        intercom::ComBox::new( intercom::typelib::InterfaceVariant {
            ts: #ts_tokens,
            iid: #iid_tokens,
            methods: vec![ #( #methods ),* ],
        })
    )
}