Skip to content

codegen: Generate constants names for unnamed enums in classes. #103

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
Oct 19, 2016
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
24 changes: 19 additions & 5 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use ir::layout::Layout;
use ir::annotations::FieldAccessorKind;

use std::ops;
use std::borrow::Cow;
use std::mem;
use std::collections::BTreeSet;
use std::collections::HashSet;
Expand Down Expand Up @@ -1232,6 +1233,9 @@ impl CodeGenerator for Enum {
result.push(constant);
}

// Used to mangle the constants we generate in the unnamed-enum case.
let mut parent_canonical_name = None;

// A map where we keep a value -> variant relation.
let mut seen_values = HashMap::<_, String>::new();
let enum_ty = item.expect_type();
Expand Down Expand Up @@ -1264,11 +1268,21 @@ impl CodeGenerator for Enum {
if enum_ty.name().is_none() {
// NB: if we want to do this for other kind of nested
// enums we can probably mangle the name.
if item.is_toplevel(ctx) {
add_constant(enum_ty, &name, &variant_name,
&variant_name, enum_rust_ty.clone(),
result);
}
let mangled_name = if item.is_toplevel(ctx) {
variant_name.clone()
} else {
if parent_canonical_name.is_none() {
parent_canonical_name = Some(item.parent_id().canonical_name(ctx));
Copy link
Member

Choose a reason for hiding this comment

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

Can we eagerly initialize this to Some above the loop if !item.is_toplevel(ctx), rather than unconditionally initializing to None and repeatedly checking on every iteration?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep!, just did that :)

}

Cow::Owned(
format!("{}_{}", parent_canonical_name.as_ref().unwrap(),
variant_name))
};

add_constant(enum_ty, &name, &mangled_name,
&variant_name, enum_rust_ty.clone(),
result);
}

entry.insert(variant_name.into_owned());
Expand Down
2 changes: 2 additions & 0 deletions tests/expectations/anon_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ pub struct Test {
pub foo: ::std::os::raw::c_int,
pub bar: f32,
}
pub const Test_T_NONE: Test__bindgen_ty_bindgen_id_6 =
Test__bindgen_ty_bindgen_id_6::T_NONE;
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Test__bindgen_ty_bindgen_id_6 { T_NONE = 0, }
Expand Down
31 changes: 31 additions & 0 deletions tests/expectations/const_enum_unnamed.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* automatically generated by rust-bindgen */


#![allow(non_snake_case)]


pub const FOO_BAR: _bindgen_ty_bindgen_id_1 =
_bindgen_ty_bindgen_id_1::FOO_BAR;
pub const FOO_BAZ: _bindgen_ty_bindgen_id_1 =
_bindgen_ty_bindgen_id_1::FOO_BAZ;
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum _bindgen_ty_bindgen_id_1 { FOO_BAR = 0, FOO_BAZ = 1, }
#[repr(C)]
#[derive(Debug, Copy)]
pub struct Foo {
pub _address: u8,
}
pub const Foo_FOO_BAR: Foo__bindgen_ty_bindgen_id_5 =
Foo__bindgen_ty_bindgen_id_5::FOO_BAR;
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum Foo__bindgen_ty_bindgen_id_5 { FOO_BAR = 10, }
#[test]
fn bindgen_test_layout_Foo() {
assert_eq!(::std::mem::size_of::<Foo>() , 1usize);
assert_eq!(::std::mem::align_of::<Foo>() , 1usize);
}
impl Clone for Foo {
fn clone(&self) -> Self { *self }
}
9 changes: 9 additions & 0 deletions tests/headers/const_enum_unnamed.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

enum {
FOO_BAR,
FOO_BAZ,
};

class Foo {
enum { FOO_BAR = 10 };
};