Skip to content

Commit b903022

Browse files
author
bors-servo
authored
Auto merge of #103 - emilio:const-enum-unnamed, r=fitzgen
codegen: Generate constants names for unnamed enums in classes. Fixes #84. cc @upsuper, sorry for not doing it before, I totally forgot about it. r? @fitzgen or @nox
2 parents 58aaf30 + fd9c1fe commit b903022

File tree

4 files changed

+61
-5
lines changed

4 files changed

+61
-5
lines changed

src/codegen/mod.rs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use ir::layout::Layout;
1616
use ir::annotations::FieldAccessorKind;
1717

1818
use std::ops;
19+
use std::borrow::Cow;
1920
use std::mem;
2021
use std::collections::BTreeSet;
2122
use std::collections::HashSet;
@@ -1232,6 +1233,9 @@ impl CodeGenerator for Enum {
12321233
result.push(constant);
12331234
}
12341235

1236+
// Used to mangle the constants we generate in the unnamed-enum case.
1237+
let mut parent_canonical_name = None;
1238+
12351239
// A map where we keep a value -> variant relation.
12361240
let mut seen_values = HashMap::<_, String>::new();
12371241
let enum_ty = item.expect_type();
@@ -1264,11 +1268,21 @@ impl CodeGenerator for Enum {
12641268
if enum_ty.name().is_none() {
12651269
// NB: if we want to do this for other kind of nested
12661270
// enums we can probably mangle the name.
1267-
if item.is_toplevel(ctx) {
1268-
add_constant(enum_ty, &name, &variant_name,
1269-
&variant_name, enum_rust_ty.clone(),
1270-
result);
1271-
}
1271+
let mangled_name = if item.is_toplevel(ctx) {
1272+
variant_name.clone()
1273+
} else {
1274+
if parent_canonical_name.is_none() {
1275+
parent_canonical_name = Some(item.parent_id().canonical_name(ctx));
1276+
}
1277+
1278+
Cow::Owned(
1279+
format!("{}_{}", parent_canonical_name.as_ref().unwrap(),
1280+
variant_name))
1281+
};
1282+
1283+
add_constant(enum_ty, &name, &mangled_name,
1284+
&variant_name, enum_rust_ty.clone(),
1285+
result);
12721286
}
12731287

12741288
entry.insert(variant_name.into_owned());

tests/expectations/anon_enum.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ pub struct Test {
1010
pub foo: ::std::os::raw::c_int,
1111
pub bar: f32,
1212
}
13+
pub const Test_T_NONE: Test__bindgen_ty_bindgen_id_6 =
14+
Test__bindgen_ty_bindgen_id_6::T_NONE;
1315
#[repr(u32)]
1416
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1517
pub enum Test__bindgen_ty_bindgen_id_6 { T_NONE = 0, }
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(non_snake_case)]
5+
6+
7+
pub const FOO_BAR: _bindgen_ty_bindgen_id_1 =
8+
_bindgen_ty_bindgen_id_1::FOO_BAR;
9+
pub const FOO_BAZ: _bindgen_ty_bindgen_id_1 =
10+
_bindgen_ty_bindgen_id_1::FOO_BAZ;
11+
#[repr(u32)]
12+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
13+
pub enum _bindgen_ty_bindgen_id_1 { FOO_BAR = 0, FOO_BAZ = 1, }
14+
#[repr(C)]
15+
#[derive(Debug, Copy)]
16+
pub struct Foo {
17+
pub _address: u8,
18+
}
19+
pub const Foo_FOO_BAR: Foo__bindgen_ty_bindgen_id_5 =
20+
Foo__bindgen_ty_bindgen_id_5::FOO_BAR;
21+
#[repr(u32)]
22+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
23+
pub enum Foo__bindgen_ty_bindgen_id_5 { FOO_BAR = 10, }
24+
#[test]
25+
fn bindgen_test_layout_Foo() {
26+
assert_eq!(::std::mem::size_of::<Foo>() , 1usize);
27+
assert_eq!(::std::mem::align_of::<Foo>() , 1usize);
28+
}
29+
impl Clone for Foo {
30+
fn clone(&self) -> Self { *self }
31+
}

tests/headers/const_enum_unnamed.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
enum {
3+
FOO_BAR,
4+
FOO_BAZ,
5+
};
6+
7+
class Foo {
8+
enum { FOO_BAR = 10 };
9+
};

0 commit comments

Comments
 (0)