Skip to content

Commit 0318b74

Browse files
author
bors-servo
authored
Auto merge of rust-lang#436 - emilio:fix-ns-typedef, r=fitzgen
codegen: Fix typedef re-export in namespaces when bindings aren't at the root.
2 parents d83e063 + d6fc044 commit 0318b74

File tree

12 files changed

+57
-35
lines changed

12 files changed

+57
-35
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# Cargo
22
target/
33
*~
4+
./bindgen-integration/Cargo.lock
45
#*#

bindgen-integration/build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ fn main() {
1313

1414
let bindings = Builder::default()
1515
.no_unstable_rust()
16+
.enable_cxx_namespaces()
17+
.raw_line("pub use self::root::*;")
1618
.header("cpp/Test.h")
1719
.clang_arg("-x")
1820
.clang_arg("c++")

bindgen-integration/cpp/Test.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,10 @@ class Test {
99
Test(double foo);
1010
};
1111

12+
namespace testing {
13+
1214
typedef Test TypeAlias;
15+
16+
} // namespace testing
17+
18+
typedef testing::TypeAlias TypeAlias;

src/codegen/mod.rs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,46 @@ use std::cell::Cell;
2323
use std::collections::{HashSet, VecDeque};
2424
use std::collections::hash_map::{Entry, HashMap};
2525
use std::fmt::Write;
26-
use std::iter;
2726
use std::mem;
2827
use std::ops;
2928
use syntax::abi::Abi;
3029
use syntax::ast;
3130
use syntax::codemap::{Span, respan};
3231
use syntax::ptr::P;
3332

33+
fn root_import_depth(ctx: &BindgenContext, item: &Item) -> usize {
34+
if !ctx.options().enable_cxx_namespaces {
35+
return 0;
36+
}
37+
38+
item.ancestors(ctx)
39+
.filter(|id| ctx.resolve_item(*id).is_module())
40+
.fold(1, |i, _| i + 1)
41+
}
42+
43+
fn top_level_path(ctx: &BindgenContext, item: &Item) -> Vec<ast::Ident> {
44+
let mut path = vec![ctx.rust_ident_raw("self")];
45+
46+
if ctx.options().enable_cxx_namespaces {
47+
let super_ = ctx.rust_ident_raw("super");
48+
49+
for _ in 0..root_import_depth(ctx, item) {
50+
path.push(super_.clone());
51+
}
52+
}
53+
54+
path
55+
}
56+
3457
fn root_import(ctx: &BindgenContext, module: &Item) -> P<ast::Item> {
3558
assert!(ctx.options().enable_cxx_namespaces, "Somebody messed it up");
3659
assert!(module.is_module());
3760

61+
let mut path = top_level_path(ctx, module);
62+
3863
let root = ctx.root_module().canonical_name(ctx);
3964
let root_ident = ctx.rust_ident(&root);
40-
41-
let super_ = aster::AstBuilder::new().id("super");
42-
let supers = module.ancestors(ctx)
43-
.filter(|id| ctx.resolve_item(*id).is_module())
44-
.map(|_| super_.clone())
45-
.chain(iter::once(super_));
46-
47-
let self_ = iter::once(aster::AstBuilder::new().id("self"));
48-
let root_ident = iter::once(root_ident);
49-
50-
let path = self_.chain(supers).chain(root_ident);
65+
path.push(root_ident);
5166

5267
let use_root = aster::AstBuilder::new()
5368
.item()
@@ -528,7 +543,7 @@ impl CodeGenerator for Type {
528543
let simple_enum_path = match inner_rust_type.node {
529544
ast::TyKind::Path(None, ref p) => {
530545
if applicable_template_args.is_empty() &&
531-
!inner_item.expect_type().canonical_type(ctx).is_builtin_or_named() &&
546+
inner_item.expect_type().canonical_type(ctx).is_enum() &&
532547
p.segments.iter().all(|p| p.parameters.is_none()) {
533548
Some(p.clone())
534549
} else {
@@ -539,9 +554,9 @@ impl CodeGenerator for Type {
539554
};
540555

541556
let typedef = if let Some(mut p) = simple_enum_path {
542-
if p.segments.len() == 1 {
557+
for ident in top_level_path(ctx, item).into_iter().rev() {
543558
p.segments.insert(0, ast::PathSegment {
544-
identifier: ctx.ext_cx().ident_of("self"),
559+
identifier: ident,
545560
parameters: None,
546561
});
547562
}

src/ir/function.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@ impl FunctionSig {
165165
let name = arg.spelling();
166166
let name =
167167
if name.is_empty() { None } else { Some(name) };
168-
let ty = Item::from_ty(&arg_ty, Some(*arg), None, ctx)
169-
.expect("Argument?");
168+
let ty = Item::from_ty_or_ref(arg_ty, Some(*arg), None, ctx);
170169
(name, ty)
171170
})
172171
.collect()
@@ -178,8 +177,7 @@ impl FunctionSig {
178177
cursor.visit(|c| {
179178
if c.kind() == CXCursor_ParmDecl {
180179
let ty =
181-
Item::from_ty(&c.cur_type(), Some(c), None, ctx)
182-
.expect("ParmDecl?");
180+
Item::from_ty_or_ref(c.cur_type(), Some(c), None, ctx);
183181
let name = c.spelling();
184182
let name =
185183
if name.is_empty() { None } else { Some(name) };
@@ -218,7 +216,7 @@ impl FunctionSig {
218216
}
219217

220218
let ty_ret_type = try!(ty.ret_type().ok_or(ParseError::Continue));
221-
let ret = try!(Item::from_ty(&ty_ret_type, None, None, ctx));
219+
let ret = Item::from_ty_or_ref(ty_ret_type, None, None, ctx);
222220
let abi = get_abi(ty.call_conv());
223221

224222
Ok(Self::new(ret, args, ty.is_variadic(), abi))

tests/expectations/tests/bitfield_method_mangling.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,4 @@ impl _bindgen_ty_1 {
4646
((val as u32 as u32) << 24u32) & (4278190080usize as u32);
4747
}
4848
}
49-
pub use self::_bindgen_ty_1 as mach_msg_type_descriptor_t;
49+
pub type mach_msg_type_descriptor_t = _bindgen_ty_1;

tests/expectations/tests/inherit_typedef.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fn bindgen_test_layout_Foo() {
1717
impl Clone for Foo {
1818
fn clone(&self) -> Self { *self }
1919
}
20-
pub use self::Foo as TypedefedFoo;
20+
pub type TypedefedFoo = Foo;
2121
#[repr(C)]
2222
#[derive(Debug, Copy)]
2323
pub struct Bar {

tests/expectations/tests/issue-410.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ pub mod root {
1010
pub mod JS {
1111
#[allow(unused_imports)]
1212
use self::super::super::root;
13-
pub use root::_bindgen_ty_1 as JSWhyMagic;
1413
#[repr(C)]
1514
#[derive(Debug, Copy)]
1615
pub struct Value {
@@ -24,18 +23,19 @@ pub mod root {
2423
extern "C" {
2524
#[link_name = "_ZN2JS5Value1aE10JSWhyMagic"]
2625
pub fn Value_a(this: *mut root::JS::Value,
27-
arg1: root::JS::JSWhyMagic);
26+
arg1: root::JSWhyMagic);
2827
}
2928
impl Clone for Value {
3029
fn clone(&self) -> Self { *self }
3130
}
3231
impl Value {
3332
#[inline]
34-
pub unsafe fn a(&mut self, arg1: root::JS::JSWhyMagic) {
33+
pub unsafe fn a(&mut self, arg1: root::JSWhyMagic) {
3534
Value_a(&mut *self, arg1)
3635
}
3736
}
3837
}
3938
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
4039
pub enum _bindgen_ty_1 { }
40+
pub use self::super::root::_bindgen_ty_1 as JSWhyMagic;
4141
}

tests/expectations/tests/reparented_replacement.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ pub mod root {
2525
fn clone(&self) -> Self { *self }
2626
}
2727
}
28-
pub use root::foo::Bar as ReferencesBar;
28+
pub type ReferencesBar = root::foo::Bar;
2929
}

tests/expectations/tests/template.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,6 @@ pub struct Foo<T, U> {
1212
pub m_member_arr: [T; 1usize],
1313
pub _phantom_1: ::std::marker::PhantomData<U>,
1414
}
15-
#[test]
16-
fn __bindgen_test_layout_template_1() {
17-
assert_eq!(::std::mem::size_of::<Foo<::std::os::raw::c_int, ::std::os::raw::c_int>>()
18-
, 24usize);
19-
assert_eq!(::std::mem::align_of::<Foo<::std::os::raw::c_int, ::std::os::raw::c_int>>()
20-
, 8usize);
21-
}
2215
extern "C" {
2316
#[link_name = "_Z3bar3FooIiiE"]
2417
pub fn bar(foo: Foo<::std::os::raw::c_int, ::std::os::raw::c_int>);
@@ -176,6 +169,13 @@ pub struct TemplateWithVar<T> {
176169
pub _phantom_0: ::std::marker::PhantomData<T>,
177170
}
178171
#[test]
172+
fn __bindgen_test_layout_template_1() {
173+
assert_eq!(::std::mem::size_of::<Foo<::std::os::raw::c_int, ::std::os::raw::c_int>>()
174+
, 24usize);
175+
assert_eq!(::std::mem::align_of::<Foo<::std::os::raw::c_int, ::std::os::raw::c_int>>()
176+
, 8usize);
177+
}
178+
#[test]
179179
fn __bindgen_test_layout_template_2() {
180180
assert_eq!(::std::mem::size_of::<WithDtor<::std::os::raw::c_int>>() ,
181181
4usize);

tests/expectations/tests/union_fields.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ fn bindgen_test_layout__bindgen_ty_1() {
4444
impl Clone for _bindgen_ty_1 {
4545
fn clone(&self) -> Self { *self }
4646
}
47-
pub use self::_bindgen_ty_1 as nsStyleUnion;
47+
pub type nsStyleUnion = _bindgen_ty_1;

tests/expectations/tests/unknown_attr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ pub struct _bindgen_ty_1 {
1313
impl Clone for _bindgen_ty_1 {
1414
fn clone(&self) -> Self { *self }
1515
}
16-
pub use self::_bindgen_ty_1 as max_align_t;
16+
pub type max_align_t = _bindgen_ty_1;

0 commit comments

Comments
 (0)