Update type mappings for bitcoin 0.28.1
[ldk-c-bindings] / c-bindings-gen / src / types.rs
index da2d1807db088e6336b7a9186a0d9df85d0e338c..228d6e8f221fda5a13424f6fe66832c67e302c4d 100644 (file)
@@ -65,6 +65,17 @@ pub fn path_matches_nongeneric(p: &syn::Path, exp: &[&str]) -> bool {
        true
 }
 
+pub fn string_path_to_syn_path(path: &str) -> syn::Path {
+       let mut segments = syn::punctuated::Punctuated::new();
+       for seg in path.split("::") {
+               segments.push(syn::PathSegment {
+                       ident: syn::Ident::new(seg, Span::call_site()),
+                       arguments: syn::PathArguments::None,
+               });
+       }
+       syn::Path { leading_colon: Some(syn::Token![::](Span::call_site())), segments }
+}
+
 #[derive(Debug, PartialEq)]
 pub enum ExportStatus {
        Export,
@@ -391,7 +402,7 @@ pub enum DeclType<'a> {
 }
 
 pub struct ImportResolver<'mod_lifetime, 'crate_lft: 'mod_lifetime> {
-       crate_name: &'mod_lifetime str,
+       pub crate_name: &'mod_lifetime str,
        dependencies: &'mod_lifetime HashSet<syn::Ident>,
        module_path: &'mod_lifetime str,
        imports: HashMap<syn::Ident, (String, syn::Path)>,
@@ -546,10 +557,6 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr
                Self { crate_name, dependencies, module_path, imports, declared, priv_modules }
        }
 
-       pub fn get_declared_type(&self, ident: &syn::Ident) -> Option<&DeclType<'crate_lft>> {
-               self.declared.get(ident)
-       }
-
        pub fn maybe_resolve_declared(&self, id: &syn::Ident) -> Option<&DeclType<'crate_lft>> {
                self.declared.get(id)
        }
@@ -562,17 +569,6 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr
                } else { None }
        }
 
