X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=c-bindings-gen%2Fsrc%2Fmain.rs;h=f8475bbdcf958ce40f90d57c16e0c3cbb6e3d41d;hb=2462e4306349623c9165ed0d36d7e9cd9115cfcb;hp=6d6296c8f48e93dc661924cf08c239694c74896b;hpb=c6d26977033f081007916a07b5a80d1d24138061;p=rust-lightning diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index 6d6296c8..f8475bbd 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -10,11 +10,10 @@ //! It also generates relevant memory-management functions and free-standing functions with //! parameters mapped. -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; use std::env; use std::fs::File; use std::io::{Read, Write}; -use std::path::Path; use std::process; use proc_macro2::{TokenTree, TokenStream, Span}; @@ -38,7 +37,7 @@ fn convert_macro(w: &mut W, macro_path: &syn::Path, stream: & if let Some(s) = types.maybe_resolve_ident(&struct_for) { if !types.crate_types.opaques.get(&s).is_some() { return; } writeln!(w, "#[no_mangle]").unwrap(); - writeln!(w, "pub extern \"C\" fn {}_write(obj: *const {}) -> crate::c_types::derived::CVec_u8Z {{", struct_for, struct_for).unwrap(); + writeln!(w, "pub extern \"C\" fn {}_write(obj: &{}) -> crate::c_types::derived::CVec_u8Z {{", struct_for, struct_for).unwrap(); writeln!(w, "\tcrate::c_types::serialize_obj(unsafe {{ &(*(*obj).inner) }})").unwrap(); writeln!(w, "}}").unwrap(); writeln!(w, "#[no_mangle]").unwrap(); @@ -58,30 +57,119 @@ fn convert_macro(w: &mut W, macro_path: &syn::Path, stream: & } } -/// Convert "impl trait_path for for_obj { .. }" for manually-mapped types (ie (de)serialization) -fn maybe_convert_trait_impl(w: &mut W, trait_path: &syn::Path, for_obj: &syn::Ident, types: &TypeResolver) { - if let Some(t) = types.maybe_resolve_path(&trait_path, None) { - let s = types.maybe_resolve_ident(for_obj).unwrap(); - if !types.crate_types.opaques.get(&s).is_some() { return; } +/// Convert "impl trait_path for for_ty { .. }" for manually-mapped types (ie (de)serialization) +fn maybe_convert_trait_impl(w: &mut W, trait_path: &syn::Path, for_ty: &syn::Type, types: &mut TypeResolver, generics: &GenericTypes) { + if let Some(t) = types.maybe_resolve_path(&trait_path, Some(generics)) { + let for_obj; + let full_obj_path; + let mut has_inner = false; + if let syn::Type::Path(ref p) = for_ty { + if let Some(ident) = single_ident_generic_path_to_ident(&p.path) { + for_obj = format!("{}", ident); + full_obj_path = for_obj.clone(); + has_inner = types.c_type_has_inner_from_path(&types.resolve_path(&p.path, Some(generics))); + } else { return; } + } else { + // We assume that anything that isn't a Path is somehow a generic that ends up in our + // derived-types module. + let mut for_obj_vec = Vec::new(); + types.write_c_type(&mut for_obj_vec, for_ty, Some(generics), false); + full_obj_path = String::from_utf8(for_obj_vec).unwrap(); + assert!(full_obj_path.starts_with(TypeResolver::generated_container_path())); + for_obj = full_obj_path[TypeResolver::generated_container_path().len() + 2..].into(); + } + match &t as &str { "util::ser::Writeable" => { writeln!(w, "#[no_mangle]").unwrap(); - writeln!(w, "pub extern \"C\" fn {}_write(obj: *const {}) -> crate::c_types::derived::CVec_u8Z {{", for_obj, for_obj).unwrap(); - writeln!(w, "\tcrate::c_types::serialize_obj(unsafe {{ &(*(*obj).inner) }})").unwrap(); - writeln!(w, "}}").unwrap(); - writeln!(w, "#[no_mangle]").unwrap(); - writeln!(w, "pub(crate) extern \"C\" fn {}_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {{", for_obj).unwrap(); - writeln!(w, "\tcrate::c_types::serialize_obj(unsafe {{ &*(obj as *const native{}) }})", for_obj).unwrap(); + writeln!(w, "pub extern \"C\" fn {}_write(obj: &{}) -> crate::c_types::derived::CVec_u8Z {{", for_obj, full_obj_path).unwrap(); + + let ref_type = syn::Type::Reference(syn::TypeReference { + and_token: syn::Token!(&)(Span::call_site()), lifetime: None, mutability: None, + elem: Box::new(for_ty.clone()) }); + assert!(!types.write_from_c_conversion_new_var(w, &syn::Ident::new("obj", Span::call_site()), &ref_type, Some(generics))); + + write!(w, "\tcrate::c_types::serialize_obj(").unwrap(); + types.write_from_c_conversion_prefix(w, &ref_type, Some(generics)); + write!(w, "unsafe {{ &*obj }}").unwrap(); + types.write_from_c_conversion_suffix(w, &ref_type, Some(generics)); + writeln!(w, ")").unwrap(); + writeln!(w, "}}").unwrap(); + if has_inner { + writeln!(w, "#[no_mangle]").unwrap(); + writeln!(w, "pub(crate) extern \"C\" fn {}_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {{", for_obj).unwrap(); + writeln!(w, "\tcrate::c_types::serialize_obj(unsafe {{ &*(obj as *const native{}) }})", for_obj).unwrap(); + writeln!(w, "}}").unwrap(); + } }, - "util::ser::Readable" => { + "util::ser::Readable"|"util::ser::ReadableArgs" => { + // Create the Result syn::Type + let mut err_segs = syn::punctuated::Punctuated::new(); + err_segs.push(syn::PathSegment { ident: syn::Ident::new("ln", Span::call_site()), arguments: syn::PathArguments::None }); + err_segs.push(syn::PathSegment { ident: syn::Ident::new("msgs", Span::call_site()), arguments: syn::PathArguments::None }); + err_segs.push(syn::PathSegment { ident: syn::Ident::new("DecodeError", Span::call_site()), arguments: syn::PathArguments::None }); + let mut args = syn::punctuated::Punctuated::new(); + args.push(syn::GenericArgument::Type(for_ty.clone())); + args.push(syn::GenericArgument::Type(syn::Type::Path(syn::TypePath { + qself: None, path: syn::Path { + leading_colon: Some(syn::Token![::](Span::call_site())), segments: err_segs, + } + }))); + let mut res_segs = syn::punctuated::Punctuated::new(); + res_segs.push(syn::PathSegment { + ident: syn::Ident::new("Result", Span::call_site()), + arguments: syn::PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments { + colon2_token: None, lt_token: syn::Token![<](Span::call_site()), args, gt_token: syn::Token![>](Span::call_site()), + }) + }); + let res_ty = syn::Type::Path(syn::TypePath { qself: None, path: syn::Path { + leading_colon: None, segments: res_segs } }); + writeln!(w, "#[no_mangle]").unwrap(); - writeln!(w, "pub extern \"C\" fn {}_read(ser: crate::c_types::u8slice) -> {} {{", for_obj, for_obj).unwrap(); - writeln!(w, "\tif let Ok(res) = crate::c_types::deserialize_obj(ser) {{").unwrap(); - writeln!(w, "\t\t{} {{ inner: Box::into_raw(Box::new(res)), is_owned: true }}", for_obj).unwrap(); - writeln!(w, "\t}} else {{").unwrap(); - writeln!(w, "\t\t{} {{ inner: std::ptr::null_mut(), is_owned: true }}", for_obj).unwrap(); - writeln!(w, "\t}}\n}}").unwrap(); + write!(w, "pub extern \"C\" fn {}_read(ser: crate::c_types::u8slice", for_obj).unwrap(); + + let mut arg_conv = Vec::new(); + if t == "util::ser::ReadableArgs" { + write!(w, ", arg: ").unwrap(); + assert!(trait_path.leading_colon.is_none()); + let args_seg = trait_path.segments.iter().last().unwrap(); + assert_eq!(format!("{}", args_seg.ident), "ReadableArgs"); + if let syn::PathArguments::AngleBracketed(args) = &args_seg.arguments { + assert_eq!(args.args.len(), 1); + if let syn::GenericArgument::Type(args_ty) = args.args.iter().next().unwrap() { + types.write_c_type(w, args_ty, Some(generics), false); + + assert!(!types.write_from_c_conversion_new_var(&mut arg_conv, &syn::Ident::new("arg", Span::call_site()), &args_ty, Some(generics))); + + write!(&mut arg_conv, "\tlet arg_conv = ").unwrap(); + types.write_from_c_conversion_prefix(&mut arg_conv, &args_ty, Some(generics)); + write!(&mut arg_conv, "arg").unwrap(); + types.write_from_c_conversion_suffix(&mut arg_conv, &args_ty, Some(generics)); + } else { unreachable!(); } + } else { unreachable!(); } + } + write!(w, ") -> ").unwrap(); + types.write_c_type(w, &res_ty, Some(generics), false); + writeln!(w, " {{").unwrap(); + + if t == "util::ser::ReadableArgs" { + w.write(&arg_conv).unwrap(); + write!(w, ";\n\tlet res: ").unwrap(); + // At least in one case we need type annotations here, so provide them. + types.write_rust_type(w, Some(generics), &res_ty); + writeln!(w, " = crate::c_types::deserialize_obj_arg(ser, arg_conv);").unwrap(); + } else { + writeln!(w, "\tlet res = crate::c_types::deserialize_obj(ser);").unwrap(); + } + write!(w, "\t").unwrap(); + if types.write_to_c_conversion_new_var(w, &syn::Ident::new("res", Span::call_site()), &res_ty, Some(generics), false) { + write!(w, "\n\t").unwrap(); + } + types.write_to_c_conversion_inline_prefix(w, &res_ty, Some(generics), false); + write!(w, "res").unwrap(); + types.write_to_c_conversion_inline_suffix(w, &res_ty, Some(generics), false); + writeln!(w, "\n}}").unwrap(); }, _ => {}, } @@ -144,15 +232,20 @@ macro_rules! walk_supertraits { ($t: expr, $types: expr, ($( $pat: pat => $e: ex } // First try to resolve path to find in-crate traits, but if that doesn't work // assume its a prelude trait (eg Clone, etc) and just use the single ident. - if let Some(path) = $types.maybe_resolve_path(&supertrait.path, None) { - match (&path as &str, &supertrait.path.segments.iter().last().unwrap().ident) { - $( $pat => $e, )* + let types_opt: Option<&TypeResolver> = $types; + if let Some(types) = types_opt { + if let Some(path) = types.maybe_resolve_path(&supertrait.path, None) { + match (&path as &str, &supertrait.path.segments.iter().last().unwrap().ident) { + $( $pat => $e, )* + } + continue; } - } else if let Some(ident) = supertrait.path.get_ident() { + } + if let Some(ident) = supertrait.path.get_ident() { match (&format!("{}", ident) as &str, &ident) { $( $pat => $e, )* } - } else { + } else if types_opt.is_some() { panic!("Supertrait unresolvable and not single-ident"); } }, @@ -248,7 +341,7 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty } } // Add functions which may be required for supertrait implementations. - walk_supertraits!(t, types, ( + walk_supertraits!(t, Some(&types), ( ("Clone", _) => { writeln!(w, "\tpub clone: Option *mut c_void>,").unwrap(); generated_fields.push("clone".to_owned()); @@ -279,7 +372,7 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty generated_fields.push("free".to_owned()); writeln!(w, "}}").unwrap(); // Implement supertraits for the C-mapped struct. - walk_supertraits!(t, types, ( + walk_supertraits!(t, Some(&types), ( ("Send", _) => writeln!(w, "unsafe impl Send for {} {{}}", trait_name).unwrap(), ("Sync", _) => writeln!(w, "unsafe impl Sync for {} {{}}", trait_name).unwrap(), ("std::cmp::Eq", _) => { @@ -454,70 +547,40 @@ fn writeln_opaque(w: &mut W, ident: &syn::Ident, struct_name: writeln!(w, "#[allow(unused)]").unwrap(); writeln!(w, "/// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy").unwrap(); writeln!(w, "impl {} {{", struct_name).unwrap(); - writeln!(w, "\tpub(crate) fn take_ptr(mut self) -> *mut native{} {{", struct_name).unwrap(); + writeln!(w, "\tpub(crate) fn take_inner(mut self) -> *mut native{} {{", struct_name).unwrap(); writeln!(w, "\t\tassert!(self.is_owned);").unwrap(); writeln!(w, "\t\tlet ret = self.inner;").unwrap(); writeln!(w, "\t\tself.inner = std::ptr::null_mut();").unwrap(); writeln!(w, "\t\tret").unwrap(); writeln!(w, "\t}}\n}}").unwrap(); - 'attr_loop: for attr in attrs.iter() { - let tokens_clone = attr.tokens.clone(); - let mut token_iter = tokens_clone.into_iter(); - if let Some(token) = token_iter.next() { - match token { - TokenTree::Group(g) => { - if format!("{}", single_ident_generic_path_to_ident(&attr.path).unwrap()) == "derive" { - for id in g.stream().into_iter() { - if let TokenTree::Ident(i) = id { - if i == "Clone" { - writeln!(w, "impl Clone for {} {{", struct_name).unwrap(); - writeln!(w, "\tfn clone(&self) -> Self {{").unwrap(); - writeln!(w, "\t\tSelf {{").unwrap(); - writeln!(w, "\t\t\tinner: Box::into_raw(Box::new(unsafe {{ &*self.inner }}.clone())),").unwrap(); - writeln!(w, "\t\t\tis_owned: true,").unwrap(); - writeln!(w, "\t\t}}\n\t}}\n}}").unwrap(); - writeln!(w, "#[allow(unused)]").unwrap(); - writeln!(w, "/// Used only if an object of this type is returned as a trait impl by a method").unwrap(); - writeln!(w, "pub(crate) extern \"C\" fn {}_clone_void(this_ptr: *const c_void) -> *mut c_void {{", struct_name).unwrap(); - writeln!(w, "\tBox::into_raw(Box::new(unsafe {{ (*(this_ptr as *mut native{})).clone() }})) as *mut c_void", struct_name).unwrap(); - writeln!(w, "}}").unwrap(); - writeln!(w, "#[no_mangle]").unwrap(); - writeln!(w, "pub extern \"C\" fn {}_clone(orig: &{}) -> {} {{", struct_name, struct_name, struct_name).unwrap(); - writeln!(w, "\t{} {{ inner: Box::into_raw(Box::new(unsafe {{ &*orig.inner }}.clone())), is_owned: true }}", struct_name).unwrap(); - writeln!(w, "}}").unwrap(); - break 'attr_loop; - } - } - } - } - }, - _ => {}, - } - } - } - write_cpp_wrapper(cpp_headers, &format!("{}", ident), true); } -/// Writes out all the relevant mappings for a Rust struct, deferring to writeln_opaque to generate -/// the struct itself, and then writing getters and setters for public, understood-type fields and -/// a constructor if every field is public. -fn writeln_struct<'a, 'b, W: std::io::Write>(w: &mut W, s: &'a syn::ItemStruct, types: &mut TypeResolver<'b, 'a>, extra_headers: &mut File, cpp_headers: &mut File) { - let struct_name = &format!("{}", s.ident); +fn declare_struct<'a, 'b>(s: &'a syn::ItemStruct, types: &mut TypeResolver<'b, 'a>) -> bool { let export = export_status(&s.attrs); match export { ExportStatus::Export => {}, - ExportStatus::TestOnly => return, + ExportStatus::TestOnly => return false, ExportStatus::NoExport => { types.struct_ignored(&s.ident); - return; + return false; } } + types.struct_imported(&s.ident); + true +} + +/// Writes out all the relevant mappings for a Rust struct, deferring to writeln_opaque to generate +/// the struct itself, and then writing getters and setters for public, understood-type fields and +/// a constructor if every field is public. +fn writeln_struct<'a, 'b, W: std::io::Write>(w: &mut W, s: &'a syn::ItemStruct, types: &mut TypeResolver<'b, 'a>, extra_headers: &mut File, cpp_headers: &mut File) { + if !declare_struct(s, types) { return; } + + let struct_name = &format!("{}", s.ident); writeln_opaque(w, &s.ident, struct_name, &s.generics, &s.attrs, types, extra_headers, cpp_headers); - eprintln!("exporting fields for {}", struct_name); if let syn::Fields::Named(fields) = &s.fields { let mut gen_types = GenericTypes::new(); assert!(gen_types.learn_generics(&s.generics, types)); @@ -598,8 +661,6 @@ fn writeln_struct<'a, 'b, W: std::io::Write>(w: &mut W, s: &'a syn::ItemStruct, writeln!(w, "\t}})), is_owned: true }}\n}}").unwrap(); } } - - types.struct_imported(&s.ident, struct_name.clone()); } /// Prints a relevant conversion for impl * @@ -612,6 +673,36 @@ fn writeln_struct<'a, 'b, W: std::io::Write>(w: &mut W, s: &'a syn::ItemStruct, /// /// A few non-crate Traits are hard-coded including Default. fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut TypeResolver) { + match export_status(&i.attrs) { + ExportStatus::Export => {}, + ExportStatus::NoExport|ExportStatus::TestOnly => return, + } + + if let syn::Type::Tuple(_) = &*i.self_ty { + if types.understood_c_type(&*i.self_ty, None) { + let mut gen_types = GenericTypes::new(); + if !gen_types.learn_generics(&i.generics, types) { + eprintln!("Not implementing anything for `impl (..)` due to not understood generics"); + return; + } + + if i.defaultness.is_some() || i.unsafety.is_some() { unimplemented!(); } + if let Some(trait_path) = i.trait_.as_ref() { + if trait_path.0.is_some() { unimplemented!(); } + if types.understood_c_path(&trait_path.1) { + eprintln!("Not implementing anything for `impl Trait for (..)` - we only support manual defines"); + return; + } else { + // Just do a manual implementation: + maybe_convert_trait_impl(w, &trait_path.1, &*i.self_ty, types, &gen_types); + } + } else { + eprintln!("Not implementing anything for plain `impl (..)` block - we only support `impl Trait for (..)` blocks"); + return; + } + } + return; + } if let &syn::Type::Path(ref p) = &*i.self_ty { if p.qself.is_some() { unimplemented!(); } if let Some(ident) = single_ident_generic_path_to_ident(&p.path) { @@ -667,7 +758,7 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ writeln!(w, "\t\tret.free = Some({}_free_void);", ident).unwrap(); writeln!(w, "\t\tret\n\t}}\n}}").unwrap(); - write!(w, "#[no_mangle]\npub extern \"C\" fn {}_as_{}(this_arg: *const {}) -> crate::{} {{\n", ident, trait_obj.ident, ident, full_trait_path).unwrap(); + write!(w, "#[no_mangle]\npub extern \"C\" fn {}_as_{}(this_arg: &{}) -> crate::{} {{\n", ident, trait_obj.ident, ident, full_trait_path).unwrap(); writeln!(w, "\tcrate::{} {{", full_trait_path).unwrap(); writeln!(w, "\t\tthis_arg: unsafe {{ (*this_arg).inner as *mut c_void }},").unwrap(); writeln!(w, "\t\tfree: None,").unwrap(); @@ -708,7 +799,7 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ _ => {}, } } - walk_supertraits!(trait_obj, types, ( + walk_supertraits!(trait_obj, Some(&types), ( ("Clone", _) => { writeln!(w, "\t\tclone: Some({}_clone_void),", ident).unwrap(); }, @@ -806,7 +897,7 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ _ => unimplemented!(), } } - walk_supertraits!(trait_obj, types, ( + walk_supertraits!(trait_obj, Some(&types), ( (s, t) => { if let Some(supertrait_obj) = types.crate_types.traits.get(s).cloned() { writeln!(w, "use {}::{} as native{}Trait;", types.orig_crate, s, t).unwrap(); @@ -822,23 +913,33 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ } ) ); write!(w, "\n").unwrap(); - } else if let Some(trait_ident) = trait_path.1.get_ident() { + } else if path_matches_nongeneric(&trait_path.1, &["From"]) { + } else if path_matches_nongeneric(&trait_path.1, &["Default"]) { + write!(w, "#[must_use]\n#[no_mangle]\npub extern \"C\" fn {}_default() -> {} {{\n", ident, ident).unwrap(); + write!(w, "\t{} {{ inner: Box::into_raw(Box::new(Default::default())), is_owned: true }}\n", ident).unwrap(); + write!(w, "}}\n").unwrap(); + } else if path_matches_nongeneric(&trait_path.1, &["core", "cmp", "PartialEq"]) { + } else if path_matches_nongeneric(&trait_path.1, &["core", "clone", "Clone"]) && types.c_type_has_inner_from_path(&resolved_path) { + writeln!(w, "impl Clone for {} {{", ident).unwrap(); + writeln!(w, "\tfn clone(&self) -> Self {{").unwrap(); + writeln!(w, "\t\tSelf {{").unwrap(); + writeln!(w, "\t\t\tinner: if self.inner.is_null() {{ std::ptr::null_mut() }} else {{").unwrap(); + writeln!(w, "\t\t\t\tBox::into_raw(Box::new(unsafe {{ &*self.inner }}.clone())) }},").unwrap(); + writeln!(w, "\t\t\tis_owned: true,").unwrap(); + writeln!(w, "\t\t}}\n\t}}\n}}").unwrap(); + writeln!(w, "#[allow(unused)]").unwrap(); + writeln!(w, "/// Used only if an object of this type is returned as a trait impl by a method").unwrap(); + writeln!(w, "pub(crate) extern \"C\" fn {}_clone_void(this_ptr: *const c_void) -> *mut c_void {{", ident).unwrap(); + writeln!(w, "\tBox::into_raw(Box::new(unsafe {{ (*(this_ptr as *mut native{})).clone() }})) as *mut c_void", ident).unwrap(); + writeln!(w, "}}").unwrap(); + writeln!(w, "#[no_mangle]").unwrap(); + writeln!(w, "pub extern \"C\" fn {}_clone(orig: &{}) -> {} {{", ident, ident, ident).unwrap(); + writeln!(w, "\torig.clone()").unwrap(); + writeln!(w, "}}").unwrap(); + } else { //XXX: implement for other things like ToString - match &format!("{}", trait_ident) as &str { - "From" => {}, - "Default" => { - write!(w, "#[must_use]\n#[no_mangle]\npub extern \"C\" fn {}_default() -> {} {{\n", ident, ident).unwrap(); - write!(w, "\t{} {{ inner: Box::into_raw(Box::new(Default::default())), is_owned: true }}\n", ident).unwrap(); - write!(w, "}}\n").unwrap(); - }, - "PartialEq" => {}, - // If we have no generics, try a manual implementation: - _ if p.path.get_ident().is_some() => maybe_convert_trait_impl(w, &trait_path.1, &ident, types), - _ => {}, - } - } else if p.path.get_ident().is_some() { // If we have no generics, try a manual implementation: - maybe_convert_trait_impl(w, &trait_path.1, &ident, types); + maybe_convert_trait_impl(w, &trait_path.1, &*i.self_ty, types, &gen_types); } } else { let declared_type = (*types.get_declared_type(&ident).unwrap()).clone(); @@ -916,6 +1017,19 @@ fn is_enum_opaque(e: &syn::ItemEnum) -> bool { false } +fn declare_enum<'a, 'b>(e: &'a syn::ItemEnum, types: &mut TypeResolver<'b, 'a>) { + match export_status(&e.attrs) { + ExportStatus::Export => {}, + ExportStatus::NoExport|ExportStatus::TestOnly => return, + } + + if is_enum_opaque(e) { + types.enum_ignored(&e.ident); + } else { + types.mirrored_enum_declared(&e.ident); + } +} + /// Print a mapping of an enum. If all of the enum's fields are C-mapped in some form (or the enum /// is unitary), we generate an equivalent enum with all types replaced with their C mapped /// versions followed by conversion functions which map between the Rust version and the C mapped @@ -929,7 +1043,6 @@ fn writeln_enum<'a, 'b, W: std::io::Write>(w: &mut W, e: &'a syn::ItemEnum, type if is_enum_opaque(e) { eprintln!("Skipping enum {} as it contains non-unit fields", e.ident); writeln_opaque(w, &e.ident, &format!("{}", e.ident), &e.generics, &e.attrs, types, extra_headers, cpp_headers); - types.enum_ignored(&e.ident); return; } writeln_docs(w, &e.attrs, ""); @@ -937,7 +1050,6 @@ fn writeln_enum<'a, 'b, W: std::io::Write>(w: &mut W, e: &'a syn::ItemEnum, type if e.generics.lt_token.is_some() { unimplemented!(); } - types.mirrored_enum_declared(&e.ident); let mut needs_free = false; @@ -1073,189 +1185,189 @@ fn writeln_fn<'a, 'b, W: std::io::Write>(w: &mut W, f: &'a syn::ItemFn, types: & // ******************************** // *** File/Crate Walking Logic *** // ******************************** - -/// Simple utility to walk the modules in a crate - iterating over the modules (with file paths) in -/// a single File. -struct FileIter<'a, I: Iterator> { - in_dir: &'a str, - path: &'a str, - module: &'a str, - item_iter: I, +/// A public module +struct ASTModule { + pub attrs: Vec, + pub items: Vec, + pub submods: Vec, } -impl<'a, I: Iterator> Iterator for FileIter<'a, I> { - type Item = (String, String, &'a syn::ItemMod); - fn next(&mut self) -> std::option::Option<::Item> { - loop { - match self.item_iter.next() { - Some(syn::Item::Mod(m)) => { - if let syn::Visibility::Public(_) = m.vis { - match export_status(&m.attrs) { - ExportStatus::Export => {}, - ExportStatus::NoExport|ExportStatus::TestOnly => continue, - } - - let f_path = format!("{}/{}.rs", (self.path.as_ref() as &Path).parent().unwrap().display(), m.ident); - let new_mod = if self.module.is_empty() { format!("{}", m.ident) } else { format!("{}::{}", self.module, m.ident) }; - if let Ok(_) = File::open(&format!("{}/{}", self.in_dir, f_path)) { - return Some((f_path, new_mod, m)); - } else { - return Some(( - format!("{}/{}/mod.rs", (self.path.as_ref() as &Path).parent().unwrap().display(), m.ident), - new_mod, m)); +/// A struct containing the syn::File AST for each file in the crate. +struct FullLibraryAST { + modules: HashMap, +} +impl FullLibraryAST { + fn load_module(&mut self, module: String, attrs: Vec, mut items: Vec) { + let mut non_mod_items = Vec::with_capacity(items.len()); + let mut submods = Vec::with_capacity(items.len()); + for item in items.drain(..) { + match item { + syn::Item::Mod(m) if m.content.is_some() => { + if export_status(&m.attrs) == ExportStatus::Export { + if let syn::Visibility::Public(_) = m.vis { + let modident = format!("{}", m.ident); + let modname = if module != "" { + module.clone() + "::" + &modident + } else { + modident.clone() + }; + self.load_module(modname, m.attrs, m.content.unwrap().1); + submods.push(modident); } } }, - Some(_) => {}, - None => return None, + _ => { non_mod_items.push(item); } } } + self.modules.insert(module, ASTModule { attrs, items: non_mod_items, submods }); } -} -fn file_iter<'a>(file: &'a syn::File, in_dir: &'a str, path: &'a str, module: &'a str) -> - impl Iterator + 'a { - FileIter { in_dir, path, module, item_iter: file.items.iter() } -} -/// A struct containing the syn::File AST for each file in the crate. -struct FullLibraryAST { - files: HashMap, + pub fn load_lib(lib: syn::File) -> Self { + assert_eq!(export_status(&lib.attrs), ExportStatus::Export); + let mut res = Self { modules: HashMap::default() }; + res.load_module("".to_owned(), lib.attrs, lib.items); + res + } } /// Do the Real Work of mapping an original file to C-callable wrappers. Creates a new file at /// `out_path` and fills it with wrapper structs/functions to allow calling the things in the AST /// at `module` from C. -fn convert_file<'a, 'b>(libast: &'a FullLibraryAST, crate_types: &mut CrateTypes<'a>, in_dir: &str, out_dir: &str, path: &str, orig_crate: &str, module: &str, header_file: &mut File, cpp_header_file: &mut File) { - let syntax = if let Some(ast) = libast.files.get(module) { ast } else { return }; - - assert!(syntax.shebang.is_none()); // Not sure what this is, hope we dont have one - - let new_file_path = format!("{}/{}", out_dir, path); - let _ = std::fs::create_dir((&new_file_path.as_ref() as &std::path::Path).parent().unwrap()); - let mut out = std::fs::OpenOptions::new().write(true).create(true).truncate(true) - .open(new_file_path).expect("Unable to open new src file"); - - assert_eq!(export_status(&syntax.attrs), ExportStatus::Export); - writeln_docs(&mut out, &syntax.attrs, ""); - - if path.ends_with("/lib.rs") { - // Special-case the top-level lib.rs with various lint allows and a pointer to the c_types - // and bitcoin hand-written modules. - writeln!(out, "#![allow(unknown_lints)]").unwrap(); - writeln!(out, "#![allow(non_camel_case_types)]").unwrap(); - writeln!(out, "#![allow(non_snake_case)]").unwrap(); - writeln!(out, "#![allow(unused_imports)]").unwrap(); - writeln!(out, "#![allow(unused_variables)]").unwrap(); - writeln!(out, "#![allow(unused_mut)]").unwrap(); - writeln!(out, "#![allow(unused_parens)]").unwrap(); - writeln!(out, "#![allow(unused_unsafe)]").unwrap(); - writeln!(out, "#![allow(unused_braces)]").unwrap(); - writeln!(out, "mod c_types;").unwrap(); - writeln!(out, "mod bitcoin;").unwrap(); - } else { - writeln!(out, "\nuse std::ffi::c_void;\nuse bitcoin::hashes::Hash;\nuse crate::c_types::*;\n").unwrap(); - } +fn convert_file<'a, 'b>(libast: &'a FullLibraryAST, crate_types: &mut CrateTypes<'a>, out_dir: &str, orig_crate: &str, header_file: &mut File, cpp_header_file: &mut File) { + for (module, astmod) in libast.modules.iter() { + let ASTModule { ref attrs, ref items, ref submods } = astmod; + assert_eq!(export_status(&attrs), ExportStatus::Export); + + let new_file_path = if submods.is_empty() { + format!("{}/{}.rs", out_dir, module.replace("::", "/")) + } else if module != "" { + format!("{}/{}/mod.rs", out_dir, module.replace("::", "/")) + } else { + format!("{}/lib.rs", out_dir) + }; + let _ = std::fs::create_dir((&new_file_path.as_ref() as &std::path::Path).parent().unwrap()); + let mut out = std::fs::OpenOptions::new().write(true).create(true).truncate(true) + .open(new_file_path).expect("Unable to open new src file"); + + writeln_docs(&mut out, &attrs, ""); + + if module == "" { + // Special-case the top-level lib.rs with various lint allows and a pointer to the c_types + // and bitcoin hand-written modules. + writeln!(out, "#![allow(unknown_lints)]").unwrap(); + writeln!(out, "#![allow(non_camel_case_types)]").unwrap(); + writeln!(out, "#![allow(non_snake_case)]").unwrap(); + writeln!(out, "#![allow(unused_imports)]").unwrap(); + writeln!(out, "#![allow(unused_variables)]").unwrap(); + writeln!(out, "#![allow(unused_mut)]").unwrap(); + writeln!(out, "#![allow(unused_parens)]").unwrap(); + writeln!(out, "#![allow(unused_unsafe)]").unwrap(); + writeln!(out, "#![allow(unused_braces)]").unwrap(); + writeln!(out, "mod c_types;").unwrap(); + writeln!(out, "mod bitcoin;").unwrap(); + } else { + writeln!(out, "\nuse std::ffi::c_void;\nuse bitcoin::hashes::Hash;\nuse crate::c_types::*;\n").unwrap(); + } - for (path, new_mod, m) in file_iter(&syntax, in_dir, path, &module) { - writeln_docs(&mut out, &m.attrs, ""); - writeln!(out, "pub mod {};", m.ident).unwrap(); - convert_file(libast, crate_types, in_dir, out_dir, &path, - orig_crate, &new_mod, header_file, cpp_header_file); - } + for m in submods { + writeln!(out, "pub mod {};", m).unwrap(); + } - eprintln!("Converting {} entries...", path); + eprintln!("Converting {} entries...", module); - let mut type_resolver = TypeResolver::new(orig_crate, module, crate_types); + let mut type_resolver = TypeResolver::new(orig_crate, module, crate_types); - for item in syntax.items.iter() { - match item { - syn::Item::Use(u) => type_resolver.process_use(&mut out, &u), - syn::Item::Static(_) => {}, - syn::Item::Enum(e) => { - if let syn::Visibility::Public(_) = e.vis { - writeln_enum(&mut out, &e, &mut type_resolver, header_file, cpp_header_file); - } - }, - syn::Item::Impl(i) => { - writeln_impl(&mut out, &i, &mut type_resolver); - }, - syn::Item::Struct(s) => { - if let syn::Visibility::Public(_) = s.vis { - writeln_struct(&mut out, &s, &mut type_resolver, header_file, cpp_header_file); - } - }, - syn::Item::Trait(t) => { - if let syn::Visibility::Public(_) = t.vis { - writeln_trait(&mut out, &t, &mut type_resolver, header_file, cpp_header_file); - } - }, - syn::Item::Mod(_) => {}, // We don't have to do anything - the top loop handles these. - syn::Item::Const(c) => { - // Re-export any primitive-type constants. - if let syn::Visibility::Public(_) = c.vis { - if let syn::Type::Path(p) = &*c.ty { - let resolved_path = type_resolver.resolve_path(&p.path, None); - if type_resolver.is_primitive(&resolved_path) { - writeln!(out, "\n#[no_mangle]").unwrap(); - writeln!(out, "pub static {}: {} = {}::{}::{};", c.ident, resolved_path, orig_crate, module, c.ident).unwrap(); - } + // First pass over the items and fill in imports and file-declared objects in the type resolver + for item in items.iter() { + match item { + syn::Item::Use(u) => type_resolver.process_use(&mut out, &u), + syn::Item::Struct(s) => { + if let syn::Visibility::Public(_) = s.vis { + declare_struct(&s, &mut type_resolver); } - } - }, - syn::Item::Type(t) => { - if let syn::Visibility::Public(_) = t.vis { - match export_status(&t.attrs) { - ExportStatus::Export => {}, - ExportStatus::NoExport|ExportStatus::TestOnly => continue, + }, + syn::Item::Enum(e) => { + if let syn::Visibility::Public(_) = e.vis { + declare_enum(&e, &mut type_resolver); } + }, + _ => {}, + } + } - let mut process_alias = true; - for tok in t.generics.params.iter() { - if let syn::GenericParam::Lifetime(_) = tok {} - else { process_alias = false; } + for item in items.iter() { + match item { + syn::Item::Use(_) => {}, // Handled above + syn::Item::Static(_) => {}, + syn::Item::Enum(e) => { + if let syn::Visibility::Public(_) = e.vis { + writeln_enum(&mut out, &e, &mut type_resolver, header_file, cpp_header_file); + } + }, + syn::Item::Impl(i) => { + writeln_impl(&mut out, &i, &mut type_resolver); + }, + syn::Item::Struct(s) => { + if let syn::Visibility::Public(_) = s.vis { + writeln_struct(&mut out, &s, &mut type_resolver, header_file, cpp_header_file); + } + }, + syn::Item::Trait(t) => { + if let syn::Visibility::Public(_) = t.vis { + writeln_trait(&mut out, &t, &mut type_resolver, header_file, cpp_header_file); } - if process_alias { - match &*t.ty { - syn::Type::Path(_) => - writeln_opaque(&mut out, &t.ident, &format!("{}", t.ident), &t.generics, &t.attrs, &type_resolver, header_file, cpp_header_file), - _ => {} + }, + syn::Item::Mod(_) => {}, // We don't have to do anything - the top loop handles these. + syn::Item::Const(c) => { + // Re-export any primitive-type constants. + if let syn::Visibility::Public(_) = c.vis { + if let syn::Type::Path(p) = &*c.ty { + let resolved_path = type_resolver.resolve_path(&p.path, None); + if type_resolver.is_primitive(&resolved_path) { + writeln!(out, "\n#[no_mangle]").unwrap(); + writeln!(out, "pub static {}: {} = {}::{}::{};", c.ident, resolved_path, orig_crate, module, c.ident).unwrap(); + } } } - } - }, - syn::Item::Fn(f) => { - if let syn::Visibility::Public(_) = f.vis { - writeln_fn(&mut out, &f, &mut type_resolver); - } - }, - syn::Item::Macro(m) => { - if m.ident.is_none() { // If its not a macro definition - convert_macro(&mut out, &m.mac.path, &m.mac.tokens, &type_resolver); - } - }, - syn::Item::Verbatim(_) => {}, - syn::Item::ExternCrate(_) => {}, - _ => unimplemented!(), - } - } - - out.flush().unwrap(); -} - -/// Load the AST for each file in the crate, filling the FullLibraryAST object -fn load_ast(in_dir: &str, path: &str, module: String, ast_storage: &mut FullLibraryAST) { - eprintln!("Loading {}{}...", in_dir, path); - - let mut file = File::open(format!("{}/{}", in_dir, path)).expect("Unable to open file"); - let mut src = String::new(); - file.read_to_string(&mut src).expect("Unable to read file"); - let syntax = syn::parse_file(&src).expect("Unable to parse file"); + }, + syn::Item::Type(t) => { + if let syn::Visibility::Public(_) = t.vis { + match export_status(&t.attrs) { + ExportStatus::Export => {}, + ExportStatus::NoExport|ExportStatus::TestOnly => continue, + } - assert_eq!(export_status(&syntax.attrs), ExportStatus::Export); + let mut process_alias = true; + for tok in t.generics.params.iter() { + if let syn::GenericParam::Lifetime(_) = tok {} + else { process_alias = false; } + } + if process_alias { + match &*t.ty { + syn::Type::Path(_) => + writeln_opaque(&mut out, &t.ident, &format!("{}", t.ident), &t.generics, &t.attrs, &type_resolver, header_file, cpp_header_file), + _ => {} + } + } + } + }, + syn::Item::Fn(f) => { + if let syn::Visibility::Public(_) = f.vis { + writeln_fn(&mut out, &f, &mut type_resolver); + } + }, + syn::Item::Macro(m) => { + if m.ident.is_none() { // If its not a macro definition + convert_macro(&mut out, &m.mac.path, &m.mac.tokens, &type_resolver); + } + }, + syn::Item::Verbatim(_) => {}, + syn::Item::ExternCrate(_) => {}, + _ => unimplemented!(), + } + } - for (path, new_mod, _) in file_iter(&syntax, in_dir, path, &module) { - load_ast(in_dir, &path, new_mod, ast_storage); + out.flush().unwrap(); } - ast_storage.files.insert(module, syntax); } /// Insert ident -> absolute Path resolutions into imports from the given UseTree and path-prefix. @@ -1305,124 +1417,151 @@ fn resolve_imported_refs(imports: &HashMap<&syn::Ident, syn::Path>, mut ty: syn: } /// Walk the FullLibraryAST, deciding how things will be mapped and adding tracking to CrateTypes. -fn walk_ast<'a>(in_dir: &str, path: &str, module: String, ast_storage: &'a FullLibraryAST, crate_types: &mut CrateTypes<'a>) { - let syntax = if let Some(ast) = ast_storage.files.get(&module) { ast } else { return }; - assert_eq!(export_status(&syntax.attrs), ExportStatus::Export); - - for (path, new_mod, _) in file_iter(&syntax, in_dir, path, &module) { - walk_ast(in_dir, &path, new_mod, ast_storage, crate_types); - } - - let mut import_maps = HashMap::new(); - - for item in syntax.items.iter() { - match item { - syn::Item::Use(u) => { - process_use_intern(&u.tree, syn::punctuated::Punctuated::new(), &mut import_maps); - }, - syn::Item::Struct(s) => { - if let syn::Visibility::Public(_) = s.vis { - match export_status(&s.attrs) { - ExportStatus::Export => {}, - ExportStatus::NoExport|ExportStatus::TestOnly => continue, - } - let struct_path = format!("{}::{}", module, s.ident); - crate_types.opaques.insert(struct_path, &s.ident); - } - }, - syn::Item::Trait(t) => { - if let syn::Visibility::Public(_) = t.vis { - match export_status(&t.attrs) { - ExportStatus::Export => {}, - ExportStatus::NoExport|ExportStatus::TestOnly => continue, - } - let trait_path = format!("{}::{}", module, t.ident); - crate_types.traits.insert(trait_path, &t); - } - }, - syn::Item::Type(t) => { - if let syn::Visibility::Public(_) = t.vis { - match export_status(&t.attrs) { - ExportStatus::Export => {}, - ExportStatus::NoExport|ExportStatus::TestOnly => continue, - } - let type_path = format!("{}::{}", module, t.ident); - let mut process_alias = true; - for tok in t.generics.params.iter() { - if let syn::GenericParam::Lifetime(_) = tok {} - else { process_alias = false; } +fn walk_ast<'a>(ast_storage: &'a FullLibraryAST, crate_types: &mut CrateTypes<'a>) { + for (module, astmod) in ast_storage.modules.iter() { + let ASTModule { ref attrs, ref items, submods: _ } = astmod; + assert_eq!(export_status(&attrs), ExportStatus::Export); + let mut import_maps = HashMap::new(); + + for item in items.iter() { + match item { + syn::Item::Use(u) => { + process_use_intern(&u.tree, syn::punctuated::Punctuated::new(), &mut import_maps); + }, + syn::Item::Struct(s) => { + if let syn::Visibility::Public(_) = s.vis { + match export_status(&s.attrs) { + ExportStatus::Export => {}, + ExportStatus::NoExport|ExportStatus::TestOnly => continue, + } + let struct_path = format!("{}::{}", module, s.ident); + crate_types.opaques.insert(struct_path, &s.ident); } - if process_alias { - match &*t.ty { - syn::Type::Path(_) => { - // If its a path with no generics, assume we don't map the aliased type and map it opaque - crate_types.opaques.insert(type_path, &t.ident); + }, + syn::Item::Trait(t) => { + if let syn::Visibility::Public(_) = t.vis { + match export_status(&t.attrs) { + ExportStatus::Export => {}, + ExportStatus::NoExport|ExportStatus::TestOnly => continue, + } + let trait_path = format!("{}::{}", module, t.ident); + walk_supertraits!(t, None, ( + ("Clone", _) => { + crate_types.clonable_types.insert("crate::".to_owned() + &trait_path); }, - _ => { - crate_types.type_aliases.insert(type_path, resolve_imported_refs(&import_maps, (*t.ty).clone())); + (_, _) => {} + ) ); + crate_types.traits.insert(trait_path, &t); + } + }, + syn::Item::Type(t) => { + if let syn::Visibility::Public(_) = t.vis { + match export_status(&t.attrs) { + ExportStatus::Export => {}, + ExportStatus::NoExport|ExportStatus::TestOnly => continue, + } + let type_path = format!("{}::{}", module, t.ident); + let mut process_alias = true; + for tok in t.generics.params.iter() { + if let syn::GenericParam::Lifetime(_) = tok {} + else { process_alias = false; } + } + if process_alias { + match &*t.ty { + syn::Type::Path(_) => { + // If its a path with no generics, assume we don't map the aliased type and map it opaque + crate_types.opaques.insert(type_path, &t.ident); + }, + _ => { + crate_types.type_aliases.insert(type_path, resolve_imported_refs(&import_maps, (*t.ty).clone())); + } } } } - } - }, - syn::Item::Enum(e) if is_enum_opaque(e) => { - if let syn::Visibility::Public(_) = e.vis { - match export_status(&e.attrs) { - ExportStatus::Export => {}, - ExportStatus::NoExport|ExportStatus::TestOnly => continue, + }, + syn::Item::Enum(e) if is_enum_opaque(e) => { + if let syn::Visibility::Public(_) = e.vis { + match export_status(&e.attrs) { + ExportStatus::Export => {}, + ExportStatus::NoExport|ExportStatus::TestOnly => continue, + } + let enum_path = format!("{}::{}", module, e.ident); + crate_types.opaques.insert(enum_path, &e.ident); } - let enum_path = format!("{}::{}", module, e.ident); - crate_types.opaques.insert(enum_path, &e.ident); - } - }, - syn::Item::Enum(e) => { - if let syn::Visibility::Public(_) = e.vis { - match export_status(&e.attrs) { - ExportStatus::Export => {}, - ExportStatus::NoExport|ExportStatus::TestOnly => continue, + }, + syn::Item::Enum(e) => { + if let syn::Visibility::Public(_) = e.vis { + match export_status(&e.attrs) { + ExportStatus::Export => {}, + ExportStatus::NoExport|ExportStatus::TestOnly => continue, + } + let enum_path = format!("{}::{}", module, e.ident); + crate_types.mirrored_enums.insert(enum_path, &e); } - let enum_path = format!("{}::{}", module, e.ident); - crate_types.mirrored_enums.insert(enum_path, &e); - } - }, - _ => {}, + }, + syn::Item::Impl(i) => { + if let &syn::Type::Path(ref p) = &*i.self_ty { + if let Some(trait_path) = i.trait_.as_ref() { + if path_matches_nongeneric(&trait_path.1, &["core", "clone", "Clone"]) { + // Note, we should probably have more full-featured type resolution here, + // but for now we don't have any cases where we impl X for A::B. + let ident = single_ident_generic_path_to_ident(&p.path).expect("impl X for A::B currently unsupported"); + assert!(import_maps.get(&ident).is_none()); + crate_types.clonable_types.insert(format!("crate::{}::{}", module, ident)); + } + } + } + }, + _ => {}, + } } } } fn main() { let args: Vec = env::args().collect(); - if args.len() != 7 { - eprintln!("Usage: source/dir target/dir source_crate_name derived_templates.rs extra/includes.h extra/cpp/includes.hpp"); + if args.len() != 6 { + eprintln!("Usage: target/dir source_crate_name derived_templates.rs extra/includes.h extra/cpp/includes.hpp"); process::exit(1); } let mut derived_templates = std::fs::OpenOptions::new().write(true).create(true).truncate(true) - .open(&args[4]).expect("Unable to open new header file"); + .open(&args[3]).expect("Unable to open new header file"); let mut header_file = std::fs::OpenOptions::new().write(true).create(true).truncate(true) - .open(&args[5]).expect("Unable to open new header file"); + .open(&args[4]).expect("Unable to open new header file"); let mut cpp_header_file = std::fs::OpenOptions::new().write(true).create(true).truncate(true) - .open(&args[6]).expect("Unable to open new header file"); + .open(&args[5]).expect("Unable to open new header file"); - writeln!(header_file, "#if defined(__GNUC__)\n#define MUST_USE_STRUCT __attribute__((warn_unused))").unwrap(); - writeln!(header_file, "#else\n#define MUST_USE_STRUCT\n#endif").unwrap(); - writeln!(header_file, "#if defined(__GNUC__)\n#define MUST_USE_RES __attribute__((warn_unused_result))").unwrap(); - writeln!(header_file, "#else\n#define MUST_USE_RES\n#endif").unwrap(); + writeln!(header_file, "#if defined(__GNUC__)").unwrap(); + writeln!(header_file, "#define MUST_USE_STRUCT __attribute__((warn_unused))").unwrap(); + writeln!(header_file, "#define MUST_USE_RES __attribute__((warn_unused_result))").unwrap(); + writeln!(header_file, "#else").unwrap(); + writeln!(header_file, "#define MUST_USE_STRUCT").unwrap(); + writeln!(header_file, "#define MUST_USE_RES").unwrap(); + writeln!(header_file, "#endif").unwrap(); + writeln!(header_file, "#if defined(__clang__)").unwrap(); + writeln!(header_file, "#define NONNULL_PTR _Nonnull").unwrap(); + writeln!(header_file, "#else").unwrap(); + writeln!(header_file, "#define NONNULL_PTR").unwrap(); + writeln!(header_file, "#endif").unwrap(); writeln!(cpp_header_file, "#include \nnamespace LDK {{").unwrap(); // First parse the full crate's ASTs, caching them so that we can hold references to the AST // objects in other datastructures: - let mut libast = FullLibraryAST { files: HashMap::new() }; - load_ast(&args[1], "/lib.rs", "".to_string(), &mut libast); + let mut lib_src = String::new(); + std::io::stdin().lock().read_to_string(&mut lib_src).unwrap(); + let lib_syntax = syn::parse_file(&lib_src).expect("Unable to parse file"); + let libast = FullLibraryAST::load_lib(lib_syntax); // ...then walk the ASTs tracking what types we will map, and how, so that we can resolve them // when parsing other file ASTs... let mut libtypes = CrateTypes { traits: HashMap::new(), opaques: HashMap::new(), mirrored_enums: HashMap::new(), - type_aliases: HashMap::new(), templates_defined: HashMap::default(), template_file: &mut derived_templates }; - walk_ast(&args[1], "/lib.rs", "".to_string(), &libast, &mut libtypes); + type_aliases: HashMap::new(), templates_defined: HashMap::default(), template_file: &mut derived_templates, + clonable_types: HashSet::new() }; + walk_ast(&libast, &mut libtypes); // ... finally, do the actual file conversion/mapping, writing out types as we go. - convert_file(&libast, &mut libtypes, &args[1], &args[2], "/lib.rs", &args[3], "", &mut header_file, &mut cpp_header_file); + convert_file(&libast, &mut libtypes, &args[1], &args[2], &mut header_file, &mut cpp_header_file); // For container templates which we created while walking the crate, make sure we add C++ // mapped types so that C++ users can utilize the auto-destructors available.