Skip to content

Commit 15630c6

Browse files
committed
Make it work in rust stable, and incidentally fix #425
The problem with #425 was the following: We were parsing the methods after reaching the JS::Value definition. Those methods contained a JSWhyMagic that we hadn't seen, so we parsed it as being in the JS:: module.
1 parent f804f9e commit 15630c6

File tree

5 files changed

+33
-29
lines changed

5 files changed

+33
-29
lines changed

src/codegen/mod.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,30 +40,34 @@ fn root_import_depth(ctx: &BindgenContext, item: &Item) -> usize {
4040
.fold(1, |i, _| i + 1)
4141
}
4242

43-
fn top_level_module_path(ctx: &BindgenContext, item: &Item) -> Vec<ast::Ident> {
43+
fn top_level_path(ctx: &BindgenContext, item: &Item) -> Vec<ast::Ident> {
4444
let mut path = vec![ctx.rust_ident_raw("self")];
4545

46-
let super_ = ctx.rust_ident_raw("super");
46+
if ctx.options().enable_cxx_namespaces {
47+
let super_ = ctx.rust_ident_raw("super");
4748

48-
for _ in 0..root_import_depth(ctx, item) {
49-
path.push(super_.clone());
49+
for _ in 0..root_import_depth(ctx, item) {
50+
path.push(super_.clone());
51+
}
5052
}
5153

52-
let root = ctx.root_module().canonical_name(ctx);
53-
let root_ident = ctx.rust_ident(&root);
54-
55-
path.push(root_ident);
5654
path
5755
}
5856

5957
fn root_import(ctx: &BindgenContext, module: &Item) -> P<ast::Item> {
6058
assert!(ctx.options().enable_cxx_namespaces, "Somebody messed it up");
6159
assert!(module.is_module());
6260

61+
let mut path = top_level_path(ctx, module);
62+
63+
let root = ctx.root_module().canonical_name(ctx);
64+
let root_ident = ctx.rust_ident(&root);
65+
path.push(root_ident);
66+
6367
let use_root = aster::AstBuilder::new()
6468
.item()
6569
.use_()
66-
.ids(top_level_module_path(ctx, module))
70+
.ids(path)
6771
.build()
6872
.build();
6973

@@ -550,10 +554,12 @@ impl CodeGenerator for Type {
550554
};
551555

552556
let typedef = if let Some(mut p) = simple_enum_path {
553-
p.segments.insert(0, ast::PathSegment {
554-
identifier: ctx.ext_cx().ident_of("self"),
555-
parameters: None,
556-
});
557+
for ident in top_level_path(ctx, item).into_iter().rev() {
558+
p.segments.insert(0, ast::PathSegment {
559+
identifier: ident,
560+
parameters: None,
561+
});
562+
}
557563
typedef.use_().build(p).as_(rust_name)
558564
} else {
559565
let mut generics = typedef.type_(rust_name).generics();

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/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 self::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 self::root::foo::Bar as ReferencesBar;
28+
pub use self::super::root::foo::Bar as ReferencesBar;
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);

0 commit comments

Comments
 (0)