From: Matt Corallo Date: Tue, 19 Apr 2022 19:39:29 +0000 (+0000) Subject: Handle impl blocks exist outside of the mod of the being-implemented object X-Git-Tag: v0.0.108.0~4^2~2 X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=cf73ef67e997143b5182153aac534d1e471b021a;p=ldk-c-bindings Handle impl blocks exist outside of the mod of the being-implemented object This allows us to implement the feature bit accessor functions. --- diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index a86605a..3814d57 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -1375,11 +1375,18 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ if !gen_types.as_mut().unwrap().learn_generics(&i.generics, types) { gen_types = None; } - 'alias_impls: for (alias, arguments) in aliases { + let alias_module = rsplit_once(&resolved_path, "::").unwrap().0; + + 'alias_impls: for (alias_resolved, arguments) in aliases { let mut new_ty_generics = Vec::new(); let mut need_generics = false; - let alias_resolved = types.resolve_path(&alias, None); + let alias_resolver_override; + let alias_resolver = if alias_module != types.module_path { + alias_resolver_override = ImportResolver::new(types.types.crate_name, &types.crate_types.lib_ast.dependencies, + alias_module, &types.crate_types.lib_ast.modules.get(alias_module).unwrap().items); + &alias_resolver_override + } else { &types.types };/*.maybe_resolve_path(&alias, None).unwrap();*/ for (idx, gen) in i.generics.params.iter().enumerate() { match gen { syn::GenericParam::Type(type_param) => { @@ -1388,10 +1395,11 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ 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] { - if let Some(generic_arg) = types.maybe_resolve_path(&p.path, None) { + if let Some(generic_arg) = alias_resolver.maybe_resolve_path(&p.path, None) { new_ty_generics.push((type_param.ident.clone(), syn::Type::Path(p.clone()))); - let generic_bound = types.resolve_path(&trait_bound.path, None); + let generic_bound = types.maybe_resolve_path(&trait_bound.path, None) + .unwrap_or_else(|| format!("{}::{}", types.module_path, single_ident_generic_path_to_ident(&trait_bound.path).unwrap())); 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; } @@ -1419,6 +1427,7 @@ fn writeln_impl(w: &mut W, i: &syn::ItemImpl, types: &mut Typ } } let mut params = syn::punctuated::Punctuated::new(); + let alias = string_path_to_syn_path(&alias_resolved); let real_aliased = if need_generics { let alias_generics = types.crate_types.opaques.get(&alias_resolved).unwrap().1; @@ -1784,11 +1793,7 @@ fn convert_priv_mod<'a, 'b: 'a, W: std::io::Write>(w: &mut W, libast: &'b FullLi match item { syn::Item::Mod(m) => convert_priv_mod(w, libast, crate_types, out_dir, &format!("{}::{}", mod_path, module.ident), m), syn::Item::Impl(i) => { - if let &syn::Type::Path(ref p) = &*i.self_ty { - if p.path.get_ident().is_some() { - writeln_impl(w, i, &mut types); - } - } + writeln_impl(w, i, &mut types); }, _ => {}, } @@ -2030,17 +2035,14 @@ fn walk_ast<'a>(ast_storage: &'a FullLibraryAST, crate_types: &mut CrateTypes<'a let type_path = format!("{}::{}", module, t.ident); match &*t.ty { syn::Type::Path(p) => { - let t_ident = &t.ident; - // If its a path with no generics, assume we don't map the aliased type and map it opaque - let path_obj = parse_quote!(#t_ident); 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)]); }, + hash_map::Entry::Occupied(mut e) => { e.get_mut().push((type_path.clone(), args_obj)); }, + hash_map::Entry::Vacant(e) => { e.insert(vec![(type_path.clone(), args_obj)]); }, } - crate_types.opaques.insert(type_path, (t_ident, &t.generics)); + crate_types.opaques.insert(type_path, (&t.ident, &t.generics)); }, _ => { crate_types.type_aliases.insert(type_path, import_resolver.resolve_imported_refs((*t.ty).clone())); diff --git a/c-bindings-gen/src/types.rs b/c-bindings-gen/src/types.rs index da2d180..f9c894a 100644 --- a/c-bindings-gen/src/types.rs +++ b/c-bindings-gen/src/types.rs @@ -65,6 +65,17 @@ pub fn path_matches_nongeneric(p: &syn::Path, exp: &[&str]) -> bool { true } +pub fn string_path_to_syn_path(path: &str) -> syn::Path { + let mut segments = syn::punctuated::Punctuated::new(); + for seg in path.split("::") { + segments.push(syn::PathSegment { + ident: syn::Ident::new(seg, Span::call_site()), + arguments: syn::PathArguments::None, + }); + } + syn::Path { leading_colon: Some(syn::Token![::](Span::call_site())), segments } +} + #[derive(Debug, PartialEq)] pub enum ExportStatus { Export, @@ -391,7 +402,7 @@ pub enum DeclType<'a> { } pub struct ImportResolver<'mod_lifetime, 'crate_lft: 'mod_lifetime> { - crate_name: &'mod_lifetime str, + pub crate_name: &'mod_lifetime str, dependencies: &'mod_lifetime HashSet, module_path: &'mod_lifetime str, imports: HashMap, @@ -741,7 +752,7 @@ pub struct CrateTypes<'a> { /// Aliases from paths to some other Type pub type_aliases: HashMap, /// Value is an alias to Key (maybe with some generics) - pub reverse_alias_map: HashMap>, + pub reverse_alias_map: HashMap>, /// Template continer types defined, map from mangled type name -> whether a destructor fn /// exists. /// @@ -785,7 +796,7 @@ impl<'a> CrateTypes<'a> { pub struct TypeResolver<'mod_lifetime, 'crate_lft: 'mod_lifetime> { pub module_path: &'mod_lifetime str, pub crate_types: &'mod_lifetime CrateTypes<'crate_lft>, - types: ImportResolver<'mod_lifetime, 'crate_lft>, + pub types: ImportResolver<'mod_lifetime, 'crate_lft>, } /// Returned by write_empty_rust_val_check_suffix to indicate what type of dereferencing needs to @@ -2874,7 +2885,6 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { assert!(self.write_c_type_intern(w, t, generics, false, false, ptr_for_ref, true, false)); } pub fn understood_c_path(&self, p: &syn::Path) -> bool { - if p.leading_colon.is_some() { return false; } self.write_c_path_intern(&mut std::io::sink(), p, None, false, false, false, false, true) } pub fn understood_c_type(&self, t: &syn::Type, generics: Option<&GenericTypes>) -> bool {