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

use super::utils;

use syn::*;
use quote::Tokens;

/// Defines Type-specific logic for handling the various parameter types in the
/// Rust/COM interface.
pub trait TyHandler {

    /// The Rust type.
    fn rust_ty( &self ) -> Ty;

    /// The COM type.
    fn com_ty( &self ) -> Ty
    {
        self.rust_ty()
    }

    /// Converts a COM parameter named by the ident into a Rust type.
    fn com_to_rust(
        &self, ident : &Ident
    ) -> Tokens
    {
        quote!( #ident.into() )
    }

    /// Converts a Rust parameter named by the ident into a COM type.
    fn rust_to_com(
        &self, ident : &Ident
    ) -> Tokens
    {
        quote!( #ident.into() )
    }

    /// Gets the default value for the type.
    fn default_value( &self) -> Tokens
    {
        match self.rust_ty() {
            Ty::Path( _, ref p ) => {
                let name : &str = p.segments.last().unwrap().ident.as_ref();
                match name {
                    "c_void"
                        | "RawComPtr"
                        | "ComRc"
                        => quote!( ::std::ptr::null_mut() ),
                    _ => quote!( Default::default() )
                }
            },
            _ => quote!( Default::default() )
        }
    }
}

/// Identity parameter handler.
///
/// No special logic.
struct IdentityParam( Ty );

impl TyHandler for IdentityParam {
    fn rust_ty( &self ) -> Ty { self.0.clone() }
}


/// `ComRc` parameter handler. Supports `ComRc` Rust type and converts this
/// to/from `RawComPtr` COM type.
struct ComRcParam( Ty );

impl TyHandler for ComRcParam {

    fn rust_ty( &self ) -> Ty { self.0.clone() }

    fn com_ty( &self ) -> Ty
    {
        parse_type( "::intercom::RawComPtr" ).unwrap()
    }

    fn rust_to_com( &self, ident : &Ident ) -> Tokens
    {
        // Get the parameter data from the type.
        let comrc_params = match self.0 {
            Ty::Path( _, ref p ) => {
                let last_segment = &p.segments.last().unwrap();
                match last_segment.parameters {
                    PathParameters::AngleBracketed( ref data ) => data,
                    _ => panic!( "ComRc doesn't have <> params" ),
                }
            }
            _ => unreachable!( "ComRcParam should only be used for Ty::Path" ),
        };

        // Get the interface type.
        let itf_ty = match comrc_params.types.first() {
            Some( ty ) => ty,
            _ => panic!( "ComRc doesn't have type parameters" ),
        };

        // Name the interface.
        let itf_ident = match utils::get_ty_ident( itf_ty ) {
            Some( ty_ident ) => ty_ident,
            _ => panic!( "Could not resolve name of {:?}", itf_ty ),
        };

        // Conversion is done with query_interface, which requires the
        // IID of the ComRc interface type.
        let iid_ident = super::idents::iid( itf_ident );
        quote!( ::intercom::ComRc::query_interface( &#ident, &#iid_ident )
                .expect( "ComRc<T> does not support interface T" ) )
    }
}

/// String parameter handler. Converts between Rust String and COM BSTR types.
struct StringParam( Ty );
impl TyHandler for StringParam
{
    fn rust_ty( &self ) -> Ty { self.0.clone() }

    fn com_ty( &self ) -> Ty
    {
        parse_type( "::intercom::BStr" ).unwrap()
    }

    fn com_to_rust( &self, ident : &Ident ) -> Tokens
    {
        quote!( #ident.into() )
    }

    fn rust_to_com( &self, ident : &Ident ) -> Tokens
    {
        quote!( #ident.into() )
    }
}

/// Resolves the `TyHandler` to use.
pub fn get_ty_handler(
    arg_ty : &Ty,
) -> Box<TyHandler>
{
    // The ParamHandler needs an owned Ty so clone it here.
    let ty = arg_ty.clone();

    // The match is done using the original ty so we can borrow it while we
    // yield ownership to the cloned 'ty'.
    match *arg_ty {

        // Ty::Path represents various qualified type names, such as structs
        // and traits.
        Ty::Path( .., ref p ) => {

            // Match based on the last segment. We can't rely on the fully
            // qualified name to be in the previous segments thanks to use-
            // statements.
            let name : &str = p.segments.last().unwrap().ident.as_ref();
            match name {

                "ComRc" => Box::new( ComRcParam( ty ) ),
                "String" => Box::new( StringParam( ty ) ),

                // Unknown. Use IdentityParam.
                _ => Box::new( IdentityParam( ty ) )
            }
        },

        // Default to identity param.
        _ => Box::new( IdentityParam( ty ) )
    }
}