Gen bindings with rustc --pretty=expanded instead of reading files
[rust-lightning] / c-bindings-gen / src / types.rs
index 0cf809c360571ff34e9f26b532877a5b6546c1f5..7f979a5675235b1b54d83e9634dc9f86199ffccc 100644 (file)
@@ -41,28 +41,13 @@ pub fn single_ident_generic_path_to_ident(p: &syn::Path) -> Option<&syn::Ident>
        } else { None }
 }
 
-pub fn attrs_derives_clone(attrs: &[syn::Attribute]) -> bool {
-       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" {
-                                                                       return true;
-                                                               }
-                                                       }
-                                               }
-                                       }
-                               },
-                               _ => {},
-                       }
-               }
+pub fn path_matches_nongeneric(p: &syn::Path, exp: &[&str]) -> bool {
+       if p.segments.len() != exp.len() { return false; }
+       for (seg, e) in p.segments.iter().zip(exp.iter()) {
+               if seg.arguments != syn::PathArguments::None { return false; }
+               if &format!("{}", seg.ident) != *e { return false; }
        }
-       false
+       true
 }
 
 #[derive(Debug, PartialEq)]
@@ -165,6 +150,7 @@ impl<'a> GenericTypes<'a> {
                                                        if let Some(ident) = single_ident_generic_path_to_ident(&trait_bound.path) {
                                                                match &format!("{}", ident) as &str { "Send" => continue, "Sync" => continue, _ => {} }
                                                        }
+                                                       if path_matches_nongeneric(&trait_bound.path, &["core", "clone", "Clone"]) { continue; }
 
                                                        assert_simple_bound(&trait_bound);
                                                        if let Some(mut path) = types.maybe_resolve_path(&trait_bound.path, None) {
@@ -298,7 +284,7 @@ pub enum DeclType<'a> {
 // the sorting is stable across runs. It is deprecated, but the "replacement" doesn't actually
 // accomplish the same goals, so we just ignore it.
 #[allow(deprecated)]
-type NonRandomHash = hash::BuildHasherDefault<hash::SipHasher>;
+pub type NonRandomHash = hash::BuildHasherDefault<hash::SipHasher>;
 
 /// Top-level struct tracking everything which has been defined while walking the crate.
 pub struct CrateTypes<'a> {
@@ -466,10 +452,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "ln::features::InitFeatures" if is_ref && ptr_for_ref => Some("crate::ln::features::InitFeatures"),
                        "ln::features::InitFeatures" if is_ref => Some("*const crate::ln::features::InitFeatures"),
                        "ln::features::InitFeatures" => Some("crate::ln::features::InitFeatures"),
-                       _ => {
-                               eprintln!("    Type {} (ref: {}) unresolvable in C", full_path, is_ref);
-                               None
-                       },
+                       _ => None,
                }
        }
 
@@ -534,10 +517,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        // List of traits we map (possibly during processing of other files):
                        "crate::util::logger::Logger" => Some(""),
 
-                       _ => {
-                               eprintln!("    Type {} unconvertable from C", full_path);
-                               None
-                       },
+                       _ => None,
                }.map(|s| s.to_owned())
        }
        fn from_c_conversion_suffix_from_path<'b>(&self, full_path: &str, is_ref: bool) -> Option<String> {
@@ -593,10 +573,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        // List of traits we map (possibly during processing of other files):
                        "crate::util::logger::Logger" => Some(""),
 
-                       _ => {
-                               eprintln!("    Type {} unconvertable from C", full_path);
-                               None
-                       },
+                       _ => None,
                }.map(|s| s.to_owned())
        }
 
@@ -678,10 +655,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "ln::features::InitFeatures" if is_ref => Some("Box::into_raw(Box::new(crate::ln::features::InitFeatures { inner: &mut "),
                        "ln::features::InitFeatures" if !is_ref => Some("crate::ln::features::InitFeatures { inner: Box::into_raw(Box::new("),
 
-                       _ => {
-                               eprintln!("    Type {} (is_ref: {}) unconvertable to C", full_path, is_ref);
-                               None
-                       },
+                       _ => None,
                }.map(|s| s.to_owned())
        }
        fn to_c_conversion_inline_suffix_from_path(&self, full_path: &str, is_ref: bool, ptr_for_ref: bool) -> Option<String> {
@@ -743,10 +717,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        "ln::features::InitFeatures" if is_ref => Some(", is_owned: false }))"),
                        "ln::features::InitFeatures" => Some(")), is_owned: true }"),
 
-                       _ => {
-                               eprintln!("    Type {} unconvertable to C", full_path);
-                               None
-                       },
+                       _ => None,
                }.map(|s| s.to_owned())
        }
 
@@ -915,6 +886,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        eprintln!("Ignoring pub(use) tree!");
                        return;
                }
+               if u.leading_colon.is_some() { eprintln!("Ignoring leading-colon use!"); return; }
                match &u.tree {
                        syn::UseTree::Path(p) => {
                                let new_path = format!("{}", p.ident);
@@ -926,18 +898,15 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                        },
                        _ => unimplemented!(),
                }
-               if u.leading_colon.is_some() { unimplemented!() }
        }
 
        pub fn mirrored_enum_declared(&mut self, ident: &syn::Ident) {
-               eprintln!("{} mirrored", ident);
                self.declared.insert(ident.clone(), DeclType::MirroredEnum);
        }
        pub fn enum_ignored(&mut self, ident: &'c syn::Ident) {
                self.declared.insert(ident.clone(), DeclType::EnumIgnored);
        }
-       pub fn struct_imported(&mut self, ident: &'c syn::Ident, named: String) {
-               eprintln!("Imported {} as {}", ident, named);
+       pub fn struct_imported(&mut self, ident: &'c syn::Ident) {
                self.declared.insert(ident.clone(), DeclType::StructImported);
        }
        pub fn struct_ignored(&mut self, ident: &syn::Ident) {
@@ -945,7 +914,6 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                self.declared.insert(ident.clone(), DeclType::StructIgnored);
        }
        pub fn trait_declared(&mut self, ident: &syn::Ident, t: &'c syn::ItemTrait) {
-               eprintln!("Trait {} created", ident);
                self.declared.insert(ident.clone(), DeclType::Trait(t));
        }
        pub fn get_declared_type(&'a self, ident: &syn::Ident) -> Option<&'a DeclType<'c>> {
@@ -1896,7 +1864,7 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> {
                                                                        generics, &subtype, is_ref, is_mut, ptr_for_ref, true);
                                                        }
                                                } else {
-                                                       let id = &&$p_arg.path.segments.iter().rev().next().unwrap().ident;
+                                                       let id = subtype.rsplitn(2, ':').next().unwrap(); // Get the "Base" name of the resolved type
                                                        write!(w, "{}", id).unwrap();
                                                        write!(mangled_type, "{}", id).unwrap();
                                                        if let Some(w2) = $extra_write as Option<&mut Vec<u8>> {