X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=c-bindings-gen%2Fsrc%2Fmain.rs;h=46ae0e75fc5580262901f91af19f68b16dfe728d;hb=d25cffee0dfb879d827717d4d90a1b4d184eed0a;hp=1572b313582db2e2275b23b99ca3ba77a900b3cb;hpb=bf77ececa4ed93dd954c13d59ad2c14f10710586;p=ldk-c-bindings diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index 1572b31..46ae0e7 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -33,7 +33,7 @@ mod blocks; use types::*; use blocks::*; -const DEFAULT_IMPORTS: &'static str = "\nuse std::str::FromStr;\nuse std::ffi::c_void;\nuse bitcoin::hashes::Hash;\nuse crate::c_types::*;\n"; +const DEFAULT_IMPORTS: &'static str = "\nuse std::str::FromStr;\nuse std::ffi::c_void;\nuse core::convert::Infallible;\nuse bitcoin::hashes::Hash;\nuse crate::c_types::*;\n"; // ************************************* // *** Manually-expanded conversions *** @@ -175,6 +175,11 @@ fn do_write_impl_trait(w: &mut W, trait_path: &str, _trait_na } } +/// Returns true if an instance of the given type must never exist +fn is_type_unconstructable(path: &str) -> bool { + path == "core::convert::Infallible" || path == "crate::c_types::NotConstructable" +} + // ******************************* // *** Per-Type Printing Logic *** // ******************************* @@ -641,7 +646,7 @@ fn writeln_struct<'a, 'b, W: std::io::Write>(w: &mut W, s: &'a syn::ItemStruct, let mut self_path_segs = syn::punctuated::Punctuated::new(); self_path_segs.push(s.ident.clone().into()); let self_path = syn::Path { leading_colon: None, segments: self_path_segs}; - let mut gen_types = GenericTypes::new(Some((types.resolve_path(&self_path, None), &self_path))); + let mut gen_types = GenericTypes::new(Some(types.resolve_path(&self_path, None))); assert!(gen_types.learn_generics(&s.generics, types)); let mut all_fields_settable = true; @@ -764,7 +769,7 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ if p.qself.is_some() { unimplemented!(); } if let Some(ident) = single_ident_generic_path_to_ident(&p.path) { if let Some(resolved_path) = types.maybe_resolve_non_ignored_ident(&ident) { - let mut gen_types = GenericTypes::new(Some((resolved_path.clone(), &p.path))); + let mut gen_types = GenericTypes::new(Some(resolved_path.clone())); if !gen_types.learn_generics(&i.generics, types) { eprintln!("Not implementing anything for impl {} due to not understood generics", ident); return; @@ -821,14 +826,27 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ // From<> implementation which does all the work to ensure free is handled // properly. This way we can call this method from deep in the // type-conversion logic without actually knowing the concrete native type. + if !resolved_path.starts_with(types.module_path) { + writeln!(w, "use {} as native{};", resolved_path, ident).unwrap(); + } writeln!(w, "impl From for crate::{} {{", ident, full_trait_path).unwrap(); writeln!(w, "\tfn from(obj: native{}) -> Self {{", ident).unwrap(); - writeln!(w, "\t\tlet mut rust_obj = {} {{ inner: ObjOps::heap_alloc(obj), is_owned: true }};", ident).unwrap(); - writeln!(w, "\t\tlet mut ret = {}_as_{}(&rust_obj);", ident, trait_obj.ident).unwrap(); - writeln!(w, "\t\t// We want to free rust_obj when ret gets drop()'d, not rust_obj, so wipe rust_obj's pointer and set ret's free() fn").unwrap(); - writeln!(w, "\t\trust_obj.inner = std::ptr::null_mut();").unwrap(); - writeln!(w, "\t\tret.free = Some({}_free_void);", ident).unwrap(); - writeln!(w, "\t\tret\n\t}}\n}}").unwrap(); + if is_type_unconstructable(&resolved_path) { + writeln!(w, "\t\tunreachable!();").unwrap(); + } else { + writeln!(w, "\t\tlet mut rust_obj = {} {{ inner: ObjOps::heap_alloc(obj), is_owned: true }};", ident).unwrap(); + writeln!(w, "\t\tlet mut ret = {}_as_{}(&rust_obj);", ident, trait_obj.ident).unwrap(); + writeln!(w, "\t\t// We want to free rust_obj when ret gets drop()'d, not rust_obj, so wipe rust_obj's pointer and set ret's free() fn").unwrap(); + writeln!(w, "\t\trust_obj.inner = std::ptr::null_mut();").unwrap(); + writeln!(w, "\t\tret.free = Some({}_free_void);", ident).unwrap(); + writeln!(w, "\t\tret").unwrap(); + } + writeln!(w, "\t}}\n}}").unwrap(); + if is_type_unconstructable(&resolved_path) { + // We don't bother with Struct_as_Trait conversion for types which must + // never be instantiated, so just return early. + return; + } writeln!(w, "/// Constructs a new {} which calls the relevant methods on this_arg.", trait_obj.ident).unwrap(); writeln!(w, "/// This copies the `inner` pointer in this_arg and thus the returned {} must be freed before this_arg is", trait_obj.ident).unwrap(); @@ -905,7 +923,7 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ writeln!(w, "\t}}\n}}\n").unwrap(); macro_rules! impl_meth { - ($m: expr, $trait_path: expr, $trait: expr, $indent: expr) => { + ($m: expr, $trait_meth: expr, $trait_path: expr, $trait: expr, $indent: expr) => { let trait_method = $trait.items.iter().filter_map(|item| { if let syn::TraitItem::Method(t_m) = item { Some(t_m) } else { None } }).find(|trait_meth| trait_meth.sig.ident == $m.sig.ident).unwrap(); @@ -921,39 +939,62 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ write!(w, "extern \"C\" fn {}_{}_{}(", ident, $trait.ident, $m.sig.ident).unwrap(); let mut meth_gen_types = gen_types.push_ctx(); assert!(meth_gen_types.learn_generics(&$m.sig.generics, types)); - write_method_params(w, &$m.sig, "c_void", types, Some(&meth_gen_types), true, true); - write!(w, " {{\n\t").unwrap(); - write_method_var_decl_body(w, &$m.sig, "", types, Some(&meth_gen_types), false); - let mut takes_self = false; + let mut uncallable_function = false; for inp in $m.sig.inputs.iter() { - if let syn::FnArg::Receiver(_) = inp { - takes_self = true; + match inp { + syn::FnArg::Typed(arg) => { + if types.skip_arg(&*arg.ty, Some(&meth_gen_types)) { continue; } + let mut c_type = Vec::new(); + types.write_c_type(&mut c_type, &*arg.ty, Some(&meth_gen_types), false); + if is_type_unconstructable(&String::from_utf8(c_type).unwrap()) { + uncallable_function = true; + } + } + _ => {} } } - - let mut t_gen_args = String::new(); - for (idx, _) in $trait.generics.params.iter().enumerate() { - if idx != 0 { t_gen_args += ", " }; - t_gen_args += "_" - } - if takes_self { - write!(w, ">::{}(unsafe {{ &mut *(this_arg as *mut native{}) }}, ", ident, $trait_path, t_gen_args, $m.sig.ident, ident).unwrap(); + if uncallable_function { + let mut trait_resolver = get_module_type_resolver!(full_trait_path, types.crate_libs, types.crate_types); + write_method_params(w, &$trait_meth.sig, "c_void", &mut trait_resolver, Some(&meth_gen_types), true, true); } else { - write!(w, ">::{}(", ident, $trait_path, t_gen_args, $m.sig.ident).unwrap(); + write_method_params(w, &$m.sig, "c_void", types, Some(&meth_gen_types), true, true); } + write!(w, " {{\n\t").unwrap(); + if uncallable_function { + write!(w, "unreachable!();").unwrap(); + } else { + write_method_var_decl_body(w, &$m.sig, "", types, Some(&meth_gen_types), false); + let mut takes_self = false; + for inp in $m.sig.inputs.iter() { + if let syn::FnArg::Receiver(_) = inp { + takes_self = true; + } + } - let mut real_type = "".to_string(); - match &$m.sig.output { - syn::ReturnType::Type(_, rtype) => { - if let Some(mut remaining_path) = first_seg_self(&*rtype) { - if let Some(associated_seg) = get_single_remaining_path_seg(&mut remaining_path) { - real_type = format!("{}", impl_associated_types.get(associated_seg).unwrap()); + let mut t_gen_args = String::new(); + for (idx, _) in $trait.generics.params.iter().enumerate() { + if idx != 0 { t_gen_args += ", " }; + t_gen_args += "_" + } + if takes_self { + write!(w, ">::{}(unsafe {{ &mut *(this_arg as *mut native{}) }}, ", ident, $trait_path, t_gen_args, $m.sig.ident, ident).unwrap(); + } else { + write!(w, ">::{}(", ident, $trait_path, t_gen_args, $m.sig.ident).unwrap(); + } + + let mut real_type = "".to_string(); + match &$m.sig.output { + syn::ReturnType::Type(_, rtype) => { + if let Some(mut remaining_path) = first_seg_self(&*rtype) { + if let Some(associated_seg) = get_single_remaining_path_seg(&mut remaining_path) { + real_type = format!("{}", impl_associated_types.get(associated_seg).unwrap()); + } } - } - }, - _ => {}, + }, + _ => {}, + } + write_method_call_params(w, &$m.sig, "", types, Some(&meth_gen_types), &real_type, false); } - write_method_call_params(w, &$m.sig, "", types, Some(&meth_gen_types), &real_type, false); write!(w, "\n}}\n").unwrap(); if let syn::ReturnType::Type(_, rtype) = &$m.sig.output { if let syn::Type::Reference(r) = &**rtype { @@ -972,10 +1013,21 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ } } - for item in i.items.iter() { + 'impl_item_loop: for item in i.items.iter() { match item { syn::ImplItem::Method(m) => { - impl_meth!(m, full_trait_path, trait_obj, ""); + for trait_item in trait_obj.items.iter() { + match trait_item { + syn::TraitItem::Method(meth) => { + if meth.sig.ident == m.sig.ident { + impl_meth!(m, meth, full_trait_path, trait_obj, ""); + continue 'impl_item_loop; + } + }, + _ => {}, + } + } + unreachable!(); }, syn::ImplItem::Type(_) => {}, _ => unimplemented!(), @@ -997,7 +1049,14 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ writeln!(w, "}}").unwrap(); } write!(w, "\n").unwrap(); - } else if path_matches_nongeneric(&trait_path.1, &["From"]) { + return; + } + if is_type_unconstructable(&resolved_path) { + // Don't bother exposing trait implementations for objects which cannot be + // instantiated. + return; + } + if path_matches_nongeneric(&trait_path.1, &["From"]) { } else if path_matches_nongeneric(&trait_path.1, &["Default"]) { writeln!(w, "/// Creates a \"default\" {}. See struct and individual field documentaiton for details on which values are used.", ident).unwrap(); write!(w, "#[must_use]\n#[no_mangle]\npub extern \"C\" fn {}_default() -> {} {{\n", ident, ident).unwrap();