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
extern crate std;

use std::rc::Rc;
use syn;
use syn::TypeParamBound;

/// Detailed information of a Rust type.
#[derive(Clone, Debug)]
pub enum RustType<'e>
{
    Ident( &'e syn::Ident ),
    Void,
    Wrapper( &'e syn::Ident, Rc< TypeInfo<'e> > ),
}

/// Defines how a value is passed to/from Rust function.
#[derive(PartialEq, PartialOrd, Clone, Copy, Debug)]
pub enum PassBy
{
    Value,

    Reference,

    Ptr,
}

/// Details of a Rust type. Used for translation to other programming languages.
#[derive(Clone, Debug)]
pub struct TypeInfo<'s>
{
    /// The type in rust.
    pub rust_type: RustType<'s>,

    /// Specifies how the value is passed to/from a function. Default: PassBy::Value
    pub pass_by: PassBy,

    /// Is the Rust type defined as mutable? Default: false
    pub is_mutable: bool,

    /// The length of the array if the Rust type denotes an array.
    pub array_length: Option<&'s syn::Expr>,

    /// Reference to the original type that this type info represents.
    pub original: &'s syn::Type,
}

pub fn parse<'a, 'b: 'a>(
        ty: &'b syn::Type,
) -> Option<TypeInfo<'a>>
{
    let resolver = TypeInfoResolver::from_type( ty )?;

    Some( TypeInfo::from_resolver( resolver, ty ) )
}

/// Collects details of a Rust type when the Rust crate is parsed.
struct TypeInfoResolver<'s>
{
    /// The type in rust.
    rust_type: RustType<'s>,

    /// Specifies how the value is passed. Default: PassBy::Value
    pass_by: Option<PassBy>,

    /// Is the Rust type defined as mutable? Default: false
    is_mutable: Option<bool>,

    /// The length of the array if the Rust type denotes an array.
    array_length: Option<&'s syn::Expr>
}


impl<'s, 'p: 's> TypeInfo<'s> {

    /// Gets the name of the type in Rust.
    pub fn get_name(
        &self
    ) -> String
    {
        // Exclude the wrappee from the name.
        match self.rust_type {
            RustType::Wrapper( wrapper, _ ) => format!( "{}", wrapper ),
            ref t => format!( "{}", t ),
        }
    }

    /// Returns the nested type info or self.
    pub fn get_leaf(
        &self
    ) -> &TypeInfo<'s>
    {
        match self.rust_type {
            RustType::Wrapper( _, ref wrappee ) => wrappee.get_leaf(),
            _ => self
        }
    }

    /// Initializes the type info from resolver which has resolved the type.
    fn from_resolver(
        resolver: TypeInfoResolver<'p>,
        original: &'p syn::Type,
    ) -> TypeInfo<'s>
    {
        // Resolve default values.
        // NOTE: The existence of the array length value identifies an array type and
        // is therefor passed as-is here.
        let pass_by = resolver.pass_by.unwrap_or( PassBy::Value );
        let is_mutable = resolver.is_mutable.unwrap_or( false );

        TypeInfo{
            rust_type: resolver.rust_type,
            pass_by,
            is_mutable,
            array_length: resolver.array_length,
            original,
        }
    }


}

impl<'s> std::fmt::Display for RustType<'s> {

    fn fmt(
        &self,
        f: &mut std::fmt::Formatter
    ) -> std::fmt::Result {
        match *self {
            RustType::Ident( syn_ident ) => write!( f, "{}", syn_ident ),
            RustType::Void => write!( f, "void" ),
            RustType::Wrapper( wrapper, ref wrapped ) => write!( f, "{}<{}>",
                    wrapper, wrapped.rust_type )
        }
    }
}

impl<'s, 'p: 's> TypeInfoResolver<'s> {

