Skip to content

codegen: Reuse the next_child_local_id hack for template instantiations. #708

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 1 commit into from
May 20, 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
4 changes: 2 additions & 2 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -760,8 +760,8 @@ impl CodeGenerator for TemplateInstantiation {

let name = item.canonical_name(ctx);
let fn_name = format!("__bindgen_test_layout_{}_instantiation_{}",
name,
item.id().as_usize());
name, item.exposed_id(ctx));

let fn_name = ctx.rust_ident_raw(&fn_name);

let prefix = ctx.trait_prefix();
Expand Down
25 changes: 19 additions & 6 deletions src/ir/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ pub struct Item {
/// This item's id.
id: ItemId,

/// The item's local id, unique only amongst its siblings. Only used for
/// The item's local id, unique only amongst its siblings. Only used for
/// anonymous items.
///
/// Lazily initialized in local_id().
Expand All @@ -379,7 +379,7 @@ pub struct Item {
/// case this is an implementation detail.
local_id: Cell<Option<usize>>,

/// The next local id to use for a child..
/// The next local id to use for a child or template instantiation.
next_child_local_id: Cell<usize>,

/// A cached copy of the canonical name, as returned by `canonical_name`.
Expand Down Expand Up @@ -490,13 +490,23 @@ impl Item {
pub fn local_id(&self, ctx: &BindgenContext) -> usize {
if self.local_id.get().is_none() {
let parent = ctx.resolve_item(self.parent_id);
let local_id = parent.next_child_local_id.get();
parent.next_child_local_id.set(local_id + 1);
self.local_id.set(Some(local_id));
self.local_id.set(Some(parent.next_child_local_id()));
}
self.local_id.get().unwrap()
}

/// Get an identifier that differentiates a child of this item of other
/// related items.
///
/// This is currently used for anonymous items, and template instantiation
/// tests, in both cases in order to reduce noise when system headers are at
/// place.
pub fn next_child_local_id(&self) -> usize {
let local_id = self.next_child_local_id.get();
self.next_child_local_id.set(local_id + 1);
local_id
}

/// Returns whether this item is a top-level item, from the point of view of
/// bindgen.
///
Expand Down Expand Up @@ -777,13 +787,16 @@ impl Item {
ctx.rust_mangle(&name).into_owned()
}

fn exposed_id(&self, ctx: &BindgenContext) -> String {
/// The exposed id that represents an unique id among the siblings of a
/// given item.
pub fn exposed_id(&self, ctx: &BindgenContext) -> String {
// Only use local ids for enums, classes, structs and union types. All
// other items use their global id.
let ty_kind = self.kind().as_type().map(|t| t.kind());
if let Some(ty_kind) = ty_kind {
match *ty_kind {
TypeKind::Comp(..) |
TypeKind::TemplateInstantiation(..) |
TypeKind::Enum(..) => return self.local_id(ctx).to_string(),
_ => {}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/anon_union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl Default for ErrorResult {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}
#[test]
fn __bindgen_test_layout_TErrorResult_instantiation_21() {
fn __bindgen_test_layout_TErrorResult_instantiation_1() {
assert_eq!(::std::mem::size_of::<TErrorResult>() , 24usize , concat ! (
"Size of template specialization: " , stringify ! (
TErrorResult ) ));
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/class_nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ extern "C" {
pub static mut var: A_B;
}
#[test]
fn __bindgen_test_layout_A_D_instantiation_16() {
fn __bindgen_test_layout_A_D_instantiation_1() {
assert_eq!(::std::mem::size_of::<A_D<::std::os::raw::c_int>>() , 4usize ,
concat ! (
"Size of template specialization: " , stringify ! (
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/class_with_dtor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl Default for WithoutDtor {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}
#[test]
fn __bindgen_test_layout_HandleWithDtor_instantiation_10() {
fn __bindgen_test_layout_HandleWithDtor_instantiation_1() {
assert_eq!(::std::mem::size_of::<HandleWithDtor<::std::os::raw::c_int>>()
, 8usize , concat ! (
"Size of template specialization: " , stringify ! (
Expand Down
4 changes: 2 additions & 2 deletions tests/expectations/tests/crtp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ impl Default for DerivedFromBaseWithDestructor {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}
#[test]
fn __bindgen_test_layout_Base_instantiation_9() {
fn __bindgen_test_layout_Base_instantiation_1() {
assert_eq!(::std::mem::size_of::<Base>() , 1usize , concat ! (
"Size of template specialization: " , stringify ! ( Base ) ));
assert_eq!(::std::mem::align_of::<Base>() , 1usize , concat ! (
"Alignment of template specialization: " , stringify ! ( Base )
));
}
#[test]
fn __bindgen_test_layout_BaseWithDestructor_instantiation_12() {
fn __bindgen_test_layout_BaseWithDestructor_instantiation_2() {
assert_eq!(::std::mem::size_of::<BaseWithDestructor>() , 1usize , concat !
(
"Size of template specialization: " , stringify ! (
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/default-template-parameter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl <T, U> Default for Foo<T, U> {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}
#[test]
fn __bindgen_test_layout_Foo_instantiation_6() {
fn __bindgen_test_layout_Foo_instantiation_1() {
assert_eq!(::std::mem::size_of::<Foo<bool, ::std::os::raw::c_int>>() ,
8usize , concat ! (
"Size of template specialization: " , stringify ! (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Default for JS_AutoIdVector {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}
#[test]
fn __bindgen_test_layout_JS_Base_instantiation_20() {
fn __bindgen_test_layout_JS_Base_instantiation_2() {
assert_eq!(::std::mem::size_of::<JS_Base>() , 1usize , concat ! (
"Size of template specialization: " , stringify ! ( JS_Base )
));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Default for ServoElementSnapshotTable {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}
#[test]
fn __bindgen_test_layout_Set_instantiation_13() {
fn __bindgen_test_layout_Set_instantiation_1() {
assert_eq!(::std::mem::size_of::<Set>() , 4usize , concat ! (
"Size of template specialization: " , stringify ! ( Set ) ));
assert_eq!(::std::mem::align_of::<Set>() , 4usize , concat ! (
Expand Down
2 changes: 1 addition & 1 deletion tests/expectations/tests/non-type-params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ impl Default for UsesArray {
fn default() -> Self { unsafe { ::std::mem::zeroed() } }
}
#[test]
fn __bindgen_test_layout_Array_instantiation_18() {
fn __bindgen_test_layout_Array_instantiation_1() {
assert_eq!(::std::mem::size_of::<[u32; 4usize]>() , 16usize , concat ! (
"Size of template specialization: " , stringify ! (
[u32; 4usize] ) ));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Derived {
pub b: bool,
}
#[test]
fn __bindgen_test_layout__bindgen_ty_id_20_instantiation_14() {
fn __bindgen_test_layout__bindgen_ty_id_20_instantiation_1() {
assert_eq!(::std::mem::size_of::<[u32; 2usize]>() , 8usize , concat ! (
"Size of template specialization: " , stringify ! (
[u32; 2usize] ) ));
Expand Down
8 changes: 4 additions & 4 deletions tests/expectations/tests/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ pub struct TemplateWithVar {
pub _address: u8,
}
#[test]
fn __bindgen_test_layout_Foo_instantiation_95() {
fn __bindgen_test_layout_Foo_instantiation_1() {
assert_eq!(::std::mem::size_of::<Foo<::std::os::raw::c_int>>() , 24usize ,
concat ! (
"Size of template specialization: " , stringify ! (
Expand All @@ -256,7 +256,7 @@ fn __bindgen_test_layout_Foo_instantiation_95() {
Foo<::std::os::raw::c_int> ) ));
}
#[test]
fn __bindgen_test_layout_Foo_instantiation_101() {
fn __bindgen_test_layout_Foo_instantiation_2() {
assert_eq!(::std::mem::size_of::<Foo<::std::os::raw::c_int>>() , 24usize ,
concat ! (
"Size of template specialization: " , stringify ! (
Expand All @@ -267,7 +267,7 @@ fn __bindgen_test_layout_Foo_instantiation_101() {
Foo<::std::os::raw::c_int> ) ));
}
#[test]
fn __bindgen_test_layout_Rooted_instantiation_111() {
fn __bindgen_test_layout_Rooted_instantiation_3() {
assert_eq!(::std::mem::size_of::<Rooted<*mut ::std::os::raw::c_void>>() ,
24usize , concat ! (
"Size of template specialization: " , stringify ! (
Expand All @@ -278,7 +278,7 @@ fn __bindgen_test_layout_Rooted_instantiation_111() {
Rooted<*mut ::std::os::raw::c_void> ) ));
}
#[test]
fn __bindgen_test_layout_WithDtor_instantiation_123() {
fn __bindgen_test_layout_WithDtor_instantiation_4() {
assert_eq!(::std::mem::size_of::<WithDtor<::std::os::raw::c_int>>() ,
4usize , concat ! (
"Size of template specialization: " , stringify ! (
Expand Down