Skip to content

Commit f36f4e3

Browse files
author
bors-servo
authored
Auto merge of #1268 - emilio:enum-replace, r=fitzgen
ir: Allow replacing enums and enum variants. Fixes #1267.
2 parents f9fe3d1 + 4c6c997 commit f36f4e3

File tree

6 files changed

+86
-19
lines changed

6 files changed

+86
-19
lines changed

src/callbacks.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,23 @@ pub trait ParseCallbacks: fmt::Debug + UnwindSafe {
3636
}
3737

3838
/// This function should return whether, given the a given enum variant
39-
/// name, and value, returns whether this enum variant will forcibly be a
40-
/// constant.
39+
/// name, and value, this enum variant will forcibly be a constant.
4140
fn enum_variant_behavior(
4241
&self,
4342
_enum_name: Option<&str>,
44-
_variant_name: &str,
43+
_original_variant_name: &str,
4544
_variant_value: EnumVariantValue,
4645
) -> Option<EnumVariantCustomBehavior> {
4746
None
4847
}
48+
49+
/// Allows to rename an enum variant, replacing `_original_variant_name`.
50+
fn enum_variant_name(
51+
&self,
52+
_enum_name: Option<&str>,
53+
_original_variant_name: &str,
54+
_variant_value: EnumVariantValue,
55+
) -> Option<String> {
56+
None
57+
}
4958
}

src/codegen/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -2228,7 +2228,7 @@ impl<'a> EnumBuilder<'a> {
22282228
self,
22292229
ctx: &BindgenContext,
22302230
variant: &EnumVariant,
2231-
mangling_prefix: Option<&String>,
2231+
mangling_prefix: Option<&str>,
22322232
rust_ty: quote::Tokens,
22332233
result: &mut CodegenResult<'b>,
22342234
) -> Self {
@@ -2548,9 +2548,9 @@ impl CodeGenerator for Enum {
25482548

25492549
let constant_mangling_prefix = if ctx.options().prepend_enum_name {
25502550
if enum_ty.name().is_none() {
2551-
parent_canonical_name.as_ref().map(|n| &*n)
2551+
parent_canonical_name.as_ref().map(|n| &**n)
25522552
} else {
2553-
Some(&name)
2553+
Some(&*name)
25542554
}
25552555
} else {
25562556
None

src/ir/context.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,7 @@ impl BindgenContext {
10601060
match *ty.kind() {
10611061
TypeKind::Comp(..) |
10621062
TypeKind::TemplateAlias(..) |
1063+
TypeKind::Enum(..) |
10631064
TypeKind::Alias(..) => {}
10641065
_ => continue,
10651066
}
@@ -2060,7 +2061,7 @@ impl BindgenContext {
20602061

20612062
/// Has the item with the given `name` and `id` been replaced by another
20622063
/// type?
2063-
pub fn is_replaced_type<Id: Into<ItemId>>(&self, path: &[String], id: Id) -> bool {
2064+
fn is_replaced_type<Id: Into<ItemId>>(&self, path: &[String], id: Id) -> bool {
20642065
let id = id.into();
20652066
match self.replacements.get(path) {
20662067
Some(replaced_by) if *replaced_by != id => true,

src/ir/enum_ty.rs

+21-12
Original file line numberDiff line numberDiff line change
@@ -98,22 +98,31 @@ impl Enum {
9898
};
9999
if let Some(val) = value {
100100
let name = cursor.spelling();
101+
let annotations = Annotations::new(&cursor);
101102
let custom_behavior = ctx.parse_callbacks()
102-
.and_then(
103-
|t| t.enum_variant_behavior(type_name, &name, val),
104-
)
103+
.and_then(|callbacks| {
104+
callbacks.enum_variant_behavior(type_name, &name, val)
105+
})
105106
.or_else(|| {
106-
Annotations::new(&cursor).and_then(
107-
|anno| if anno.hide() {
108-
Some(EnumVariantCustomBehavior::Hide)
109-
} else if anno.constify_enum_variant() {
110-
Some(EnumVariantCustomBehavior::Constify)
111-
} else {
112-
None
113-
},
114-
)
107+
let annotations = annotations.as_ref()?;
108+
if annotations.hide() {
109+
Some(EnumVariantCustomBehavior::Hide)
110+
} else if annotations.constify_enum_variant() {
111+
Some(EnumVariantCustomBehavior::Constify)
112+
} else {
113+
None
114+
}
115115
});
116116

117+
let name = ctx.parse_callbacks()
118+
.and_then(|callbacks| {
119+
callbacks.enum_variant_name(type_name, &name, val)
120+
})
121+
.or_else(|| {
122+
annotations.as_ref()?.use_instead_of()?.last().cloned()
123+
})
124+
.unwrap_or(name);
125+
117126
let comment = cursor.raw_comment();
118127
variants.push(EnumVariant::new(
119128
name,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* automatically generated by rust-bindgen */
2+
3+
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)]
4+
5+
/// <div rustbindgen replaces="PASS"></div>
6+
///
7+
/// Should see PASS below.
8+
pub const OGRErr_PASS: OGRErr = 0;
9+
/// <div rustbindgen replaces="OGRERR_NONE"></div>
10+
///
11+
/// Should see OGRERR_NONE instead of CUSTOM_OGRERR_NONE below.
12+
pub const OGRErr_OGRERR_NONE: OGRErr = 1;
13+
/// <div rustbindgen replaces="OGRErr"></div>
14+
pub type OGRErr = u32;

tests/headers/enum-variant-replaces.h

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
/** Type for a OGR error */
3+
typedef enum
4+
{
5+
OGRERR_NONE, /**< Success */
6+
OGRERR_NOT_ENOUGH_DATA, /**< Not enough data to deserialize */
7+
OGRERR_NOT_ENOUGH_MEMORY, /**< Not enough memory */
8+
OGRERR_UNSUPPORTED_GEOMETRY_TYPE, /**< Unsupported geometry type */
9+
OGRERR_UNSUPPORTED_OPERATION, /**< Unsupported operation */
10+
OGRERR_CORRUPT_DATA, /**< Corrupt data */
11+
OGRERR_FAILURE, /**< Failure */
12+
OGRERR_UNSUPPORTED_SRS, /**< Unsupported SRS */
13+
OGRERR_INVALID_HANDLE, /**< Invalid handle */
14+
OGRERR_NON_EXISTING_FEATURE /**< Non existing feature. Added in GDAL 2.0 */
15+
} OGRErr;
16+
17+
/**
18+
* <div rustbindgen replaces="OGRErr"></div>
19+
*/
20+
typedef enum
21+
{
22+
/**
23+
* <div rustbindgen replaces="PASS"></div>
24+
*
25+
* Should see PASS below.
26+
*/
27+
FAIL,
28+
/**
29+
* <div rustbindgen replaces="OGRERR_NONE"></div>
30+
*
31+
* Should see OGRERR_NONE instead of CUSTOM_OGRERR_NONE below.
32+
*/
33+
CUSTOM_OGRERR_NONE
34+
} StrictOGRErr;

0 commit comments

Comments
 (0)