-       pub fn maybe_resolve_non_ignored_ident(&self, id: &syn::Ident) -> Option<String> {
-               if let Some((imp, _)) = self.imports.get(id) {
-                       Some(imp.clone())
-               } else if let Some(decl_type) = self.declared.get(id) {
-                       match decl_type {
-                               DeclType::StructIgnored => None,
-                               _ => Some(self.module_path.to_string() + "::" + &format!("{}", id)),
-                       }
-               } else { None }
-       }
-
        pub fn maybe_resolve_path(&self, p: &syn::Path, generics: Option<&GenericTypes>) -> Option<String> {
                if let Some(gen_types) = generics {
                        if let Some(resp) = gen_types.maybe_resolve_path(p) {
@@ -741,7 +737,7 @@ pub struct CrateTypes<'a> {
        /// Aliases from paths to some other Type
        pub type_aliases: HashMap<String, syn::Type>,
        /// Value is an alias to Key (maybe with some generics)
-       pub reverse_alias_map: HashMap<String, Vec<(syn::Path, syn::PathArguments)>>,
+       pub reverse_alias_map: HashMap<String, Vec<(String, syn::PathArguments)>>,
        /// Template continer types defined, map from mangled type name -> whether a destructor fn
        /// exists.
        ///
@@ -785,7 +781,7 @@ impl<'a> CrateTypes<'a> {
 pub struct TypeResolver<'mod_lifetime, 'crate_lft: 'mod_lifetime> {
        pub module_path: &'mod_lifetime str,
        pub crate_types: &'mod_lifetime CrateTypes<'crate_lft>,
-       types: ImportResolver<'mod_lifetime, 'crate_lft>,
+       pub types: ImportResolver<'mod_lifetime, 'crate_lft>,
 }
 
 /// Returned by write_empty_rust_val_check_suffix to indicate what type of dereferencing needs to
@@ -894,14 +890,11 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "bitcoin::bech32::u5"|"bech32::u5" => Some("crate::c_types::u5"),
                        "core::num::NonZeroU8" => Some("u8"),
 
-                       "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"|"secp256k1::key::PublicKey"
-                               => Some("crate::c_types::PublicKey"),
-                       "bitcoin::secp256k1::Signature" => Some("crate::c_types::Signature"),
-                       "bitcoin::secp256k1::recovery::RecoverableSignature" => Some("crate::c_types::RecoverableSignature"),
-                       "bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey"
-                               if is_ref  => Some("*const [u8; 32]"),
-                       "bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey"
-                               if !is_ref => Some("crate::c_types::SecretKey"),
+                       "bitcoin::secp256k1::PublicKey" => Some("crate::c_types::PublicKey"),
+                       "bitcoin::secp256k1::ecdsa::Signature" => Some("crate::c_types::Signature"),
+                       "bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some("crate::c_types::RecoverableSignature"),
+                       "bitcoin::secp256k1::SecretKey" if is_ref  => Some("*const [u8; 32]"),
+                       "bitcoin::secp256k1::SecretKey" if !is_ref => Some("crate::c_types::SecretKey"),
                        "bitcoin::blockdata::script::Script" if is_ref => Some("crate::c_types::u8slice"),
                        "bitcoin::blockdata::script::Script" if !is_ref => Some("crate::c_types::derived::CVec_u8Z"),
                        "bitcoin::blockdata::transaction::OutPoint" => Some("crate::lightning::chain::transaction::OutPoint"),
@@ -979,17 +972,13 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "bitcoin::bech32::u5"|"bech32::u5" => Some(""),
                        "core::num::NonZeroU8" => Some("core::num::NonZeroU8::new("),
 
-                       "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"|"secp256k1::key::PublicKey"
-                               if is_ref => Some("&"),
-                       "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"|"secp256k1::key::PublicKey"
-                               => Some(""),
-                       "bitcoin::secp256k1::Signature" if is_ref => Some("&"),
-                       "bitcoin::secp256k1::Signature" => Some(""),
-                       "bitcoin::secp256k1::recovery::RecoverableSignature" => Some(""),
-                       "bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey"
-                               if is_ref => Some("&::bitcoin::secp256k1::key::SecretKey::from_slice(&unsafe { *"),
-                       "bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey"
-                               if !is_ref => Some(""),
+                       "bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" if is_ref => Some("&"),
+                       "bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" => Some(""),
+                       "bitcoin::secp256k1::ecdsa::Signature" if is_ref => Some("&"),
+                       "bitcoin::secp256k1::ecdsa::Signature" => Some(""),
+                       "bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some(""),
+                       "bitcoin::secp256k1::SecretKey" if is_ref => Some("&::bitcoin::secp256k1::SecretKey::from_slice(&unsafe { *"),
+                       "bitcoin::secp256k1::SecretKey" if !is_ref => Some(""),
                        "bitcoin::blockdata::script::Script" if is_ref => Some("&::bitcoin::blockdata::script::Script::from(Vec::from("),
                        "bitcoin::blockdata::script::Script" if !is_ref => Some("::bitcoin::blockdata::script::Script::from("),
                        "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" if is_ref => Some("&"),
@@ -1067,14 +1056,11 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "bitcoin::bech32::u5"|"bech32::u5" => Some(".into()"),
                        "core::num::NonZeroU8" => Some(").expect(\"Value must be non-zero\")"),
 
-                       "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"|"secp256k1::key::PublicKey"
-                               => Some(".into_rust()"),
-                       "bitcoin::secp256k1::Signature" => Some(".into_rust()"),
-                       "bitcoin::secp256k1::recovery::RecoverableSignature" => Some(".into_rust()"),
-                       "bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey"
-                               if !is_ref => Some(".into_rust()"),
-                       "bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey"
-                               if is_ref => Some("}[..]).unwrap()"),
+                       "bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" => Some(".into_rust()"),
+                       "bitcoin::secp256k1::ecdsa::Signature" => Some(".into_rust()"),
+                       "bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some(".into_rust()"),
+                       "bitcoin::secp256k1::SecretKey" if !is_ref => Some(".into_rust()"),
+                       "bitcoin::secp256k1::SecretKey" if is_ref => Some("}[..]).unwrap()"),
                        "bitcoin::blockdata::script::Script" if is_ref => Some(".to_slice()))"),
                        "bitcoin::blockdata::script::Script" if !is_ref => Some(".into_rust())"),
                        "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" => Some(".into_bitcoin()"),
@@ -1161,14 +1147,11 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
 
                        "bitcoin::bech32::u5"|"bech32::u5" => Some(""),
 
-                       "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"|"secp256k1::key::PublicKey"
-                               => Some("crate::c_types::PublicKey::from_rust(&"),
-                       "bitcoin::secp256k1::Signature" => Some("crate::c_types::Signature::from_rust(&"),
-                       "bitcoin::secp256k1::recovery::RecoverableSignature" => Some("crate::c_types::RecoverableSignature::from_rust(&"),
-                       "bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey"
-                               if is_ref => Some(""),
-                       "bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey"
-                               if !is_ref => Some("crate::c_types::SecretKey::from_rust("),
+                       "bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" => Some("crate::c_types::PublicKey::from_rust(&"),
+                       "bitcoin::secp256k1::ecdsa::Signature" => Some("crate::c_types::Signature::from_rust(&"),
+                       "bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some("crate::c_types::RecoverableSignature::from_rust(&"),
+                       "bitcoin::secp256k1::SecretKey" if is_ref => Some(""),
+                       "bitcoin::secp256k1::SecretKey" if !is_ref => Some("crate::c_types::SecretKey::from_rust("),
                        "bitcoin::blockdata::script::Script" if is_ref => Some("crate::c_types::u8slice::from_slice(&"),
                        "bitcoin::blockdata::script::Script" if !is_ref => Some(""),
                        "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" if is_ref => Some("crate::c_types::Transaction::from_bitcoin("),
@@ -1240,14 +1223,11 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
 
                        "bitcoin::bech32::u5"|"bech32::u5" => Some(".into()"),
 
-                       "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"|"secp256k1::key::PublicKey"
-                               => Some(")"),
-                       "bitcoin::secp256k1::Signature" => Some(")"),
-                       "bitcoin::secp256k1::recovery::RecoverableSignature" => Some(")"),
-                       "bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey"
-                               if !is_ref => Some(")"),
-                       "bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey"
-                               if is_ref => Some(".as_ref()"),
+                       "bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" => Some(")"),
+                       "bitcoin::secp256k1::ecdsa::Signature" => Some(")"),
+                       "bitcoin::secp256k1::ecdsa::RecoverableSignature" => Some(")"),
+                       "bitcoin::secp256k1::SecretKey" if !is_ref => Some(")"),
+                       "bitcoin::secp256k1::SecretKey" if is_ref => Some(".as_ref()"),
                        "bitcoin::blockdata::script::Script" if is_ref => Some("[..])"),
                        "bitcoin::blockdata::script::Script" if !is_ref => Some(".into_bytes().into()"),
                        "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" => Some(")"),
@@ -1281,8 +1261,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
        fn empty_val_check_suffix_from_path(&self, full_path: &str) -> Option<&str> {
                match full_path {
                        "lightning::ln::PaymentSecret" => Some(".data == [0; 32]"),
-                       "secp256k1::key::PublicKey"|"bitcoin::secp256k1::key::PublicKey" => Some(".is_null()"),
-                       "bitcoin::secp256k1::Signature" => Some(".is_null()"),
+                       "secp256k1::PublicKey"|"bitcoin::secp256k1::PublicKey" => Some(".is_null()"),
+                       "bitcoin::secp256k1::ecdsa::Signature" => Some(".is_null()"),
                        _ => None
                }
        }
@@ -1583,9 +1563,6 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
        // *** Type definition during main.rs processing ***
        // *************************************************
 
-       pub fn get_declared_type(&'a self, ident: &syn::Ident) -> Option<&'a DeclType<'c>> {
-               self.types.get_declared_type(ident)
-       }
        /// Returns true if the object at the given path is mapped as X { inner: *mut origX, .. }.
        pub fn c_type_has_inner_from_path(&self, full_path: &str) -> bool {
                self.crate_types.opaques.get(full_path).is_some()
@@ -1610,10 +1587,6 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                self.types.maybe_resolve_ident(id)
        }
 
-       pub fn maybe_resolve_non_ignored_ident(&self, id: &syn::Ident) -> Option<String> {
-               self.types.maybe_resolve_non_ignored_ident(id)
-       }
-
        pub fn maybe_resolve_path(&self, p_arg: &syn::Path, generics: Option<&GenericTypes>) -> Option<String> {
                self.types.maybe_resolve_path(p_arg, generics)
        }
@@ -2874,7 +2847,6 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                assert!(self.write_c_type_intern(w, t, generics, false, false, ptr_for_ref, true, false));
        }
        pub fn understood_c_path(&self, p: &syn::Path) -> bool {
-               if p.leading_colon.is_some() { return false; }
                self.write_c_path_intern(&mut std::io::sink(), p, None, false, false, false, false, true)
        }
        pub fn understood_c_type(&self, t: &syn::Type, generics: Option<&GenericTypes>) -> bool {