Skip to content

Commit eb9247c

Browse files
authored
Merge pull request rust-lang#1 from martinboehme/nested-types-original-name
Emit `bindgen_original_name` attributes for nested types
2 parents e4e7fc8 + 7d3b749 commit eb9247c

File tree

7 files changed

+45
-1
lines changed

7 files changed

+45
-1
lines changed

src/codegen/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2008,6 +2008,11 @@ impl CodeGenerator for CompInfo {
20082008
attributes.push(attributes::derives(&derives))
20092009
}
20102010

2011+
let original_name = item.original_name(ctx);
2012+
if canonical_name != original_name {
2013+
attributes.push(attributes::original_name(&original_name));
2014+
}
2015+
20112016
let mut tokens = if is_union && struct_layout.is_rust_union() {
20122017
quote! {
20132018
#( #attributes )*

src/ir/item.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,32 @@ impl Item {
808808
}
809809
}
810810

811+
/// Get this item's original C++ name, including any containing types, but without
812+
/// the namespace name. For nested types, the C++ name differs from the Rust name, e.g.
813+
/// a nested C++ type `A::B` would correspond to the Rust type `A_B`.
814+
pub fn original_name(
815+
&self,
816+
ctx: &BindgenContext,
817+
) -> String {
818+
let target = ctx.resolve_item(self.name_target(ctx));
819+
820+
// Concatenate name of item and all ancestors until we reach a namespace.
821+
let mut names: Vec<_> = target
822+
.ancestors(ctx)
823+
.map(|id| ctx.resolve_item(id))
824+
.take_while(|item| !item.is_module())
825+
.map(|item| {
826+
let target = ctx.resolve_item(item.name_target(ctx));
827+
target.base_name(ctx)
828+
})
829+
.filter(|name| !name.is_empty())
830+
.collect();
831+
832+
names.reverse();
833+
834+
names.join("::")
835+
}
836+
811837
/// Get the canonical name without taking into account the replaces
812838
/// annotation.
813839
///

tests/expectations/tests/class_nested.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub struct A {
1212
}
1313
#[repr(C)]
1414
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
15+
#[bindgen_original_name("A::B")]
1516
pub struct A_B {
1617
pub member_b: ::std::os::raw::c_int,
1718
}
@@ -42,6 +43,7 @@ fn bindgen_test_layout_A_B() {
4243
}
4344
#[repr(C)]
4445
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
46+
#[bindgen_original_name("A::D")]
4547
pub struct A_D<T> {
4648
pub foo: T,
4749
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,
@@ -76,6 +78,7 @@ fn bindgen_test_layout_A() {
7678
}
7779
#[repr(C)]
7880
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
81+
#[bindgen_original_name("A::C")]
7982
pub struct A_C {
8083
pub baz: ::std::os::raw::c_int,
8184
}
@@ -153,6 +156,7 @@ pub struct Templated<T> {
153156
}
154157
#[repr(C)]
155158
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
159+
#[bindgen_original_name("Templated::Templated_inner")]
156160
pub struct Templated_Templated_inner<T> {
157161
pub member_ptr: *mut T,
158162
pub _phantom_0: ::std::marker::PhantomData<::std::cell::UnsafeCell<T>>,

tests/expectations/tests/disable-nested-struct-naming.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,28 @@ pub struct foo {
1212
}
1313
#[repr(C)]
1414
#[derive(Debug, Default, Copy, Clone)]
15+
#[bindgen_original_name("foo::bar1")]
1516
pub struct bar1 {
1617
pub x1: ::std::os::raw::c_int,
1718
pub b2: bar1__bindgen_ty_1,
1819
}
1920
#[repr(C)]
2021
#[derive(Debug, Default, Copy, Clone)]
22+
#[bindgen_original_name("foo::bar1::_bindgen_ty_1")]
2123
pub struct bar1__bindgen_ty_1 {
2224
pub x2: ::std::os::raw::c_int,
2325
pub b3: bar1__bindgen_ty_1__bindgen_ty_1,
2426
}
2527
#[repr(C)]
2628
#[derive(Debug, Default, Copy, Clone)]
29+
#[bindgen_original_name("foo::bar1::_bindgen_ty_1::_bindgen_ty_1")]
2730
pub struct bar1__bindgen_ty_1__bindgen_ty_1 {
2831
pub x3: ::std::os::raw::c_int,
2932
pub b4: bar4,
3033
}
3134
#[repr(C)]
3235
#[derive(Debug, Default, Copy, Clone)]
36+
#[bindgen_original_name("foo::bar1::_bindgen_ty_1::_bindgen_ty_1::bar4")]
3337
pub struct bar4 {
3438
pub x4: ::std::os::raw::c_int,
3539
}
@@ -180,11 +184,13 @@ pub struct _bindgen_ty_1 {
180184
}
181185
#[repr(C)]
182186
#[derive(Debug, Default, Copy, Clone)]
187+
#[bindgen_original_name("_bindgen_ty_1::_bindgen_ty_1")]
183188
pub struct _bindgen_ty_1__bindgen_ty_1 {
184189
pub b: baz,
185190
}
186191
#[repr(C)]
187192
#[derive(Debug, Default, Copy, Clone)]
193+
#[bindgen_original_name("_bindgen_ty_1::_bindgen_ty_1::baz")]
188194
pub struct baz {
189195
pub x: ::std::os::raw::c_int,
190196
}

tests/expectations/tests/nested.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,14 @@ pub struct Test {
3535
}
3636
#[repr(C)]
3737
#[derive(Debug, Default, Copy, Clone)]
38+
#[bindgen_original_name("Test::Size")]
3839
pub struct Test_Size {
3940
pub mWidth: Test_Size_Dimension,
4041
pub mHeight: Test_Size_Dimension,
4142
}
4243
#[repr(C)]
4344
#[derive(Debug, Default, Copy, Clone)]
45+
#[bindgen_original_name("Test::Size::Dimension")]
4446
pub struct Test_Size_Dimension {
4547
pub _base: Calc,
4648
}

tests/expectations/tests/nested_within_namespace.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub mod root {
1919
}
2020
#[repr(C)]
2121
#[derive(Debug, Default, Copy, Clone)]
22+
#[bindgen_original_name("Bar::Baz")]
2223
pub struct Bar_Baz {
2324
pub foo: ::std::os::raw::c_int,
2425
}

tests/parse_callbacks/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use bindgen::callbacks::*;
1+
use crate::bindgen::callbacks::*;
22

33
#[derive(Debug)]
44
struct EnumVariantRename;

0 commit comments

Comments
 (0)