Skip to content

Add --newtype-global-enum option #2270

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

Merged
merged 1 commit into from
Sep 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 40 additions & 11 deletions src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2577,6 +2577,8 @@ pub enum EnumVariation {
NewType {
/// Indicates whether the newtype will have bitwise operators
is_bitfield: bool,
/// Indicates whether the variants will be represented as global constants
is_global: bool,
},
/// The code for this enum will use consts
Consts,
Expand Down Expand Up @@ -2614,16 +2616,26 @@ impl std::str::FromStr for EnumVariation {
"rust_non_exhaustive" => Ok(EnumVariation::Rust {
non_exhaustive: true,
}),
"bitfield" => Ok(EnumVariation::NewType { is_bitfield: true }),
"bitfield" => Ok(EnumVariation::NewType {
is_bitfield: true,
is_global: false,
}),
"consts" => Ok(EnumVariation::Consts),
"moduleconsts" => Ok(EnumVariation::ModuleConsts),
"newtype" => Ok(EnumVariation::NewType { is_bitfield: false }),
"newtype" => Ok(EnumVariation::NewType {
is_bitfield: false,
is_global: false,
}),
"newtype_global" => Ok(EnumVariation::NewType {
is_bitfield: false,
is_global: true,
}),
_ => Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
concat!(
"Got an invalid EnumVariation. Accepted values ",
"are 'rust', 'rust_non_exhaustive', 'bitfield', 'consts',",
"'moduleconsts', and 'newtype'."
"'moduleconsts', 'newtype' and 'newtype_global'."
),
)),
}
Expand All @@ -2644,6 +2656,7 @@ enum EnumBuilder<'a> {
canonical_name: &'a str,
tokens: proc_macro2::TokenStream,
is_bitfield: bool,
is_global: bool,
},
Consts {
variants: Vec<proc_macro2::TokenStream>,
Expand Down Expand Up @@ -2684,14 +2697,18 @@ impl<'a> EnumBuilder<'a> {
let ident = Ident::new(name, Span::call_site());

match enum_variation {
EnumVariation::NewType { is_bitfield } => EnumBuilder::NewType {
EnumVariation::NewType {
is_bitfield,
is_global,
} => EnumBuilder::NewType {
codegen_depth: enum_codegen_depth,
canonical_name: name,
tokens: quote! {
#( #attrs )*
pub struct #ident (pub #repr);
},
is_bitfield,
is_global,
},

EnumVariation::Rust { .. } => {
Expand Down Expand Up @@ -2792,17 +2809,29 @@ impl<'a> EnumBuilder<'a> {
}
}

EnumBuilder::NewType { canonical_name, .. } => {
EnumBuilder::NewType {
canonical_name,
is_global,
..
} => {
if ctx.options().rust_features().associated_const && is_ty_named
{
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 );
}
});
let tokens = quote! {
#doc
pub const #variant_ident : #rust_ty = #rust_ty ( #expr );
};

if is_global {
result.push(tokens);
} else {
result.push(quote! {
impl #enum_ident {
#tokens
}
});
}
} else {
let ident = ctx.rust_ident(match mangling_prefix {
Some(prefix) => {
Expand Down
19 changes: 17 additions & 2 deletions src/ir/enum_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,25 @@ impl Enum {
&ctx.options().bitfield_enums,
item,
) {
EnumVariation::NewType { is_bitfield: true }
EnumVariation::NewType {
is_bitfield: true,
is_global: false,
}
} else if self.is_matching_enum(ctx, &ctx.options().newtype_enums, item)
{
EnumVariation::NewType { is_bitfield: false }
EnumVariation::NewType {
is_bitfield: false,
is_global: false,
}
} else if self.is_matching_enum(
ctx,
&ctx.options().newtype_global_enums,
item,
) {
EnumVariation::NewType {
is_bitfield: false,
is_global: true,
}
} else if self.is_matching_enum(
ctx,
&ctx.options().rustified_enums,
Expand Down
34 changes: 29 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,11 +279,19 @@ impl Builder {
codegen::EnumVariation::Rust {
non_exhaustive: true,
} => "rust_non_exhaustive",
codegen::EnumVariation::NewType { is_bitfield: true } => {
"bitfield"
}
codegen::EnumVariation::NewType { is_bitfield: false } => {
"newtype"
codegen::EnumVariation::NewType {
is_bitfield: true,
..
} => "bitfield",
codegen::EnumVariation::NewType {
is_bitfield: false,
is_global,
} => {
if is_global {
"newtype_global"
} else {
"newtype"
}
}
codegen::EnumVariation::Consts => "consts",
codegen::EnumVariation::ModuleConsts => "moduleconsts",
Expand Down Expand Up @@ -984,6 +992,18 @@ impl Builder {
self
}

/// Mark the given enum (or set of enums, if using a pattern) as a newtype
/// whose variants are exposed as global constants.
///
/// Regular expressions are supported.
///
/// This makes bindgen generate a type that isn't a Rust `enum`. Regular
/// expressions are supported.
pub fn newtype_global_enum<T: AsRef<str>>(mut self, arg: T) -> Builder {
self.options.newtype_global_enums.insert(arg);
self
}

/// Mark the given enum (or set of enums, if using a pattern) as a Rust
/// enum.
///
Expand Down Expand Up @@ -1759,6 +1779,9 @@ struct BindgenOptions {
/// The enum patterns to mark an enum as a newtype.
newtype_enums: RegexSet,

/// The enum patterns to mark an enum as a global newtype.
newtype_global_enums: RegexSet,

/// The enum patterns to mark an enum as a Rust enum.
rustified_enums: RegexSet,

Expand Down Expand Up @@ -2097,6 +2120,7 @@ impl Default for BindgenOptions {
default_enum_style: Default::default(),
bitfield_enums: Default::default(),
newtype_enums: Default::default(),
newtype_global_enums: Default::default(),
rustified_enums: Default::default(),
rustified_non_exhaustive_enums: Default::default(),
constified_enums: Default::default(),
Expand Down
12 changes: 12 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ where
.value_name("regex")
.multiple_occurrences(true)
.number_of_values(1),
Arg::new("newtype-global-enum")
.long("newtype-global-enum")
.help("Mark any enum whose name matches <regex> as a global newtype.")
.value_name("regex")
.multiple_occurrences(true)
.number_of_values(1),
Arg::new("rustified-enum")
.long("rustified-enum")
.help("Mark any enum whose name matches <regex> as a Rust enum.")
Expand Down Expand Up @@ -573,6 +579,12 @@ where
}
}

if let Some(newtypes) = matches.values_of("newtype-global-enum") {
for regex in newtypes {
builder = builder.newtype_global_enum(regex);
}
}

if let Some(rustifieds) = matches.values_of("rustified-enum") {
for regex in rustifieds {
builder = builder.rustified_enum(regex);
Expand Down
12 changes: 12 additions & 0 deletions tests/expectations/tests/newtype-global-enum.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/headers/newtype-global-enum.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// bindgen-flags: --newtype-global-enum "Foo" --rust-target 1.28 -- -std=c++11

enum Foo {
Bar = 1 << 1,
Baz = 1 << 2,
Duplicated = 1 << 2,
Negative = -3,
};