Skip to content

codegen: Fix typedef re-export in namespaces when bindings aren't at the root. #436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Cargo
target/
*~
./bindgen-integration/Cargo.lock
#*#
2 changes: 2 additions & 0 deletions bindgen-integration/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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++")
Expand Down
6 changes: 6 additions & 0 deletions bindgen-integration/cpp/Test.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ class Test {
Test(double foo);
};

namespace testing {

typedef Test TypeAlias;

} // namespace testing

typedef testing::TypeAlias TypeAlias;
45 changes: 30 additions & 15 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,46 @@ 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;
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<ast::Ident> {
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());
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole function body could be

let super_ = ctx.rust_ident_raw("super");
let root = ctx.root_module().canonical_name(ctx);
let root_ident = ctx.rust_ident(&root);

iter::once(ctx.rust_ident_raw("self"))
    .chain(iter::repeat(super_.clone()).take(root_import_depth(ctx, item)))
    .chain(iter::once(root_ident))
    .collect()


path
}

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 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()
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
});
}
Expand Down
8 changes: 3 additions & 5 deletions src/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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) };
Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/bitfield_method_mangling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion tests/expectations/tests/inherit_typedef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions tests/expectations/tests/issue-410.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
2 changes: 1 addition & 1 deletion tests/expectations/tests/reparented_replacement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
14 changes: 7 additions & 7 deletions tests/expectations/tests/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,6 @@ pub struct Foo<T, U> {
pub m_member_arr: [T; 1usize],
pub _phantom_1: ::std::marker::PhantomData<U>,
}
#[test]
fn __bindgen_test_layout_template_1() {
assert_eq!(::std::mem::size_of::<Foo<::std::os::raw::c_int, ::std::os::raw::c_int>>()
, 24usize);
assert_eq!(::std::mem::align_of::<Foo<::std::os::raw::c_int, ::std::os::raw::c_int>>()
, 8usize);
}
extern "C" {
#[link_name = "_Z3bar3FooIiiE"]
pub fn bar(foo: Foo<::std::os::raw::c_int, ::std::os::raw::c_int>);
Expand Down Expand Up @@ -176,6 +169,13 @@ pub struct TemplateWithVar<T> {
pub _phantom_0: ::std::marker::PhantomData<T>,
}
#[test]
fn __bindgen_test_layout_template_1() {
assert_eq!(::std::mem::size_of::<Foo<::std::os::raw::c_int, ::std::os::raw::c_int>>()
, 24usize);
assert_eq!(::std::mem::align_of::<Foo<::std::os::raw::c_int, ::std::os::raw::c_int>>()
, 8usize);
}
#[test]
fn __bindgen_test_layout_template_2() {
assert_eq!(::std::mem::size_of::<WithDtor<::std::os::raw::c_int>>() ,
4usize);
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/union_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
2 changes: 1 addition & 1 deletion tests/expectations/tests/unknown_attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;