Support tuples containing array elements
[ldk-c-bindings] / c-bindings-gen / src / types.rs
index 5ced9104b030b44aa78fb6a7586565491cdf9a48..73ba02a3e1dc87b4501c0a38990c5f4b12ef54dd 100644 (file)
@@ -539,6 +539,7 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr
                let mut imports = HashMap::new();
                // Add primitives to the "imports" list:
                Self::insert_primitive(&mut imports, "bool");
+               Self::insert_primitive(&mut imports, "u128");
                Self::insert_primitive(&mut imports, "u64");
                Self::insert_primitive(&mut imports, "u32");
                Self::insert_primitive(&mut imports, "u16");
@@ -667,7 +668,7 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr
                                // Now that we've resolved the path to the path as-imported, check whether the path
                                // is actually a pub(.*) use statement and map it to the real path.
                                let path_tmp = path.clone();
-                               let crate_name = path_tmp.splitn(1, "::").next().unwrap();
+                               let crate_name = path_tmp.splitn(2, "::").next().unwrap();
                                let mut module_riter = path_tmp.rsplitn(2, "::");
                                let obj = module_riter.next().unwrap();
                                if let Some(module_path) = module_riter.next() {
@@ -797,15 +798,18 @@ impl FullLibraryAST {
 /// List of manually-generated types which are clonable
 fn initial_clonable_types() -> HashSet<String> {
        let mut res = HashSet::new();
-       res.insert("crate::c_types::u5".to_owned());
+       res.insert("crate::c_types::U5".to_owned());
+       res.insert("crate::c_types::U128".to_owned());
        res.insert("crate::c_types::FourBytes".to_owned());
        res.insert("crate::c_types::TwelveBytes".to_owned());
        res.insert("crate::c_types::SixteenBytes".to_owned());
        res.insert("crate::c_types::TwentyBytes".to_owned());
        res.insert("crate::c_types::ThirtyTwoBytes".to_owned());
+       res.insert("crate::c_types::EightU16s".to_owned());
        res.insert("crate::c_types::SecretKey".to_owned());
        res.insert("crate::c_types::PublicKey".to_owned());
        res.insert("crate::c_types::Transaction".to_owned());
+       res.insert("crate::c_types::Witness".to_owned());
        res.insert("crate::c_types::TxOut".to_owned());
        res.insert("crate::c_types::Signature".to_owned());
        res.insert("crate::c_types::RecoverableSignature".to_owned());
@@ -968,6 +972,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "[u8; 12]" if !is_ref => Some("crate::c_types::TwelveBytes"),
                        "[u8; 4]" if !is_ref => Some("crate::c_types::FourBytes"),
                        "[u8; 3]" if !is_ref => Some("crate::c_types::ThreeBytes"), // Used for RGB values
+                       "[u16; 8]" if !is_ref => Some("crate::c_types::EightU16s"),
 
                        "str" if is_ref => Some("crate::c_types::Str"),
                        "alloc::string::String"|"String" => Some("crate::c_types::Str"),
@@ -987,7 +992,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "core::num::ParseIntError" => Some("crate::c_types::Error"),
                        "core::str::Utf8Error" => Some("crate::c_types::Error"),
 
-                       "bitcoin::bech32::u5"|"bech32::u5" => Some("crate::c_types::u5"),
+                       "bitcoin::bech32::u5"|"bech32::u5" => Some("crate::c_types::U5"),
+                       "u128" => Some("crate::c_types::U128"),
                        "core::num::NonZeroU8" => Some("u8"),
 
                        "secp256k1::PublicKey"|"bitcoin::secp256k1::PublicKey" => Some("crate::c_types::PublicKey"),
@@ -1003,6 +1009,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "bitcoin::blockdata::script::Script" if !is_ref => Some("crate::c_types::derived::CVec_u8Z"),
                        "bitcoin::blockdata::transaction::OutPoint" => Some("crate::lightning::chain::transaction::OutPoint"),
                        "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" => Some("crate::c_types::Transaction"),
+                       "bitcoin::Witness" => Some("crate::c_types::Witness"),
                        "bitcoin::blockdata::transaction::TxOut" if !is_ref => Some("crate::c_types::TxOut"),
                        "bitcoin::network::constants::Network" => Some("crate::bitcoin::network::Network"),
                        "bitcoin::util::address::WitnessVersion" => Some("crate::c_types::WitnessVersion"),
@@ -1021,10 +1028,12 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                if !is_ref => Some("crate::c_types::ThirtyTwoBytes"),
                        "bitcoin::secp256k1::Message" if !is_ref => Some("crate::c_types::ThirtyTwoBytes"),
                        "lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"
-                       |"lightning::ln::channelmanager::PaymentId"|"lightning::chain::keysinterface::KeyMaterial"
+                       |"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId"
+                       |"lightning::chain::keysinterface::KeyMaterial"
                                if is_ref => Some("*const [u8; 32]"),
                        "lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"
-                       |"lightning::ln::channelmanager::PaymentId"|"lightning::chain::keysinterface::KeyMaterial"
+                       |"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId"
+                       |"lightning::chain::keysinterface::KeyMaterial"
                                if !is_ref => Some("crate::c_types::ThirtyTwoBytes"),
 
                        "lightning::io::Read" => Some("crate::c_types::u8slice"),
@@ -1053,6 +1062,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "[u8; 12]" if !is_ref => Some(""),
                        "[u8; 4]" if !is_ref => Some(""),
                        "[u8; 3]" if !is_ref => Some(""),
+                       "[u16; 8]" if !is_ref => Some(""),
 
                        "[u8]" if is_ref => Some(""),
                        "[usize]" if is_ref => Some(""),
@@ -1075,6 +1085,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "std::time::SystemTime" => Some("(::std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs("),
 
                        "bitcoin::bech32::u5"|"bech32::u5" => Some(""),
+                       "u128" => Some(""),
                        "core::num::NonZeroU8" => Some("core::num::NonZeroU8::new("),
 
                        "bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" if is_ref => Some("&"),
@@ -1091,6 +1102,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "bitcoin::blockdata::script::Script" if !is_ref => Some("::bitcoin::blockdata::script::Script::from("),
                        "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" if is_ref => Some("&"),
                        "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" => Some(""),
+                       "bitcoin::Witness" if is_ref => Some("&"),
+                       "bitcoin::Witness" => Some(""),
                        "bitcoin::blockdata::transaction::OutPoint" => Some("crate::c_types::C_to_bitcoin_outpoint("),
                        "bitcoin::blockdata::transaction::TxOut" if !is_ref => Some(""),
                        "bitcoin::network::constants::Network" => Some(""),
@@ -1118,6 +1131,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "lightning::ln::PaymentSecret" if !is_ref => Some("::lightning::ln::PaymentSecret("),
                        "lightning::ln::channelmanager::PaymentId" if !is_ref => Some("::lightning::ln::channelmanager::PaymentId("),
                        "lightning::ln::channelmanager::PaymentId" if is_ref=> Some("&::lightning::ln::channelmanager::PaymentId( unsafe { *"),
+                       "lightning::ln::channelmanager::InterceptId" if !is_ref => Some("::lightning::ln::channelmanager::InterceptId("),
+                       "lightning::ln::channelmanager::InterceptId" if is_ref=> Some("&::lightning::ln::channelmanager::InterceptId( unsafe { *"),
                        "lightning::chain::keysinterface::KeyMaterial" if !is_ref => Some("::lightning::chain::keysinterface::KeyMaterial("),
                        "lightning::chain::keysinterface::KeyMaterial" if is_ref=> Some("&::lightning::chain::keysinterface::KeyMaterial( unsafe { *"),
 
@@ -1143,6 +1158,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "[u8; 12]" if !is_ref => Some(".data"),
                        "[u8; 4]" if !is_ref => Some(".data"),
                        "[u8; 3]" if !is_ref => Some(".data"),
+                       "[u16; 8]" if !is_ref => Some(".data"),
 
                        "[u8]" if is_ref => Some(".to_slice()"),
                        "[usize]" if is_ref => Some(".to_slice()"),
@@ -1164,6 +1180,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "std::time::SystemTime" => Some("))"),
 
                        "bitcoin::bech32::u5"|"bech32::u5" => Some(".into()"),
+                       "u128" => Some(".into()"),
                        "core::num::NonZeroU8" => Some(").expect(\"Value must be non-zero\")"),
 
                        "bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" => Some(".into_rust()"),
@@ -1177,6 +1194,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "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()"),
+                       "bitcoin::Witness" => Some(".into_bitcoin()"),
                        "bitcoin::blockdata::transaction::OutPoint" => Some(")"),
                        "bitcoin::blockdata::transaction::TxOut" if !is_ref => Some(".into_rust()"),
                        "bitcoin::network::constants::Network" => Some(".into_bitcoin()"),
@@ -1193,10 +1211,12 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "bitcoin::hash_types::Txid" => Some(".data[..]).unwrap()"),
                        "bitcoin::hash_types::BlockHash" if !is_ref => Some(".data[..]).unwrap()"),
                        "lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"
-                       |"lightning::ln::channelmanager::PaymentId"|"lightning::chain::keysinterface::KeyMaterial"
+                       |"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId"
+                       |"lightning::chain::keysinterface::KeyMaterial"
                                if !is_ref => Some(".data)"),
                        "lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"
-                       |"lightning::ln::channelmanager::PaymentId"|"lightning::chain::keysinterface::KeyMaterial"
+                       |"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId"
+                       |"lightning::chain::keysinterface::KeyMaterial"
                                if is_ref => Some(" })"),
 
                        // List of traits we map (possibly during processing of other files):
@@ -1237,6 +1257,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "[u8; 12]" if !is_ref => Some("crate::c_types::TwelveBytes { data: "),
                        "[u8; 4]" if !is_ref => Some("crate::c_types::FourBytes { data: "),
                        "[u8; 3]" if is_ref => Some(""),
+                       "[u16; 8]" if !is_ref => Some("crate::c_types::EightU16s { data: "),
 
                        "[u8]" if is_ref => Some("local_"),
                        "[usize]" if is_ref => Some("local_"),
@@ -1261,6 +1282,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "core::str::Utf8Error" => Some("crate::c_types::Error { _dummy: 0 } /*"),
 
                        "bitcoin::bech32::u5"|"bech32::u5" => Some(""),
+                       "u128" => Some(""),
 
                        "bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" => Some("crate::c_types::PublicKey::from_rust(&"),
                        "bitcoin::secp256k1::ecdsa::Signature" => Some("crate::c_types::Signature::from_rust(&"),
@@ -1274,6 +1296,8 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "bitcoin::blockdata::script::Script" if !is_ref => Some(""),
                        "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" if is_ref => Some("crate::c_types::Transaction::from_bitcoin("),
                        "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" => Some("crate::c_types::Transaction::from_bitcoin(&"),
+                       "bitcoin::Witness" if is_ref => Some("crate::c_types::Witness::from_bitcoin("),
+                       "bitcoin::Witness" if !is_ref => Some("crate::c_types::Witness::from_bitcoin(&"),
                        "bitcoin::blockdata::transaction::OutPoint" => Some("crate::c_types::bitcoin_to_C_outpoint("),
                        "bitcoin::blockdata::transaction::TxOut" if !is_ref => Some("crate::c_types::TxOut::from_rust("),
                        "bitcoin::network::constants::Network" => Some("crate::bitcoin::network::Network::from_bitcoin("),
@@ -1290,10 +1314,12 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                if !is_ref => Some("crate::c_types::ThirtyTwoBytes { data: "),
                        "bitcoin::secp256k1::Message" if !is_ref => Some("crate::c_types::ThirtyTwoBytes { data: "),
                        "lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"
-                       |"lightning::ln::channelmanager::PaymentId"|"lightning::chain::keysinterface::KeyMaterial"
+                       |"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId"
+                       |"lightning::chain::keysinterface::KeyMaterial"
                                if is_ref => Some("&"),
                        "lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"
-                       |"lightning::ln::channelmanager::PaymentId"|"lightning::chain::keysinterface::KeyMaterial"
+                       |"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId"
+                       |"lightning::chain::keysinterface::KeyMaterial"
                                if !is_ref => Some("crate::c_types::ThirtyTwoBytes { data: "),
 
                        "lightning::io::Read" => Some("crate::c_types::u8slice::from_vec(&crate::c_types::reader_to_vec("),
@@ -1317,6 +1343,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "[u8; 12]" if !is_ref => Some(" }"),
                        "[u8; 4]" if !is_ref => Some(" }"),
                        "[u8; 3]" if is_ref => Some(""),
+                       "[u16; 8]" if !is_ref => Some(" }"),
 
                        "[u8]" if is_ref => Some(""),
                        "[usize]" if is_ref => Some(""),
@@ -1341,6 +1368,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "core::str::Utf8Error" => Some("*/"),
 
                        "bitcoin::bech32::u5"|"bech32::u5" => Some(".into()"),
+                       "u128" => Some(".into()"),
 
                        "bitcoin::secp256k1::PublicKey"|"secp256k1::PublicKey" => Some(")"),
                        "bitcoin::secp256k1::ecdsa::Signature" => Some(")"),
@@ -1353,6 +1381,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "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(")"),
+                       "bitcoin::Witness" => Some(")"),
                        "bitcoin::blockdata::transaction::OutPoint" => Some(")"),
                        "bitcoin::blockdata::transaction::TxOut" if !is_ref => Some(")"),
                        "bitcoin::network::constants::Network" => Some(")"),
@@ -1369,10 +1398,12 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                if !is_ref => Some(".into_inner() }"),
                        "bitcoin::secp256k1::Message" if !is_ref => Some(".as_ref().clone() }"),
                        "lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"
-                       |"lightning::ln::channelmanager::PaymentId"|"lightning::chain::keysinterface::KeyMaterial"
+                       |"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId"
+                       |"lightning::chain::keysinterface::KeyMaterial"
                                if is_ref => Some(".0"),
                        "lightning::ln::PaymentHash"|"lightning::ln::PaymentPreimage"|"lightning::ln::PaymentSecret"
-                       |"lightning::ln::channelmanager::PaymentId"|"lightning::chain::keysinterface::KeyMaterial"
+                       |"lightning::ln::channelmanager::PaymentId"|"lightning::ln::channelmanager::InterceptId"
+                       |"lightning::chain::keysinterface::KeyMaterial"
                                if !is_ref => Some(".0 }"),
 
                        "lightning::io::Read" => Some("))"),
@@ -2079,11 +2110,12 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                } else { unimplemented!(); }
                        },
                        syn::Type::Array(a) => {
-                               // We assume all arrays contain only [int_literal; X]s.
-                               // This may result in some outputs not compiling.
-                               if let syn::Expr::Lit(l) = &a.len {
-                                       if let syn::Lit::Int(i) = &l.lit {
-                                               write!(w, "{}", path_lookup(&format!("[u8; {}]", i.base10_digits()), is_ref, ptr_for_ref).unwrap()).unwrap();
+                               if let syn::Type::Path(p) = &*a.elem {
+                                       let inner_ty = self.resolve_path(&p.path, generics);
+                                       if let syn::Expr::Lit(l) = &a.len {
+                                               if let syn::Lit::Int(i) = &l.lit {
+                                                       write!(w, "{}", path_lookup(&format!("[{}; {}]", inner_ty, i.base10_digits()), is_ref, ptr_for_ref).unwrap()).unwrap();
+                                               } else { unimplemented!(); }
                                        } else { unimplemented!(); }
                                } else { unimplemented!(); }
                        },
@@ -2352,7 +2384,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                        write!(w, "let mut local_{}{} = ", ident,
                                                if (!to_c && needs_ref_map) || (to_c && $container_type == "Option" && contains_slice) {"_base"} else { "" }).unwrap();
                                        if prefix_location == ContainerPrefixLocation::OutsideConv {
-                                               var_prefix(w, $args_iter().next().unwrap(), generics, is_ref, ptr_for_ref, true);
+                                               var_prefix(w, $args_iter().next().unwrap(), generics, is_ref, true, true);
                                        }
                                        write!(w, "{}{}", prefix, var).unwrap();
 
@@ -2785,6 +2817,13 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                                                if let syn::Type::Path(p) = &*refelem.elem {
                                                                        write_path!(p, Some(&mut mangled_tuple_type));
                                                                } else { return false; }
+                                                       } else if let syn::Type::Array(_) = elem {
+                                                               let mut resolved = Vec::new();
+                                                               if !self.write_c_type_intern(&mut resolved, &elem, generics, false, false, true, false, true) { return false; }
+                                                               let array_inner = String::from_utf8(resolved).unwrap();
+                                                               let arr_name = array_inner.split("::").last().unwrap();
+                                                               write!(w, "{}", arr_name).unwrap();
+                                                               write!(mangled_type, "{}", arr_name).unwrap();
                                                        } else { return false; }
                                                }
                                                write!(w, "Z").unwrap();
