X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=c-bindings-gen%2Fsrc%2Fmain.rs;h=7d6a452b04a08fe34d8cf53ee1bf001e1f968865;hb=1ec49fac7433e5b8693c3858a3e25998bc888eeb;hp=8876583315246524ffb938df804234b9a2784d86;hpb=8f4db29a29bcce3b945f698ab81dee2306770832;p=ldk-c-bindings diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index 8876583..7d6a452 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -94,12 +94,15 @@ fn maybe_convert_trait_impl(w: &mut W, trait_path: &syn::Path writeln!(w, ")").unwrap(); writeln!(w, "}}").unwrap(); + + writeln!(w, "#[allow(unused)]").unwrap(); + writeln!(w, "pub(crate) extern \"C\" fn {}_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {{", for_obj).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(); + } else { + writeln!(w, "\t{}_write(unsafe {{ &*(obj as *const {}) }})", for_obj, for_obj).unwrap(); } + writeln!(w, "}}").unwrap(); }, "lightning::util::ser::Readable"|"lightning::util::ser::ReadableArgs"|"lightning::util::ser::MaybeReadable" => { // Create the Result syn::Type @@ -267,8 +270,8 @@ macro_rules! walk_supertraits { ($t: expr, $types: expr, ($( $($pat: pat)|* => $ } } } macro_rules! get_module_type_resolver { - ($module: expr, $crate_libs: expr, $crate_types: expr) => { { - let module: &str = &$module; + ($type_in_module: expr, $crate_types: expr) => { { + let module: &str = &$type_in_module; let mut module_iter = module.rsplitn(2, "::"); module_iter.next().unwrap(); let module = module_iter.next().unwrap(); @@ -304,7 +307,7 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty (s, _i, _) => { if let Some(supertrait) = types.crate_types.traits.get(s) { supertrait_name = s.to_string(); - supertrait_resolver = get_module_type_resolver!(supertrait_name, types.crate_libs, types.crate_types); + supertrait_resolver = get_module_type_resolver!(supertrait_name, types.crate_types); gen_types.learn_associated_types(&supertrait, &supertrait_resolver); break; } @@ -554,8 +557,8 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty writeln!(w, "unsafe impl Send for {} {{}}", trait_name).unwrap(); writeln!(w, "unsafe impl Sync for {} {{}}", trait_name).unwrap(); - writeln!(w, "#[no_mangle]").unwrap(); - writeln!(w, "pub(crate) extern \"C\" fn {}_clone_fields(orig: &{}) -> {} {{", trait_name, trait_name, trait_name).unwrap(); + writeln!(w, "#[allow(unused)]").unwrap(); + writeln!(w, "pub(crate) fn {}_clone_fields(orig: &{}) -> {} {{", trait_name, trait_name, trait_name).unwrap(); writeln!(w, "\t{} {{", trait_name).unwrap(); writeln!(w, "\t\tthis_arg: orig.this_arg,").unwrap(); for (field, clone_fn, _) in generated_fields.iter() { @@ -601,10 +604,10 @@ fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, ty }, (s, i, generic_args) => { if let Some(supertrait) = types.crate_types.traits.get(s) { - let resolver = get_module_type_resolver!(s, types.crate_libs, types.crate_types); + let resolver = get_module_type_resolver!(s, types.crate_types); macro_rules! impl_supertrait { ($s: expr, $supertrait: expr, $i: expr, $generic_args: expr) => { - let resolver = get_module_type_resolver!($s, types.crate_libs, types.crate_types); + let resolver = get_module_type_resolver!($s, types.crate_types); // Blindly assume that the same imports where `supertrait` is defined are also // imported here. This will almost certainly break at some point, but it should be @@ -813,6 +816,19 @@ fn writeln_struct<'a, 'b, W: std::io::Write>(w: &mut W, s: &'a syn::ItemStruct, if all_fields_settable { // Build a constructor! writeln!(w, "/// Constructs a new {} given each field", struct_name).unwrap(); + match &s.fields { + syn::Fields::Named(fields) => { + writeln_arg_docs(w, &[], "", types, Some(&gen_types), + fields.named.iter().map(|field| (format!("{}_arg", field.ident.as_ref().unwrap()), &field.ty)), + None); + }, + syn::Fields::Unnamed(fields) => { + writeln_arg_docs(w, &[], "", types, Some(&gen_types), + fields.unnamed.iter().enumerate().map(|(idx, field)| (format!("{}_arg", ('a' as u8 + idx as u8)), &field.ty)), + None); + }, + syn::Fields::Unit => {}, + } write!(w, "#[must_use]\n#[no_mangle]\npub extern \"C\" fn {}_new(", struct_name).unwrap(); match &s.fields { @@ -959,7 +975,7 @@ fn writeln_impl(w: &mut W, w_uses: &mut HashSet { if let Some(supertrait) = types.crate_types.traits.get(s) { supertrait_name = s.to_string(); - supertrait_resolver = get_module_type_resolver!(supertrait_name, types.crate_libs, types.crate_types); + supertrait_resolver = get_module_type_resolver!(supertrait_name, types.crate_types); gen_types.learn_associated_types(&supertrait, &supertrait_resolver); break; } @@ -970,7 +986,7 @@ fn writeln_impl(w: &mut W, w_uses: &mut HashSet(w: &mut W, w_uses: &mut HashSet(w: &mut W, w_uses: &mut HashSet crate::{} {{\n", ident, trait_obj.ident, ident, full_trait_path).unwrap(); writeln!(w, "\tcrate::{} {{", full_trait_path).unwrap(); - writeln!(w, "\t\tthis_arg: unsafe {{ ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void }},").unwrap(); + if types.c_type_has_inner_from_path(&resolved_path) { + writeln!(w, "\t\tthis_arg: unsafe {{ ObjOps::untweak_ptr((*this_arg).inner) as *mut c_void }},").unwrap(); + } else { + writeln!(w, "\t\tthis_arg: unsafe {{ ObjOps::untweak_ptr(this_arg as *const {} as *mut {}) as *mut c_void }},", ident, ident).unwrap(); + } writeln!(w, "\t\tfree: None,").unwrap(); macro_rules! write_meth { @@ -1103,7 +1127,7 @@ fn writeln_impl(w: &mut W, w_uses: &mut HashSet { if let Some(supertrait_obj) = types.crate_types.traits.get(s) { @@ -1242,15 +1266,22 @@ fn writeln_impl(w: &mut W, w_uses: &mut HashSet { - if types.crate_types.traits.get(s).is_some() { - assert!(!types.is_clonable(s)); // We don't currently support cloning with a clonable supertrait - writeln!(w, "\tnew_obj.{}.this_arg = new_obj.this_arg;", t).unwrap(); - writeln!(w, "\tnew_obj.{}.free = None;", t).unwrap(); + + fn seek_supertraits(w: &mut W, pfx: &str, tr: &syn::ItemTrait, types: &TypeResolver) { + walk_supertraits!(tr, Some(types), ( + (s, t, _) => { + if types.crate_types.traits.get(s).is_some() { + assert!(!types.is_clonable(s)); // We don't currently support cloning with a clonable supertrait + writeln!(w, "\tnew_obj.{}{}.this_arg = new_obj.this_arg;", pfx, t).unwrap(); + writeln!(w, "\tnew_obj.{}{}.free = None;", pfx, t).unwrap(); + let tr = types.crate_types.traits.get(s).unwrap(); + let resolver = get_module_type_resolver!(s, types.crate_types); + seek_supertraits(w, &format!("{}.", t), tr, &resolver); + } } - } - ) ); + ) ); + } + seek_supertraits(w, "", trait_obj, types); writeln!(w, "}}").unwrap(); } write!(w, "\n").unwrap(); @@ -1326,7 +1357,7 @@ fn writeln_impl(w: &mut W, w_uses: &mut HashSet *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, "\tBox::into_raw(Box::new(unsafe {{ (*(this_ptr as *const native{})).clone() }})) as *mut c_void", ident).unwrap(); writeln!(w, "}}").unwrap(); writeln!(w, "#[no_mangle]").unwrap(); writeln!(w, "/// Creates a copy of the {}", ident).unwrap(); @@ -1917,7 +1948,18 @@ fn writeln_enum<'a, 'b, W: std::io::Write>(w: &mut W, e: &'a syn::ItemEnum, type writeln!(w, "pub extern \"C\" fn {}_clone(orig: &{}) -> {} {{", e.ident, e.ident, e.ident).unwrap(); writeln!(w, "\torig.clone()").unwrap(); writeln!(w, "}}").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 {{", e.ident).unwrap(); + writeln!(w, "\tBox::into_raw(Box::new(unsafe {{ (*(this_ptr as *const {})).clone() }})) as *mut c_void", e.ident).unwrap(); + writeln!(w, "}}").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 {}_free_void(this_ptr: *mut c_void) {{", e.ident).unwrap(); + writeln!(w, "\tlet _ = unsafe {{ Box::from_raw(this_ptr as *mut {}) }};\n}}", e.ident).unwrap(); + w.write_all(&constr).unwrap(); write_cpp_wrapper(cpp_headers, &format!("{}", e.ident), needs_free, None); }