diff options
10 files changed, 50 insertions, 28 deletions
diff --git a/libbindgen/src/codegen/mod.rs b/libbindgen/src/codegen/mod.rs index ad9ac2de..7fa76d56 100644 --- a/libbindgen/src/codegen/mod.rs +++ b/libbindgen/src/codegen/mod.rs @@ -9,7 +9,7 @@ use ir::context::{BindgenContext, ItemId}; use ir::enum_ty::{Enum, EnumVariant, EnumVariantValue}; use ir::function::{Function, FunctionSig}; use ir::int::IntKind; -use ir::item::{Item, ItemCanonicalName, ItemCanonicalPath}; +use ir::item::{Item, ItemAncestors, ItemCanonicalName, ItemCanonicalPath}; use ir::item_kind::ItemKind; use ir::layout::Layout; use ir::module::Module; @@ -22,6 +22,7 @@ use std::borrow::Cow; use std::collections::HashSet; use std::collections::hash_map::{Entry, HashMap}; use std::fmt::Write; +use std::iter; use std::mem; use std::ops; use syntax::abi::Abi; @@ -29,12 +30,33 @@ use syntax::ast; use syntax::codemap::{Span, respan}; use syntax::ptr::P; -fn root_import(ctx: &BindgenContext) -> P<ast::Item> { +fn root_import(ctx: &BindgenContext, module: &Item) -> P<ast::Item> { assert!(ctx.options().enable_cxx_namespaces, "Somebody messed it up"); + assert!(module.is_module()); + let root = ctx.root_module().canonical_name(ctx); let root_ident = ctx.rust_ident(&root); - quote_item!(ctx.ext_cx(), #[allow(unused_imports)] use $root_ident;) - .unwrap() + + let super_ = aster::AstBuilder::new().id("super"); + let supers = module + .ancestors(ctx) + .filter(|id| ctx.resolve_item(*id).is_module()) + .map(|_| super_.clone()) + .chain(iter::once(super_)); + + let self_ = iter::once(aster::AstBuilder::new().id("self")); + let root_ident = iter::once(root_ident); + + let path = self_.chain(supers).chain(root_ident); + + let use_root = aster::AstBuilder::new() + .item() + .use_() + .ids(path) + .build() + .build(); + + quote_item!(ctx.ext_cx(), #[allow(unused_imports)] $use_root).unwrap() } struct CodegenResult { @@ -286,7 +308,7 @@ impl CodeGenerator for Module { let mut found_any = false; let inner_items = result.inner(|result| { - result.push(root_import(ctx)); + result.push(root_import(ctx, item)); codegen_self(result, &mut found_any); }); diff --git a/libbindgen/tests/expectations/tests/duplicated-namespaces-definitions.rs b/libbindgen/tests/expectations/tests/duplicated-namespaces-definitions.rs index 314a24a3..da06a2a9 100644 --- a/libbindgen/tests/expectations/tests/duplicated-namespaces-definitions.rs +++ b/libbindgen/tests/expectations/tests/duplicated-namespaces-definitions.rs @@ -6,10 +6,10 @@ pub mod root { #[allow(unused_imports)] - use root; + use self::super::root; pub mod foo { #[allow(unused_imports)] - use root; + use self::super::super::root; #[repr(C)] #[derive(Debug, Copy)] pub struct Bar { @@ -27,7 +27,7 @@ pub mod root { } pub mod bar { #[allow(unused_imports)] - use root; + use self::super::super::root; #[repr(C)] #[derive(Debug, Copy)] pub struct Foo { diff --git a/libbindgen/tests/expectations/tests/duplicated-namespaces.rs b/libbindgen/tests/expectations/tests/duplicated-namespaces.rs index 7a283160..42976657 100644 --- a/libbindgen/tests/expectations/tests/duplicated-namespaces.rs +++ b/libbindgen/tests/expectations/tests/duplicated-namespaces.rs @@ -6,5 +6,5 @@ pub mod root { #[allow(unused_imports)] - use root; + use self::super::root; } diff --git a/libbindgen/tests/expectations/tests/duplicated_constants_in_ns.rs b/libbindgen/tests/expectations/tests/duplicated_constants_in_ns.rs index 3acc53e8..3721740c 100644 --- a/libbindgen/tests/expectations/tests/duplicated_constants_in_ns.rs +++ b/libbindgen/tests/expectations/tests/duplicated_constants_in_ns.rs @@ -6,15 +6,15 @@ pub mod root { #[allow(unused_imports)] - use root; + use self::super::root; pub mod foo { #[allow(unused_imports)] - use root; + use self::super::super::root; pub const FOO: ::std::os::raw::c_int = 4; } pub mod bar { #[allow(unused_imports)] - use root; + use self::super::super::root; pub const FOO: ::std::os::raw::c_int = 5; } } diff --git a/libbindgen/tests/expectations/tests/module-whitelisted.rs b/libbindgen/tests/expectations/tests/module-whitelisted.rs index 7e63c8c8..6f88c54b 100644 --- a/libbindgen/tests/expectations/tests/module-whitelisted.rs +++ b/libbindgen/tests/expectations/tests/module-whitelisted.rs @@ -6,7 +6,7 @@ pub mod root { #[allow(unused_imports)] - use root; + use self::super::root; #[repr(C)] #[derive(Debug, Copy)] pub struct Test { diff --git a/libbindgen/tests/expectations/tests/namespace.rs b/libbindgen/tests/expectations/tests/namespace.rs index 34c2da1e..3d6e5974 100644 --- a/libbindgen/tests/expectations/tests/namespace.rs +++ b/libbindgen/tests/expectations/tests/namespace.rs @@ -6,14 +6,14 @@ pub mod root { #[allow(unused_imports)] - use root; + use self::super::root; extern "C" { #[link_name = "_Z9top_levelv"] pub fn top_level(); } pub mod whatever { #[allow(unused_imports)] - use root; + use self::super::super::root; pub type whatever_int_t = ::std::os::raw::c_int; extern "C" { #[link_name = "_ZN8whatever11in_whateverEv"] @@ -22,7 +22,7 @@ pub mod root { } pub mod _bindgen_mod_id_13 { #[allow(unused_imports)] - use root; + use self::super::super::root; extern "C" { #[link_name = "_ZN12_GLOBAL__N_13fooEv"] pub fn foo(); @@ -51,7 +51,7 @@ pub mod root { } pub mod w { #[allow(unused_imports)] - use root; + use self::super::super::root; pub type whatever_int_t = ::std::os::raw::c_uint; #[repr(C)] #[derive(Debug)] diff --git a/libbindgen/tests/expectations/tests/nested_within_namespace.rs b/libbindgen/tests/expectations/tests/nested_within_namespace.rs index 949b5adb..0c9c31ef 100644 --- a/libbindgen/tests/expectations/tests/nested_within_namespace.rs +++ b/libbindgen/tests/expectations/tests/nested_within_namespace.rs @@ -6,10 +6,10 @@ pub mod root { #[allow(unused_imports)] - use root; + use self::super::root; pub mod foo { #[allow(unused_imports)] - use root; + use self::super::super::root; #[repr(C)] #[derive(Debug, Copy)] pub struct Bar { diff --git a/libbindgen/tests/expectations/tests/template_alias_namespace.rs b/libbindgen/tests/expectations/tests/template_alias_namespace.rs index 36806d44..cf4a079c 100644 --- a/libbindgen/tests/expectations/tests/template_alias_namespace.rs +++ b/libbindgen/tests/expectations/tests/template_alias_namespace.rs @@ -6,13 +6,13 @@ pub mod root { #[allow(unused_imports)] - use root; + use self::super::root; pub mod JS { #[allow(unused_imports)] - use root; + use self::super::super::root; pub mod detail { #[allow(unused_imports)] - use root; + use self::super::super::super::root; pub type Wrapped<T> = T; } #[repr(C)] diff --git a/libbindgen/tests/expectations/tests/whitelist-namespaces-basic.rs b/libbindgen/tests/expectations/tests/whitelist-namespaces-basic.rs index 8806cbf4..cbb12f6b 100644 --- a/libbindgen/tests/expectations/tests/whitelist-namespaces-basic.rs +++ b/libbindgen/tests/expectations/tests/whitelist-namespaces-basic.rs @@ -6,13 +6,13 @@ pub mod root { #[allow(unused_imports)] - use root; + use self::super::root; pub mod outer { #[allow(unused_imports)] - use root; + use self::super::super::root; pub mod inner { #[allow(unused_imports)] - use root; + use self::super::super::super::root; #[repr(C)] #[derive(Debug, Copy)] pub struct Helper { diff --git a/libbindgen/tests/expectations/tests/whitelist-namespaces.rs b/libbindgen/tests/expectations/tests/whitelist-namespaces.rs index 42cccdee..bc257af6 100644 --- a/libbindgen/tests/expectations/tests/whitelist-namespaces.rs +++ b/libbindgen/tests/expectations/tests/whitelist-namespaces.rs @@ -6,13 +6,13 @@ pub mod root { #[allow(unused_imports)] - use root; + use self::super::root; pub mod outer { #[allow(unused_imports)] - use root; + use self::super::super::root; pub mod inner { #[allow(unused_imports)] - use root; + use self::super::super::super::root; #[repr(C)] #[derive(Debug, Copy)] pub struct Helper { |