@@ -2933,15 +2972,14 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                } else if is_ref {
                                        write!(w, "*const [").unwrap();
                                        if !self.write_c_type_intern(w, &a.elem, generics, false, false, ptr_for_ref, with_ref_lifetime, c_ty) { return false; }
-                               } else {
-                                       let mut typecheck = Vec::new();
-                                       if !self.write_c_type_intern(&mut typecheck, &a.elem, generics, false, false, ptr_for_ref, with_ref_lifetime, c_ty) { return false; }
-                                       if typecheck[..] != ['u' as u8, '8' as u8] { return false; }
                                }
                                if let syn::Expr::Lit(l) = &a.len {
                                        if let syn::Lit::Int(i) = &l.lit {
+                                               let mut inner_ty = Vec::new();
+                                               if !self.write_c_type_intern(&mut inner_ty, &*a.elem, generics, false, false, ptr_for_ref, false, c_ty) { return false; }
+                                               let inner_ty_str = String::from_utf8(inner_ty).unwrap();
                                                if !is_ref {
-                                                       if let Some(ty) = self.c_type_from_path(&format!("[u8; {}]", i.base10_digits()), false, ptr_for_ref) {
+                                                       if let Some(ty) = self.c_type_from_path(&format!("[{}; {}]", inner_ty_str, i.base10_digits()), false, ptr_for_ref) {
                                                                write!(w, "{}", ty).unwrap();
                                                                true
                                                        } else { false }