]> git.bitcoin.ninja Git - ldk-c-bindings/commitdiff
Somewhat more consistently map references to C
authorMatt Corallo <git@bluematt.me>
Mon, 26 Apr 2021 15:48:06 +0000 (15:48 +0000)
committerMatt Corallo <git@bluematt.me>
Mon, 26 Apr 2021 16:03:40 +0000 (16:03 +0000)
Instead of assuming that in some cases references are
pre-dereferenced before the to-C prefix and suffix conversions, we
no longer do that.

This matches other cases and is simply more consistent, apparently
fixing an old oversight in early generation code.

c-bindings-gen/src/blocks.rs
c-bindings-gen/src/main.rs
c-bindings-gen/src/types.rs
lightning-c-bindings/src/bitcoin/network.rs

index 56267792adecc779da22f0bd631449522dade700..2c51572a9378150fdc861c12c5c9e3edec11c7aa 100644 (file)
@@ -488,12 +488,7 @@ pub fn write_method_params<W: std::io::Write>(w: &mut W, sig: &syn::Signature, t
                                        return;
                                }
                        }
-                       if let syn::Type::Reference(r) = &**rtype {
-                               // We can't return a reference, cause we allocate things on the stack.
-                               types.write_c_type(w, &*r.elem, generics, true);
-                       } else {
-                               types.write_c_type(w, &*rtype, generics, true);
-                       }
+                       types.write_c_type(w, &*rtype, generics, true);
                },
                _ => {},
        }
@@ -634,13 +629,12 @@ pub fn write_method_call_params<W: std::io::Write>(w: &mut W, sig: &syn::Signatu
                                write!(w, "ret").unwrap();
                                types.write_from_c_conversion_suffix(w, &*rtype, generics);
                        } else {
-                               let ret_returned = if let syn::Type::Reference(_) = &**rtype { true } else { false };
                                let new_var = types.write_to_c_conversion_new_var(w, &format_ident!("ret"), &rtype, generics, true);
                                if new_var {
                                        write!(w, "\n\t{}", extra_indent).unwrap();
                                }
                                types.write_to_c_conversion_inline_prefix(w, &rtype, generics, true);
-                               write!(w, "{}ret", if ret_returned && !new_var { "*" } else { "" }).unwrap();
+                               write!(w, "ret").unwrap();
                                types.write_to_c_conversion_inline_suffix(w, &rtype, generics, true);
                        }
                }
index d107f3e9a6c66af726c4c84602c45c209686712c..a283c35d40d41d0612836ee3befdee6725f53182 100644 (file)
@@ -606,11 +606,7 @@ fn writeln_struct<'a, 'b, W: std::io::Write>(w: &mut W, s: &'a syn::ItemStruct,
                                                let local_var = types.write_to_c_conversion_new_var(w, &format_ident!("inner_val"), &ref_type, Some(&gen_types), true);
                                                if local_var { write!(w, "\n\t").unwrap(); }
                                                types.write_to_c_conversion_inline_prefix(w, &ref_type, Some(&gen_types), true);
-                                               if local_var {
-                                                       write!(w, "inner_val").unwrap();
-                                               } else {
-                                                       write!(w, "(*inner_val)").unwrap();
-                                               }
+                                               write!(w, "inner_val").unwrap();
                                                types.write_to_c_conversion_inline_suffix(w, &ref_type, Some(&gen_types), true);
                                                writeln!(w, "\n}}").unwrap();
                                        }
index c8b71d6b71b93cd99a302db51496b2a247f0c9c5..74379b843b9f67e751bb4a1b0ed7576a509e61e7 100644 (file)
@@ -971,11 +971,11 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "Option" => Some("local_"),
 
                        "[u8; 32]" if !is_ref => Some("crate::c_types::ThirtyTwoBytes { data: "),
-                       "[u8; 32]" if is_ref => Some("&"),
+                       "[u8; 32]" if is_ref => Some(""),
                        "[u8; 16]" if !is_ref => Some("crate::c_types::SixteenBytes { data: "),
                        "[u8; 10]" if !is_ref => Some("crate::c_types::TenBytes { data: "),
                        "[u8; 4]" if !is_ref => Some("crate::c_types::FourBytes { data: "),
