X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=c-bindings-gen%2Fsrc%2Fmain.rs;h=8d158e2f4816ae2997ad4aa6be404d45ef7f76c2;hb=edf9c17157cacd2a213be288922f957a5cc2d892;hp=9bedbf577a7d43901e1c1a23e256e7db4b719b9c;hpb=805585a423a0f4553fb04845def2ccd64adb3efd;p=rust-lightning diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index 9bedbf577..8d158e2f4 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -38,7 +38,7 @@ fn convert_macro(w: &mut W, macro_path: &syn::Path, stream: & if let Some(s) = types.maybe_resolve_ident(&struct_for) { if !types.crate_types.opaques.get(&s).is_some() { return; } writeln!(w, "#[no_mangle]").unwrap(); - writeln!(w, "pub extern \"C\" fn {}_write(obj: *const {}) -> crate::c_types::derived::CVec_u8Z {{", struct_for, struct_for).unwrap(); + writeln!(w, "pub extern \"C\" fn {}_write(obj: &{}) -> crate::c_types::derived::CVec_u8Z {{", struct_for, struct_for).unwrap(); writeln!(w, "\tcrate::c_types::serialize_obj(unsafe {{ &(*(*obj).inner) }})").unwrap(); writeln!(w, "}}").unwrap(); writeln!(w, "#[no_mangle]").unwrap(); @@ -65,7 +65,7 @@ fn maybe_convert_trait_impl(w: &mut W, trait_path: &syn::Path let full_obj_path; let mut has_inner = false; if let syn::Type::Path(ref p) = for_ty { - if let Some(ident) = p.path.get_ident() { + if let Some(ident) = single_ident_generic_path_to_ident(&p.path) { for_obj = format!("{}", ident); full_obj_path = for_obj.clone(); has_inner = types.c_type_has_inner_from_path(&types.resolve_path(&p.path, Some(generics))); @@ -83,7 +83,7 @@ fn maybe_convert_trait_impl(w: &mut W, trait_path: &syn::Path match &t as &str { "util::ser::Writeable" => { writeln!(w, "#[no_mangle]").unwrap(); - writeln!(w, "pub extern \"C\" fn {}_write(obj: *const {}) -> crate::c_types::derived::CVec_u8Z {{", for_obj, full_obj_path).unwrap(); + writeln!(w, "pub extern \"C\" fn {}_write(obj: &{}) -> crate::c_types::derived::CVec_u8Z {{", for_obj, full_obj_path).unwrap(); let ref_type = syn::Type::Reference(syn::TypeReference { and_token: syn::Token!(&)(Span::call_site()), lifetime: None, mutability: None, @@ -706,6 +706,11 @@ fn writeln_struct<'a, 'b, W: std::io::Write>(w: &mut W, s: &'a syn::ItemStruct, /// /// A few non-crate Traits are hard-coded including Default. fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut TypeResolver) { + match export_status(&i.attrs) { + ExportStatus::Export => {}, + ExportStatus::NoExport|ExportStatus::TestOnly => return, + } + if let syn::Type::Tuple(_) = &*i.self_ty { if types.understood_c_type(&*i.self_ty, None) { let mut gen_types = GenericTypes::new(); @@ -785,7 +790,7 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ writeln!(w, "\t\tret.free = Some({}_free_void);", ident).unwrap(); writeln!(w, "\t\tret\n\t}}\n}}").unwrap(); - write!(w, "#[no_mangle]\npub extern \"C\" fn {}_as_{}(this_arg: *const {}) -> crate::{} {{\n", ident, trait_obj.ident, ident, full_trait_path).unwrap(); + write!(w, "#[no_mangle]\npub extern \"C\" fn {}_as_{}(this_arg: &{}) -> crate::{} {{\n", ident, trait_obj.ident, ident, full_trait_path).unwrap(); writeln!(w, "\tcrate::{} {{", full_trait_path).unwrap(); writeln!(w, "\t\tthis_arg: unsafe {{ (*this_arg).inner as *mut c_void }},").unwrap(); writeln!(w, "\t\tfree: None,").unwrap(); @@ -951,10 +956,9 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ }, "PartialEq" => {}, // If we have no generics, try a manual implementation: - _ if p.path.get_ident().is_some() => maybe_convert_trait_impl(w, &trait_path.1, &*i.self_ty, types, &gen_types), - _ => {}, + _ => maybe_convert_trait_impl(w, &trait_path.1, &*i.self_ty, types, &gen_types), } - } else if p.path.get_ident().is_some() { + } else { // If we have no generics, try a manual implementation: maybe_convert_trait_impl(w, &trait_path.1, &*i.self_ty, types, &gen_types); } @@ -1551,10 +1555,18 @@ fn main() { let mut cpp_header_file = std::fs::OpenOptions::new().write(true).create(true).truncate(true) .open(&args[6]).expect("Unable to open new header file"); - writeln!(header_file, "#if defined(__GNUC__)\n#define MUST_USE_STRUCT __attribute__((warn_unused))").unwrap(); - writeln!(header_file, "#else\n#define MUST_USE_STRUCT\n#endif").unwrap(); - writeln!(header_file, "#if defined(__GNUC__)\n#define MUST_USE_RES __attribute__((warn_unused_result))").unwrap(); - writeln!(header_file, "#else\n#define MUST_USE_RES\n#endif").unwrap(); + writeln!(header_file, "#if defined(__GNUC__)").unwrap(); + writeln!(header_file, "#define MUST_USE_STRUCT __attribute__((warn_unused))").unwrap(); + writeln!(header_file, "#define MUST_USE_RES __attribute__((warn_unused_result))").unwrap(); + writeln!(header_file, "#else").unwrap(); + writeln!(header_file, "#define MUST_USE_STRUCT").unwrap(); + writeln!(header_file, "#define MUST_USE_RES").unwrap(); + writeln!(header_file, "#endif").unwrap(); + writeln!(header_file, "#if defined(__clang__)").unwrap(); + writeln!(header_file, "#define NONNULL_PTR _Nonnull").unwrap(); + writeln!(header_file, "#else").unwrap(); + writeln!(header_file, "#define NONNULL_PTR").unwrap(); + writeln!(header_file, "#endif").unwrap(); writeln!(cpp_header_file, "#include \nnamespace LDK {{").unwrap(); // First parse the full crate's ASTs, caching them so that we can hold references to the AST