    /// Parses the type info from the specified Type.
    fn from_type(
        syn_type: &'p syn::Type,
    ) -> Option<TypeInfoResolver<'s>>
    {
        match *syn_type {

            // Delegate to appropriate conversion.
            syn::Type::Slice( ref slice ) => TypeInfoResolver::from_slice( slice ),
            syn::Type::Reference( ref reference ) => TypeInfoResolver::from_reference( reference ),
            syn::Type::Ptr( ref ptr ) => TypeInfoResolver::from_pointer( ptr ),
            syn::Type::Array( ref arr ) => TypeInfoResolver::from_array( arr ),
            syn::Type::Path( ref p ) => TypeInfoResolver::from_path( p ),
            syn::Type::Tuple( ref t ) if t.elems.is_empty() => Some( TypeInfoResolver::void() ),
            syn::Type::TraitObject( ref trait_object ) =>
                    TypeInfoResolver::from_trait_object( trait_object ),

            syn::Type::BareFn(..)
                | syn::Type::Never(..)
                | syn::Type::Tuple(..)
                | syn::Type::ImplTrait(..)
                | syn::Type::Paren(..)
                | syn::Type::Infer(..)
                | syn::Type::Macro(..)
                | syn::Type::Verbatim(..)
                | syn::Type::Group(..)
                => { dbg!( syn_type ); None },
        }
    }

    fn new(
        rust_type: RustType<'s>
    ) -> TypeInfoResolver<'s>
    {
        TypeInfoResolver {
            rust_type,
            pass_by: None,
            is_mutable: None,
            array_length: None,
        }
    }

    fn void() -> TypeInfoResolver<'s>
    {
        TypeInfoResolver::new( RustType::Void )
    }

    fn pass_by(
        resolver: TypeInfoResolver<'s>,
        pass_by: PassBy,
    ) -> TypeInfoResolver<'s>
    {
        if resolver.pass_by.is_some() {
            panic!("Cannot set pass_by twice.")
        }

        TypeInfoResolver {
            rust_type: resolver.rust_type,
            pass_by: Some( pass_by ),
            is_mutable: resolver.is_mutable,
            array_length: resolver.array_length,
        }
    }

    fn mutable(
        resolver: TypeInfoResolver<'s>,
        is_mutable: bool,
    ) -> TypeInfoResolver<'s>
    {
        if resolver.is_mutable.is_some() {
            panic!("Cannot set is_mutable twice.")
        }

        TypeInfoResolver {
            rust_type: resolver.rust_type,
            pass_by: resolver.pass_by,
            is_mutable: Some( is_mutable ),
            array_length: resolver.array_length,
        }
    }

    fn array(
        resolver: TypeInfoResolver<'s>,
        array_length: &'p syn::Expr,
    ) -> TypeInfoResolver<'s>
    {
        if resolver.array_length.is_some() {
            panic!("Cannot set array_length twice.")
        }

        TypeInfoResolver {
            rust_type: resolver.rust_type,
            pass_by: resolver.pass_by,
            is_mutable: resolver.is_mutable,
            array_length: Some( array_length ),
        }
    }

    fn wrapped(
        resolver: &TypeInfoResolver<'s>,
        args: &'p syn::punctuated::Punctuated< syn::GenericArgument, syn::token::Comma >,
    ) -> Option<TypeInfoResolver<'s>>
    {
        if let RustType::Wrapper( _, _ ) = resolver.rust_type {
            panic!("Nested wrappers are not allowed.")
        }

        // Determine the TypeInfo of the nested type.
        let nested_type = match **args.first().unwrap().value() {
                                    syn::GenericArgument::Type( ref t ) => t,
                                    _ => return None,
                            };
        let nested_type = TypeInfo::from_resolver(
                        TypeInfoResolver::from_type( nested_type )?, nested_type );

        Some( TypeInfoResolver {
            rust_type: RustType::Wrapper(
                    resolver.get_ident_for_wrapping(), Rc::new( nested_type ) ),
            pass_by: resolver.pass_by,
            is_mutable: resolver.is_mutable,
            array_length: resolver.array_length,
        } )
    }

