Skip to content

Commit cbbd9be

Browse files
committed
ir: Support hiding enum variants.
Signed-off-by: Emilio Cobos Álvarez <[email protected]>
1 parent 8183e61 commit cbbd9be

File tree

5 files changed

+48
-21
lines changed

5 files changed

+48
-21
lines changed

libbindgen/src/chooser.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! A public API for more fine-grained customization of bindgen behavior.
22
33
pub use ir::int::IntKind;
4-
pub use ir::enum_ty::EnumVariantValue;
4+
pub use ir::enum_ty::{EnumVariantValue, EnumVariantCustomBehavior};
55
use std::fmt;
66

77
/// A trait to allow configuring different kinds of types in different
@@ -16,10 +16,11 @@ pub trait TypeChooser: fmt::Debug {
1616
/// This function should return whether, given the a given enum variant
1717
/// name, and value, returns whether this enum variant will forcibly be a
1818
/// constant.
19-
fn constify_enum_variant(&self,
19+
fn enum_variant_behavior(&self,
2020
_enum_name: Option<&str>,
2121
_variant_name: &str,
22-
_variant_value: EnumVariantValue) -> bool {
23-
false
22+
_variant_value: EnumVariantValue)
23+
-> Option<EnumVariantCustomBehavior> {
24+
None
2425
}
2526
}

libbindgen/src/codegen/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1677,6 +1677,10 @@ impl CodeGenerator for Enum {
16771677
let variant = iter.next()
16781678
.unwrap_or_else(|| constified_variants.remove(0));
16791679

1680+
if variant.hidden() {
1681+
continue;
1682+
}
1683+
16801684
if variant.force_constification() && iter.peek().is_some() {
16811685
constified_variants.push(variant);
16821686
continue;

libbindgen/src/ir/enum_ty.rs

+38-14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ use super::context::{BindgenContext, ItemId};
77
use super::item::Item;
88
use super::ty::TypeKind;
99

10+
/// An enum representing custom handling that can be given to a variant.
11+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
12+
pub enum EnumVariantCustomBehavior {
13+
/// This variant will be constified, that is, forced to generate a constant.
14+
Constify,
15+
/// This variant will be hidden entirely from the resulting enum.
16+
Hide,
17+
}
18+
1019
/// A C/C++ enumeration.
1120
#[derive(Debug)]
1221
pub struct Enum {
@@ -80,16 +89,25 @@ impl Enum {
8089
};
8190
if let Some(val) = value {
8291
let name = cursor.spelling();
83-
let should_constify = ctx.type_chooser()
84-
.map_or(false, |c| {
85-
c.constify_enum_variant(type_name, &name, val)
86-
}) ||
87-
Annotations::new(&cursor).map_or(false, |anno| {
88-
anno.constify_enum_variant()
92+
let custom_behavior = ctx.type_chooser()
93+
.and_then(|t| {
94+
t.enum_variant_behavior(type_name, &name, val)
95+
})
96+
.or_else(|| {
97+
Annotations::new(&cursor).and_then(|anno| {
98+
if anno.hide() {
99+
Some(EnumVariantCustomBehavior::Hide)
100+
} else if anno.constify_enum_variant() {
101+
Some(EnumVariantCustomBehavior::Constify)
102+
} else {
103+
None
104+
}
105+
})
89106
});
107+
90108
let comment = cursor.raw_comment();
91109
variants.push(
92-
EnumVariant::new(name, comment, val, should_constify));
110+
EnumVariant::new(name, comment, val, custom_behavior));
93111
}
94112
}
95113
CXChildVisit_Continue
@@ -110,10 +128,8 @@ pub struct EnumVariant {
110128
/// The integer value of the variant.
111129
val: EnumVariantValue,
112130

113-
/// Whether this value should explicitly be constified.
114-
///
115-
/// This allows us to solve situations as described in #392.
116-
force_constify: bool,
131+
/// The custom behavior this variant may have, if any.
132+
custom_behavior: Option<EnumVariantCustomBehavior>,
117133
}
118134

119135
/// A constant value assigned to an enumeration variant.
@@ -131,13 +147,13 @@ impl EnumVariant {
131147
pub fn new(name: String,
132148
comment: Option<String>,
133149
val: EnumVariantValue,
134-
force_constify: bool)
150+
custom_behavior: Option<EnumVariantCustomBehavior>)
135151
-> Self {
136152
EnumVariant {
137153
name: name,
138154
comment: comment,
139155
val: val,
140-
force_constify: force_constify,
156+
custom_behavior: custom_behavior,
141157
}
142158
}
143159

@@ -154,6 +170,14 @@ impl EnumVariant {
154170
/// Returns whether this variant should be enforced to be a constant by code
155171
/// generation.
156172
pub fn force_constification(&self) -> bool {
157-
self.force_constify
173+
self.custom_behavior
174+
.map_or(false, |b| b == EnumVariantCustomBehavior::Constify)
175+
}
176+
177+
/// Returns whether the current variant should be hidden completely from the
178+
/// resulting rust enum.
179+
pub fn hidden(&self) -> bool {
180+
self.custom_behavior
181+
.map_or(false, |b| b == EnumVariantCustomBehavior::Hide)
158182
}
159183
}

libbindgen/tests/expectations/tests/constify-enum.rs

-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ pub const nsCSSPropertyID_eCSSProperty_COUNT_unexistingVariantValue:
99
nsCSSPropertyID::eCSSProperty_COUNT_unexistingVariantValue;
1010
pub const nsCSSPropertyID_eCSSProperty_COUNT: nsCSSPropertyID =
1111
nsCSSPropertyID::eCSSPropertyAlias_aa;
12-
pub const nsCSSPropertyID_eCSSProperty_COUNT_DUMMY2: nsCSSPropertyID =
13-
nsCSSPropertyID::eCSSProperty_b;
1412
#[repr(u32)]
1513
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1614
pub enum nsCSSPropertyID {

libbindgen/tests/headers/constify-enum.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ enum nsCSSPropertyID {
44
eCSSProperty_b,
55

66
eCSSProperty_COUNT, /**< <div rustbindgen constant></div> */
7-
eCSSProperty_COUNT_DUMMY2 = eCSSProperty_COUNT - 1, /**< <div rustbindgen constant></div> */
7+
eCSSProperty_COUNT_DUMMY2 = eCSSProperty_COUNT - 1, /**< <div rustbindgen hide></div> */
88

99
eCSSPropertyAlias_aa,
1010
eCSSPropertyAlias_bb,

0 commit comments

Comments
 (0)