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
//!
//! COM library parse model.
//!
//! Defines the items constructed from the various COM attributes.
//!
//! Should unify COM attribute expansion and crate parsing for IDL/Manifest/etc.
//! purposes in the future.
//!

use super::*;

use ::ast_converters::*;
use ::builtin_model;
use ::std::path::{Path, PathBuf};
use ::std::fs;
use ::std::io::Read;
use ::ordermap::OrderMap;
use ::std::iter::FromIterator;
use toml;

#[derive(Debug, PartialEq)]
pub struct ComCrate {
    lib : Option<ComLibrary>,
    interfaces : OrderMap<String, ComInterface>,
    interfaces_by_variants : OrderMap<String, String>,
    structs : OrderMap<String, ComStruct>,
    impls : Vec<ComImpl>,
    incomplete : bool,
}

#[derive(Default)]
struct ComCrateBuilder {
    pub libs : Vec<ComLibrary>,
    pub interfaces : Vec<ComInterface>,
    pub structs : Vec<ComStruct>,
    pub impls : Vec<ComImpl>,
    pub incomplete : bool,
}

impl ComCrateBuilder {

    pub fn build( self ) -> ParseResult<ComCrate>
    {
        if self.libs.len() > 1 {
            return Err( ParseError::ComLibrary(
                    "Multiple [com_library] attributes".into() ) );
        }

        let interfaces_by_variants = {
            OrderMap::from_iter( self.interfaces.iter()
                    .flat_map( |itf| itf.variants().iter().map( |(_, itf_variant)|
                         ( itf_variant.unique_name().to_string(),
                            itf.name().to_string() ) ).collect::<Vec<_>>() ) )
        };

        Ok( ComCrate {
            lib: self.libs.into_iter().next(),
            interfaces: OrderMap::from_iter(
                self.interfaces.into_iter().map( |i| ( i.name().to_string(), i ) ) ),
            interfaces_by_variants,
            structs: OrderMap::from_iter(
                self.structs.into_iter().map( |i| ( i.name().to_string(), i ) ) ),
            impls: self.impls,
            incomplete: self.incomplete,
        } )
    }

