[bindings] Ensure owned objects in Vec conversion are marked `mut`
[rust-lightning] / c-bindings-gen / src / main.rs
index 07948ed42fa4b737b8dc9584a81e0925783772d7..e919dc758283d8f7bacd78ba678b398e4d1f903c 100644 (file)
@@ -10,7 +10,7 @@
 //! It also generates relevant memory-management functions and free-standing functions with
 //! parameters mapped.
 
-use std::collections::{HashMap, HashSet};
+use std::collections::{HashMap, hash_map, HashSet};
 use std::env;
 use std::fs::File;
 use std::io::{Read, Write};
@@ -903,7 +903,8 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
                                                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"]) && types.c_type_has_inner_from_path(&resolved_path) {
+                                       } 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();
@@ -975,8 +976,62 @@ fn writeln_impl<W: std::io::Write>(w: &mut W, i: &syn::ItemImpl, types: &mut Typ
                                                }
                                        }
                                }
+                       } 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);
                        }
                }
        }
@@ -1165,9 +1220,12 @@ impl FullLibraryAST {
                                                        };
                                                        self.load_module(modname, m.attrs, m.content.unwrap().1);
                                                        submods.push(modident);
+                                               } else {
+                                                       non_mod_items.push(syn::Item::Mod(m));
                                                }
                                        }
                                },
+                               syn::Item::Mod(_) => panic!("--pretty=expanded output should never have non-body modules"),
                                _ => { non_mod_items.push(item); }
                        }
                }
@@ -1306,50 +1364,28 @@ fn convert_file<'a, 'b>(libast: &'a FullLibraryAST, crate_types: &mut CrateTypes
        }
 }
 
-/// 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);
-                       }
-               },
-               _ => {}
-       }
-}
-
-/// 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();
+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::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]); },
+                                                               }
+                                                       }
+                                               }
+                                       }
                                }
-                       } 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!(),
+                       },
+                       _ => {},
+               }
        }
-       ty
 }
 
 /// Walk the FullLibraryAST, deciding how things will be mapped and adding tracking to CrateTypes.
@@ -1357,13 +1393,10 @@ 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 mut import_maps = HashMap::new();
+               let import_resolver = ImportResolver::new(module, items);
 
                for item in items.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) {
@@ -1404,12 +1437,24 @@ fn walk_ast<'a>(ast_storage: &'a FullLibraryAST, crate_types: &mut CrateTypes<'a
                                                }
                                                if process_alias {
                                                        match &*t.ty {
-                                                               syn::Type::Path(_) => {
+                                                               syn::Type::Path(p) => {
                                                                        // 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);
+                                                                       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, resolve_imported_refs(&import_maps, (*t.ty).clone()));
+                                                                       crate_types.type_aliases.insert(type_path, import_resolver.resolve_imported_refs((*t.ty).clone()));
                                                                }
                                                        }
                                                }
@@ -1439,15 +1484,22 @@ fn walk_ast<'a>(ast_storage: &'a FullLibraryAST, crate_types: &mut CrateTypes<'a
                                        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"]) {
-                                                               // Note, we should probably have more full-featured type resolution here,
-                                                               // but for now we don't have any cases where we impl X for A::B.
-                                                               let ident = single_ident_generic_path_to_ident(&p.path).expect("impl X for A::B currently unsupported");
-                                                               assert!(import_maps.get(&ident).is_none());
-                                                               crate_types.clonable_types.insert(format!("crate::{}::{}", module, ident));
+                                                               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),
                                _ => {},
                        }
                }
@@ -1492,8 +1544,9 @@ fn main() {
        // ...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,
-               clonable_types: HashSet::new() };
+               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.