1 //! Converts a rust crate into a rust crate containing a number of C-exported wrapper functions and
2 //! classes (which is exportable using cbindgen).
3 //! In general, supports convering:
4 //! * structs as a pointer to the underlying type (either owned or not owned),
5 //! * traits as a void-ptr plus a jump table,
6 //! * enums as an equivalent enum with all the inner fields mapped to the mapped types,
7 //! * certain containers (tuples, slices, Vecs, Options, and Results currently) to a concrete
8 //! version of a defined container template.
10 //! It also generates relevant memory-management functions and free-standing functions with
11 //! parameters mapped.
13 use std::collections::HashMap;
16 use std::io::{Read, Write};
20 use proc_macro2::{TokenTree, TokenStream, Span};
27 // *************************************
28 // *** Manually-expanded conversions ***
29 // *************************************
31 /// Because we don't expand macros, any code that we need to generated based on their contents has
32 /// to be completely manual. In this case its all just serialization, so its not too hard.
33 fn convert_macro<W: std::io::Write>(w: &mut W, macro_path: &syn::Path, stream: &TokenStream, types: &TypeResolver) {
34 assert_eq!(macro_path.segments.len(), 1);
35 match &format!("{}", macro_path.segments.iter().next().unwrap().ident) as &str {
36 "impl_writeable" | "impl_writeable_len_match" => {
37 let struct_for = if let TokenTree::Ident(i) = stream.clone().into_iter().next().unwrap() { i } else { unimplemented!(); };
38 if let Some(s) = types.maybe_resolve_ident(&struct_for) {
39 if !types.crate_types.opaques.get(&s).is_some() { return; }
40 writeln!(w, "#[no_mangle]").unwrap();
41 writeln!(w, "pub extern \"C\" fn {}_write(obj: *const {}) -> crate::c_types::derived::CVec_u8Z {{", struct_for, struct_for).unwrap();
42 writeln!(w, "\tcrate::c_types::serialize_obj(unsafe {{ &(*(*obj).inner) }})").unwrap();
43 writeln!(w, "}}").unwrap();
44 writeln!(w, "#[no_mangle]").unwrap();
45 writeln!(w, "pub(crate) extern \"C\" fn {}_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {{", struct_for).unwrap();
46 writeln!(w, "\tcrate::c_types::serialize_obj(unsafe {{ &*(obj as *const native{}) }})", struct_for).unwrap();
47 writeln!(w, "}}").unwrap();
48 writeln!(w, "#[no_mangle]").unwrap();
49 writeln!(w, "pub extern \"C\" fn {}_read(ser: crate::c_types::u8slice) -> {} {{", struct_for, struct_for).unwrap();
50 writeln!(w, "\tif let Ok(res) = crate::c_types::deserialize_obj(ser) {{").unwrap();
51 writeln!(w, "\t\t{} {{ inner: Box::into_raw(Box::new(res)), is_owned: true }}", struct_for).unwrap();
52 writeln!(w, "\t}} else {{").unwrap();
53 writeln!(w, "\t\t{} {{ inner: std::ptr::null_mut(), is_owned: true }}", struct_for).unwrap();
54 writeln!(w, "\t}}\n}}").unwrap();
61 /// Convert "impl trait_path for for_ty { .. }" for manually-mapped types (ie (de)serialization)
62 fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path, for_ty: &syn::Type, types: &mut TypeResolver) {
63 if let Some(t) = types.maybe_resolve_path(&trait_path, None) {
66 if let syn::Type::Path(ref p) = for_ty {
67 if let Some(ident) = p.path.get_ident() {
68 let s = types.maybe_resolve_ident(ident).unwrap();
69 if !types.crate_types.opaques.get(&s).is_some() { return; }
71 for_obj = format!("{}", ident);
72 full_obj_path = for_obj.clone();
79 "util::ser::Writeable" => {
80 writeln!(w, "#[no_mangle]").unwrap();
81 writeln!(w, "pub extern \"C\" fn {}_write(obj: *const {}) -> crate::c_types::derived::CVec_u8Z {{", for_obj, full_obj_path).unwrap();
82 writeln!(w, "\tcrate::c_types::serialize_obj(unsafe {{ &(*(*obj).inner) }})").unwrap();
83 writeln!(w, "}}").unwrap();
84 writeln!(w, "#[no_mangle]").unwrap();
85 writeln!(w, "pub(crate) extern \"C\" fn {}_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {{", for_obj).unwrap();
86 writeln!(w, "\tcrate::c_types::serialize_obj(unsafe {{ &*(obj as *const native{}) }})", for_obj).unwrap();
87 writeln!(w, "}}").unwrap();
89 "util::ser::Readable" => {
90 // Create the Result<Object, DecodeError> syn::Type
91 let mut err_segs = syn::punctuated::Punctuated::new();
92 err_segs.push(syn::PathSegment { ident: syn::Ident::new("ln", Span::call_site()), arguments: syn::PathArguments::None });
93 err_segs.push(syn::PathSegment { ident: syn::Ident::new("msgs", Span::call_site()), arguments: syn::PathArguments::None });
94 err_segs.push(syn::PathSegment { ident: syn::Ident::new("DecodeError", Span::call_site()), arguments: syn::PathArguments::None });
95 let mut args = syn::punctuated::Punctuated::new();
96 args.push(syn::GenericArgument::Type(for_ty.clone()));
97 args.push(syn::GenericArgument::Type(syn::Type::Path(syn::TypePath {
98 qself: None, path: syn::Path {
99 leading_colon: Some(syn::Token![::](Span::call_site())), segments: err_segs,
102 let mut res_segs = syn::punctuated::Punctuated::new();
103 res_segs.push(syn::PathSegment {
104 ident: syn::Ident::new("Result", Span::call_site()),
105 arguments: syn::PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments {
106 colon2_token: None, lt_token: syn::Token![<](Span::call_site()), args, gt_token: syn::Token![>](Span::call_site()),
109 let res_ty = syn::Type::Path(syn::TypePath { qself: None, path: syn::Path {
110 leading_colon: None, segments: res_segs } });
112 writeln!(w, "#[no_mangle]").unwrap();
113 write!(w, "pub extern \"C\" fn {}_read(ser: crate::c_types::u8slice) -> ", for_obj).unwrap();
114 types.write_c_type(w, &res_ty, None, false);
115 writeln!(w, " {{").unwrap();
116 writeln!(w, "\tlet res = crate::c_types::deserialize_obj(ser);").unwrap();
117 write!(w, "\t").unwrap();
118 if types.write_to_c_conversion_new_var(w, &syn::Ident::new("res", Span::call_site()), &res_ty, None, false) {
119 write!(w, "\n\t").unwrap();
121 types.write_to_c_conversion_inline_prefix(w, &res_ty, None, false);
122 write!(w, "res").unwrap();
123 types.write_to_c_conversion_inline_suffix(w, &res_ty, None, false);
124 writeln!(w, "\n}}").unwrap();
131 /// Convert "TraitA : TraitB" to a single function name and return type.
133 /// This is (obviously) somewhat over-specialized and only useful for TraitB's that only require a
134 /// single function (eg for serialization).
135 fn convert_trait_impl_field(trait_path: &str) -> (String, &'static str) {
137 "util::ser::Writeable" => ("write".to_owned(), "crate::c_types::derived::CVec_u8Z"),
138 _ => unimplemented!(),
142 /// Companion to convert_trait_impl_field, write an assignment for the function defined by it for
143 /// `for_obj` which implements the the trait at `trait_path`.
144 fn write_trait_impl_field_assign<W: std::io::Write>(w: &mut W, trait_path: &str, for_obj: &syn::Ident) {
146 "util::ser::Writeable" => {
147 writeln!(w, "\t\twrite: {}_write_void,", for_obj).unwrap();
149 _ => unimplemented!(),
153 /// Write out the impl block for a defined trait struct which has a supertrait
154 fn do_write_impl_trait<W: std::io::Write>(w: &mut W, trait_path: &str, trait_name: &syn::Ident, for_obj: &str) {
156 "util::events::MessageSendEventsProvider" => {
157 writeln!(w, "impl lightning::{} for {} {{", trait_path, for_obj).unwrap();
158 writeln!(w, "\tfn get_and_clear_pending_msg_events(&self) -> Vec<lightning::util::events::MessageSendEvent> {{").unwrap();
159 writeln!(w, "\t\t<crate::{} as lightning::{}>::get_and_clear_pending_msg_events(&self.{})", trait_path, trait_path, trait_name).unwrap();
160 writeln!(w, "\t}}\n}}").unwrap();
162 "util::ser::Writeable" => {
163 writeln!(w, "impl lightning::{} for {} {{", trait_path, for_obj).unwrap();
164 writeln!(w, "\tfn write<W: lightning::util::ser::Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {{").unwrap();
165 writeln!(w, "\t\tlet vec = (self.write)(self.this_arg);").unwrap();
166 writeln!(w, "\t\tw.write_all(vec.as_slice())").unwrap();
167 writeln!(w, "\t}}\n}}").unwrap();
173 // *******************************
174 // *** Per-Type Printing Logic ***
175 // *******************************
177 macro_rules! walk_supertraits { ($t: expr, $types: expr, ($( $pat: pat => $e: expr),*) ) => { {
178 if $t.colon_token.is_some() {
179 for st in $t.supertraits.iter() {
181 syn::TypeParamBound::Trait(supertrait) => {
182 if supertrait.paren_token.is_some() || supertrait.lifetimes.is_some() {
185 // First try to resolve path to find in-crate traits, but if that doesn't work
186 // assume its a prelude trait (eg Clone, etc) and just use the single ident.
187 if let Some(path) = $types.maybe_resolve_path(&supertrait.path, None) {
188 match (&path as &str, &supertrait.path.segments.iter().last().unwrap().ident) {
191 } else if let Some(ident) = supertrait.path.get_ident() {
192 match (&format!("{}", ident) as &str, &ident) {
196 panic!("Supertrait unresolvable and not single-ident");
199 syn::TypeParamBound::Lifetime(_) => unimplemented!(),
205 /// Prints a C-mapped trait object containing a void pointer and a jump table for each function in
206 /// the original trait.
207 /// Implements the native Rust trait and relevant parent traits for the new C-mapped trait.
209 /// Finally, implements Deref<MappedTrait> for MappedTrait which allows its use in types which need
210 /// a concrete Deref to the Rust trait.
211 fn writeln_trait<'a, 'b, W: std::io::Write>(w: &mut W, t: &'a syn::ItemTrait, types: &mut TypeResolver<'b, 'a>, extra_headers: &mut File, cpp_headers: &mut File) {
212 let trait_name = format!("{}", t.ident);
213 match export_status(&t.attrs) {
214 ExportStatus::Export => {},
215 ExportStatus::NoExport|ExportStatus::TestOnly => return,
217 writeln_docs(w, &t.attrs, "");
219 let mut gen_types = GenericTypes::new();
220 assert!(gen_types.learn_generics(&t.generics, types));
221 gen_types.learn_associated_types(&t, types);
223 writeln!(w, "#[repr(C)]\npub struct {} {{", trait_name).unwrap();
224 writeln!(w, "\tpub this_arg: *mut c_void,").unwrap();
225 let mut generated_fields = Vec::new(); // Every field's name except this_arg, used in Clone generation
226 for item in t.items.iter() {
228 &syn::TraitItem::Method(ref m) => {
229 match export_status(&m.attrs) {
230 ExportStatus::NoExport => {
231 // NoExport in this context means we'll hit an unimplemented!() at runtime,
235 ExportStatus::Export => {},
236 ExportStatus::TestOnly => continue,
238 if m.default.is_some() { unimplemented!(); }
240 gen_types.push_ctx();
241 assert!(gen_types.learn_generics(&m.sig.generics, types));
243 writeln_docs(w, &m.attrs, "\t");
245 if let syn::ReturnType::Type(_, rtype) = &m.sig.output {
246 if let syn::Type::Reference(r) = &**rtype {
247 // We have to do quite a dance for trait functions which return references
248 // - they ultimately require us to have a native Rust object stored inside
249 // our concrete trait to return a reference to. However, users may wish to
250 // update the value to be returned each time the function is called (or, to
251 // make C copies of Rust impls equivalent, we have to be able to).
253 // Thus, we store a copy of the C-mapped type (which is just a pointer to
254 // the Rust type and a flag to indicate whether deallocation needs to
255 // happen) as well as provide an Option<>al function pointer which is
256 // called when the trait method is called which allows updating on the fly.
257 write!(w, "\tpub {}: ", m.sig.ident).unwrap();
258 generated_fields.push(format!("{}", m.sig.ident));
259 types.write_c_type(w, &*r.elem, Some(&gen_types), false);
260 writeln!(w, ",").unwrap();
261 writeln!(w, "\t/// Fill in the {} field as a reference to it will be given to Rust after this returns", m.sig.ident).unwrap();
262 writeln!(w, "\t/// Note that this takes a pointer to this object, not the this_ptr like other methods do").unwrap();
263 writeln!(w, "\t/// This function pointer may be NULL if {} is filled in when this object is created and never needs updating.", m.sig.ident).unwrap();
264 writeln!(w, "\tpub set_{}: Option<extern \"C\" fn(&{})>,", m.sig.ident, trait_name).unwrap();
265 generated_fields.push(format!("set_{}", m.sig.ident));
266 // Note that cbindgen will now generate
267 // typedef struct Thing {..., set_thing: (const Thing*), ...} Thing;
268 // which does not compile since Thing is not defined before it is used.
269 writeln!(extra_headers, "struct LDK{};", trait_name).unwrap();
270 writeln!(extra_headers, "typedef struct LDK{} LDK{};", trait_name, trait_name).unwrap();
274 // Sadly, this currently doesn't do what we want, but it should be easy to get
275 // cbindgen to support it. See https://github.com/eqrion/cbindgen/issues/531
276 writeln!(w, "\t#[must_use]").unwrap();
279 write!(w, "\tpub {}: extern \"C\" fn (", m.sig.ident).unwrap();
280 generated_fields.push(format!("{}", m.sig.ident));
281 write_method_params(w, &m.sig, "c_void", types, Some(&gen_types), true, false);
282 writeln!(w, ",").unwrap();
286 &syn::TraitItem::Type(_) => {},
287 _ => unimplemented!(),
290 // Add functions which may be required for supertrait implementations.
291 walk_supertraits!(t, types, (
293 writeln!(w, "\tpub clone: Option<extern \"C\" fn (this_arg: *const c_void) -> *mut c_void>,").unwrap();
294 generated_fields.push("clone".to_owned());
296 ("std::cmp::Eq", _) => {
297 writeln!(w, "\tpub eq: extern \"C\" fn (this_arg: *const c_void, other_arg: &{}) -> bool,", trait_name).unwrap();
298 writeln!(extra_headers, "typedef struct LDK{} LDK{};", trait_name, trait_name).unwrap();
299 generated_fields.push("eq".to_owned());
301 ("std::hash::Hash", _) => {
302 writeln!(w, "\tpub hash: extern \"C\" fn (this_arg: *const c_void) -> u64,").unwrap();
303 generated_fields.push("hash".to_owned());
305 ("Send", _) => {}, ("Sync", _) => {},
307 generated_fields.push(if types.crate_types.traits.get(s).is_none() {
308 let (name, ret) = convert_trait_impl_field(s);
309 writeln!(w, "\tpub {}: extern \"C\" fn (this_arg: *const c_void) -> {},", name, ret).unwrap();
312 // For in-crate supertraits, just store a C-mapped copy of the supertrait as a member.
313 writeln!(w, "\tpub {}: crate::{},", i, s).unwrap();
318 writeln!(w, "\tpub free: Option<extern \"C\" fn(this_arg: *mut c_void)>,").unwrap();
319 generated_fields.push("free".to_owned());
320 writeln!(w, "}}").unwrap();
321 // Implement supertraits for the C-mapped struct.
322 walk_supertraits!(t, types, (
323 ("Send", _) => writeln!(w, "unsafe impl Send for {} {{}}", trait_name).unwrap(),
324 ("Sync", _) => writeln!(w, "unsafe impl Sync for {} {{}}", trait_name).unwrap(),
325 ("std::cmp::Eq", _) => {
326 writeln!(w, "impl std::cmp::Eq for {} {{}}", trait_name).unwrap();
327 writeln!(w, "impl std::cmp::PartialEq for {} {{", trait_name).unwrap();
328 writeln!(w, "\tfn eq(&self, o: &Self) -> bool {{ (self.eq)(self.this_arg, o) }}\n}}").unwrap();
330 ("std::hash::Hash", _) => {
331 writeln!(w, "impl std::hash::Hash for {} {{", trait_name).unwrap();
332 writeln!(w, "\tfn hash<H: std::hash::Hasher>(&self, hasher: &mut H) {{ hasher.write_u64((self.hash)(self.this_arg)) }}\n}}").unwrap();
335 writeln!(w, "#[no_mangle]").unwrap();
336 writeln!(w, "pub extern \"C\" fn {}_clone(orig: &{}) -> {} {{", trait_name, trait_name, trait_name).unwrap();
337 writeln!(w, "\t{} {{", trait_name).unwrap();
338 writeln!(w, "\t\tthis_arg: if let Some(f) = orig.clone {{ (f)(orig.this_arg) }} else {{ orig.this_arg }},").unwrap();
339 for field in generated_fields.iter() {
340 writeln!(w, "\t\t{}: orig.{}.clone(),", field, field).unwrap();
342 writeln!(w, "\t}}\n}}").unwrap();
343 writeln!(w, "impl Clone for {} {{", trait_name).unwrap();
344 writeln!(w, "\tfn clone(&self) -> Self {{").unwrap();
345 writeln!(w, "\t\t{}_clone(self)", trait_name).unwrap();
346 writeln!(w, "\t}}\n}}").unwrap();
349 do_write_impl_trait(w, s, i, &trait_name);
353 // Finally, implement the original Rust trait for the newly created mapped trait.
354 writeln!(w, "\nuse {}::{}::{} as rust{};", types.orig_crate, types.module_path, t.ident, trait_name).unwrap();
355 write!(w, "impl rust{}", t.ident).unwrap();
356 maybe_write_generics(w, &t.generics, types, false);
357 writeln!(w, " for {} {{", trait_name).unwrap();
358 for item in t.items.iter() {
360 syn::TraitItem::Method(m) => {
361 if let ExportStatus::TestOnly = export_status(&m.attrs) { continue; }
362 if m.default.is_some() { unimplemented!(); }
363 if m.sig.constness.is_some() || m.sig.asyncness.is_some() || m.sig.unsafety.is_some() ||
364 m.sig.abi.is_some() || m.sig.variadic.is_some() {
367 gen_types.push_ctx();
368 assert!(gen_types.learn_generics(&m.sig.generics, types));
369 write!(w, "\tfn {}", m.sig.ident).unwrap();
370 types.write_rust_generic_param(w, Some(&gen_types), m.sig.generics.params.iter());
371 write!(w, "(").unwrap();
372 for inp in m.sig.inputs.iter() {
374 syn::FnArg::Receiver(recv) => {
375 if !recv.attrs.is_empty() || recv.reference.is_none() { unimplemented!(); }
376 write!(w, "&").unwrap();
377 if let Some(lft) = &recv.reference.as_ref().unwrap().1 {
378 write!(w, "'{} ", lft.ident).unwrap();
380 if recv.mutability.is_some() {
381 write!(w, "mut self").unwrap();
383 write!(w, "self").unwrap();
386 syn::FnArg::Typed(arg) => {
387 if !arg.attrs.is_empty() { unimplemented!(); }
389 syn::Pat::Ident(ident) => {
390 if !ident.attrs.is_empty() || ident.by_ref.is_some() ||
391 ident.mutability.is_some() || ident.subpat.is_some() {
394 write!(w, ", {}{}: ", if types.skip_arg(&*arg.ty, Some(&gen_types)) { "_" } else { "" }, ident.ident).unwrap();
396 _ => unimplemented!(),
398 types.write_rust_type(w, Some(&gen_types), &*arg.ty);
402 write!(w, ")").unwrap();
403 match &m.sig.output {
404 syn::ReturnType::Type(_, rtype) => {
405 write!(w, " -> ").unwrap();
406 types.write_rust_type(w, Some(&gen_types), &*rtype)
410 write!(w, " {{\n\t\t").unwrap();
411 match export_status(&m.attrs) {
412 ExportStatus::NoExport => {
417 if let syn::ReturnType::Type(_, rtype) = &m.sig.output {
418 if let syn::Type::Reference(r) = &**rtype {
419 assert_eq!(m.sig.inputs.len(), 1); // Must only take self!
420 writeln!(w, "if let Some(f) = self.set_{} {{", m.sig.ident).unwrap();
421 writeln!(w, "\t\t\t(f)(self);").unwrap();
422 write!(w, "\t\t}}\n\t\t").unwrap();
423 types.write_from_c_conversion_to_ref_prefix(w, &*r.elem, Some(&gen_types));
424 write!(w, "self.{}", m.sig.ident).unwrap();
425 types.write_from_c_conversion_to_ref_suffix(w, &*r.elem, Some(&gen_types));
426 writeln!(w, "\n\t}}").unwrap();
431 write_method_var_decl_body(w, &m.sig, "\t", types, Some(&gen_types), true);
432 write!(w, "(self.{})(", m.sig.ident).unwrap();
433 write_method_call_params(w, &m.sig, "\t", types, Some(&gen_types), "", true);
435 writeln!(w, "\n\t}}").unwrap();
438 &syn::TraitItem::Type(ref t) => {
439 if t.default.is_some() || t.generics.lt_token.is_some() { unimplemented!(); }
440 let mut bounds_iter = t.bounds.iter();
441 match bounds_iter.next().unwrap() {
442 syn::TypeParamBound::Trait(tr) => {
443 writeln!(w, "\ttype {} = crate::{};", t.ident, types.resolve_path(&tr.path, Some(&gen_types))).unwrap();
445 _ => unimplemented!(),
447 if bounds_iter.next().is_some() { unimplemented!(); }
449 _ => unimplemented!(),
452 writeln!(w, "}}\n").unwrap();
453 writeln!(w, "// We're essentially a pointer already, or at least a set of pointers, so allow us to be used").unwrap();
454 writeln!(w, "// directly as a Deref trait in higher-level structs:").unwrap();
455 writeln!(w, "impl std::ops::Deref for {} {{\n\ttype Target = Self;", trait_name).unwrap();
456 writeln!(w, "\tfn deref(&self) -> &Self {{\n\t\tself\n\t}}\n}}").unwrap();
458 writeln!(w, "/// Calls the free function if one is set").unwrap();
459 writeln!(w, "#[no_mangle]\npub extern \"C\" fn {}_free(this_ptr: {}) {{ }}", trait_name, trait_name).unwrap();
460 writeln!(w, "impl Drop for {} {{", trait_name).unwrap();
461 writeln!(w, "\tfn drop(&mut self) {{").unwrap();
462 writeln!(w, "\t\tif let Some(f) = self.free {{").unwrap();
463 writeln!(w, "\t\t\tf(self.this_arg);").unwrap();
464 writeln!(w, "\t\t}}\n\t}}\n}}").unwrap();
466 write_cpp_wrapper(cpp_headers, &trait_name, true);
467 types.trait_declared(&t.ident, t);
470 /// Write out a simple "opaque" type (eg structs) which contain a pointer to the native Rust type
471 /// and a flag to indicate whether Drop'ing the mapped struct drops the underlying Rust type.
473 /// Also writes out a _free function and a C++ wrapper which handles calling _free.
474 fn writeln_opaque<W: std::io::Write>(w: &mut W, ident: &syn::Ident, struct_name: &str, generics: &syn::Generics, attrs: &[syn::Attribute], types: &TypeResolver, extra_headers: &mut File, cpp_headers: &mut File) {
475 // If we directly read the original type by its original name, cbindgen hits
476 // https://github.com/eqrion/cbindgen/issues/286 Thus, instead, we import it as a temporary
477 // name and then reference it by that name, which works around the issue.
478 write!(w, "\nuse {}::{}::{} as native{}Import;\ntype native{} = native{}Import", types.orig_crate, types.module_path, ident, ident, ident, ident).unwrap();
479 maybe_write_generics(w, &generics, &types, true);
480 writeln!(w, ";\n").unwrap();
481 writeln!(extra_headers, "struct native{}Opaque;\ntypedef struct native{}Opaque LDKnative{};", ident, ident, ident).unwrap();
482 writeln_docs(w, &attrs, "");
483 writeln!(w, "#[must_use]\n#[repr(C)]\npub struct {} {{\n\t/// Nearly everywhere, inner must be non-null, however in places where", struct_name).unwrap();
484 writeln!(w, "\t/// the Rust equivalent takes an Option, it may be set to null to indicate None.").unwrap();
485 writeln!(w, "\tpub inner: *mut native{},\n\tpub is_owned: bool,\n}}\n", ident).unwrap();
486 writeln!(w, "impl Drop for {} {{\n\tfn drop(&mut self) {{", struct_name).unwrap();
487 writeln!(w, "\t\tif self.is_owned && !self.inner.is_null() {{").unwrap();
488 writeln!(w, "\t\t\tlet _ = unsafe {{ Box::from_raw(self.inner) }};\n\t\t}}\n\t}}\n}}").unwrap();
489 writeln!(w, "#[no_mangle]\npub extern \"C\" fn {}_free(this_ptr: {}) {{ }}", struct_name, struct_name).unwrap();
490 writeln!(w, "#[allow(unused)]").unwrap();
491 writeln!(w, "/// Used only if an object of this type is returned as a trait impl by a method").unwrap();
492 writeln!(w, "extern \"C\" fn {}_free_void(this_ptr: *mut c_void) {{", struct_name).unwrap();
493 writeln!(w, "\tunsafe {{ let _ = Box::from_raw(this_ptr as *mut native{}); }}\n}}", struct_name).unwrap();
494 writeln!(w, "#[allow(unused)]").unwrap();
495 writeln!(w, "/// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy").unwrap();
496 writeln!(w, "impl {} {{", struct_name).unwrap();
497 writeln!(w, "\tpub(crate) fn take_inner(mut self) -> *mut native{} {{", struct_name).unwrap();
498 writeln!(w, "\t\tassert!(self.is_owned);").unwrap();
499 writeln!(w, "\t\tlet ret = self.inner;").unwrap();
500 writeln!(w, "\t\tself.inner = std::ptr::null_mut();").unwrap();
501 writeln!(w, "\t\tret").unwrap();
502 writeln!(w, "\t}}\n}}").unwrap();
504 'attr_loop: for attr in attrs.iter() {
505 let tokens_clone = attr.tokens.clone();
506 let mut token_iter = tokens_clone.into_iter();
507 if let Some(token) = token_iter.next() {
509 TokenTree::Group(g) => {
510 if format!("{}", single_ident_generic_path_to_ident(&attr.path).unwrap()) == "derive" {
511 for id in g.stream().into_iter() {
512 if let TokenTree::Ident(i) = id {
514 writeln!(w, "impl Clone for {} {{", struct_name).unwrap();
515 writeln!(w, "\tfn clone(&self) -> Self {{").unwrap();
516 writeln!(w, "\t\tSelf {{").unwrap();
517 writeln!(w, "\t\t\tinner: Box::into_raw(Box::new(unsafe {{ &*self.inner }}.clone())),").unwrap();
518 writeln!(w, "\t\t\tis_owned: true,").unwrap();
519 writeln!(w, "\t\t}}\n\t}}\n}}").unwrap();
520 writeln!(w, "#[allow(unused)]").unwrap();
521 writeln!(w, "/// Used only if an object of this type is returned as a trait impl by a method").unwrap();
522 writeln!(w, "pub(crate) extern \"C\" fn {}_clone_void(this_ptr: *const c_void) -> *mut c_void {{", struct_name).unwrap();
523 writeln!(w, "\tBox::into_raw(Box::new(unsafe {{ (*(this_ptr as *mut native{})).clone() }})) as *mut c_void", struct_name).unwrap();
524 writeln!(w, "}}").unwrap();
525 writeln!(w, "#[no_mangle]").unwrap();
526 writeln!(w, "pub extern \"C\" fn {}_clone(orig: &{}) -> {} {{", struct_name, struct_name, struct_name).unwrap();
527 writeln!(w, "\t{} {{ inner: Box::into_raw(Box::new(unsafe {{ &*orig.inner }}.clone())), is_owned: true }}", struct_name).unwrap();
528 writeln!(w, "}}").unwrap();
540 write_cpp_wrapper(cpp_headers, &format!("{}", ident), true);
543 fn declare_struct<'a, 'b>(s: &'a syn::ItemStruct, types: &mut TypeResolver<'b, 'a>) -> bool {
544 let export = export_status(&s.attrs);
546 ExportStatus::Export => {},
547 ExportStatus::TestOnly => return false,
548 ExportStatus::NoExport => {
549 types.struct_ignored(&s.ident);
554 types.struct_imported(&s.ident, format!("{}", s.ident));
558 /// Writes out all the relevant mappings for a Rust struct, deferring to writeln_opaque to generate
559 /// the struct itself, and then writing getters and setters for public, understood-type fields and
560 /// a constructor if every field is public.
561 fn writeln_struct<'a, 'b, W: std::io::Write>(w: &mut W, s: &'a syn::ItemStruct, types: &mut TypeResolver<'b, 'a>, extra_headers: &mut File, cpp_headers: &mut File) {
562 if !declare_struct(s, types) { return; }
564 let struct_name = &format!("{}", s.ident);
565 writeln_opaque(w, &s.ident, struct_name, &s.generics, &s.attrs, types, extra_headers, cpp_headers);
567 eprintln!("exporting fields for {}", struct_name);
568 if let syn::Fields::Named(fields) = &s.fields {
569 let mut gen_types = GenericTypes::new();
570 assert!(gen_types.learn_generics(&s.generics, types));
572 let mut all_fields_settable = true;
573 for field in fields.named.iter() {
574 if let syn::Visibility::Public(_) = field.vis {
575 let export = export_status(&field.attrs);
577 ExportStatus::Export => {},
578 ExportStatus::NoExport|ExportStatus::TestOnly => {
579 all_fields_settable = false;
584 if let Some(ident) = &field.ident {
585 let ref_type = syn::Type::Reference(syn::TypeReference {
586 and_token: syn::Token!(&)(Span::call_site()), lifetime: None, mutability: None,
587 elem: Box::new(field.ty.clone()) });
588 if types.understood_c_type(&ref_type, Some(&gen_types)) {
589 writeln_docs(w, &field.attrs, "");
590 write!(w, "#[no_mangle]\npub extern \"C\" fn {}_get_{}(this_ptr: &{}) -> ", struct_name, ident, struct_name).unwrap();
591 types.write_c_type(w, &ref_type, Some(&gen_types), true);
592 write!(w, " {{\n\tlet mut inner_val = &mut unsafe {{ &mut *this_ptr.inner }}.{};\n\t", ident).unwrap();
593 let local_var = types.write_to_c_conversion_new_var(w, &syn::Ident::new("inner_val", Span::call_site()), &ref_type, Some(&gen_types), true);
594 if local_var { write!(w, "\n\t").unwrap(); }
595 types.write_to_c_conversion_inline_prefix(w, &ref_type, Some(&gen_types), true);
597 write!(w, "inner_val").unwrap();
599 write!(w, "(*inner_val)").unwrap();
601 types.write_to_c_conversion_inline_suffix(w, &ref_type, Some(&gen_types), true);
602 writeln!(w, "\n}}").unwrap();
605 if types.understood_c_type(&field.ty, Some(&gen_types)) {
606 writeln_docs(w, &field.attrs, "");
607 write!(w, "#[no_mangle]\npub extern \"C\" fn {}_set_{}(this_ptr: &mut {}, mut val: ", struct_name, ident, struct_name).unwrap();
608 types.write_c_type(w, &field.ty, Some(&gen_types), false);
609 write!(w, ") {{\n\t").unwrap();
610 let local_var = types.write_from_c_conversion_new_var(w, &syn::Ident::new("val", Span::call_site()), &field.ty, Some(&gen_types));
611 if local_var { write!(w, "\n\t").unwrap(); }
612 write!(w, "unsafe {{ &mut *this_ptr.inner }}.{} = ", ident).unwrap();
613 types.write_from_c_conversion_prefix(w, &field.ty, Some(&gen_types));
614 write!(w, "val").unwrap();
615 types.write_from_c_conversion_suffix(w, &field.ty, Some(&gen_types));
616 writeln!(w, ";\n}}").unwrap();
617 } else { all_fields_settable = false; }
618 } else { all_fields_settable = false; }
619 } else { all_fields_settable = false; }
622 if all_fields_settable {
623 // Build a constructor!
624 write!(w, "#[must_use]\n#[no_mangle]\npub extern \"C\" fn {}_new(", struct_name).unwrap();
625 for (idx, field) in fields.named.iter().enumerate() {
626 if idx != 0 { write!(w, ", ").unwrap(); }
627 write!(w, "mut {}_arg: ", field.ident.as_ref().unwrap()).unwrap();
628 types.write_c_type(w, &field.ty, Some(&gen_types), false);
630 write!(w, ") -> {} {{\n\t", struct_name).unwrap();
631 for field in fields.named.iter() {
632 let field_name = format!("{}_arg", field.ident.as_ref().unwrap());
633 if types.write_from_c_conversion_new_var(w, &syn::Ident::new(&field_name, Span::call_site()), &field.ty, Some(&gen_types)) {
634 write!(w, "\n\t").unwrap();
637 writeln!(w, "{} {{ inner: Box::into_raw(Box::new(native{} {{", struct_name, s.ident).unwrap();
638 for field in fields.named.iter() {
639 write!(w, "\t\t{}: ", field.ident.as_ref().unwrap()).unwrap();
640 types.write_from_c_conversion_prefix(w, &field.ty, Some(&gen_types));
641 write!(w, "{}_arg", field.ident.as_ref().unwrap()).unwrap();
642 types.write_from_c_conversion_suffix(w, &field.ty, Some(&gen_types));
643 writeln!(w, ",").unwrap();
645 writeln!(w, "\t}})), is_owned: true }}\n}}").unwrap();
650 /// Prints a relevant conversion for impl *
652 /// For simple impl Struct {}s, this just outputs the wrapper functions as Struct_fn_name() { .. }.
654 /// For impl Trait for Struct{}s, this non-exported generates wrapper functions as
655 /// Trait_Struct_fn_name and a Struct_as_Trait(&struct) -> Trait function which returns a populated
656 /// Trait struct containing a pointer to the passed struct's inner field and the wrapper functions.
658 /// A few non-crate Traits are hard-coded including Default.
659 fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut TypeResolver) {
660 if let &syn::Type::Path(ref p) = &*i.self_ty {
661 if p.qself.is_some() { unimplemented!(); }
662 if let Some(ident) = single_ident_generic_path_to_ident(&p.path) {
663 if let Some(resolved_path) = types.maybe_resolve_non_ignored_ident(&ident) {
664 let mut gen_types = GenericTypes::new();
665 if !gen_types.learn_generics(&i.generics, types) {
666 eprintln!("Not implementing anything for impl {} due to not understood generics", ident);
670 if i.defaultness.is_some() || i.unsafety.is_some() { unimplemented!(); }
671 if let Some(trait_path) = i.trait_.as_ref() {
672 if trait_path.0.is_some() { unimplemented!(); }
673 if types.understood_c_path(&trait_path.1) {
674 let full_trait_path = types.resolve_path(&trait_path.1, None);
675 let trait_obj = *types.crate_types.traits.get(&full_trait_path).unwrap();
676 // We learn the associated types maping from the original trait object.
677 // That's great, except that they are unresolved idents, so if we learn
678 // mappings from a trai defined in a different file, we may mis-resolve or
679 // fail to resolve the mapped types.
680 gen_types.learn_associated_types(trait_obj, types);
681 let mut impl_associated_types = HashMap::new();
682 for item in i.items.iter() {
684 syn::ImplItem::Type(t) => {
685 if let syn::Type::Path(p) = &t.ty {
686 if let Some(id) = single_ident_generic_path_to_ident(&p.path) {
687 impl_associated_types.insert(&t.ident, id);
695 let export = export_status(&trait_obj.attrs);
697 ExportStatus::Export => {},
698 ExportStatus::NoExport|ExportStatus::TestOnly => return,
701 // For cases where we have a concrete native object which implements a
702 // trait and need to return the C-mapped version of the trait, provide a
703 // From<> implementation which does all the work to ensure free is handled
704 // properly. This way we can call this method from deep in the
705 // type-conversion logic without actually knowing the concrete native type.
706 writeln!(w, "impl From<native{}> for crate::{} {{", ident, full_trait_path).unwrap();
707 writeln!(w, "\tfn from(obj: native{}) -> Self {{", ident).unwrap();
708 writeln!(w, "\t\tlet mut rust_obj = {} {{ inner: Box::into_raw(Box::new(obj)), is_owned: true }};", ident).unwrap();
709 writeln!(w, "\t\tlet mut ret = {}_as_{}(&rust_obj);", ident, trait_obj.ident).unwrap();
710 writeln!(w, "\t\t// We want to free rust_obj when ret gets drop()'d, not rust_obj, so wipe rust_obj's pointer and set ret's free() fn").unwrap();
711 writeln!(w, "\t\trust_obj.inner = std::ptr::null_mut();").unwrap();
712 writeln!(w, "\t\tret.free = Some({}_free_void);", ident).unwrap();
713 writeln!(w, "\t\tret\n\t}}\n}}").unwrap();
715 write!(w, "#[no_mangle]\npub extern \"C\" fn {}_as_{}(this_arg: *const {}) -> crate::{} {{\n", ident, trait_obj.ident, ident, full_trait_path).unwrap();
716 writeln!(w, "\tcrate::{} {{", full_trait_path).unwrap();
717 writeln!(w, "\t\tthis_arg: unsafe {{ (*this_arg).inner as *mut c_void }},").unwrap();
718 writeln!(w, "\t\tfree: None,").unwrap();
720 macro_rules! write_meth {
721 ($m: expr, $trait: expr, $indent: expr) => {
722 let trait_method = $trait.items.iter().filter_map(|item| {
723 if let syn::TraitItem::Method(t_m) = item { Some(t_m) } else { None }
724 }).find(|trait_meth| trait_meth.sig.ident == $m.sig.ident).unwrap();
725 match export_status(&trait_method.attrs) {
726 ExportStatus::Export => {},
727 ExportStatus::NoExport => {
728 write!(w, "{}\t\t//XXX: Need to export {}\n", $indent, $m.sig.ident).unwrap();
731 ExportStatus::TestOnly => continue,
734 let mut printed = false;
735 if let syn::ReturnType::Type(_, rtype) = &$m.sig.output {
736 if let syn::Type::Reference(r) = &**rtype {
737 write!(w, "\n\t\t{}{}: ", $indent, $m.sig.ident).unwrap();
738 types.write_empty_rust_val(Some(&gen_types), w, &*r.elem);
739 writeln!(w, ",\n{}\t\tset_{}: Some({}_{}_set_{}),", $indent, $m.sig.ident, ident, trait_obj.ident, $m.sig.ident).unwrap();
744 write!(w, "{}\t\t{}: {}_{}_{},\n", $indent, $m.sig.ident, ident, trait_obj.ident, $m.sig.ident).unwrap();
748 for item in trait_obj.items.iter() {
750 syn::TraitItem::Method(m) => {
751 write_meth!(m, trait_obj, "");
756 walk_supertraits!(trait_obj, types, (
758 writeln!(w, "\t\tclone: Some({}_clone_void),", ident).unwrap();
760 ("Sync", _) => {}, ("Send", _) => {},
761 ("std::marker::Sync", _) => {}, ("std::marker::Send", _) => {},
763 if let Some(supertrait_obj) = types.crate_types.traits.get(s) {
764 writeln!(w, "\t\t{}: crate::{} {{", t, s).unwrap();
765 writeln!(w, "\t\t\tthis_arg: unsafe {{ (*this_arg).inner as *mut c_void }},").unwrap();
766 writeln!(w, "\t\t\tfree: None,").unwrap();
767 for item in supertrait_obj.items.iter() {
769 syn::TraitItem::Method(m) => {
770 write_meth!(m, supertrait_obj, "\t");
775 write!(w, "\t\t}},\n").unwrap();
777 write_trait_impl_field_assign(w, s, ident);
781 write!(w, "\t}}\n}}\nuse {}::{} as {}TraitImport;\n", types.orig_crate, full_trait_path, trait_obj.ident).unwrap();
783 macro_rules! impl_meth {
784 ($m: expr, $trait: expr, $indent: expr) => {
785 let trait_method = $trait.items.iter().filter_map(|item| {
786 if let syn::TraitItem::Method(t_m) = item { Some(t_m) } else { None }
787 }).find(|trait_meth| trait_meth.sig.ident == $m.sig.ident).unwrap();
788 match export_status(&trait_method.attrs) {
789 ExportStatus::Export => {},
790 ExportStatus::NoExport|ExportStatus::TestOnly => continue,
793 if let syn::ReturnType::Type(_, _) = &$m.sig.output {
794 writeln!(w, "#[must_use]").unwrap();
796 write!(w, "extern \"C\" fn {}_{}_{}(", ident, trait_obj.ident, $m.sig.ident).unwrap();
797 gen_types.push_ctx();
798 assert!(gen_types.learn_generics(&$m.sig.generics, types));
799 write_method_params(w, &$m.sig, "c_void", types, Some(&gen_types), true, true);
800 write!(w, " {{\n\t").unwrap();
801 write_method_var_decl_body(w, &$m.sig, "", types, Some(&gen_types), false);
802 let mut takes_self = false;
803 for inp in $m.sig.inputs.iter() {
804 if let syn::FnArg::Receiver(_) = inp {
809 write!(w, "unsafe {{ &mut *(this_arg as *mut native{}) }}.{}(", ident, $m.sig.ident).unwrap();
811 write!(w, "{}::{}::{}(", types.orig_crate, resolved_path, $m.sig.ident).unwrap();
814 let mut real_type = "".to_string();
815 match &$m.sig.output {
816 syn::ReturnType::Type(_, rtype) => {
817 if let Some(mut remaining_path) = first_seg_self(&*rtype) {
818 if let Some(associated_seg) = get_single_remaining_path_seg(&mut remaining_path) {
819 real_type = format!("{}", impl_associated_types.get(associated_seg).unwrap());
825 write_method_call_params(w, &$m.sig, "", types, Some(&gen_types), &real_type, false);
827 write!(w, "\n}}\n").unwrap();
828 if let syn::ReturnType::Type(_, rtype) = &$m.sig.output {
829 if let syn::Type::Reference(r) = &**rtype {
830 assert_eq!($m.sig.inputs.len(), 1); // Must only take self
831 writeln!(w, "extern \"C\" fn {}_{}_set_{}(trait_self_arg: &{}) {{", ident, trait_obj.ident, $m.sig.ident, trait_obj.ident).unwrap();
832 writeln!(w, "\t// This is a bit race-y in the general case, but for our specific use-cases today, we're safe").unwrap();
833 writeln!(w, "\t// Specifically, we must ensure that the first time we're called it can never be in parallel").unwrap();
834 write!(w, "\tif ").unwrap();
835 types.write_empty_rust_val_check(Some(&gen_types), w, &*r.elem, &format!("trait_self_arg.{}", $m.sig.ident));
836 writeln!(w, " {{").unwrap();
837 writeln!(w, "\t\tunsafe {{ &mut *(trait_self_arg as *const {} as *mut {}) }}.{} = {}_{}_{}(trait_self_arg.this_arg);", trait_obj.ident, trait_obj.ident, $m.sig.ident, ident, trait_obj.ident, $m.sig.ident).unwrap();
838 writeln!(w, "\t}}").unwrap();
839 writeln!(w, "}}").unwrap();
845 for item in i.items.iter() {
847 syn::ImplItem::Method(m) => {
848 impl_meth!(m, trait_obj, "");
850 syn::ImplItem::Type(_) => {},
851 _ => unimplemented!(),
854 walk_supertraits!(trait_obj, types, (
856 if let Some(supertrait_obj) = types.crate_types.traits.get(s).cloned() {
857 writeln!(w, "use {}::{} as native{}Trait;", types.orig_crate, s, t).unwrap();
858 for item in supertrait_obj.items.iter() {
860 syn::TraitItem::Method(m) => {
861 impl_meth!(m, supertrait_obj, "\t");
869 write!(w, "\n").unwrap();
870 } else if let Some(trait_ident) = trait_path.1.get_ident() {
871 //XXX: implement for other things like ToString
872 match &format!("{}", trait_ident) as &str {
875 write!(w, "#[must_use]\n#[no_mangle]\npub extern \"C\" fn {}_default() -> {} {{\n", ident, ident).unwrap();
876 write!(w, "\t{} {{ inner: Box::into_raw(Box::new(Default::default())), is_owned: true }}\n", ident).unwrap();
877 write!(w, "}}\n").unwrap();
880 // If we have no generics, try a manual implementation:
881 _ if p.path.get_ident().is_some() => maybe_convert_trait_impl(w, &trait_path.1, &*i.self_ty, types),
884 } else if p.path.get_ident().is_some() {
885 // If we have no generics, try a manual implementation:
886 maybe_convert_trait_impl(w, &trait_path.1, &*i.self_ty, types);
889 let declared_type = (*types.get_declared_type(&ident).unwrap()).clone();
890 for item in i.items.iter() {
892 syn::ImplItem::Method(m) => {
893 if let syn::Visibility::Public(_) = m.vis {
894 match export_status(&m.attrs) {
895 ExportStatus::Export => {},
896 ExportStatus::NoExport|ExportStatus::TestOnly => continue,
898 if m.defaultness.is_some() { unimplemented!(); }
899 writeln_docs(w, &m.attrs, "");
900 if let syn::ReturnType::Type(_, _) = &m.sig.output {
901 writeln!(w, "#[must_use]").unwrap();
903 write!(w, "#[no_mangle]\npub extern \"C\" fn {}_{}(", ident, m.sig.ident).unwrap();
904 let ret_type = match &declared_type {
905 DeclType::MirroredEnum => format!("{}", ident),
906 DeclType::StructImported => format!("{}", ident),
907 _ => unimplemented!(),
909 gen_types.push_ctx();
910 assert!(gen_types.learn_generics(&m.sig.generics, types));
911 write_method_params(w, &m.sig, &ret_type, types, Some(&gen_types), false, true);
912 write!(w, " {{\n\t").unwrap();
913 write_method_var_decl_body(w, &m.sig, "", types, Some(&gen_types), false);
914 let mut takes_self = false;
915 let mut takes_mut_self = false;
916 for inp in m.sig.inputs.iter() {
917 if let syn::FnArg::Receiver(r) = inp {
919 if r.mutability.is_some() { takes_mut_self = true; }
923 write!(w, "unsafe {{ &mut (*(this_arg.inner as *mut native{})) }}.{}(", ident, m.sig.ident).unwrap();
924 } else if takes_self {
925 write!(w, "unsafe {{ &*this_arg.inner }}.{}(", m.sig.ident).unwrap();
927 write!(w, "{}::{}::{}(", types.orig_crate, resolved_path, m.sig.ident).unwrap();
929 write_method_call_params(w, &m.sig, "", types, Some(&gen_types), &ret_type, false);
931 writeln!(w, "\n}}\n").unwrap();
939 eprintln!("Not implementing anything for {} due to no-resolve (probably the type isn't pub or its marked not exported)", ident);
945 /// Returns true if the enum will be mapped as an opaue (ie struct with a pointer to the underlying
946 /// type), otherwise it is mapped into a transparent, C-compatible version of itself.
947 fn is_enum_opaque(e: &syn::ItemEnum) -> bool {
948 for var in e.variants.iter() {
949 if let syn::Fields::Unit = var.fields {
950 } else if let syn::Fields::Named(fields) = &var.fields {
951 for field in fields.named.iter() {
952 match export_status(&field.attrs) {
953 ExportStatus::Export|ExportStatus::TestOnly => {},
954 ExportStatus::NoExport => return true,
964 fn declare_enum<'a, 'b>(e: &'a syn::ItemEnum, types: &mut TypeResolver<'b, 'a>) {
965 match export_status(&e.attrs) {
966 ExportStatus::Export => {},
967 ExportStatus::NoExport|ExportStatus::TestOnly => return,
970 if is_enum_opaque(e) {
971 types.enum_ignored(&e.ident);
973 types.mirrored_enum_declared(&e.ident);
977 /// Print a mapping of an enum. If all of the enum's fields are C-mapped in some form (or the enum
978 /// is unitary), we generate an equivalent enum with all types replaced with their C mapped
979 /// versions followed by conversion functions which map between the Rust version and the C mapped
981 fn writeln_enum<'a, 'b, W: std::io::Write>(w: &mut W, e: &'a syn::ItemEnum, types: &mut TypeResolver<'b, 'a>, extra_headers: &mut File, cpp_headers: &mut File) {
982 match export_status(&e.attrs) {
983 ExportStatus::Export => {},
984 ExportStatus::NoExport|ExportStatus::TestOnly => return,
987 if is_enum_opaque(e) {
988 eprintln!("Skipping enum {} as it contains non-unit fields", e.ident);
989 writeln_opaque(w, &e.ident, &format!("{}", e.ident), &e.generics, &e.attrs, types, extra_headers, cpp_headers);
992 writeln_docs(w, &e.attrs, "");
994 if e.generics.lt_token.is_some() {
998 let mut needs_free = false;
1000 writeln!(w, "#[must_use]\n#[derive(Clone)]\n#[repr(C)]\npub enum {} {{", e.ident).unwrap();
1001 for var in e.variants.iter() {
1002 assert_eq!(export_status(&var.attrs), ExportStatus::Export); // We can't partially-export a mirrored enum
1003 writeln_docs(w, &var.attrs, "\t");
1004 write!(w, "\t{}", var.ident).unwrap();
1005 if let syn::Fields::Named(fields) = &var.fields {
1007 writeln!(w, " {{").unwrap();
1008 for field in fields.named.iter() {
1009 if export_status(&field.attrs) == ExportStatus::TestOnly { continue; }
1010 write!(w, "\t\t{}: ", field.ident.as_ref().unwrap()).unwrap();
1011 types.write_c_type(w, &field.ty, None, false);
1012 writeln!(w, ",").unwrap();
1014 write!(w, "\t}}").unwrap();
1016 if var.discriminant.is_some() { unimplemented!(); }
1017 writeln!(w, ",").unwrap();
1019 writeln!(w, "}}\nuse {}::{}::{} as native{};\nimpl {} {{", types.orig_crate, types.module_path, e.ident, e.ident, e.ident).unwrap();
1021 macro_rules! write_conv {
1022 ($fn_sig: expr, $to_c: expr, $ref: expr) => {
1023 writeln!(w, "\t#[allow(unused)]\n\tpub(crate) fn {} {{\n\t\tmatch {} {{", $fn_sig, if $to_c { "native" } else { "self" }).unwrap();
1024 for var in e.variants.iter() {
1025 write!(w, "\t\t\t{}{}::{} ", if $to_c { "native" } else { "" }, e.ident, var.ident).unwrap();
1026 if let syn::Fields::Named(fields) = &var.fields {
1027 write!(w, "{{").unwrap();
1028 for field in fields.named.iter() {
1029 if export_status(&field.attrs) == ExportStatus::TestOnly { continue; }
1030 write!(w, "{}{}, ", if $ref { "ref " } else { "mut " }, field.ident.as_ref().unwrap()).unwrap();
1032 write!(w, "}} ").unwrap();
1034 write!(w, "=>").unwrap();
1035 if let syn::Fields::Named(fields) = &var.fields {
1036 write!(w, " {{\n\t\t\t\t").unwrap();
1037 for field in fields.named.iter() {
1038 if export_status(&field.attrs) == ExportStatus::TestOnly { continue; }
1039 let mut sink = ::std::io::sink();
1040 let mut out: &mut dyn std::io::Write = if $ref { &mut sink } else { w };
1041 let new_var = if $to_c {
1042 types.write_to_c_conversion_new_var(&mut out, field.ident.as_ref().unwrap(), &field.ty, None, false)
1044 types.write_from_c_conversion_new_var(&mut out, field.ident.as_ref().unwrap(), &field.ty, None)
1046 if $ref || new_var {
1048 write!(w, "let mut {}_nonref = (*{}).clone();\n\t\t\t\t", field.ident.as_ref().unwrap(), field.ident.as_ref().unwrap()).unwrap();
1050 let nonref_ident = syn::Ident::new(&format!("{}_nonref", field.ident.as_ref().unwrap()), Span::call_site());
1052 types.write_to_c_conversion_new_var(w, &nonref_ident, &field.ty, None, false);
1054 types.write_from_c_conversion_new_var(w, &nonref_ident, &field.ty, None);
1056 write!(w, "\n\t\t\t\t").unwrap();
1059 write!(w, "\n\t\t\t\t").unwrap();
1063 } else { write!(w, " ").unwrap(); }
1064 write!(w, "{}{}::{}", if $to_c { "" } else { "native" }, e.ident, var.ident).unwrap();
1065 if let syn::Fields::Named(fields) = &var.fields {
1066 write!(w, " {{").unwrap();
1067 for field in fields.named.iter() {
1068 if export_status(&field.attrs) == ExportStatus::TestOnly { continue; }
1069 write!(w, "\n\t\t\t\t\t{}: ", field.ident.as_ref().unwrap()).unwrap();
1071 types.write_to_c_conversion_inline_prefix(w, &field.ty, None, false);
1073 types.write_from_c_conversion_prefix(w, &field.ty, None);
1076 field.ident.as_ref().unwrap(),
1077 if $ref { "_nonref" } else { "" }).unwrap();
1079 types.write_to_c_conversion_inline_suffix(w, &field.ty, None, false);
1081 types.write_from_c_conversion_suffix(w, &field.ty, None);
1083 write!(w, ",").unwrap();
1085 writeln!(w, "\n\t\t\t\t}}").unwrap();
1086 write!(w, "\t\t\t}}").unwrap();
1088 writeln!(w, ",").unwrap();
1090 writeln!(w, "\t\t}}\n\t}}").unwrap();
1094 write_conv!(format!("to_native(&self) -> native{}", e.ident), false, true);
1095 write_conv!(format!("into_native(self) -> native{}", e.ident), false, false);
1096 write_conv!(format!("from_native(native: &native{}) -> Self", e.ident), true, true);
1097 write_conv!(format!("native_into(native: native{}) -> Self", e.ident), true, false);
1098 writeln!(w, "}}").unwrap();
1101 writeln!(w, "#[no_mangle]\npub extern \"C\" fn {}_free(this_ptr: {}) {{ }}", e.ident, e.ident).unwrap();
1103 writeln!(w, "#[no_mangle]").unwrap();
1104 writeln!(w, "pub extern \"C\" fn {}_clone(orig: &{}) -> {} {{", e.ident, e.ident, e.ident).unwrap();
1105 writeln!(w, "\torig.clone()").unwrap();
1106 writeln!(w, "}}").unwrap();
1107 write_cpp_wrapper(cpp_headers, &format!("{}", e.ident), needs_free);
1110 fn writeln_fn<'a, 'b, W: std::io::Write>(w: &mut W, f: &'a syn::ItemFn, types: &mut TypeResolver<'b, 'a>) {
1111 match export_status(&f.attrs) {
1112 ExportStatus::Export => {},
1113 ExportStatus::NoExport|ExportStatus::TestOnly => return,
1115 writeln_docs(w, &f.attrs, "");
1117 let mut gen_types = GenericTypes::new();
1118 if !gen_types.learn_generics(&f.sig.generics, types) { return; }
1120 write!(w, "#[no_mangle]\npub extern \"C\" fn {}(", f.sig.ident).unwrap();
1121 write_method_params(w, &f.sig, "", types, Some(&gen_types), false, true);
1122 write!(w, " {{\n\t").unwrap();
1123 write_method_var_decl_body(w, &f.sig, "", types, Some(&gen_types), false);
1124 write!(w, "{}::{}::{}(", types.orig_crate, types.module_path, f.sig.ident).unwrap();
1125 write_method_call_params(w, &f.sig, "", types, Some(&gen_types), "", false);
1126 writeln!(w, "\n}}\n").unwrap();
1129 // ********************************
1130 // *** File/Crate Walking Logic ***
1131 // ********************************
1133 /// Simple utility to walk the modules in a crate - iterating over the modules (with file paths) in
1135 struct FileIter<'a, I: Iterator<Item = &'a syn::Item>> {
1141 impl<'a, I: Iterator<Item = &'a syn::Item>> Iterator for FileIter<'a, I> {
1142 type Item = (String, String, &'a syn::ItemMod);
1143 fn next(&mut self) -> std::option::Option<<Self as std::iter::Iterator>::Item> {
1145 match self.item_iter.next() {
1146 Some(syn::Item::Mod(m)) => {
1147 if let syn::Visibility::Public(_) = m.vis {
1148 match export_status(&m.attrs) {
1149 ExportStatus::Export => {},
1150 ExportStatus::NoExport|ExportStatus::TestOnly => continue,
1153 let f_path = format!("{}/{}.rs", (self.path.as_ref() as &Path).parent().unwrap().display(), m.ident);
1154 let new_mod = if self.module.is_empty() { format!("{}", m.ident) } else { format!("{}::{}", self.module, m.ident) };
1155 if let Ok(_) = File::open(&format!("{}/{}", self.in_dir, f_path)) {
1156 return Some((f_path, new_mod, m));
1159 format!("{}/{}/mod.rs", (self.path.as_ref() as &Path).parent().unwrap().display(), m.ident),
1165 None => return None,
1170 fn file_iter<'a>(file: &'a syn::File, in_dir: &'a str, path: &'a str, module: &'a str) ->
1171 impl Iterator<Item = (String, String, &'a syn::ItemMod)> + 'a {
1172 FileIter { in_dir, path, module, item_iter: file.items.iter() }
1175 /// A struct containing the syn::File AST for each file in the crate.
1176 struct FullLibraryAST {
1177 files: HashMap<String, syn::File>,
1180 /// Do the Real Work of mapping an original file to C-callable wrappers. Creates a new file at
1181 /// `out_path` and fills it with wrapper structs/functions to allow calling the things in the AST
1182 /// at `module` from C.
1183 fn convert_file<'a, 'b>(libast: &'a FullLibraryAST, crate_types: &mut CrateTypes<'a>, in_dir: &str, out_dir: &str, path: &str, orig_crate: &str, module: &str, header_file: &mut File, cpp_header_file: &mut File) {
1184 let syntax = if let Some(ast) = libast.files.get(module) { ast } else { return };
1186 assert!(syntax.shebang.is_none()); // Not sure what this is, hope we dont have one
1188 let new_file_path = format!("{}/{}", out_dir, path);
1189 let _ = std::fs::create_dir((&new_file_path.as_ref() as &std::path::Path).parent().unwrap());
1190 let mut out = std::fs::OpenOptions::new().write(true).create(true).truncate(true)
1191 .open(new_file_path).expect("Unable to open new src file");
1193 assert_eq!(export_status(&syntax.attrs), ExportStatus::Export);
1194 writeln_docs(&mut out, &syntax.attrs, "");
1196 if path.ends_with("/lib.rs") {
1197 // Special-case the top-level lib.rs with various lint allows and a pointer to the c_types
1198 // and bitcoin hand-written modules.
1199 writeln!(out, "#![allow(unknown_lints)]").unwrap();
1200 writeln!(out, "#![allow(non_camel_case_types)]").unwrap();
1201 writeln!(out, "#![allow(non_snake_case)]").unwrap();
1202 writeln!(out, "#![allow(unused_imports)]").unwrap();
1203 writeln!(out, "#![allow(unused_variables)]").unwrap();
1204 writeln!(out, "#![allow(unused_mut)]").unwrap();
1205 writeln!(out, "#![allow(unused_parens)]").unwrap();
1206 writeln!(out, "#![allow(unused_unsafe)]").unwrap();
1207 writeln!(out, "#![allow(unused_braces)]").unwrap();
1208 writeln!(out, "mod c_types;").unwrap();
1209 writeln!(out, "mod bitcoin;").unwrap();
1211 writeln!(out, "\nuse std::ffi::c_void;\nuse bitcoin::hashes::Hash;\nuse crate::c_types::*;\n").unwrap();
1214 for (path, new_mod, m) in file_iter(&syntax, in_dir, path, &module) {
1215 writeln_docs(&mut out, &m.attrs, "");
1216 writeln!(out, "pub mod {};", m.ident).unwrap();
1217 convert_file(libast, crate_types, in_dir, out_dir, &path,
1218 orig_crate, &new_mod, header_file, cpp_header_file);
1221 eprintln!("Converting {} entries...", path);
1223 let mut type_resolver = TypeResolver::new(orig_crate, module, crate_types);
1225 // First pass over the items and fill in imports and file-declared objects in the type resolver
1226 for item in syntax.items.iter() {
1228 syn::Item::Use(u) => type_resolver.process_use(&mut out, &u),
1229 syn::Item::Struct(s) => {
1230 if let syn::Visibility::Public(_) = s.vis {
1231 declare_struct(&s, &mut type_resolver);
1234 syn::Item::Enum(e) => {
1235 if let syn::Visibility::Public(_) = e.vis {
1236 declare_enum(&e, &mut type_resolver);
1243 for item in syntax.items.iter() {
1245 syn::Item::Use(_) => {}, // Handled above
1246 syn::Item::Static(_) => {},
1247 syn::Item::Enum(e) => {
1248 if let syn::Visibility::Public(_) = e.vis {
1249 writeln_enum(&mut out, &e, &mut type_resolver, header_file, cpp_header_file);
1252 syn::Item::Impl(i) => {
1253 writeln_impl(&mut out, &i, &mut type_resolver);
1255 syn::Item::Struct(s) => {
1256 if let syn::Visibility::Public(_) = s.vis {
1257 writeln_struct(&mut out, &s, &mut type_resolver, header_file, cpp_header_file);
1260 syn::Item::Trait(t) => {
1261 if let syn::Visibility::Public(_) = t.vis {
1262 writeln_trait(&mut out, &t, &mut type_resolver, header_file, cpp_header_file);
1265 syn::Item::Mod(_) => {}, // We don't have to do anything - the top loop handles these.
1266 syn::Item::Const(c) => {
1267 // Re-export any primitive-type constants.
1268 if let syn::Visibility::Public(_) = c.vis {
1269 if let syn::Type::Path(p) = &*c.ty {
1270 let resolved_path = type_resolver.resolve_path(&p.path, None);
1271 if type_resolver.is_primitive(&resolved_path) {
1272 writeln!(out, "\n#[no_mangle]").unwrap();
1273 writeln!(out, "pub static {}: {} = {}::{}::{};", c.ident, resolved_path, orig_crate, module, c.ident).unwrap();
1278 syn::Item::Type(t) => {
1279 if let syn::Visibility::Public(_) = t.vis {
1280 match export_status(&t.attrs) {
1281 ExportStatus::Export => {},
1282 ExportStatus::NoExport|ExportStatus::TestOnly => continue,
1285 let mut process_alias = true;
1286 for tok in t.generics.params.iter() {
1287 if let syn::GenericParam::Lifetime(_) = tok {}
1288 else { process_alias = false; }
1292 syn::Type::Path(_) =>
1293 writeln_opaque(&mut out, &t.ident, &format!("{}", t.ident), &t.generics, &t.attrs, &type_resolver, header_file, cpp_header_file),
1299 syn::Item::Fn(f) => {
1300 if let syn::Visibility::Public(_) = f.vis {
1301 writeln_fn(&mut out, &f, &mut type_resolver);
1304 syn::Item::Macro(m) => {
1305 if m.ident.is_none() { // If its not a macro definition
1306 convert_macro(&mut out, &m.mac.path, &m.mac.tokens, &type_resolver);
1309 syn::Item::Verbatim(_) => {},
1310 syn::Item::ExternCrate(_) => {},
1311 _ => unimplemented!(),
1315 out.flush().unwrap();
1318 /// Load the AST for each file in the crate, filling the FullLibraryAST object
1319 fn load_ast(in_dir: &str, path: &str, module: String, ast_storage: &mut FullLibraryAST) {
1320 eprintln!("Loading {}{}...", in_dir, path);
1322 let mut file = File::open(format!("{}/{}", in_dir, path)).expect("Unable to open file");
1323 let mut src = String::new();
1324 file.read_to_string(&mut src).expect("Unable to read file");
1325 let syntax = syn::parse_file(&src).expect("Unable to parse file");
1327 assert_eq!(export_status(&syntax.attrs), ExportStatus::Export);
1329 for (path, new_mod, _) in file_iter(&syntax, in_dir, path, &module) {
1330 load_ast(in_dir, &path, new_mod, ast_storage);
1332 ast_storage.files.insert(module, syntax);
1335 /// Insert ident -> absolute Path resolutions into imports from the given UseTree and path-prefix.
1336 fn process_use_intern<'a>(u: &'a syn::UseTree, mut path: syn::punctuated::Punctuated<syn::PathSegment, syn::token::Colon2>, imports: &mut HashMap<&'a syn::Ident, syn::Path>) {
1338 syn::UseTree::Path(p) => {
1339 path.push(syn::PathSegment { ident: p.ident.clone(), arguments: syn::PathArguments::None });
1340 process_use_intern(&p.tree, path, imports);
1342 syn::UseTree::Name(n) => {
1343 path.push(syn::PathSegment { ident: n.ident.clone(), arguments: syn::PathArguments::None });
1344 imports.insert(&n.ident, syn::Path { leading_colon: Some(syn::Token![::](Span::call_site())), segments: path });
1346 syn::UseTree::Group(g) => {
1347 for i in g.items.iter() {
1348 process_use_intern(i, path.clone(), imports);
1355 /// Map all the Paths in a Type into absolute paths given a set of imports (generated via process_use_intern)
1356 fn resolve_imported_refs(imports: &HashMap<&syn::Ident, syn::Path>, mut ty: syn::Type) -> syn::Type {
1358 syn::Type::Path(p) => {
1359 if let Some(ident) = p.path.get_ident() {
1360 if let Some(newpath) = imports.get(ident) {
1361 p.path = newpath.clone();
1363 } else { unimplemented!(); }
1365 syn::Type::Reference(r) => {
1366 r.elem = Box::new(resolve_imported_refs(imports, (*r.elem).clone()));
1368 syn::Type::Slice(s) => {
1369 s.elem = Box::new(resolve_imported_refs(imports, (*s.elem).clone()));
1371 syn::Type::Tuple(t) => {
1372 for e in t.elems.iter_mut() {
1373 *e = resolve_imported_refs(imports, e.clone());
1376 _ => unimplemented!(),
1381 /// Walk the FullLibraryAST, deciding how things will be mapped and adding tracking to CrateTypes.
1382 fn walk_ast<'a>(in_dir: &str, path: &str, module: String, ast_storage: &'a FullLibraryAST, crate_types: &mut CrateTypes<'a>) {
1383 let syntax = if let Some(ast) = ast_storage.files.get(&module) { ast } else { return };
1384 assert_eq!(export_status(&syntax.attrs), ExportStatus::Export);
1386 for (path, new_mod, _) in file_iter(&syntax, in_dir, path, &module) {
1387 walk_ast(in_dir, &path, new_mod, ast_storage, crate_types);
1390 let mut import_maps = HashMap::new();
1392 for item in syntax.items.iter() {
1394 syn::Item::Use(u) => {
1395 process_use_intern(&u.tree, syn::punctuated::Punctuated::new(), &mut import_maps);
1397 syn::Item::Struct(s) => {
1398 if let syn::Visibility::Public(_) = s.vis {
1399 match export_status(&s.attrs) {
1400 ExportStatus::Export => {},
1401 ExportStatus::NoExport|ExportStatus::TestOnly => continue,
1403 let struct_path = format!("{}::{}", module, s.ident);
1404 crate_types.opaques.insert(struct_path, &s.ident);
1407 syn::Item::Trait(t) => {
1408 if let syn::Visibility::Public(_) = t.vis {
1409 match export_status(&t.attrs) {
1410 ExportStatus::Export => {},
1411 ExportStatus::NoExport|ExportStatus::TestOnly => continue,
1413 let trait_path = format!("{}::{}", module, t.ident);
1414 crate_types.traits.insert(trait_path, &t);
1417 syn::Item::Type(t) => {
1418 if let syn::Visibility::Public(_) = t.vis {
1419 match export_status(&t.attrs) {
1420 ExportStatus::Export => {},
1421 ExportStatus::NoExport|ExportStatus::TestOnly => continue,
1423 let type_path = format!("{}::{}", module, t.ident);
1424 let mut process_alias = true;
1425 for tok in t.generics.params.iter() {
1426 if let syn::GenericParam::Lifetime(_) = tok {}
1427 else { process_alias = false; }
1431 syn::Type::Path(_) => {
1432 // If its a path with no generics, assume we don't map the aliased type and map it opaque
1433 crate_types.opaques.insert(type_path, &t.ident);
1436 crate_types.type_aliases.insert(type_path, resolve_imported_refs(&import_maps, (*t.ty).clone()));
1442 syn::Item::Enum(e) if is_enum_opaque(e) => {
1443 if let syn::Visibility::Public(_) = e.vis {
1444 match export_status(&e.attrs) {
1445 ExportStatus::Export => {},
1446 ExportStatus::NoExport|ExportStatus::TestOnly => continue,
1448 let enum_path = format!("{}::{}", module, e.ident);
1449 crate_types.opaques.insert(enum_path, &e.ident);
1452 syn::Item::Enum(e) => {
1453 if let syn::Visibility::Public(_) = e.vis {
1454 match export_status(&e.attrs) {
1455 ExportStatus::Export => {},
1456 ExportStatus::NoExport|ExportStatus::TestOnly => continue,
1458 let enum_path = format!("{}::{}", module, e.ident);
1459 crate_types.mirrored_enums.insert(enum_path, &e);
1468 let args: Vec<String> = env::args().collect();
1469 if args.len() != 7 {
1470 eprintln!("Usage: source/dir target/dir source_crate_name derived_templates.rs extra/includes.h extra/cpp/includes.hpp");
1474 let mut derived_templates = std::fs::OpenOptions::new().write(true).create(true).truncate(true)
1475 .open(&args[4]).expect("Unable to open new header file");
1476 let mut header_file = std::fs::OpenOptions::new().write(true).create(true).truncate(true)
1477 .open(&args[5]).expect("Unable to open new header file");
1478 let mut cpp_header_file = std::fs::OpenOptions::new().write(true).create(true).truncate(true)
1479 .open(&args[6]).expect("Unable to open new header file");
1481 writeln!(header_file, "#if defined(__GNUC__)\n#define MUST_USE_STRUCT __attribute__((warn_unused))").unwrap();
1482 writeln!(header_file, "#else\n#define MUST_USE_STRUCT\n#endif").unwrap();
1483 writeln!(header_file, "#if defined(__GNUC__)\n#define MUST_USE_RES __attribute__((warn_unused_result))").unwrap();
1484 writeln!(header_file, "#else\n#define MUST_USE_RES\n#endif").unwrap();
1485 writeln!(cpp_header_file, "#include <string.h>\nnamespace LDK {{").unwrap();
1487 // First parse the full crate's ASTs, caching them so that we can hold references to the AST
1488 // objects in other datastructures:
1489 let mut libast = FullLibraryAST { files: HashMap::new() };
1490 load_ast(&args[1], "/lib.rs", "".to_string(), &mut libast);
1492 // ...then walk the ASTs tracking what types we will map, and how, so that we can resolve them
1493 // when parsing other file ASTs...
1494 let mut libtypes = CrateTypes { traits: HashMap::new(), opaques: HashMap::new(), mirrored_enums: HashMap::new(),
1495 type_aliases: HashMap::new(), templates_defined: HashMap::default(), template_file: &mut derived_templates };
1496 walk_ast(&args[1], "/lib.rs", "".to_string(), &libast, &mut libtypes);
1498 // ... finally, do the actual file conversion/mapping, writing out types as we go.
1499 convert_file(&libast, &mut libtypes, &args[1], &args[2], "/lib.rs", &args[3], "", &mut header_file, &mut cpp_header_file);
1501 // For container templates which we created while walking the crate, make sure we add C++
1502 // mapped types so that C++ users can utilize the auto-destructors available.
1503 for (ty, has_destructor) in libtypes.templates_defined.iter() {
1504 write_cpp_wrapper(&mut cpp_header_file, ty, *has_destructor);
1506 writeln!(cpp_header_file, "}}").unwrap();
1508 header_file.flush().unwrap();
1509 cpp_header_file.flush().unwrap();
1510 derived_templates.flush().unwrap();