From 2b101baf6ca4204a989b55fc4fa4aee55c82bdff Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Mon, 8 Feb 2021 22:19:48 -0500 Subject: [PATCH] [bindings] Use new ImportResolver during first AST pass This removes some redundant logic and ensures we handle more import-resolution cases in both contexts. --- c-bindings-gen/src/main.rs | 61 ++--------------------- c-bindings-gen/src/types.rs | 96 +++++++++++++++++++++++-------------- 2 files changed, 66 insertions(+), 91 deletions(-) diff --git a/c-bindings-gen/src/main.rs b/c-bindings-gen/src/main.rs index 07948ed42..198e0cfdd 100644 --- a/c-bindings-gen/src/main.rs +++ b/c-bindings-gen/src/main.rs @@ -1306,64 +1306,15 @@ 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, 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(); - } - } 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. 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) { @@ -1409,7 +1360,7 @@ fn walk_ast<'a>(ast_storage: &'a FullLibraryAST, crate_types: &mut CrateTypes<'a crate_types.opaques.insert(type_path, &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,11 +1390,9 @@ 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); + } } } } diff --git a/c-bindings-gen/src/types.rs b/c-bindings-gen/src/types.rs index 46973e0b1..cba9407ae 100644 --- a/c-bindings-gen/src/types.rs +++ b/c-bindings-gen/src/types.rs @@ -300,28 +300,31 @@ pub enum DeclType<'a> { pub struct ImportResolver<'mod_lifetime, 'crate_lft: 'mod_lifetime> { module_path: &'mod_lifetime str, - imports: HashMap, + imports: HashMap, declared: HashMap>, } impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'crate_lft> { - fn process_use_intern(imports: &mut HashMap, u: &syn::UseTree, partial_path: &str) { + fn process_use_intern(imports: &mut HashMap, u: &syn::UseTree, partial_path: &str, mut path: syn::punctuated::Punctuated) { match u { syn::UseTree::Path(p) => { - let new_path = format!("{}::{}", partial_path, p.ident); - Self::process_use_intern(imports, &p.tree, &new_path); + let new_path = format!("{}{}::", partial_path, p.ident); + path.push(syn::PathSegment { ident: p.ident.clone(), arguments: syn::PathArguments::None }); + Self::process_use_intern(imports, &p.tree, &new_path, path); }, syn::UseTree::Name(n) => { - let full_path = format!("{}::{}", partial_path, n.ident); - imports.insert(n.ident.clone(), full_path); + let full_path = format!("{}{}", partial_path, n.ident); + path.push(syn::PathSegment { ident: n.ident.clone(), arguments: syn::PathArguments::None }); + imports.insert(n.ident.clone(), (full_path, syn::Path { leading_colon: Some(syn::Token![::](Span::call_site())), segments: path })); }, syn::UseTree::Group(g) => { for i in g.items.iter() { - Self::process_use_intern(imports, i, partial_path); + Self::process_use_intern(imports, i, partial_path, path.clone()); } }, syn::UseTree::Rename(r) => { - let full_path = format!("{}::{}", partial_path, r.ident); - imports.insert(r.rename.clone(), full_path); + let full_path = format!("{}{}", partial_path, r.ident); + path.push(syn::PathSegment { ident: r.ident.clone(), arguments: syn::PathArguments::None }); + imports.insert(r.rename.clone(), (full_path, syn::Path { leading_colon: Some(syn::Token![::](Span::call_site())), segments: path })); }, syn::UseTree::Glob(_) => { eprintln!("Ignoring * use for {} - this may result in resolution failures", partial_path); @@ -329,43 +332,40 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr } } - pub fn process_use(imports: &mut HashMap, u: &syn::ItemUse) { + fn process_use(imports: &mut HashMap, u: &syn::ItemUse) { if let syn::Visibility::Public(_) = u.vis { // We actually only use these for #[cfg(fuzztarget)] 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); - Self::process_use_intern(imports, &p.tree, &new_path); - }, - syn::UseTree::Name(n) => { - let full_path = format!("{}", n.ident); - imports.insert(n.ident.clone(), full_path); - }, - _ => unimplemented!(), - } + Self::process_use_intern(imports, &u.tree, "", syn::punctuated::Punctuated::new()); + } + + fn insert_primitive(imports: &mut HashMap, id: &str) { + let ident = syn::Ident::new(id, Span::call_site()); + let mut path = syn::punctuated::Punctuated::new(); + path.push(syn::PathSegment { ident: ident.clone(), arguments: syn::PathArguments::None }); + imports.insert(ident, (id.to_owned(), syn::Path { leading_colon: Some(syn::Token![::](Span::call_site())), segments: path })); } pub fn new(module_path: &'mod_lifetime str, contents: &'crate_lft [syn::Item]) -> Self { let mut imports = HashMap::new(); // Add primitives to the "imports" list: - imports.insert(syn::Ident::new("bool", Span::call_site()), "bool".to_string()); - imports.insert(syn::Ident::new("u64", Span::call_site()), "u64".to_string()); - imports.insert(syn::Ident::new("u32", Span::call_site()), "u32".to_string()); - imports.insert(syn::Ident::new("u16", Span::call_site()), "u16".to_string()); - imports.insert(syn::Ident::new("u8", Span::call_site()), "u8".to_string()); - imports.insert(syn::Ident::new("usize", Span::call_site()), "usize".to_string()); - imports.insert(syn::Ident::new("str", Span::call_site()), "str".to_string()); - imports.insert(syn::Ident::new("String", Span::call_site()), "String".to_string()); + Self::insert_primitive(&mut imports, "bool"); + Self::insert_primitive(&mut imports, "u64"); + Self::insert_primitive(&mut imports, "u32"); + Self::insert_primitive(&mut imports, "u16"); + Self::insert_primitive(&mut imports, "u8"); + Self::insert_primitive(&mut imports, "usize"); + Self::insert_primitive(&mut imports, "str"); + Self::insert_primitive(&mut imports, "String"); // These are here to allow us to print native Rust types in trait fn impls even if we don't // have C mappings: - imports.insert(syn::Ident::new("Result", Span::call_site()), "Result".to_string()); - imports.insert(syn::Ident::new("Vec", Span::call_site()), "Vec".to_string()); - imports.insert(syn::Ident::new("Option", Span::call_site()), "Option".to_string()); + Self::insert_primitive(&mut imports, "Result"); + Self::insert_primitive(&mut imports, "Vec"); + Self::insert_primitive(&mut imports, "Option"); let mut declared = HashMap::new(); @@ -411,7 +411,7 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr } pub fn maybe_resolve_ident(&self, id: &syn::Ident) -> Option { - if let Some(imp) = self.imports.get(id) { + if let Some((imp, _)) = self.imports.get(id) { Some(imp.clone()) } else if self.declared.get(id).is_some() { Some(self.module_path.to_string() + "::" + &format!("{}", id)) @@ -419,7 +419,7 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr } pub fn maybe_resolve_non_ignored_ident(&self, id: &syn::Ident) -> Option { - if let Some(imp) = self.imports.get(id) { + if let Some((imp, _)) = self.imports.get(id) { Some(imp.clone()) } else if let Some(decl_type) = self.declared.get(id) { match decl_type { @@ -452,7 +452,7 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr let remaining: String = seg_iter.map(|seg| { format!("::{}", seg.ident) }).collect(); - if let Some(imp) = self.imports.get(&first_seg.ident) { + if let Some((imp, _)) = self.imports.get(&first_seg.ident) { if remaining != "" { Some(imp.clone() + &remaining) } else { @@ -461,6 +461,32 @@ impl<'mod_lifetime, 'crate_lft: 'mod_lifetime> ImportResolver<'mod_lifetime, 'cr } else { None } } } + + /// Map all the Paths in a Type into absolute paths given a set of imports (generated via process_use_intern) + pub fn resolve_imported_refs(&self, 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)) = self.imports.get(ident) { + p.path = newpath.clone(); + } + } else { unimplemented!(); } + }, + syn::Type::Reference(r) => { + r.elem = Box::new(self.resolve_imported_refs((*r.elem).clone())); + }, + syn::Type::Slice(s) => { + s.elem = Box::new(self.resolve_imported_refs((*s.elem).clone())); + }, + syn::Type::Tuple(t) => { + for e in t.elems.iter_mut() { + *e = self.resolve_imported_refs(e.clone()); + } + }, + _ => unimplemented!(), + } + ty + } } // templates_defined is walked to write the C++ header, so if we use the default hashing it get -- 2.39.5