Skip to content

Commit d794345

Browse files
committed
codegen: Constify enum constants.
1 parent 4663ae9 commit d794345

File tree

6 files changed

+64
-8
lines changed

6 files changed

+64
-8
lines changed

libbindgen/src/chooser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub trait TypeChooser: fmt::Debug {
1919
fn constify_enum_variant(&self,
2020
_enum_name: Option<&str>,
2121
_variant_name: &str,
22-
_variant_value: Option<EnumVariantValue>) -> bool {
22+
_variant_value: EnumVariantValue) -> bool {
2323
false
2424
}
2525
}

libbindgen/src/codegen/mod.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
mod helpers;
22

3-
43
use aster;
54

65
use ir::annotations::FieldAccessorKind;
@@ -1667,7 +1666,22 @@ impl CodeGenerator for Enum {
16671666
Some(&name)
16681667
};
16691668

1670-
for variant in self.variants().iter() {
1669+
// NB: We defer the creation of constified variants, in case we find
1670+
// another variant with the same value (which is the common thing to
1671+
// do).
1672+
let mut constified_variants = vec![];
1673+
1674+
let mut iter = self.variants().iter().peekable();
1675+
1676+
while iter.peek().is_some() || !constified_variants.is_empty() {
1677+
let variant = iter.next()
1678+
.unwrap_or_else(|| constified_variants.remove(0));
1679+
1680+
if variant.force_constification() && iter.peek().is_some() {
1681+
constified_variants.push(variant);
1682+
continue;
1683+
}
1684+
16711685
match seen_values.entry(variant.val()) {
16721686
Entry::Occupied(ref entry) => {
16731687
if is_rust_enum {
@@ -1707,9 +1721,11 @@ impl CodeGenerator for Enum {
17071721

17081722
let variant_name = ctx.rust_mangle(variant.name());
17091723

1710-
// If it's an unnamed enum, we also generate a constant so
1711-
// it can be properly accessed.
1712-
if is_rust_enum && enum_ty.name().is_none() {
1724+
// If it's an unnamed enum, or constification is enforced,
1725+
// we also generate a constant so it can be properly
1726+
// accessed.
1727+
if (is_rust_enum && enum_ty.name().is_none()) ||
1728+
variant.force_constification() {
17131729
let mangled_name = if is_toplevel {
17141730
variant_name.clone()
17151731
} else {

libbindgen/src/ir/annotations.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,18 @@ pub struct Annotations {
4545
///
4646
/// ```cpp
4747
/// enum Foo {
48-
/// Bar = 0, //< <div rustbindgen constant></div>
48+
/// Bar = 0, /**< <div rustbindgen constant></div> */
4949
/// Baz = 0,
5050
/// };
5151
/// ```
5252
///
5353
/// In that case, bindgen will generate a constant for `Bar` instead of
5454
/// `Baz`.
55+
///
56+
/// You can see the kind of comments that are accepted in the Doxygen
57+
/// documentation:
58+
///
59+
/// http://www.stack.nl/~dimitri/doxygen/manual/docblocks.html
5560
constify_enum_variant: bool,
5661
}
5762

libbindgen/src/ir/enum_ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ impl Enum {
8282
let name = cursor.spelling();
8383
let should_constify = ctx.type_chooser()
8484
.map_or(false, |c| {
85-
c.constify_enum_variant(type_name, &name, value)
85+
c.constify_enum_variant(type_name, &name, val)
8686
}) ||
8787
Annotations::new(&cursor).map_or(false, |anno| {
8888
anno.constify_enum_variant()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
4+
#![allow(non_snake_case)]
5+
6+
7+
pub const nsCSSPropertyID_eCSSProperty_COUNT_unexistingVariantValue:
8+
nsCSSPropertyID =
9+
nsCSSPropertyID::eCSSProperty_COUNT_unexistingVariantValue;
10+
pub const nsCSSPropertyID_eCSSProperty_COUNT: nsCSSPropertyID =
11+
nsCSSPropertyID::eCSSPropertyAlias_aa;
12+
pub const nsCSSPropertyID_eCSSProperty_COUNT_DUMMY2: nsCSSPropertyID =
13+
nsCSSPropertyID::eCSSProperty_b;
14+
#[repr(u32)]
15+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
16+
pub enum nsCSSPropertyID {
17+
eCSSProperty_a = 0,
18+
eCSSProperty_b = 1,
19+
eCSSPropertyAlias_aa = 2,
20+
eCSSPropertyAlias_bb = 3,
21+
eCSSProperty_COUNT_unexistingVariantValue = 4,
22+
}
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
enum nsCSSPropertyID {
3+
eCSSProperty_a,
4+
eCSSProperty_b,
5+
6+
eCSSProperty_COUNT, /**< <div rustbindgen constant></div> */
7+
eCSSProperty_COUNT_DUMMY2 = eCSSProperty_COUNT - 1, /**< <div rustbindgen constant></div> */
8+
9+
eCSSPropertyAlias_aa,
10+
eCSSPropertyAlias_bb,
11+
12+
eCSSProperty_COUNT_unexistingVariantValue, /**< <div rustbindgen constant></div> */
13+
};

0 commit comments

Comments
 (0)