-                       "[u8; 3]" if is_ref => Some("&"),
+                       "[u8; 3]" if is_ref => Some(""),
 
                        "[u8]" if is_ref => Some("local_"),
                        "[usize]" if is_ref => Some("local_"),
@@ -1583,6 +1583,10 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                let resolved_path = self.resolve_path(&p.path, generics);
                                if let Some(aliased_type) = self.crate_types.type_aliases.get(&resolved_path) {
                                        return self.write_conversion_inline_intern(w, aliased_type, None, is_ref, is_mut, ptr_for_ref, tupleconv, prefix, sliceconv, path_lookup, decl_lookup);
+                               } else if self.is_primitive(&resolved_path) {
+                                       if is_ref && prefix {
+                                               write!(w, "*").unwrap();
+                                       }
                                } else if let Some(c_type) = path_lookup(&resolved_path, is_ref, ptr_for_ref) {
                                        write!(w, "{}", c_type).unwrap();
                                } else if self.crate_types.opaques.get(&resolved_path).is_some() {
@@ -1664,20 +1668,20 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                |a, b, c| self.to_c_conversion_inline_prefix_from_path(a, b, c),
                                |w, decl_type, decl_path, is_ref, _is_mut| {
                                        match decl_type {
-                                               DeclType::MirroredEnum if is_ref && ptr_for_ref => write!(w, "crate::{}::from_native(&", decl_path).unwrap(),
-                                               DeclType::MirroredEnum if is_ref => write!(w, "&crate::{}::from_native(&", decl_path).unwrap(),
+                                               DeclType::MirroredEnum if is_ref && ptr_for_ref => write!(w, "crate::{}::from_native(", decl_path).unwrap(),
+                                               DeclType::MirroredEnum if is_ref => write!(w, "&crate::{}::from_native(", decl_path).unwrap(),
                                                DeclType::MirroredEnum => write!(w, "crate::{}::native_into(", decl_path).unwrap(),
                                                DeclType::EnumIgnored|DeclType::StructImported if is_ref && ptr_for_ref && from_ptr =>
                                                        write!(w, "crate::{} {{ inner: unsafe {{ (", decl_path).unwrap(),
                                                DeclType::EnumIgnored|DeclType::StructImported if is_ref && ptr_for_ref =>
-                                                       write!(w, "crate::{} {{ inner: unsafe {{ ( (&(", decl_path).unwrap(),
+                                                       write!(w, "crate::{} {{ inner: unsafe {{ ( (&(*", decl_path).unwrap(),
                                                DeclType::EnumIgnored|DeclType::StructImported if is_ref =>
                                                        write!(w, "&crate::{} {{ inner: unsafe {{ (", decl_path).unwrap(),
                                                DeclType::EnumIgnored|DeclType::StructImported if !is_ref && from_ptr =>
                                                        write!(w, "crate::{} {{ inner: ", decl_path).unwrap(),
                                                DeclType::EnumIgnored|DeclType::StructImported if !is_ref =>
                                                        write!(w, "crate::{} {{ inner: Box::into_raw(Box::new(", decl_path).unwrap(),
-                                               DeclType::Trait(_) if is_ref => write!(w, "&").unwrap(),
+                                               DeclType::Trait(_) if is_ref => write!(w, "").unwrap(),
                                                DeclType::Trait(_) if !is_ref => {},
                                                _ => panic!("{:?}", decl_path),
                                        }
index 02052e8213c82cb046ccedbf434e786cc3b76e10..0603404d51bb2594f98bc6ca40085ce9a2ac08cf 100644 (file)
@@ -24,7 +24,7 @@ impl Network {
                        Network::Signet => BitcoinNetwork::Signet,
                }
        }
-       pub(crate) fn from_bitcoin(net: BitcoinNetwork) -> Self {
+       pub(crate) fn from_bitcoin(net: &BitcoinNetwork) -> Self {
                match net {
                        BitcoinNetwork::Bitcoin => Network::Bitcoin,
                        BitcoinNetwork::Testnet => Network::Testnet,