diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/callbacks.rs | 15 | ||||
-rw-r--r-- | src/codegen/mod.rs | 6 | ||||
-rw-r--r-- | src/ir/context.rs | 3 | ||||
-rw-r--r-- | src/ir/enum_ty.rs | 33 |
4 files changed, 38 insertions, 19 deletions
diff --git a/src/callbacks.rs b/src/callbacks.rs index 469314c4..f6935a55 100644 --- a/src/callbacks.rs +++ b/src/callbacks.rs @@ -36,14 +36,23 @@ pub trait ParseCallbacks: fmt::Debug + UnwindSafe { } /// This function should return whether, given the a given enum variant - /// name, and value, returns whether this enum variant will forcibly be a - /// constant. + /// name, and value, this enum variant will forcibly be a constant. fn enum_variant_behavior( &self, _enum_name: Option<&str>, - _variant_name: &str, + _original_variant_name: &str, _variant_value: EnumVariantValue, ) -> Option<EnumVariantCustomBehavior> { None } + + /// Allows to rename an enum variant, replacing `_original_variant_name`. + fn enum_variant_name( + &self, + _enum_name: Option<&str>, + _original_variant_name: &str, + _variant_value: EnumVariantValue, + ) -> Option<String> { + None + } } diff --git a/src/codegen/mod.rs b/src/codegen/mod.rs index b21c0dc4..1801520a 100644 --- a/src/codegen/mod.rs +++ b/src/codegen/mod.rs @@ -2228,7 +2228,7 @@ impl<'a> EnumBuilder<'a> { self, ctx: &BindgenContext, variant: &EnumVariant, - mangling_prefix: Option<&String>, + mangling_prefix: Option<&str>, rust_ty: quote::Tokens, result: &mut CodegenResult<'b>, ) -> Self { @@ -2548,9 +2548,9 @@ impl CodeGenerator for Enum { let constant_mangling_prefix = if ctx.options().prepend_enum_name { if enum_ty.name().is_none() { - parent_canonical_name.as_ref().map(|n| &*n) + parent_canonical_name.as_ref().map(|n| &**n) } else { - Some(&name) + Some(&*name) } } else { None diff --git a/src/ir/context.rs b/src/ir/context.rs index df8a84ef..b453378d 100644 --- a/src/ir/context.rs +++ b/src/ir/context.rs @@ -1060,6 +1060,7 @@ impl BindgenContext { match *ty.kind() { TypeKind::Comp(..) | TypeKind::TemplateAlias(..) | + TypeKind::Enum(..) | TypeKind::Alias(..) => {} _ => continue, } @@ -2060,7 +2061,7 @@ impl BindgenContext { /// Has the item with the given `name` and `id` been replaced by another /// type? - pub fn is_replaced_type<Id: Into<ItemId>>(&self, path: &[String], id: Id) -> bool { + fn is_replaced_type<Id: Into<ItemId>>(&self, path: &[String], id: Id) -> bool { let id = id.into(); match self.replacements.get(path) { Some(replaced_by) if *replaced_by != id => true, diff --git a/src/ir/enum_ty.rs b/src/ir/enum_ty.rs index bc8e37eb..4ae311e2 100644 --- a/src/ir/enum_ty.rs +++ b/src/ir/enum_ty.rs @@ -98,22 +98,31 @@ impl Enum { }; if let Some(val) = value { let name = cursor.spelling(); + let annotations = Annotations::new(&cursor); let custom_behavior = ctx.parse_callbacks() - .and_then( - |t| t.enum_variant_behavior(type_name, &name, val), - ) + .and_then(|callbacks| { + callbacks.enum_variant_behavior(type_name, &name, val) + }) .or_else(|| { - Annotations::new(&cursor).and_then( - |anno| if anno.hide() { - Some(EnumVariantCustomBehavior::Hide) - } else if anno.constify_enum_variant() { - Some(EnumVariantCustomBehavior::Constify) - } else { - None - }, - ) + let annotations = annotations.as_ref()?; + if annotations.hide() { + Some(EnumVariantCustomBehavior::Hide) + } else if annotations.constify_enum_variant() { + Some(EnumVariantCustomBehavior::Constify) + } else { + None + } }); + let name = ctx.parse_callbacks() + .and_then(|callbacks| { + callbacks.enum_variant_name(type_name, &name, val) + }) + .or_else(|| { + annotations.as_ref()?.use_instead_of()?.last().cloned() + }) + .unwrap_or(name); + let comment = cursor.raw_comment(); variants.push(EnumVariant::new( name, |