//! It also generates relevant memory-management functions and free-standing functions with
//! parameters mapped.
-use std::collections::HashMap;
+use std::collections::{HashMap, hash_map, HashSet};
use std::env;
use std::fs::File;
use std::io::{Read, Write};
-use std::path::Path;
use std::process;
use proc_macro2::{TokenTree, TokenStream, Span};
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();
+ writeln!(w, "pub(crate) extern \"C\" fn {}_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {{", struct_for).unwrap();
+ writeln!(w, "\tcrate::c_types::serialize_obj(unsafe {{ &*(obj as *const native{}) }})", struct_for).unwrap();
+ writeln!(w, "}}").unwrap();
+ writeln!(w, "#[no_mangle]").unwrap();
writeln!(w, "pub extern \"C\" fn {}_read(ser: crate::c_types::u8slice) -> {} {{", struct_for, struct_for).unwrap();
writeln!(w, "\tif let Ok(res) = crate::c_types::deserialize_obj(ser) {{").unwrap();
writeln!(w, "\t\t{} {{ inner: Box::into_raw(Box::new(res)), is_owned: true }}", struct_for).unwrap();
}
}
-/// Convert "impl trait_path for for_obj { .. }" for manually-mapped types (ie (de)serialization)
-fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path, for_obj: &syn::Ident, types: &TypeResolver) {
- if let Some(t) = types.maybe_resolve_path(&trait_path, None) {
- let s = types.maybe_resolve_ident(for_obj).unwrap();
- if !types.crate_types.opaques.get(&s).is_some() { return; }
+/// Convert "impl trait_path for for_ty { .. }" for manually-mapped types (ie (de)serialization)
+fn maybe_convert_trait_impl<W: std::io::Write>(w: &mut W, trait_path: &syn::Path, for_ty: &syn::Type, types: &mut TypeResolver, generics: &GenericTypes) {
+ if let Some(t) = types.maybe_resolve_path(&trait_path, Some(generics)) {
+ let for_obj;
+ let full_obj_path;
+ let mut has_inner = false;
+ if let syn::Type::Path(ref p) = for_ty {
+ 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)));
+ } else { return; }
+ } else {
+ // We assume that anything that isn't a Path is somehow a generic that ends up in our
+ // derived-types module.
+ let mut for_obj_vec = Vec::new();
+ types.write_c_type(&mut for_obj_vec, for_ty, Some(generics), false);
+ full_obj_path = String::from_utf8(for_obj_vec).unwrap();
+ assert!(full_obj_path.starts_with(TypeResolver::generated_container_path()));
+ for_obj = full_obj_path[TypeResolver::generated_container_path().len() + 2..].into();
+ }
+
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, for_obj).unwrap();
- writeln!(w, "\tcrate::c_types::serialize_obj(unsafe {{ &(*(*obj).inner) }})").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,
+ elem: Box::new(for_ty.clone()) });
+ assert!(!types.write_from_c_conversion_new_var(w, &syn::Ident::new("obj", Span::call_site()), &ref_type, Some(generics)));
+
+ write!(w, "\tcrate::c_types::serialize_obj(").unwrap();
+ types.write_from_c_conversion_prefix(w, &ref_type, Some(generics));
+ write!(w, "unsafe {{ &*obj }}").unwrap();
+ types.write_from_c_conversion_suffix(w, &ref_type, Some(generics));
+ writeln!(w, ")").unwrap();
+
writeln!(w, "}}").unwrap();
+ if has_inner {
+ writeln!(w, "#[no_mangle]").unwrap();
+ writeln!(w, "pub(crate) extern \"C\" fn {}_write_void(obj: *const c_void) -> crate::c_types::derived::CVec_u8Z {{", for_obj).unwrap();
+ writeln!(w, "\tcrate::c_types::serialize_obj(unsafe {{ &*(obj as *const native{}) }})", for_obj).unwrap();
+ writeln!(w, "}}").unwrap();
+ }
},
- "util::ser::Readable" => {
+ "util::ser::Readable"|"util::ser::ReadableArgs" => {
+ // Create the Result<Object, DecodeError> syn::Type
+ let mut err_segs = syn::punctuated::Punctuated::new();
+ err_segs.push(syn::PathSegment { ident: syn::Ident::new("ln", Span::call_site()), arguments: syn::PathArguments::None });
+ err_segs.push(syn::PathSegment { ident: syn::Ident::new("msgs", Span::call_site()), arguments: syn::PathArguments::None });
+ err_segs.push(syn::PathSegment { ident: syn::Ident::new("DecodeError", Span::call_site()), arguments: syn::PathArguments::None });
+ let mut args = syn::punctuated::Punctuated::new();
+ args.push(syn::GenericArgument::Type(for_ty.clone()));
+ args.push(syn::GenericArgument::Type(syn::Type::Path(syn::TypePath {
+ qself: None, path: syn::Path {
+ leading_colon: Some(syn::Token![::](Span::call_site())), segments: err_segs,
+ }
+ })));
+ let mut res_segs = syn::punctuated::Punctuated::new();
+ res_segs.push(syn::PathSegment {
+ ident: syn::Ident::new("Result", Span::call_site()),
+ arguments: syn::PathArguments::AngleBracketed(syn::AngleBracketedGenericArguments {
+ colon2_token: None, lt_token: syn::Token![<](Span::call_site()), args, gt_token: syn::Token![>](Span::call_site()),
+ })
+ });
+ let res_ty = syn::Type::Path(syn::TypePath { qself: None, path: syn::Path {
+ leading_colon: None, segments: res_segs } });
+
writeln!(w, "#[no_mangle]").unwrap();
- writeln!(w, "pub extern \"C\" fn {}_read(ser: crate::c_types::u8slice) -> {} {{", for_obj, for_obj).unwrap();
- writeln!(w, "\tif let Ok(res) = crate::c_types::deserialize_obj(ser) {{").unwrap();
- writeln!(w, "\t\t{} {{ inner: Box::into_raw(Box::new(res)), is_owned: true }}", for_obj).unwrap();
- writeln!(w, "\t}} else {{").unwrap();
- writeln!(w, "\t\t{} {{ inner: std::ptr::null_mut(), is_owned: true }}", for_obj).unwrap();
- writeln!(w, "\t}}\n}}").unwrap();
+ write!(w, "pub extern \"C\" fn {}_read(ser: crate::c_types::u8slice", for_obj).unwrap();
+
+ let mut arg_conv = Vec::new();
+ if t == "util::ser::ReadableArgs" {
+ write!(w, ", arg: ").unwrap();
+ assert!(trait_path.leading_colon.is_none());
+ let args_seg = trait_path.segments.iter().last().unwrap();
+ assert_eq!(format!("{}", args_seg.ident), "ReadableArgs");
+ if let syn::PathArguments::AngleBracketed(args) = &args_seg.arguments {
+ assert_eq!(args.args.len(), 1);
+ if let syn::GenericArgument::Type(args_ty) = args.args.iter().next().unwrap() {
+ types.write_c_type(w, args_ty, Some(generics), false);
+
+ assert!(!types.write_from_c_conversion_new_var(&mut arg_conv, &syn::Ident::new("arg", Span::call_site()), &args_ty, Some(generics)));
+
+ write!(&mut arg_conv, "\tlet arg_conv = ").unwrap();
+ types.write_from_c_conversion_prefix(&mut arg_conv, &args_ty, Some(generics));
+ write!(&mut arg_conv, "arg").unwrap();
+ types.write_from_c_conversion_suffix(&mut arg_conv, &args_ty, Some(generics));
+ } else { unreachable!(); }
+ } else { unreachable!(); }
+ }
+ write!(w, ") -> ").unwrap();
+ types.write_c_type(w, &res_ty, Some(generics), false);
+ writeln!(w, " {{").unwrap();
+
+ if t == "util::ser::ReadableArgs" {
+ w.write(&arg_conv).unwrap();
+ write!(w, ";\n\tlet res: ").unwrap();
+ // At least in one case we need type annotations here, so provide them.
+ types.write_rust_type(w, Some(generics), &res_ty);
+ writeln!(w, " = crate::c_types::deserialize_obj_arg(ser, arg_conv);").unwrap();
+ } else {
+ writeln!(w, "\tlet res = crate::c_types::deserialize_obj(ser);").unwrap();
+ }
+ write!(w, "\t").unwrap();
+ if types.write_to_c_conversion_new_var(w, &syn::Ident::new("res", Span::call_site()), &res_ty, Some(generics), false) {
+ write!(w, "\n\t").unwrap();
+ }
+ types.write_to_c_conversion_inline_prefix(w, &res_ty, Some(generics), false);
+ write!(w, "res").unwrap();
+ types.write_to_c_conversion_inline_suffix(w, &res_ty, Some(generics), false);
+ writeln!(w, "\n}}").unwrap();
},
_ => {},
}
}
}
+/// Convert "TraitA : TraitB" to a single function name and return type.
+///
+/// This is (obviously) somewhat over-specialized and only useful for TraitB's that only require a
+/// single function (eg for serialization).
+fn convert_trait_impl_field(trait_path: &str) -> (String, &'static str) {
+ match trait_path {
+ "util::ser::Writeable" => ("write".to_owned(), "crate::c_types::derived::CVec_u8Z"),
+ _ => unimplemented!(),
+ }
+}
+
+/// Companion to convert_trait_impl_field, write an assignment for the function defined by it for
+/// `for_obj` which implements the the trait at `trait_path`.
+fn write_trait_impl_field_assign<W: std::io::Write>(w: &mut W, trait_path: &str, for_obj: &syn::Ident) {
+ match trait_path {
+ "util::ser::Writeable" => {
+ writeln!(w, "\t\twrite: {}_write_void,", for_obj).unwrap();
+ },
+ _ => unimplemented!(),
+ }
+}
+
+/// Write out the impl block for a defined trait struct which has a supertrait
+fn do_write_impl_trait<W: std::io::Write>(w: &mut W, trait_path: &str, trait_name: &syn::Ident, for_obj: &str) {
+ match trait_path {
+ "util::events::MessageSendEventsProvider" => {
+ writeln!(w, "impl lightning::{} for {} {{", trait_path, for_obj).unwrap();
+ writeln!(w, "\tfn get_and_clear_pending_msg_events(&self) -> Vec<lightning::util::events::MessageSendEvent> {{").unwrap();
+ writeln!(w, "\t\t<crate::{} as lightning::{}>::get_and_clear_pending_msg_events(&self.{})", trait_path, trait_path, trait_name).unwrap();
+ writeln!(w, "\t}}\n}}").unwrap();
+ },
+ "util::ser::Writeable" => {
+ writeln!(w, "impl lightning::{} for {} {{", trait_path, for_obj).unwrap();
+ writeln!(w, "\tfn write<W: lightning::util::ser::Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {{").unwrap();
+ writeln!(w, "\t\tlet vec = (self.write)(self.this_arg);").unwrap();
+ writeln!(w, "\t\tw.write_all(vec.as_slice())").unwrap();
+ writeln!(w, "\t}}\n}}").unwrap();
+ },
+ _ => panic!(),
+ }
+}
+
// *******************************
// *** Per-Type Printing Logic ***
// *******************************
if supertrait.paren_token.is_some() || supertrait.lifetimes.is_some() {
unimplemented!();
}
+ // First try to resolve path to find in-crate traits, but if that doesn't work
+ // assume its a prelude trait (eg Clone, etc) and just use the single ident.
+ let types_opt: Option<&TypeResolver> = $types;
+ if let Some(types) = types_opt {
+ if let Some(path) = types.maybe_resolve_path(&supertrait.path, None) {
+ match (&path as &str, &supertrait.path.segments.iter().last().unwrap().ident) {
+ $( $pat => $e, )*
+ }
+ continue;
+ }
+ }
if let Some(ident) = supertrait.path.get_ident() {
match (&format!("{}", ident) as &str, &ident) {
$( $pat => $e, )*
}
- } else {
- let path = $types.resolve_path(&supertrait.path, None);
- match (&path as &str, &supertrait.path.segments.iter().last().unwrap().ident) {
- $( $pat => $e, )*
- }
+ } else if types_opt.is_some() {
+ panic!("Supertrait unresolvable and not single-ident");
}
},
syn::TypeParamBound::Lifetime(_) => unimplemented!(),
}
}
// Add functions which may be required for supertrait implementations.
- walk_supertraits!(t, types, (
+ walk_supertraits!(t, Some(&types), (
("Clone", _) => {
writeln!(w, "\tpub clone: Option<extern \"C\" fn (this_arg: *const c_void) -> *mut c_void>,").unwrap();
generated_fields.push("clone".to_owned());
},
("Send", _) => {}, ("Sync", _) => {},
(s, i) => {
- // For in-crate supertraits, just store a C-mapped copy of the supertrait as a member.
- if types.crate_types.traits.get(s).is_none() { unimplemented!(); }
- writeln!(w, "\tpub {}: crate::{},", i, s).unwrap();
- generated_fields.push(format!("{}", i));
+ generated_fields.push(if types.crate_types.traits.get(s).is_none() {
+ let (name, ret) = convert_trait_impl_field(s);
+ writeln!(w, "\tpub {}: extern \"C\" fn (this_arg: *const c_void) -> {},", name, ret).unwrap();
+ name
+ } else {
+ // For in-crate supertraits, just store a C-mapped copy of the supertrait as a member.
+ writeln!(w, "\tpub {}: crate::{},", i, s).unwrap();
+ format!("{}", i)
+ });
}
) );
writeln!(w, "\tpub free: Option<extern \"C\" fn(this_arg: *mut c_void)>,").unwrap();
generated_fields.push("free".to_owned());
writeln!(w, "}}").unwrap();
// Implement supertraits for the C-mapped struct.
- walk_supertraits!(t, types, (
+ walk_supertraits!(t, Some(&types), (
("Send", _) => writeln!(w, "unsafe impl Send for {} {{}}", trait_name).unwrap(),
("Sync", _) => writeln!(w, "unsafe impl Sync for {} {{}}", trait_name).unwrap(),
("std::cmp::Eq", _) => {
writeln!(w, "\t}}\n}}").unwrap();
},
(s, i) => {
- if s != "util::events::MessageSendEventsProvider" { unimplemented!(); }
- // XXX: We straight-up cheat here - instead of bothering to get the trait object we
- // just print what we need since this is only used in one place.
- writeln!(w, "impl lightning::{} for {} {{", s, trait_name).unwrap();
- writeln!(w, "\tfn get_and_clear_pending_msg_events(&self) -> Vec<lightning::util::events::MessageSendEvent> {{").unwrap();
- writeln!(w, "\t\t<crate::{} as lightning::{}>::get_and_clear_pending_msg_events(&self.{})", s, s, i).unwrap();
- writeln!(w, "\t}}\n}}").unwrap();
+ do_write_impl_trait(w, s, i, &trait_name);
}
) );
writeln!(w, "\t\t}}\n\t}}\n}}").unwrap();
write_cpp_wrapper(cpp_headers, &trait_name, true);
- types.trait_declared(&t.ident, t);
}
/// Write out a simple "opaque" type (eg structs) which contain a pointer to the native Rust type
writeln!(w, "#[allow(unused)]").unwrap();
writeln!(w, "/// When moving out of the pointer, we have to ensure we aren't a reference, this makes that easy").unwrap();
writeln!(w, "impl {} {{", struct_name).unwrap();
- writeln!(w, "\tpub(crate) fn take_ptr(mut self) -> *mut native{} {{", struct_name).unwrap();
+ writeln!(w, "\tpub(crate) fn take_inner(mut self) -> *mut native{} {{", struct_name).unwrap();
writeln!(w, "\t\tassert!(self.is_owned);").unwrap();
writeln!(w, "\t\tlet ret = self.inner;").unwrap();
writeln!(w, "\t\tself.inner = std::ptr::null_mut();").unwrap();
writeln!(w, "\t\tret").unwrap();
writeln!(w, "\t}}\n}}").unwrap();
- 'attr_loop: for attr in attrs.iter() {
- let tokens_clone = attr.tokens.clone();
- let mut token_iter = tokens_clone.into_iter();
- if let Some(token) = token_iter.next() {
- match token {
- TokenTree::Group(g) => {
- if format!("{}", single_ident_generic_path_to_ident(&attr.path).unwrap()) == "derive" {
- for id in g.stream().into_iter() {
- if let TokenTree::Ident(i) = id {
- if i == "Clone" {
- writeln!(w, "impl Clone for {} {{", struct_name).unwrap();
- writeln!(w, "\tfn clone(&self) -> Self {{").unwrap();
- writeln!(w, "\t\tSelf {{").unwrap();
- writeln!(w, "\t\t\tinner: Box::into_raw(Box::new(unsafe {{ &*self.inner }}.clone())),").unwrap();
- writeln!(w, "\t\t\tis_owned: true,").unwrap();
- writeln!(w, "\t\t}}\n\t}}\n}}").unwrap();
- writeln!(w, "#[allow(unused)]").unwrap();
- writeln!(w, "/// Used only if an object of this type is returned as a trait impl by a method").unwrap();
- writeln!(w, "pub(crate) extern \"C\" fn {}_clone_void(this_ptr: *const c_void) -> *mut c_void {{", struct_name).unwrap();
- writeln!(w, "\tBox::into_raw(Box::new(unsafe {{ (*(this_ptr as *mut native{})).clone() }})) as *mut c_void", struct_name).unwrap();
- writeln!(w, "}}").unwrap();
- writeln!(w, "#[no_mangle]").unwrap();
- writeln!(w, "pub extern \"C\" fn {}_clone(orig: &{}) -> {} {{", struct_name, struct_name, struct_name).unwrap();
- writeln!(w, "\t{} {{ inner: Box::into_raw(Box::new(unsafe {{ &*orig.inner }}.clone())), is_owned: true }}", struct_name).unwrap();
- writeln!(w, "}}").unwrap();
- break 'attr_loop;
- }
- }
- }
- }
- },
- _ => {},
- }
- }
- }
-
write_cpp_wrapper(cpp_headers, &format!("{}", ident), true);
}
/// the struct itself, and then writing getters and setters for public, understood-type fields and
/// a constructor if every field is public.
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) {
- let struct_name = &format!("{}", s.ident);
- let export = export_status(&s.attrs);
- match export {
- ExportStatus::Export => {},
- ExportStatus::TestOnly => return,
- ExportStatus::NoExport => {
- types.struct_ignored(&s.ident);
- return;
- }
- }
+ if export_status(&s.attrs) != ExportStatus::Export { return; }
+ let struct_name = &format!("{}", s.ident);
writeln_opaque(w, &s.ident, struct_name, &s.generics, &s.attrs, types, extra_headers, cpp_headers);
- eprintln!("exporting fields for {}", struct_name);
if let syn::Fields::Named(fields) = &s.fields {
let mut gen_types = GenericTypes::new();
assert!(gen_types.learn_generics(&s.generics, types));
writeln!(w, "\t}})), is_owned: true }}\n}}").unwrap();
}
}
-
- types.struct_imported(&s.ident, struct_name.clone());
}
/// Prints a relevant conversion for impl *
///
/// A few non-crate Traits are hard-coded including Default.
fn writeln_impl<W: std::io::Write>(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();
+ if !gen_types.learn_generics(&i.generics, types) {
+ eprintln!("Not implementing anything for `impl (..)` due to not understood generics");
+ return;
+ }
+
+ if i.defaultness.is_some() || i.unsafety.is_some() { unimplemented!(); }
+ if let Some(trait_path) = i.trait_.as_ref() {
+ if trait_path.0.is_some() { unimplemented!(); }
+ if types.understood_c_path(&trait_path.1) {
+ eprintln!("Not implementing anything for `impl Trait for (..)` - we only support manual defines");
+ return;
+ } else {
+ // Just do a manual implementation:
+ maybe_convert_trait_impl(w, &trait_path.1, &*i.self_ty, types, &gen_types);
+ }
+ } else {
+ eprintln!("Not implementing anything for plain `impl (..)` block - we only support `impl Trait for (..)` blocks");
+ return;
+ }
+ }
+ return;
+ }
if let &syn::Type::Path(ref p) = &*i.self_ty {
if p.qself.is_some() { unimplemented!(); }
if let Some(ident) = single_ident_generic_path_to_ident(&p.path) {
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();
_ => {},
}
}
- walk_supertraits!(trait_obj, types, (
+ walk_supertraits!(trait_obj, Some(&types), (
("Clone", _) => {
writeln!(w, "\t\tclone: Some({}_clone_void),", ident).unwrap();
},
+ ("Sync", _) => {}, ("Send", _) => {},
+ ("std::marker::Sync", _) => {}, ("std::marker::Send", _) => {},
(s, t) => {
- if s.starts_with("util::") {
- let supertrait_obj = types.crate_types.traits.get(s).unwrap();
+ if let Some(supertrait_obj) = types.crate_types.traits.get(s) {
writeln!(w, "\t\t{}: crate::{} {{", t, s).unwrap();
writeln!(w, "\t\t\tthis_arg: unsafe {{ (*this_arg).inner as *mut c_void }},").unwrap();
writeln!(w, "\t\t\tfree: None,").unwrap();
}
}
write!(w, "\t\t}},\n").unwrap();
+ } else {
+ write_trait_impl_field_assign(w, s, ident);
}
}
) );
_ => unimplemented!(),
}
}
- walk_supertraits!(trait_obj, types, (
+ walk_supertraits!(trait_obj, Some(&types), (
(s, t) => {
- if s.starts_with("util::") {
+ if let Some(supertrait_obj) = types.crate_types.traits.get(s).cloned() {
writeln!(w, "use {}::{} as native{}Trait;", types.orig_crate, s, t).unwrap();
- let supertrait_obj = *types.crate_types.traits.get(s).unwrap();
for item in supertrait_obj.items.iter() {
match item {
syn::TraitItem::Method(m) => {
}
) );
write!(w, "\n").unwrap();
- } else if let Some(trait_ident) = trait_path.1.get_ident() {
+ } else if path_matches_nongeneric(&trait_path.1, &["From"]) {
+ } else if path_matches_nongeneric(&trait_path.1, &["Default"]) {
+ write!(w, "#[must_use]\n#[no_mangle]\npub extern \"C\" fn {}_default() -> {} {{\n", ident, ident).unwrap();
+ write!(w, "\t{} {{ inner: Box::into_raw(Box::new(Default::default())), is_owned: true }}\n", ident).unwrap();
+ write!(w, "}}\n").unwrap();
+ } else if path_matches_nongeneric(&trait_path.1, &["core", "cmp", "PartialEq"]) {
+ } else if (path_matches_nongeneric(&trait_path.1, &["core", "clone", "Clone"]) || path_matches_nongeneric(&trait_path.1, &["Clone"])) &&
+ types.c_type_has_inner_from_path(&resolved_path) {
+ writeln!(w, "impl Clone for {} {{", ident).unwrap();
+ writeln!(w, "\tfn clone(&self) -> Self {{").unwrap();
+ writeln!(w, "\t\tSelf {{").unwrap();
+ writeln!(w, "\t\t\tinner: if self.inner.is_null() {{ std::ptr::null_mut() }} else {{").unwrap();
+ writeln!(w, "\t\t\t\tBox::into_raw(Box::new(unsafe {{ &*self.inner }}.clone())) }},").unwrap();
+ writeln!(w, "\t\t\tis_owned: true,").unwrap();
+ writeln!(w, "\t\t}}\n\t}}\n}}").unwrap();
+ writeln!(w, "#[allow(unused)]").unwrap();
+ writeln!(w, "/// Used only if an object of this type is returned as a trait impl by a method").unwrap();
+ writeln!(w, "pub(crate) extern \"C\" fn {}_clone_void(this_ptr: *const c_void) -> *mut c_void {{", ident).unwrap();
+ writeln!(w, "\tBox::into_raw(Box::new(unsafe {{ (*(this_ptr as *mut native{})).clone() }})) as *mut c_void", ident).unwrap();
+ writeln!(w, "}}").unwrap();
+ writeln!(w, "#[no_mangle]").unwrap();
+ writeln!(w, "pub extern \"C\" fn {}_clone(orig: &{}) -> {} {{", ident, ident, ident).unwrap();
+ writeln!(w, "\torig.clone()").unwrap();
+ writeln!(w, "}}").unwrap();
+ } else {
//XXX: implement for other things like ToString
- match &format!("{}", trait_ident) as &str {
- "From" => {},
- "Default" => {
- write!(w, "#[must_use]\n#[no_mangle]\npub extern \"C\" fn {}_default() -> {} {{\n", ident, ident).unwrap();
- write!(w, "\t{} {{ inner: Box::into_raw(Box::new(Default::default())), is_owned: true }}\n", ident).unwrap();
- write!(w, "}}\n").unwrap();
- },
- "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, &ident, types),
- _ => {},
- }
- } else if p.path.get_ident().is_some() {
// If we have no generics, try a manual implementation:
- maybe_convert_trait_impl(w, &trait_path.1, &ident, types);
+ maybe_convert_trait_impl(w, &trait_path.1, &*i.self_ty, types, &gen_types);
}
} else {
let declared_type = (*types.get_declared_type(&ident).unwrap()).clone();
}
}
}
+ } else if let Some(resolved_path) = types.maybe_resolve_ident(&ident) {
+ if let Some(aliases) = types.crate_types.reverse_alias_map.get(&resolved_path).cloned() {
+ 'alias_impls: for (alias, arguments) in aliases {
+ let alias_resolved = types.resolve_path(&alias, None);
+ for (idx, gen) in i.generics.params.iter().enumerate() {
+ match gen {
+ syn::GenericParam::Type(type_param) => {
+ 'bounds_check: for bound in type_param.bounds.iter() {
+ if let syn::TypeParamBound::Trait(trait_bound) = bound {
+ if let syn::PathArguments::AngleBracketed(ref t) = &arguments {
+ assert!(idx < t.args.len());
+ if let syn::GenericArgument::Type(syn::Type::Path(p)) = &t.args[idx] {
+ let generic_arg = types.resolve_path(&p.path, None);
+ let generic_bound = types.resolve_path(&trait_bound.path, None);
+ if let Some(traits_impld) = types.crate_types.trait_impls.get(&generic_arg) {
+ for trait_impld in traits_impld {
+ if *trait_impld == generic_bound { continue 'bounds_check; }
+ }
+ eprintln!("struct {}'s generic arg {} didn't match bound {}", alias_resolved, generic_arg, generic_bound);
+ continue 'alias_impls;
+ } else {
+ eprintln!("struct {}'s generic arg {} didn't match bound {}", alias_resolved, generic_arg, generic_bound);
+ continue 'alias_impls;
+ }
+ } else { unimplemented!(); }
+ } else { unimplemented!(); }
+ } else { unimplemented!(); }
+ }
+ },
+ syn::GenericParam::Lifetime(_) => {},
+ syn::GenericParam::Const(_) => unimplemented!(),
+ }
+ }
+ let aliased_impl = syn::ItemImpl {
+ attrs: i.attrs.clone(),
+ brace_token: syn::token::Brace(Span::call_site()),
+ defaultness: None,
+ generics: syn::Generics {
+ lt_token: None,
+ params: syn::punctuated::Punctuated::new(),
+ gt_token: None,
+ where_clause: None,
+ },
+ impl_token: syn::Token![impl](Span::call_site()),
+ items: i.items.clone(),
+ self_ty: Box::new(syn::Type::Path(syn::TypePath { qself: None, path: alias.clone() })),
+ trait_: i.trait_.clone(),
+ unsafety: None,
+ };
+ writeln_impl(w, &aliased_impl, types);
+ }
+ } else {
+ eprintln!("Not implementing anything for {} due to it being marked not exported", ident);
+ }
} else {
- eprintln!("Not implementing anything for {} due to no-resolve (probably the type isn't pub or its marked not exported)", ident);
+ eprintln!("Not implementing anything for {} due to no-resolve (probably the type isn't pub)", ident);
}
}
}
}
-/// Returns true if the enum will be mapped as an opaue (ie struct with a pointer to the underlying
-/// type), otherwise it is mapped into a transparent, C-compatible version of itself.
-fn is_enum_opaque(e: &syn::ItemEnum) -> bool {
- for var in e.variants.iter() {
- if let syn::Fields::Unit = var.fields {
- } else if let syn::Fields::Named(fields) = &var.fields {
- for field in fields.named.iter() {
- match export_status(&field.attrs) {
- ExportStatus::Export|ExportStatus::TestOnly => {},
- ExportStatus::NoExport => return true,
- }
- }
- } else {
- return true;
- }
- }
- false
-}
/// Print a mapping of an enum. If all of the enum's fields are C-mapped in some form (or the enum
/// is unitary), we generate an equivalent enum with all types replaced with their C mapped
if is_enum_opaque(e) {
eprintln!("Skipping enum {} as it contains non-unit fields", e.ident);
writeln_opaque(w, &e.ident, &format!("{}", e.ident), &e.generics, &e.attrs, types, extra_headers, cpp_headers);
- types.enum_ignored(&e.ident);
return;
}
writeln_docs(w, &e.attrs, "");
if e.generics.lt_token.is_some() {
unimplemented!();
}
- types.mirrored_enum_declared(&e.ident);
let mut needs_free = false;
// ********************************
// *** File/Crate Walking Logic ***
// ********************************
-
-/// Simple utility to walk the modules in a crate - iterating over the modules (with file paths) in
-/// a single File.
-struct FileIter<'a, I: Iterator<Item = &'a syn::Item>> {
- in_dir: &'a str,
- path: &'a str,
- module: &'a str,
- item_iter: I,
+/// A public module
+struct ASTModule {
+ pub attrs: Vec<syn::Attribute>,
+ pub items: Vec<syn::Item>,
+ pub submods: Vec<String>,
}
-impl<'a, I: Iterator<Item = &'a syn::Item>> Iterator for FileIter<'a, I> {
- type Item = (String, String, &'a syn::ItemMod);
- fn next(&mut self) -> std::option::Option<<Self as std::iter::Iterator>::Item> {
- loop {
- match self.item_iter.next() {
- Some(syn::Item::Mod(m)) => {
- if let syn::Visibility::Public(_) = m.vis {
- match export_status(&m.attrs) {
- ExportStatus::Export => {},
- ExportStatus::NoExport|ExportStatus::TestOnly => continue,
- }
-
- let f_path = format!("{}/{}.rs", (self.path.as_ref() as &Path).parent().unwrap().display(), m.ident);
- let new_mod = if self.module.is_empty() { format!("{}", m.ident) } else { format!("{}::{}", self.module, m.ident) };
- if let Ok(_) = File::open(&format!("{}/{}", self.in_dir, f_path)) {
- return Some((f_path, new_mod, m));
+/// A struct containing the syn::File AST for each file in the crate.
+struct FullLibraryAST {
+ modules: HashMap<String, ASTModule, NonRandomHash>,
+}
+impl FullLibraryAST {
+ fn load_module(&mut self, module: String, attrs: Vec<syn::Attribute>, mut items: Vec<syn::Item>) {
+ let mut non_mod_items = Vec::with_capacity(items.len());
+ let mut submods = Vec::with_capacity(items.len());
+ for item in items.drain(..) {
+ match item {
+ syn::Item::Mod(m) if m.content.is_some() => {
+ if export_status(&m.attrs) == ExportStatus::Export {
+ if let syn::Visibility::Public(_) = m.vis {
+ let modident = format!("{}", m.ident);
+ let modname = if module != "" {
+ module.clone() + "::" + &modident
+ } else {
+ modident.clone()
+ };
+ self.load_module(modname, m.attrs, m.content.unwrap().1);
+ submods.push(modident);
} else {
- return Some((
- format!("{}/{}/mod.rs", (self.path.as_ref() as &Path).parent().unwrap().display(), m.ident),
- new_mod, m));
+ non_mod_items.push(syn::Item::Mod(m));
}
}
},
- Some(_) => {},
- None => return None,
+ syn::Item::Mod(_) => panic!("--pretty=expanded output should never have non-body modules"),
+ _ => { non_mod_items.push(item); }
}
}
+ self.modules.insert(module, ASTModule { attrs, items: non_mod_items, submods });
}
-}
-fn file_iter<'a>(file: &'a syn::File, in_dir: &'a str, path: &'a str, module: &'a str) ->
- impl Iterator<Item = (String, String, &'a syn::ItemMod)> + 'a {
- FileIter { in_dir, path, module, item_iter: file.items.iter() }
-}
-/// A struct containing the syn::File AST for each file in the crate.
-struct FullLibraryAST {
- files: HashMap<String, syn::File>,
+ pub fn load_lib(lib: syn::File) -> Self {
+ assert_eq!(export_status(&lib.attrs), ExportStatus::Export);
+ let mut res = Self { modules: HashMap::default() };
+ res.load_module("".to_owned(), lib.attrs, lib.items);
+ res
+ }
}
/// Do the Real Work of mapping an original file to C-callable wrappers. Creates a new file at
/// `out_path` and fills it with wrapper structs/functions to allow calling the things in the AST
/// at `module` from C.
-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) {
- let syntax = if let Some(ast) = libast.files.get(module) { ast } else { return };
-
- assert!(syntax.shebang.is_none()); // Not sure what this is, hope we dont have one
-
- let new_file_path = format!("{}/{}", out_dir, path);
- let _ = std::fs::create_dir((&new_file_path.as_ref() as &std::path::Path).parent().unwrap());
- let mut out = std::fs::OpenOptions::new().write(true).create(true).truncate(true)
- .open(new_file_path).expect("Unable to open new src file");
-
- assert_eq!(export_status(&syntax.attrs), ExportStatus::Export);
- writeln_docs(&mut out, &syntax.attrs, "");
-
- if path.ends_with("/lib.rs") {
- // Special-case the top-level lib.rs with various lint allows and a pointer to the c_types
- // and bitcoin hand-written modules.
- writeln!(out, "#![allow(unknown_lints)]").unwrap();
- writeln!(out, "#![allow(non_camel_case_types)]").unwrap();
- writeln!(out, "#![allow(non_snake_case)]").unwrap();
- writeln!(out, "#![allow(unused_imports)]").unwrap();
- writeln!(out, "#![allow(unused_variables)]").unwrap();
- writeln!(out, "#![allow(unused_mut)]").unwrap();
- writeln!(out, "#![allow(unused_parens)]").unwrap();
- writeln!(out, "#![allow(unused_unsafe)]").unwrap();
- writeln!(out, "#![allow(unused_braces)]").unwrap();
- writeln!(out, "mod c_types;").unwrap();
- writeln!(out, "mod bitcoin;").unwrap();
- } else {
- writeln!(out, "\nuse std::ffi::c_void;\nuse bitcoin::hashes::Hash;\nuse crate::c_types::*;\n").unwrap();
- }
+fn convert_file<'a, 'b>(libast: &'a FullLibraryAST, crate_types: &mut CrateTypes<'a>, out_dir: &str, orig_crate: &str, header_file: &mut File, cpp_header_file: &mut File) {
+ for (module, astmod) in libast.modules.iter() {
+ let ASTModule { ref attrs, ref items, ref submods } = astmod;
+ assert_eq!(export_status(&attrs), ExportStatus::Export);
+
+ let new_file_path = if submods.is_empty() {
+ format!("{}/{}.rs", out_dir, module.replace("::", "/"))
+ } else if module != "" {
+ format!("{}/{}/mod.rs", out_dir, module.replace("::", "/"))
+ } else {
+ format!("{}/lib.rs", out_dir)
+ };
+ let _ = std::fs::create_dir((&new_file_path.as_ref() as &std::path::Path).parent().unwrap());
+ let mut out = std::fs::OpenOptions::new().write(true).create(true).truncate(true)
+ .open(new_file_path).expect("Unable to open new src file");
+
+ writeln_docs(&mut out, &attrs, "");
+
+ if module == "" {
+ // Special-case the top-level lib.rs with various lint allows and a pointer to the c_types
+ // and bitcoin hand-written modules.
+ writeln!(out, "#![allow(unknown_lints)]").unwrap();
+ writeln!(out, "#![allow(non_camel_case_types)]").unwrap();
+ writeln!(out, "#![allow(non_snake_case)]").unwrap();
+ writeln!(out, "#![allow(unused_imports)]").unwrap();
+ writeln!(out, "#![allow(unused_variables)]").unwrap();
+ writeln!(out, "#![allow(unused_mut)]").unwrap();
+ writeln!(out, "#![allow(unused_parens)]").unwrap();
+ writeln!(out, "#![allow(unused_unsafe)]").unwrap();
+ writeln!(out, "#![allow(unused_braces)]").unwrap();
+ writeln!(out, "mod c_types;").unwrap();
+ writeln!(out, "mod bitcoin;").unwrap();
+ } else {
+ writeln!(out, "\nuse std::ffi::c_void;\nuse bitcoin::hashes::Hash;\nuse crate::c_types::*;\n").unwrap();
+ }
- for (path, new_mod, m) in file_iter(&syntax, in_dir, path, &module) {
- writeln_docs(&mut out, &m.attrs, "");
- writeln!(out, "pub mod {};", m.ident).unwrap();
- convert_file(libast, crate_types, in_dir, out_dir, &path,
- orig_crate, &new_mod, header_file, cpp_header_file);
- }
+ for m in submods {
+ writeln!(out, "pub mod {};", m).unwrap();
+ }
- eprintln!("Converting {} entries...", path);
+ eprintln!("Converting {} entries...", module);
- let mut type_resolver = TypeResolver::new(orig_crate, module, crate_types);
+ let import_resolver = ImportResolver::new(module, items);
+ let mut type_resolver = TypeResolver::new(orig_crate, module, import_resolver, crate_types);
- for item in syntax.items.iter() {
- match item {
- syn::Item::Use(u) => type_resolver.process_use(&mut out, &u),
- syn::Item::Static(_) => {},
- syn::Item::Enum(e) => {
- if let syn::Visibility::Public(_) = e.vis {
- writeln_enum(&mut out, &e, &mut type_resolver, header_file, cpp_header_file);
- }
- },
- syn::Item::Impl(i) => {
- writeln_impl(&mut out, &i, &mut type_resolver);
- },
- syn::Item::Struct(s) => {
- if let syn::Visibility::Public(_) = s.vis {
- writeln_struct(&mut out, &s, &mut type_resolver, header_file, cpp_header_file);
- }
- },
- syn::Item::Trait(t) => {
- if let syn::Visibility::Public(_) = t.vis {
- writeln_trait(&mut out, &t, &mut type_resolver, header_file, cpp_header_file);
- }
- },
- syn::Item::Mod(_) => {}, // We don't have to do anything - the top loop handles these.
- syn::Item::Const(c) => {
- // Re-export any primitive-type constants.
- if let syn::Visibility::Public(_) = c.vis {
- if let syn::Type::Path(p) = &*c.ty {
- let resolved_path = type_resolver.resolve_path(&p.path, None);
- if type_resolver.is_primitive(&resolved_path) {
- writeln!(out, "\n#[no_mangle]").unwrap();
- writeln!(out, "pub static {}: {} = {}::{}::{};", c.ident, resolved_path, orig_crate, module, c.ident).unwrap();
- }
+ for item in items.iter() {
+ match item {
+ syn::Item::Use(_) => {}, // Handled above
+ syn::Item::Static(_) => {},
+ syn::Item::Enum(e) => {
+ if let syn::Visibility::Public(_) = e.vis {
+ writeln_enum(&mut out, &e, &mut type_resolver, header_file, cpp_header_file);
}
- }
- },
- syn::Item::Type(t) => {
- if let syn::Visibility::Public(_) = t.vis {
- match export_status(&t.attrs) {
- ExportStatus::Export => {},
- ExportStatus::NoExport|ExportStatus::TestOnly => continue,
+ },
+ syn::Item::Impl(i) => {
+ writeln_impl(&mut out, &i, &mut type_resolver);
+ },
+ syn::Item::Struct(s) => {
+ if let syn::Visibility::Public(_) = s.vis {
+ writeln_struct(&mut out, &s, &mut type_resolver, header_file, cpp_header_file);
}
-
- let mut process_alias = true;
- for tok in t.generics.params.iter() {
- if let syn::GenericParam::Lifetime(_) = tok {}
- else { process_alias = false; }
+ },
+ syn::Item::Trait(t) => {
+ if let syn::Visibility::Public(_) = t.vis {
+ writeln_trait(&mut out, &t, &mut type_resolver, header_file, cpp_header_file);
}
- if process_alias {
- match &*t.ty {
- syn::Type::Path(_) =>
- writeln_opaque(&mut out, &t.ident, &format!("{}", t.ident), &t.generics, &t.attrs, &type_resolver, header_file, cpp_header_file),
- _ => {}
+ },
+ syn::Item::Mod(_) => {}, // We don't have to do anything - the top loop handles these.
+ syn::Item::Const(c) => {
+ // Re-export any primitive-type constants.
+ if let syn::Visibility::Public(_) = c.vis {
+ if let syn::Type::Path(p) = &*c.ty {
+ let resolved_path = type_resolver.resolve_path(&p.path, None);
+ if type_resolver.is_primitive(&resolved_path) {
+ writeln!(out, "\n#[no_mangle]").unwrap();
+ writeln!(out, "pub static {}: {} = {}::{}::{};", c.ident, resolved_path, orig_crate, module, c.ident).unwrap();
+ }
}
}
- }
- },
- syn::Item::Fn(f) => {
- if let syn::Visibility::Public(_) = f.vis {
- writeln_fn(&mut out, &f, &mut type_resolver);
- }
- },
- syn::Item::Macro(m) => {
- if m.ident.is_none() { // If its not a macro definition
- convert_macro(&mut out, &m.mac.path, &m.mac.tokens, &type_resolver);
- }
- },
- syn::Item::Verbatim(_) => {},
- syn::Item::ExternCrate(_) => {},
- _ => unimplemented!(),
- }
- }
-
- out.flush().unwrap();
-}
-
-/// Load the AST for each file in the crate, filling the FullLibraryAST object
-fn load_ast(in_dir: &str, path: &str, module: String, ast_storage: &mut FullLibraryAST) {
- eprintln!("Loading {}{}...", in_dir, path);
-
- let mut file = File::open(format!("{}/{}", in_dir, path)).expect("Unable to open file");
- let mut src = String::new();
- file.read_to_string(&mut src).expect("Unable to read file");
- let syntax = syn::parse_file(&src).expect("Unable to parse file");
-
- assert_eq!(export_status(&syntax.attrs), ExportStatus::Export);
-
- for (path, new_mod, _) in file_iter(&syntax, in_dir, path, &module) {
- load_ast(in_dir, &path, new_mod, ast_storage);
- }
- ast_storage.files.insert(module, syntax);
-}
+ },
+ syn::Item::Type(t) => {
+ if let syn::Visibility::Public(_) = t.vis {
+ match export_status(&t.attrs) {
+ ExportStatus::Export => {},
+ ExportStatus::NoExport|ExportStatus::TestOnly => continue,
+ }
-/// Insert ident -> absolute Path resolutions into imports from the given UseTree and path-prefix.
-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>) {
- match u {
- syn::UseTree::Path(p) => {
- path.push(syn::PathSegment { ident: p.ident.clone(), arguments: syn::PathArguments::None });
- process_use_intern(&p.tree, path, imports);
- },
- syn::UseTree::Name(n) => {
- path.push(syn::PathSegment { ident: n.ident.clone(), arguments: syn::PathArguments::None });
- imports.insert(&n.ident, syn::Path { leading_colon: Some(syn::Token![::](Span::call_site())), segments: path });
- },
- syn::UseTree::Group(g) => {
- for i in g.items.iter() {
- process_use_intern(i, path.clone(), imports);
+ let mut process_alias = true;
+ for tok in t.generics.params.iter() {
+ if let syn::GenericParam::Lifetime(_) = tok {}
+ else { process_alias = false; }
+ }
+ if process_alias {
+ match &*t.ty {
+ syn::Type::Path(_) =>
+ writeln_opaque(&mut out, &t.ident, &format!("{}", t.ident), &t.generics, &t.attrs, &type_resolver, header_file, cpp_header_file),
+ _ => {}
+ }
+ }
+ }
+ },
+ syn::Item::Fn(f) => {
+ if let syn::Visibility::Public(_) = f.vis {
+ writeln_fn(&mut out, &f, &mut type_resolver);
+ }
+ },
+ syn::Item::Macro(m) => {
+ if m.ident.is_none() { // If its not a macro definition
+ convert_macro(&mut out, &m.mac.path, &m.mac.tokens, &type_resolver);
+ }
+ },
+ syn::Item::Verbatim(_) => {},
+ syn::Item::ExternCrate(_) => {},
+ _ => unimplemented!(),
}
- },
- _ => {}
- }
-}
+ }
-/// Map all the Paths in a Type into absolute paths given a set of imports (generated via process_use_intern)
-fn resolve_imported_refs(imports: &HashMap<&syn::Ident, syn::Path>, mut ty: syn::Type) -> syn::Type {
- match &mut ty {
- syn::Type::Path(p) => {
- if let Some(ident) = p.path.get_ident() {
- if let Some(newpath) = imports.get(ident) {
- p.path = newpath.clone();
- }
- } else { unimplemented!(); }
- },
- syn::Type::Reference(r) => {
- r.elem = Box::new(resolve_imported_refs(imports, (*r.elem).clone()));
- },
- syn::Type::Slice(s) => {
- s.elem = Box::new(resolve_imported_refs(imports, (*s.elem).clone()));
- },
- syn::Type::Tuple(t) => {
- for e in t.elems.iter_mut() {
- *e = resolve_imported_refs(imports, e.clone());
- }
- },
- _ => unimplemented!(),
+ out.flush().unwrap();
}
- ty
}
-/// Walk the FullLibraryAST, deciding how things will be mapped and adding tracking to CrateTypes.
-fn walk_ast<'a>(in_dir: &str, path: &str, module: String, ast_storage: &'a FullLibraryAST, crate_types: &mut CrateTypes<'a>) {
- let syntax = if let Some(ast) = ast_storage.files.get(&module) { ast } else { return };
- assert_eq!(export_status(&syntax.attrs), ExportStatus::Export);
-
- for (path, new_mod, _) in file_iter(&syntax, in_dir, path, &module) {
- walk_ast(in_dir, &path, new_mod, ast_storage, crate_types);
- }
-
- let mut import_maps = HashMap::new();
-
- for item in syntax.items.iter() {
+fn walk_private_mod<'a>(module: String, items: &'a syn::ItemMod, crate_types: &mut CrateTypes<'a>) {
+ let import_resolver = ImportResolver::new(&module, &items.content.as_ref().unwrap().1);
+ for item in items.content.as_ref().unwrap().1.iter() {
match item {
- syn::Item::Use(u) => {
- process_use_intern(&u.tree, syn::punctuated::Punctuated::new(), &mut import_maps);
- },
- syn::Item::Struct(s) => {
- if let syn::Visibility::Public(_) = s.vis {
- match export_status(&s.attrs) {
- ExportStatus::Export => {},
- ExportStatus::NoExport|ExportStatus::TestOnly => continue,
- }
- let struct_path = format!("{}::{}", module, s.ident);
- crate_types.opaques.insert(struct_path, &s.ident);
- }
- },
- syn::Item::Trait(t) => {
- if let syn::Visibility::Public(_) = t.vis {
- match export_status(&t.attrs) {
- ExportStatus::Export => {},
- ExportStatus::NoExport|ExportStatus::TestOnly => continue,
+ syn::Item::Mod(m) => walk_private_mod(format!("{}::{}", module, m.ident), m, crate_types),
+ syn::Item::Impl(i) => {
+ if let &syn::Type::Path(ref p) = &*i.self_ty {
+ if let Some(trait_path) = i.trait_.as_ref() {
+ if let Some(tp) = import_resolver.maybe_resolve_path(&trait_path.1, None) {
+ if let Some(sp) = import_resolver.maybe_resolve_path(&p.path, None) {
+ match crate_types.trait_impls.entry(sp) {
+ hash_map::Entry::Occupied(mut e) => { e.get_mut().push(tp); },
+ hash_map::Entry::Vacant(e) => { e.insert(vec![tp]); },
+ }
+ }
+ }
}
- let trait_path = format!("{}::{}", module, t.ident);
- crate_types.traits.insert(trait_path, &t);
}
},
- syn::Item::Type(t) => {
- if let syn::Visibility::Public(_) = t.vis {
- match export_status(&t.attrs) {
- ExportStatus::Export => {},
- ExportStatus::NoExport|ExportStatus::TestOnly => continue,
- }
- let type_path = format!("{}::{}", module, t.ident);
- let mut process_alias = true;
- for tok in t.generics.params.iter() {
- if let syn::GenericParam::Lifetime(_) = tok {}
- else { process_alias = false; }
+ _ => {},
+ }
+ }
+}
+
+/// Walk the FullLibraryAST, deciding how things will be mapped and adding tracking to CrateTypes.
+fn walk_ast<'a>(ast_storage: &'a FullLibraryAST, crate_types: &mut CrateTypes<'a>) {
+ for (module, astmod) in ast_storage.modules.iter() {
+ let ASTModule { ref attrs, ref items, submods: _ } = astmod;
+ assert_eq!(export_status(&attrs), ExportStatus::Export);
+ let import_resolver = ImportResolver::new(module, items);
+
+ for item in items.iter() {
+ match item {
+ syn::Item::Struct(s) => {
+ if let syn::Visibility::Public(_) = s.vis {
+ match export_status(&s.attrs) {
+ ExportStatus::Export => {},
+ ExportStatus::NoExport|ExportStatus::TestOnly => continue,
+ }
+ let struct_path = format!("{}::{}", module, s.ident);
+ crate_types.opaques.insert(struct_path, &s.ident);
}
- if process_alias {
- match &*t.ty {
- syn::Type::Path(_) => {
- // If its a path with no generics, assume we don't map the aliased type and map it opaque
- crate_types.opaques.insert(type_path, &t.ident);
+ },
+ syn::Item::Trait(t) => {
+ if let syn::Visibility::Public(_) = t.vis {
+ match export_status(&t.attrs) {
+ ExportStatus::Export => {},
+ ExportStatus::NoExport|ExportStatus::TestOnly => continue,
+ }
+ let trait_path = format!("{}::{}", module, t.ident);
+ walk_supertraits!(t, None, (
+ ("Clone", _) => {
+ crate_types.clonable_types.insert("crate::".to_owned() + &trait_path);
},
- _ => {
- crate_types.type_aliases.insert(type_path, resolve_imported_refs(&import_maps, (*t.ty).clone()));
+ (_, _) => {}
+ ) );
+ crate_types.traits.insert(trait_path, &t);
+ }
+ },
+ syn::Item::Type(t) => {
+ if let syn::Visibility::Public(_) = t.vis {
+ match export_status(&t.attrs) {
+ ExportStatus::Export => {},
+ ExportStatus::NoExport|ExportStatus::TestOnly => continue,
+ }
+ let type_path = format!("{}::{}", module, t.ident);
+ let mut process_alias = true;
+ for tok in t.generics.params.iter() {
+ if let syn::GenericParam::Lifetime(_) = tok {}
+ else { process_alias = false; }
+ }
+ if process_alias {
+ match &*t.ty {
+ syn::Type::Path(p) => {
+ // If its a path with no generics, assume we don't map the aliased type and map it opaque
+ let mut segments = syn::punctuated::Punctuated::new();
+ segments.push(syn::PathSegment {
+ ident: t.ident.clone(),
+ arguments: syn::PathArguments::None,
+ });
+ let path_obj = syn::Path { leading_colon: None, segments };
+ let args_obj = p.path.segments.last().unwrap().arguments.clone();
+ match crate_types.reverse_alias_map.entry(import_resolver.maybe_resolve_path(&p.path, None).unwrap()) {
+ hash_map::Entry::Occupied(mut e) => { e.get_mut().push((path_obj, args_obj)); },
+ hash_map::Entry::Vacant(e) => { e.insert(vec![(path_obj, args_obj)]); },
+ }
+
+ crate_types.opaques.insert(type_path.clone(), &t.ident);
+ },
+ _ => {
+ crate_types.type_aliases.insert(type_path, import_resolver.resolve_imported_refs((*t.ty).clone()));
+ }
}
}
}
- }
- },
- syn::Item::Enum(e) if is_enum_opaque(e) => {
- if let syn::Visibility::Public(_) = e.vis {
- match export_status(&e.attrs) {
- ExportStatus::Export => {},
- ExportStatus::NoExport|ExportStatus::TestOnly => continue,
+ },
+ syn::Item::Enum(e) if is_enum_opaque(e) => {
+ if let syn::Visibility::Public(_) = e.vis {
+ match export_status(&e.attrs) {
+ ExportStatus::Export => {},
+ ExportStatus::NoExport|ExportStatus::TestOnly => continue,
+ }
+ let enum_path = format!("{}::{}", module, e.ident);
+ crate_types.opaques.insert(enum_path, &e.ident);
}
- let enum_path = format!("{}::{}", module, e.ident);
- crate_types.opaques.insert(enum_path, &e.ident);
- }
- },
- syn::Item::Enum(e) => {
- if let syn::Visibility::Public(_) = e.vis {
- match export_status(&e.attrs) {
- ExportStatus::Export => {},
- ExportStatus::NoExport|ExportStatus::TestOnly => continue,
+ },
+ syn::Item::Enum(e) => {
+ if let syn::Visibility::Public(_) = e.vis {
+ match export_status(&e.attrs) {
+ ExportStatus::Export => {},
+ ExportStatus::NoExport|ExportStatus::TestOnly => continue,
+ }
+ let enum_path = format!("{}::{}", module, e.ident);
+ crate_types.mirrored_enums.insert(enum_path, &e);
}
- let enum_path = format!("{}::{}", module, e.ident);
- crate_types.mirrored_enums.insert(enum_path, &e);
- }
- },
- _ => {},
+ },
+ syn::Item::Impl(i) => {
+ if let &syn::Type::Path(ref p) = &*i.self_ty {
+ if let Some(trait_path) = i.trait_.as_ref() {
+ if path_matches_nongeneric(&trait_path.1, &["core", "clone", "Clone"]) {
+ if let Some(full_path) = import_resolver.maybe_resolve_path(&p.path, None) {
+ crate_types.clonable_types.insert("crate::".to_owned() + &full_path);
+ }
+ }
+ if let Some(tp) = import_resolver.maybe_resolve_path(&trait_path.1, None) {
+ if let Some(sp) = import_resolver.maybe_resolve_path(&p.path, None) {
+ match crate_types.trait_impls.entry(sp) {
+ hash_map::Entry::Occupied(mut e) => { e.get_mut().push(tp); },
+ hash_map::Entry::Vacant(e) => { e.insert(vec![tp]); },
+ }
+ }
+ }
+ }
+ }
+ },
+ syn::Item::Mod(m) => walk_private_mod(format!("{}::{}", module, m.ident), m, crate_types),
+ _ => {},
+ }
}
}
}
fn main() {
let args: Vec<String> = env::args().collect();
- if args.len() != 7 {
- eprintln!("Usage: source/dir target/dir source_crate_name derived_templates.rs extra/includes.h extra/cpp/includes.hpp");
+ if args.len() != 6 {
+ eprintln!("Usage: target/dir source_crate_name derived_templates.rs extra/includes.h extra/cpp/includes.hpp");
process::exit(1);
}
let mut derived_templates = std::fs::OpenOptions::new().write(true).create(true).truncate(true)
- .open(&args[4]).expect("Unable to open new header file");
+ .open(&args[3]).expect("Unable to open new header file");
let mut header_file = std::fs::OpenOptions::new().write(true).create(true).truncate(true)
- .open(&args[5]).expect("Unable to open new header file");
+ .open(&args[4]).expect("Unable to open new header file");
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");
+ .open(&args[5]).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 <string.h>\nnamespace LDK {{").unwrap();
// First parse the full crate's ASTs, caching them so that we can hold references to the AST
// objects in other datastructures:
- let mut libast = FullLibraryAST { files: HashMap::new() };
- load_ast(&args[1], "/lib.rs", "".to_string(), &mut libast);
+ let mut lib_src = String::new();
+ std::io::stdin().lock().read_to_string(&mut lib_src).unwrap();
+ let lib_syntax = syn::parse_file(&lib_src).expect("Unable to parse file");
+ let libast = FullLibraryAST::load_lib(lib_syntax);
// ...then walk the ASTs tracking what types we will map, and how, so that we can resolve them
// when parsing other file ASTs...
let mut libtypes = CrateTypes { traits: HashMap::new(), opaques: HashMap::new(), mirrored_enums: HashMap::new(),
- type_aliases: HashMap::new(), templates_defined: HashMap::default(), template_file: &mut derived_templates };
- walk_ast(&args[1], "/lib.rs", "".to_string(), &libast, &mut libtypes);
+ type_aliases: HashMap::new(), reverse_alias_map: HashMap::new(), templates_defined: HashMap::default(),
+ template_file: &mut derived_templates,
+ clonable_types: HashSet::new(), trait_impls: HashMap::new() };
+ walk_ast(&libast, &mut libtypes);
// ... finally, do the actual file conversion/mapping, writing out types as we go.
- convert_file(&libast, &mut libtypes, &args[1], &args[2], "/lib.rs", &args[3], "", &mut header_file, &mut cpp_header_file);
+ convert_file(&libast, &mut libtypes, &args[1], &args[2], &mut header_file, &mut cpp_header_file);
// For container templates which we created while walking the crate, make sure we add C++
// mapped types so that C++ users can utilize the auto-destructors available.