-
Notifications
You must be signed in to change notification settings - Fork 746
optionally use associated constants in bitfields #1293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2126,7 +2126,7 @@ impl EnumVariation { | |
|
||
fn is_bitfield(&self) -> bool { | ||
match *self { | ||
EnumVariation::Bitfield => true, | ||
EnumVariation::Bitfield {..} => true, | ||
_ => false | ||
} | ||
} | ||
|
@@ -2278,19 +2278,28 @@ impl<'a> EnumBuilder<'a> { | |
} | ||
} | ||
|
||
EnumBuilder::Bitfield { .. } => { | ||
let constant_name = match mangling_prefix { | ||
Some(prefix) => { | ||
Cow::Owned(format!("{}_{}", prefix, variant_name)) | ||
} | ||
None => variant_name, | ||
}; | ||
|
||
let ident = ctx.rust_ident(constant_name); | ||
result.push(quote! { | ||
#doc | ||
pub const #ident : #rust_ty = #rust_ty ( #expr ); | ||
}); | ||
EnumBuilder::Bitfield { canonical_name, .. } => { | ||
if ctx.options().rust_features().associated_const { | ||
let enum_ident = ctx.rust_ident(canonical_name); | ||
let variant_ident = ctx.rust_ident(variant_name); | ||
result.push(quote! { | ||
impl #enum_ident { | ||
#doc | ||
pub const #variant_ident : #rust_ty = #rust_ty ( #expr ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is probably even not worth the option, and just worth guarding it after a feature guard, wdyt? see |
||
} | ||
}); | ||
} else { | ||
let ident = ctx.rust_ident(match mangling_prefix { | ||
Some(prefix) => { | ||
Cow::Owned(format!("{}_{}", prefix, variant_name)) | ||
} | ||
None => variant_name, | ||
}); | ||
result.push(quote! { | ||
#doc | ||
pub const #ident : #rust_ty = #rust_ty ( #expr ); | ||
}); | ||
} | ||
|
||
self | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,18 @@ | |
|
||
#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] | ||
|
||
pub const Foo_Bar: Foo = Foo(2); | ||
pub const Foo_Baz: Foo = Foo(4); | ||
pub const Foo_Duplicated: Foo = Foo(4); | ||
pub const Foo_Negative: Foo = Foo(-3); | ||
impl Foo { | ||
pub const Bar: Foo = Foo(2); | ||
} | ||
impl Foo { | ||
pub const Baz: Foo = Foo(4); | ||
} | ||
impl Foo { | ||
pub const Duplicated: Foo = Foo(4); | ||
} | ||
impl Foo { | ||
pub const Negative: Foo = Foo(-3); | ||
} | ||
impl ::std::ops::BitOr<Foo> for Foo { | ||
type Output = Self; | ||
#[inline] | ||
|
@@ -35,10 +43,18 @@ impl ::std::ops::BitAndAssign for Foo { | |
#[repr(C)] | ||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] | ||
pub struct Foo(pub i32); | ||
pub const Buz_Bar: Buz = Buz(2); | ||
pub const Buz_Baz: Buz = Buz(4); | ||
pub const Buz_Duplicated: Buz = Buz(4); | ||
pub const Buz_Negative: Buz = Buz(-3); | ||
impl Buz { | ||
pub const Bar: Buz = Buz(2); | ||
} | ||
impl Buz { | ||
pub const Baz: Buz = Buz(4); | ||
} | ||
impl Buz { | ||
pub const Duplicated: Buz = Buz(4); | ||
} | ||
impl Buz { | ||
pub const Negative: Buz = Buz(-3); | ||
} | ||
impl ::std::ops::BitOr<Buz> for Buz { | ||
type Output = Self; | ||
#[inline] | ||
|
@@ -68,8 +84,12 @@ impl ::std::ops::BitAndAssign for Buz { | |
#[repr(C)] | ||
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] | ||
pub struct Buz(pub i8); | ||
pub const NS_FOO: _bindgen_ty_1 = _bindgen_ty_1(1); | ||
pub const NS_BAR: _bindgen_ty_1 = _bindgen_ty_1(2); | ||
impl _bindgen_ty_1 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I didn't think about unnamed enums, but this is a problem... Because the constants are no longer in the top-level namespace, and their name is not deterministic. |
||
pub const NS_FOO: _bindgen_ty_1 = _bindgen_ty_1(1); | ||
} | ||
impl _bindgen_ty_1 { | ||
pub const NS_BAR: _bindgen_ty_1 = _bindgen_ty_1(2); | ||
} | ||
impl ::std::ops::BitOr<_bindgen_ty_1> for _bindgen_ty_1 { | ||
type Output = Self; | ||
#[inline] | ||
|
@@ -104,8 +124,12 @@ pub struct _bindgen_ty_1(pub u32); | |
pub struct Dummy { | ||
pub _address: u8, | ||
} | ||
pub const Dummy_DUMMY_FOO: Dummy__bindgen_ty_1 = Dummy__bindgen_ty_1(1); | ||
pub const Dummy_DUMMY_BAR: Dummy__bindgen_ty_1 = Dummy__bindgen_ty_1(2); | ||
impl Dummy__bindgen_ty_1 { | ||
pub const DUMMY_FOO: Dummy__bindgen_ty_1 = Dummy__bindgen_ty_1(1); | ||
} | ||
impl Dummy__bindgen_ty_1 { | ||
pub const DUMMY_BAR: Dummy__bindgen_ty_1 = Dummy__bindgen_ty_1(2); | ||
} | ||
impl ::std::ops::BitOr<Dummy__bindgen_ty_1> for Dummy__bindgen_ty_1 { | ||
type Output = Self; | ||
#[inline] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we need to guard this condition to prevent entering here if the enum is unnamed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How can we check? I see
"_bindgen_ty_{}"
inItem::base_name
but am not sure how this information is propagated further. (I'm not sure whether we care about cases where the user actually defines a type called_bindgen_ty_1
or such.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnamed enums can be detected using
ir::Type::name
, in which case thename would be
None`.