    pub fn include_builtin( &mut self, crate_name : &str ) {

        let built_in_types = builtin_model::builtin_intercom_types( crate_name );
        for bti in built_in_types {
            self.structs.push( bti.class );
            self.interfaces.push( bti.interface );
            self.impls.push( bti.implementation );
        }

        let built_in_types = builtin_model::builtin_intercom_types( crate_name );
        for lib in &mut self.libs {
            for clsid in built_in_types.iter().filter_map( |bti|
                    if bti.class.clsid().is_some() {
                        Some( bti.class.name().clone() )
                    } else {
                        None
                    } ) {

                lib.add_coclass( parse_quote!( ::intercom::#clsid ) )
            }
        }
    }
}

impl ComCrate
{
    pub fn parse(
        crate_name : &str,
        sources : &[&str]
    ) -> ParseResult<ComCrate>
    {
        let mut builder : ComCrateBuilder = Default::default();

        for src in sources {
            let krate : ::syn::File = ::syn::parse_str( src )
                .map_err( |_| ParseError::ComCrate(
                        "Failed to parse source".into() ) )?;

            Self::process_crate_items(
                crate_name,
                None,
                &krate.items,
                &mut builder )?;
        }

        builder.include_builtin( crate_name );
        builder.build()
    }

    pub fn parse_package(
        crate_path : &Path,
    ) -> ParseResult<ComCrate>
    {
        if crate_path.is_file() {
            Self::parse_cargo_toml( crate_path )
        } else {
            Self::parse_cargo_toml( &crate_path.join( "Cargo.toml" ) )
        }
    }

    pub fn parse_cargo_toml(
        toml_path : &Path,
    ) -> ParseResult<ComCrate>
    {
        let mut f = fs::File::open( toml_path )
                .map_err( |_| ParseError::CargoToml(
                        "Could not open Cargo toml".into() ) )?;
        let mut buf = String::new();
        f.read_to_string( &mut buf )
                .map_err( |_| ParseError::CargoToml(
                        "Could not read Cargo toml".into() ) )?;

        let toml = buf.parse::<toml::Value>()
                .map_err( |_| ParseError::CargoToml(
                        "Could not parse Cargo toml".into() ) )?;
        let root = match toml {
            toml::Value::Table( root ) => root,
            _ => return Err( ParseError::CargoToml(
                        "Invalid TOML root element".into() ) ),
        };

        let lib_name = match root.get( "package" ) {
                    Some( &toml::Value::Table( ref package ) )
                        => match package.get( "name" ) {
                            Some( &toml::Value::String( ref name ) )
                                => name,
                            _ => return Err( ParseError::CargoToml(
                                    "No 'name' parameter under [package]".into() ) ),
                        },
                    _ => return Err( ParseError::CargoToml(
                            "Could not find [package] in Cargo.toml".into() ) ),
                };

        let rel_lib_path = PathBuf::from( &match root.get( "lib" ) {
                    Some( &toml::Value::Table( ref package ) )
                        => match package.get( "path" ) {
                            Some( &toml::Value::String( ref path ) )
                                => path.clone(),
                            _ => "src/lib.rs".to_owned(),
                        },
                    _ => "src/lib.rs".to_owned(),
                } );
        let lib_path = match toml_path.parent() {
                    Some( p ) => p.join( rel_lib_path ),
                    _ => rel_lib_path
                };

        Self::parse_file( lib_name, &lib_path )
    }

    pub fn parse_file(
        crate_name : &str,
        path : &Path
    ) -> ParseResult<ComCrate>
    {
        let mut builder : ComCrateBuilder = Default::default();

        Self::parse_file_internal( crate_name, path, &mut builder )?;

        builder.include_builtin( crate_name );
        builder.build()
    }

    fn parse_file_internal(
        crate_name : &str,
        path : &Path,
        b : &mut ComCrateBuilder
    ) -> ParseResult<()>
    {
        let mut f = fs::File::open( path )
                .map_err( |_| ParseError::ComCrate(
                        format!( "Could not open file {}", path.to_string_lossy() ) ) )?;

        let mut buf = String::new();
        f.read_to_string( &mut buf )
                .map_err( |_| ParseError::ComCrate(
                        format!( "Could not read file {}", path.to_string_lossy() ) ) )?;

        let krate = ::syn::parse_file( &buf )
                .map_err( |_| ParseError::ComCrate(
                        format!( "Failed to parse source {}", path.to_string_lossy() ) ) )?;

        Self::process_crate_items( crate_name, Some( path ), &krate.items, b )
    }

    fn process_crate_items(
        crate_name : &str,
        path : Option< &Path >,
        items : &[ ::syn::Item ],
        b : &mut ComCrateBuilder,
    ) -> ParseResult<()>
    {
        Self::collect_items( crate_name, items, b )?;

        for item in items {
            let mod_item =
                    if let ::syn::Item::Mod( ref m ) = *item {
                        m
                    } else {
                        continue;
                    };

            match mod_item.content {
                None => {

                    // The mod doesn't have immediate items so this is an
                    // external mod. We need to resolve the file.
                    let path = if let Some( p ) = path { p } else {

                        // No path given. Mark the crate as incomplete as we
                        // couldn't resolve all pieces but return with Ok
                        // result.
                        //
                        // This is a case where we were given file contents
                        // without the caller knowing (or telling) where the
                        // file was located. We can't resolve relative mod-paths
                        // in this case.
                        b.incomplete = true;
                        return Ok(());
                    };

                    // We have couple of options. Find the first one that
                    // matches an existing file.
                    let mut mod_paths = vec![
                        path.parent().unwrap().join( format!( "{}.rs", mod_item.ident ) ),
                        path.parent().unwrap().join( format!( "{}/mod.rs", mod_item.ident ) ),
                    ].into_iter()
                        .filter( |p| p.exists() );

                    let mod_path = mod_paths.next()
                        .ok_or_else( || ParseError::ComCrate(
                                format!( "Could not find mod {}", mod_item.ident ) ) )?;

                    let more = mod_paths.next();
                    if more.is_some() {
                        return Err( ParseError::ComCrate(
                                format!( "Ambiguous mod, both {0}.rs and \
                                          {0}/mod.rs present", mod_item.ident ) ) );
                    }

                    Self::parse_file_internal( crate_name, &mod_path, b )?;
                },
                Some( ( _, ref mod_items ) )
                    => Self::process_crate_items( crate_name, path, mod_items, b )?
            }
        }

        Ok(())
    }

    fn collect_items(
        crate_name : &str,
        items : &[ ::syn::Item ],
        b : &mut ComCrateBuilder,
    ) -> ParseResult<()>
    {
        for item in items {

            // Test for 'com_library' macro.
            if let ::syn::Item::Macro( m ) = item {
                if let Ok( ref ident ) = m.mac.path.get_ident() {
                    if ident == "com_library" {
                        b.libs.push(ComLibrary::parse(crate_name, m.mac.tts.clone())?);
                    }
                }
            }

            // The rest of the items we're interested in are attributes.
            for attr in &item.get_attributes().unwrap() {
                match attr.path.get_ident().unwrap().to_string().as_ref() {
                    "com_interface" =>
                        b.interfaces.push( ComInterface::from_ast(
                                crate_name, attr.tts.clone(), item )? ),
                    "com_class" =>
                        if let ::syn::Item::Struct( ref s ) = *item {
                            b.structs.push( ComStruct::from_ast(
                                    crate_name, attr.tts.clone(), s )? )
                        } else {
                            return Err( ParseError::ComStruct(
                                    item.get_ident().unwrap().to_string(),
                                    "Only structs may be COM classes".to_string() ) );
                        },
                    "com_impl" =>
                        b.impls.push( ComImpl::from_ast( item )? ),
                    _ => { }
                }
            }
        }

        Ok(())
    }


    pub fn lib( &self ) -> &Option<ComLibrary> { &self.lib }
    pub fn interfaces( &self ) -> &OrderMap<String, ComInterface> { &self.interfaces }
    pub fn structs( &self ) -> &OrderMap<String, ComStruct> { &self.structs }
    pub fn impls( &self ) -> &Vec<ComImpl> { &self.impls }
    pub fn is_incomplete( &self ) -> bool { self.incomplete }

    pub fn interface_by_name( &self, name : &str ) -> Option<&ComInterface> {
        self.interfaces_by_variants
                .get( name )
                .and_then( |itf_name| self.interfaces.get( itf_name ) )
                .or_else( || self.interfaces.get( name ) )
    }
}

#[cfg(test)]
mod test
{
    use super::*;
    use guid::GUID;
    use tyhandlers::ModelTypeSystem::*;

    #[test]
    fn parse_crate() {
        let krate = ComCrate::parse( "my_crate", &[
            r#"
                com_library!(
                        libid = "12345678-1234-1234-1234-567890000000",
                        Foo, Bar );

                #[com_interface(
                        com_iid = "12345678-1234-1234-1234-567890000001",
                        raw_iid = "12345678-1234-1234-1234-567890000002",
                )]
                trait IFoo {}

                trait IBar {}
            "#,
            r#"
                #[com_class(
                        clsid = "12345678-1234-1234-1234-567890000003",
                        IFoo )]
                struct S;

                #[com_impl]
                impl IFoo for S {}
            "#
        ] ).expect( "Parsing the crate failed" );

        assert!( krate.lib.is_some() );
        assert_eq!( krate.lib.as_ref().unwrap().libid(),
            &GUID::parse( "12345678-1234-1234-1234-567890000000" ).unwrap() );

        // The interfaces should contain the built-in interface.
        assert_eq!( krate.interfaces().len(), 3 );

        assert_eq!( krate.interfaces()[ "IFoo" ].variants()[ &Automation ].iid(),
            &GUID::parse( "12345678-1234-1234-1234-567890000001" ).unwrap() );
        assert_eq!( krate.interfaces()[ "IFoo" ].variants()[ &Raw ].iid(),
            &GUID::parse( "12345678-1234-1234-1234-567890000002" ).unwrap() );

        assert_eq!( krate.interfaces()[ "Allocator" ].variants()[ &Automation ].iid(),
            &GUID::parse( "18EE22B3-B0C6-44A5-A94A-7A417676FB66" ).unwrap() );
        assert_eq!( krate.interfaces()[ "Allocator" ].variants()[ &Raw ].iid(),
            &GUID::parse( "7A6F6564-04B5-4455-A223-EA0512B8CC63" ).unwrap() );

        assert_eq!( krate.interfaces()[ "ErrorStore" ].variants()[ &Automation ].iid(),
            &GUID::parse( "D7F996C5-0B51-4053-82F8-19A7261793A9" ).unwrap() );
        assert_eq!( krate.interfaces()[ "ErrorStore" ].variants()[ &Raw ].iid(),
            &GUID::parse( "7586C49A-ABBD-4A06-B588-E3D02B431F01" ).unwrap() );

        assert_eq!( krate.structs().len(), 3 );
        assert_eq!( krate.structs()[ "S" ].clsid().as_ref().unwrap(),
            &GUID::parse( "12345678-1234-1234-1234-567890000003" ).unwrap() );
        assert_eq!( krate.structs()[ "Allocator" ].clsid().as_ref().unwrap(),
            &GUID::parse( "1582F0E9-9CAB-3E18-7F37-0CF2CD9DA33A" ).unwrap() );
        assert_eq!( krate.structs()[ "ErrorStore" ].clsid().as_ref().unwrap(),
            &GUID::parse( "1CC4A0E8-C882-37B4-53FA-BC9E6030DF56" ).unwrap() );

        assert_eq!( krate.impls().len(), 3 );
        assert_eq!( krate.impls()[0].struct_name(), "S" );
        assert_eq!( krate.impls()[0].interface_name(), "IFoo" );
        assert_eq!( krate.impls()[1].struct_name(), "Allocator" );
        assert_eq!( krate.impls()[1].interface_name(), "Allocator" );
        assert_eq!( krate.impls()[2].struct_name(), "ErrorStore" );
        assert_eq!( krate.impls()[2].interface_name(), "ErrorStore" );
    }

    #[test]
    fn parse_incomplete_crate() {
        let krate = ComCrate::parse( "my_crate", &[
            r#"
                mod foo;
            "#,
        ] ).expect( "Parsing the crate failed" );

        assert!( krate.is_incomplete() );
    }
}