diff --git a/.gitignore b/.gitignore index 12b6e6a6dc..7c2aef6614 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ # Cargo target/ *~ +./bindgen-integration/Cargo.lock #*# diff --git a/bindgen-integration/build.rs b/bindgen-integration/build.rs index 2c18c94a44..9b157c5aab 100644 --- a/bindgen-integration/build.rs +++ b/bindgen-integration/build.rs @@ -13,6 +13,8 @@ fn main() { let bindings = Builder::default() .no_unstable_rust() + .enable_cxx_namespaces() + .raw_line("pub use self::root::*;") .header("cpp/Test.h") .clang_arg("-x") .clang_arg("c++") diff --git a/bindgen-integration/cpp/Test.h b/bindgen-integration/cpp/Test.h index ebd58649e8..f1e38c40f6 100644 --- a/bindgen-integration/cpp/Test.h +++ b/bindgen-integration/cpp/Test.h @@ -9,4 +9,10 @@ class Test { Test(double foo); }; +namespace testing { + typedef Test TypeAlias; + +} // namespace testing + +typedef testing::TypeAlias TypeAlias; diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index 7451dd1120..fa34d341c7 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -23,7 +23,6 @@ use std::cell::Cell; use std::collections::{HashSet, VecDeque}; use std::collections::hash_map::{Entry, HashMap}; use std::fmt::Write; -use std::iter; use std::mem; use std::ops; use syntax::abi::Abi; @@ -31,23 +30,39 @@ use syntax::ast; use syntax::codemap::{Span, respan}; use syntax::ptr::P; +fn root_import_depth(ctx: &BindgenContext, item: &Item) -> usize { + if !ctx.options().enable_cxx_namespaces { + return 0; + } + + item.ancestors(ctx) + .filter(|id| ctx.resolve_item(*id).is_module()) + .fold(1, |i, _| i + 1) +} + +fn top_level_path(ctx: &BindgenContext, item: &Item) -> Vec { + let mut path = vec![ctx.rust_ident_raw("self")]; + + if ctx.options().enable_cxx_namespaces { + let super_ = ctx.rust_ident_raw("super"); + + for _ in 0..root_import_depth(ctx, item) { + path.push(super_.clone()); + } + } + + path +} + fn root_import(ctx: &BindgenContext, module: &Item) -> P { assert!(ctx.options().enable_cxx_namespaces, "Somebody messed it up"); assert!(module.is_module()); + let mut path = top_level_path(ctx, module); + let root = ctx.root_module().canonical_name(ctx); let root_ident = ctx.rust_ident(&root); - - 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); + path.push(root_ident); let use_root = aster::AstBuilder::new() .item() @@ -528,7 +543,7 @@ impl CodeGenerator for Type { let simple_enum_path = match inner_rust_type.node { ast::TyKind::Path(None, ref p) => { if applicable_template_args.is_empty() && - !inner_item.expect_type().canonical_type(ctx).is_builtin_or_named() && + inner_item.expect_type().canonical_type(ctx).is_enum() && p.segments.iter().all(|p| p.parameters.is_none()) { Some(p.clone()) } else { @@ -539,9 +554,9 @@ impl CodeGenerator for Type { }; let typedef = if let Some(mut p) = simple_enum_path { - if p.segments.len() == 1 { + for ident in top_level_path(ctx, item).into_iter().rev() { p.segments.insert(0, ast::PathSegment { - identifier: ctx.ext_cx().ident_of("self"), + identifier: ident, parameters: None, }); } diff --git a/src/ir/function.rs b/src/ir/function.rs index 50c442db10..6e205f1b0f 100644 --- a/src/ir/function.rs +++ b/src/ir/function.rs @@ -165,8 +165,7 @@ impl FunctionSig { let name = arg.spelling(); let name = if name.is_empty() { None } else { Some(name) }; - let ty = Item::from_ty(&arg_ty, Some(*arg), None, ctx) - .expect("Argument?"); + let ty = Item::from_ty_or_ref(arg_ty, Some(*arg), None, ctx); (name, ty) }) .collect() @@ -178,8 +177,7 @@ impl FunctionSig { cursor.visit(|c| { if c.kind() == CXCursor_ParmDecl { let ty = - Item::from_ty(&c.cur_type(), Some(c), None, ctx) - .expect("ParmDecl?"); + Item::from_ty_or_ref(c.cur_type(), Some(c), None, ctx); let name = c.spelling(); let name = if name.is_empty() { None } else { Some(name) }; @@ -218,7 +216,7 @@ impl FunctionSig { } let ty_ret_type = try!(ty.ret_type().ok_or(ParseError::Continue)); - let ret = try!(Item::from_ty(&ty_ret_type, None, None, ctx)); + let ret = Item::from_ty_or_ref(ty_ret_type, None, None, ctx); let abi = get_abi(ty.call_conv()); Ok(Self::new(ret, args, ty.is_variadic(), abi)) diff --git a/tests/expectations/tests/bitfield_method_mangling.rs b/tests/expectations/tests/bitfield_method_mangling.rs index b650a38f8c..5aba8abb08 100644 --- a/tests/expectations/tests/bitfield_method_mangling.rs +++ b/tests/expectations/tests/bitfield_method_mangling.rs @@ -46,4 +46,4 @@ impl _bindgen_ty_1 { ((val as u32 as u32) << 24u32) & (4278190080usize as u32); } } -pub use self::_bindgen_ty_1 as mach_msg_type_descriptor_t; +pub type mach_msg_type_descriptor_t = _bindgen_ty_1; diff --git a/tests/expectations/tests/inherit_typedef.rs b/tests/expectations/tests/inherit_typedef.rs index 2b9742231c..ca9041e202 100644 --- a/tests/expectations/tests/inherit_typedef.rs +++ b/tests/expectations/tests/inherit_typedef.rs @@ -17,7 +17,7 @@ fn bindgen_test_layout_Foo() { impl Clone for Foo { fn clone(&self) -> Self { *self } } -pub use self::Foo as TypedefedFoo; +pub type TypedefedFoo = Foo; #[repr(C)] #[derive(Debug, Copy)] pub struct Bar { diff --git a/tests/expectations/tests/issue-410.rs b/tests/expectations/tests/issue-410.rs index a5d9496039..0c91e2b7de 100644 --- a/tests/expectations/tests/issue-410.rs +++ b/tests/expectations/tests/issue-410.rs @@ -10,7 +10,6 @@ pub mod root { pub mod JS { #[allow(unused_imports)] use self::super::super::root; - pub use root::_bindgen_ty_1 as JSWhyMagic; #[repr(C)] #[derive(Debug, Copy)] pub struct Value { @@ -24,18 +23,19 @@ pub mod root { extern "C" { #[link_name = "_ZN2JS5Value1aE10JSWhyMagic"] pub fn Value_a(this: *mut root::JS::Value, - arg1: root::JS::JSWhyMagic); + arg1: root::JSWhyMagic); } impl Clone for Value { fn clone(&self) -> Self { *self } } impl Value { #[inline] - pub unsafe fn a(&mut self, arg1: root::JS::JSWhyMagic) { + pub unsafe fn a(&mut self, arg1: root::JSWhyMagic) { Value_a(&mut *self, arg1) } } } #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] pub enum _bindgen_ty_1 { } + pub use self::super::root::_bindgen_ty_1 as JSWhyMagic; } diff --git a/tests/expectations/tests/reparented_replacement.rs b/tests/expectations/tests/reparented_replacement.rs index e8ccc9315b..74ee229c15 100644 --- a/tests/expectations/tests/reparented_replacement.rs +++ b/tests/expectations/tests/reparented_replacement.rs @@ -25,5 +25,5 @@ pub mod root { fn clone(&self) -> Self { *self } } } - pub use root::foo::Bar as ReferencesBar; + pub type ReferencesBar = root::foo::Bar; } diff --git a/tests/expectations/tests/template.rs b/tests/expectations/tests/template.rs index 5864ac73dd..22e5a08774 100644 --- a/tests/expectations/tests/template.rs +++ b/tests/expectations/tests/template.rs @@ -12,13 +12,6 @@ pub struct Foo { pub m_member_arr: [T; 1usize], pub _phantom_1: ::std::marker::PhantomData, } -#[test] -fn __bindgen_test_layout_template_1() { - assert_eq!(::std::mem::size_of::>() - , 24usize); - assert_eq!(::std::mem::align_of::>() - , 8usize); -} extern "C" { #[link_name = "_Z3bar3FooIiiE"] pub fn bar(foo: Foo<::std::os::raw::c_int, ::std::os::raw::c_int>); @@ -176,6 +169,13 @@ pub struct TemplateWithVar { pub _phantom_0: ::std::marker::PhantomData, } #[test] +fn __bindgen_test_layout_template_1() { + assert_eq!(::std::mem::size_of::>() + , 24usize); + assert_eq!(::std::mem::align_of::>() + , 8usize); +} +#[test] fn __bindgen_test_layout_template_2() { assert_eq!(::std::mem::size_of::>() , 4usize); diff --git a/tests/expectations/tests/union_fields.rs b/tests/expectations/tests/union_fields.rs index 21d8791923..495e80f984 100644 --- a/tests/expectations/tests/union_fields.rs +++ b/tests/expectations/tests/union_fields.rs @@ -44,4 +44,4 @@ fn bindgen_test_layout__bindgen_ty_1() { impl Clone for _bindgen_ty_1 { fn clone(&self) -> Self { *self } } -pub use self::_bindgen_ty_1 as nsStyleUnion; +pub type nsStyleUnion = _bindgen_ty_1; diff --git a/tests/expectations/tests/unknown_attr.rs b/tests/expectations/tests/unknown_attr.rs index 541bee5d18..fd9cce4593 100644 --- a/tests/expectations/tests/unknown_attr.rs +++ b/tests/expectations/tests/unknown_attr.rs @@ -13,4 +13,4 @@ pub struct _bindgen_ty_1 { impl Clone for _bindgen_ty_1 { fn clone(&self) -> Self { *self } } -pub use self::_bindgen_ty_1 as max_align_t; +pub type max_align_t = _bindgen_ty_1;