    fn from_array(
        array: &'p syn::TypeArray,
    ) -> Option<TypeInfoResolver<'s>>
    {
        let resolver = TypeInfoResolver::from_type( &array.elem )?;

        Some( TypeInfoResolver::array( resolver, &array.len ) )
    }

    fn from_slice(
        slice: &'p syn::TypeSlice,
    ) -> Option<TypeInfoResolver<'s>>
    {
        TypeInfoResolver::from_type( &slice.elem )
    }

    fn from_reference(
        reference : &'p syn::TypeReference,
    ) -> Option<TypeInfoResolver<'s>>
    {
        let resolver = TypeInfoResolver::from_type( &reference.elem )?;
        let resolver = TypeInfoResolver::mutable( resolver,
                TypeInfoResolver::is_mutable( reference.mutability ) );

        Some( TypeInfoResolver::pass_by( resolver, PassBy::Reference ) )
    }

    fn from_pointer(
        ptr : &'p syn::TypePtr,
    ) -> Option<TypeInfoResolver<'s>>
    {
        let resolver = TypeInfoResolver::from_type( &ptr.elem )?;
        let resolver = TypeInfoResolver::mutable( resolver,
                TypeInfoResolver::is_mutable( ptr.mutability ) );

        Some( TypeInfoResolver::pass_by( resolver, PassBy::Ptr ) )
    }

    fn from_path(
        type_path: &'p syn::TypePath,
    ) -> Option<TypeInfoResolver<'s>>
    {
        TypeInfoResolver::from_segment( type_path.path.segments.last().unwrap().value() )
    }

    fn from_segment(
        segment: &'p syn::PathSegment,
    ) -> Option<TypeInfoResolver<'s>>
    {
        // Get the segment as a string.
        let rust_type = format!( "{}", segment.ident );

        // Get the type information.
        let args = match segment.arguments {
            syn::PathArguments::None
                    => None,

            syn::PathArguments::AngleBracketed( ref data )
                    => Some( &data.args ),

            // Parenthesized path parameters should be valid only for Fn-types.
            // These types are unsupported, but we'll match for them here anyway.
            syn::PathArguments::Parenthesized( .. )
                    => panic!( "Fn-types are unsupported." ),
        };

        match rust_type.as_str() {

            // Extract a wrapped type.
            "ComRc" | "ComItf" | "ComResult" | "InterfacePtr"
                => TypeInfoResolver::wrapped(
                        &TypeInfoResolver::new( RustType::Ident( &segment.ident ) ),
                        args.expect( "Wrapper types requires valid wrappee.")
                    ),

            // Bare type.
            _t => Some( TypeInfoResolver::new( RustType::Ident( &segment.ident ) ) ),
        }
    }

    /// Resolves the type from a trait object.
    fn from_trait_object(
        trait_object: &'p syn::TypeTraitObject,
    ) -> Option<TypeInfoResolver<'s>>
    {
        // Find the first actual trait. Fro example lifetime parameters are ignored.
        let trait_bound = trait_object.bounds.iter().find_map( |parameter: &TypeParamBound|
                                                 if let syn::TypeParamBound::Trait( ref tr ) = parameter  { Some( tr ) }
                                                 else { None } )?;
        TypeInfoResolver::from_segment( trait_bound.path.segments.last().unwrap().value() )
    }

    /// Determines if the given type is mutable
    fn is_mutable(
        mutability: Option<syn::token::Mut>
    ) -> bool
    {
        mutability.is_some()
    }

    /// Gets the identifier used to wrap another type.
    fn get_ident_for_wrapping(
        &self
    ) -> &'s syn::Ident
    {
        match self.rust_type
        {
            RustType::Ident( ident ) => ident,
            _ => panic!( "Only identifiers can wrap other rust types." ),
        }
    }
}