From 52851c76c336b9365eff80c2c7478914d1ab0999 Mon Sep 17 00:00:00 2001 From: John Baublitz Date: Tue, 6 Aug 2024 15:29:26 -0400 Subject: [PATCH] Add option to generate safe and unsafe conversions for rustified enums --- bindgen-cli/options.rs | 38 +- bindgen-integration/build.rs | 2 + .../tests/expectations/tests/anon_enum.rs | 5 + .../expectations/tests/anon_enum_allowlist.rs | 3 + .../expectations/tests/anon_enum_trait.rs | 4 + .../tests/expectations/tests/anon_union.rs | 2 + .../expectations/tests/anon_union_1_0.rs | 2 + .../expectations/tests/bitfield_align_2.rs | 5 + .../tests/class_with_inner_struct.rs | 5 + .../tests/class_with_inner_struct_1_0.rs | 5 + .../expectations/tests/const_enum_unnamed.rs | 5 + .../expectations/tests/constant-evaluate.rs | 3 + .../tests/expectations/tests/constify-enum.rs | 7 + .../expectations/tests/derive-custom-cli.rs | 2 + .../tests/dupe-enum-variant-in-namespace.rs | 3 + .../tests/expectations/tests/empty-enum.rs | 3 + .../expectations/tests/enum-default-rust.rs | 12 + .../expectations/tests/enum-doc-rusty.rs | 16 + .../expectations/tests/enum-no-debug-rust.rs | 12 + .../expectations/tests/enum-undefault.rs | 3 + .../tests/expectations/tests/enum_alias.rs | 2 + .../tests/enum_and_vtable_mangling.rs | 3 + .../tests/expectations/tests/enum_dupe.rs | 2 + .../expectations/tests/enum_explicit_type.rs | 22 + .../tests/enum_in_template_with_typedef.rs | 2 + .../tests/expectations/tests/enum_negative.rs | 3 + .../tests/expectations/tests/enum_packed.rs | 9 + .../expectations/tests/forward-enum-decl.rs | 3 + .../expectations/tests/func_ptr_in_struct.rs | 1 + .../tests/issue-1198-alias-rust-enum.rs | 6 + .../tests/issue-1488-enum-new-type.rs | 3 + .../tests/expectations/tests/issue-1554.rs | 3 + .../tests/expectations/tests/issue-2646.rs | 101 +++++ .../tests/expectations/tests/issue-372.rs | 13 + .../tests/expectations/tests/issue-410.rs | 1 + .../tests/expectations/tests/issue-493.rs | 4 + .../tests/expectations/tests/issue-493_1_0.rs | 4 + ...ate-params-causing-layout-test-failures.rs | 3 + .../tests/issue-888-enum-var-decl-jump.rs | 1 + .../expectations/tests/jsval_layout_opaque.rs | 71 +++ .../tests/jsval_layout_opaque_1_0.rs | 71 +++ .../tests/layout_array_too_long.rs | 8 + .../tests/layout_cmdline_token.rs | 9 + .../expectations/tests/layout_eth_conf.rs | 72 +++ .../expectations/tests/layout_eth_conf_1_0.rs | 72 +++ .../tests/layout_large_align_field.rs | 8 + .../tests/libclang-9/struct_typedef_ns.rs | 4 + .../expectations/tests/nsStyleAutoArray.rs | 2 + .../tests/expectations/tests/ord-enum.rs | 10 + .../expectations/tests/overflowed_enum.rs | 7 + .../tests/prepend-enum-constified-variant.rs | 2 + .../tests/expectations/tests/short-enums.rs | 6 + .../expectations/tests/struct_typedef.rs | 4 + .../expectations/tests/struct_typedef_ns.rs | 4 + .../expectations/tests/weird_bitfields.rs | 4 + bindgen-tests/tests/headers/issue-2646.h | 30 ++ bindgen/codegen/mod.rs | 419 +++++++++++++----- bindgen/ir/enum_ty.rs | 102 ++++- bindgen/lib.rs | 17 +- bindgen/options/mod.rs | 17 +- 60 files changed, 1116 insertions(+), 146 deletions(-) create mode 100644 bindgen-tests/tests/expectations/tests/issue-2646.rs create mode 100644 bindgen-tests/tests/headers/issue-2646.h diff --git a/bindgen-cli/options.rs b/bindgen-cli/options.rs index cd5e9bb127..38d891293f 100644 --- a/bindgen-cli/options.rs +++ b/bindgen-cli/options.rs @@ -2,7 +2,8 @@ use bindgen::callbacks::TypeKind; use bindgen::{ builder, Abi, AliasVariation, Builder, CodegenConfig, EnumVariation, FieldVisibilityKind, Formatter, MacroTypeVariation, NonCopyUnionStyle, - RegexSet, RustTarget, DEFAULT_ANON_FIELDS_PREFIX, RUST_TARGET_STRINGS, + RegexSet, RustEnumOptions, RustTarget, DEFAULT_ANON_FIELDS_PREFIX, + RUST_TARGET_STRINGS, }; use clap::error::{Error, ErrorKind}; use clap::{CommandFactory, Parser}; @@ -75,6 +76,21 @@ fn parse_abi_override(abi_override: &str) -> Result<(Abi, String), Error> { Ok((abi, regex.to_owned())) } +fn parse_rustified_enum( + rustified_enum: &str, +) -> Result<(RustEnumOptions, String), Error> { + let (regex, options) = match rustified_enum.rsplit_once('=') { + Some((regex, options)) => (regex, options), + None => (rustified_enum, ""), + }; + + let options = options + .parse() + .map_err(|err| Error::raw(ErrorKind::InvalidValue, err))?; + + Ok((options, regex.to_owned())) +} + fn parse_custom_derive( custom_derive: &str, ) -> Result<(Vec, String), Error> { @@ -111,12 +127,11 @@ struct BindgenCommand { /// Mark any enum whose name matches REGEX as a global newtype. #[arg(long, value_name = "REGEX")] newtype_global_enum: Vec, - /// Mark any enum whose name matches REGEX as a Rust enum. - #[arg(long, value_name = "REGEX")] - rustified_enum: Vec, - /// Mark any enum whose name matches REGEX as a non-exhaustive Rust enum. - #[arg(long, value_name = "REGEX")] - rustified_non_exhaustive_enum: Vec, + /// Mark any enum whose name matches the provided regex as a Rust enum. This parameter takes + /// options in the shape REGEX or REGEX=OPTIONS where OPTIONS can be a comma separated list of + /// options from non_exhaustive, try_from_raw, and from_raw_unchecked. + #[arg(long, value_parser = parse_rustified_enum)] + rustified_enum: Vec<(RustEnumOptions, String)>, /// Mark any enum whose name matches REGEX as a series of constants. #[arg(long, value_name = "REGEX")] constified_enum: Vec, @@ -472,7 +487,6 @@ where newtype_enum, newtype_global_enum, rustified_enum, - rustified_non_exhaustive_enum, constified_enum, constified_enum_module, default_macro_constant_type, @@ -635,12 +649,8 @@ where builder = builder.newtype_global_enum(regex); } - for regex in rustified_enum { - builder = builder.rustified_enum(regex); - } - - for regex in rustified_non_exhaustive_enum { - builder = builder.rustified_non_exhaustive_enum(regex); + for (options, regex) in rustified_enum { + builder = builder.rustified_enum(options, regex); } for regex in constified_enum { diff --git a/bindgen-integration/build.rs b/bindgen-integration/build.rs index 6b06c91bc3..c90411e43f 100644 --- a/bindgen-integration/build.rs +++ b/bindgen-integration/build.rs @@ -180,6 +180,8 @@ fn setup_macro_test() { .enable_cxx_namespaces() .default_enum_style(EnumVariation::Rust { non_exhaustive: false, + safe_conversion: false, + unsafe_conversion: false, }) .raw_line("pub use self::root::*;") .raw_line("extern { fn my_prefixed_function_to_remove(i: i32); }") diff --git a/bindgen-tests/tests/expectations/tests/anon_enum.rs b/bindgen-tests/tests/expectations/tests/anon_enum.rs index c3790a2a24..209a0a1a11 100644 --- a/bindgen-tests/tests/expectations/tests/anon_enum.rs +++ b/bindgen-tests/tests/expectations/tests/anon_enum.rs @@ -6,6 +6,8 @@ pub struct Test { pub bar: f32, } pub const Test_T_NONE: Test__bindgen_ty_1 = Test__bindgen_ty_1::T_NONE; +pub type Test__bindgen_ty_1_ctype = ::std::os::raw::c_uint; +pub const Test__bindgen_ty_1_T_NONE: Test__bindgen_ty_1_ctype = 0; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum Test__bindgen_ty_1 { @@ -18,6 +20,9 @@ const _: () = { ["Offset of field: Test::foo"][::std::mem::offset_of!(Test, foo) - 0usize]; ["Offset of field: Test::bar"][::std::mem::offset_of!(Test, bar) - 4usize]; }; +pub type Baz_ctype = ::std::os::raw::c_uint; +pub const Baz_Foo: Baz_ctype = 0; +pub const Baz_Bar: Baz_ctype = 1; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum Baz { diff --git a/bindgen-tests/tests/expectations/tests/anon_enum_allowlist.rs b/bindgen-tests/tests/expectations/tests/anon_enum_allowlist.rs index 4b172d7aeb..63b8d01255 100644 --- a/bindgen-tests/tests/expectations/tests/anon_enum_allowlist.rs +++ b/bindgen-tests/tests/expectations/tests/anon_enum_allowlist.rs @@ -1,6 +1,9 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] pub const NODE_FLAG_FOO: _bindgen_ty_1 = _bindgen_ty_1::NODE_FLAG_FOO; pub const NODE_FLAG_BAR: _bindgen_ty_1 = _bindgen_ty_1::NODE_FLAG_BAR; +pub type _bindgen_ty_1_ctype = ::std::os::raw::c_uint; +pub const _bindgen_ty_1_NODE_FLAG_FOO: _bindgen_ty_1_ctype = 0; +pub const _bindgen_ty_1_NODE_FLAG_BAR: _bindgen_ty_1_ctype = 1; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum _bindgen_ty_1 { diff --git a/bindgen-tests/tests/expectations/tests/anon_enum_trait.rs b/bindgen-tests/tests/expectations/tests/anon_enum_trait.rs index cfd4d03200..312e200901 100644 --- a/bindgen-tests/tests/expectations/tests/anon_enum_trait.rs +++ b/bindgen-tests/tests/expectations/tests/anon_enum_trait.rs @@ -13,6 +13,8 @@ pub const DataType_depth: DataType__bindgen_ty_1 = DataType__bindgen_ty_1::gener pub const DataType_channels: DataType__bindgen_ty_1 = DataType__bindgen_ty_1::generic_type; pub const DataType_fmt: DataType__bindgen_ty_1 = DataType__bindgen_ty_1::generic_type; pub const DataType_type_: DataType__bindgen_ty_1 = DataType__bindgen_ty_1::generic_type; +pub type DataType__bindgen_ty_1_ctype = i32; +pub const DataType__bindgen_ty_1_generic_type: DataType__bindgen_ty_1_ctype = 0; #[repr(i32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum DataType__bindgen_ty_1 { @@ -25,6 +27,8 @@ pub struct Foo { } pub const Foo_Bar: Foo__bindgen_ty_1 = Foo__bindgen_ty_1::Bar; pub const Foo_Baz: Foo__bindgen_ty_1 = Foo__bindgen_ty_1::Bar; +pub type Foo__bindgen_ty_1_ctype = ::std::os::raw::c_uint; +pub const Foo__bindgen_ty_1_Bar: Foo__bindgen_ty_1_ctype = 0; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum Foo__bindgen_ty_1 { diff --git a/bindgen-tests/tests/expectations/tests/anon_union.rs b/bindgen-tests/tests/expectations/tests/anon_union.rs index d9bf3cf183..4c9947e0c3 100644 --- a/bindgen-tests/tests/expectations/tests/anon_union.rs +++ b/bindgen-tests/tests/expectations/tests/anon_union.rs @@ -9,6 +9,8 @@ pub struct TErrorResult { impl TErrorResult_UnionState { pub const HasException: TErrorResult_UnionState = TErrorResult_UnionState::HasMessage; } +pub type TErrorResult_UnionState_ctype = i32; +pub const TErrorResult_UnionState_HasMessage: TErrorResult_UnionState_ctype = 0; #[repr(i32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum TErrorResult_UnionState { diff --git a/bindgen-tests/tests/expectations/tests/anon_union_1_0.rs b/bindgen-tests/tests/expectations/tests/anon_union_1_0.rs index 29b13d010d..f223fa893b 100644 --- a/bindgen-tests/tests/expectations/tests/anon_union_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/anon_union_1_0.rs @@ -51,6 +51,8 @@ pub struct TErrorResult { pub mUnionState: TErrorResult_UnionState, } pub const TErrorResult_UnionState_HasException: TErrorResult_UnionState = TErrorResult_UnionState::HasMessage; +pub type TErrorResult_UnionState_ctype = i32; +pub const TErrorResult_UnionState_HasMessage: TErrorResult_UnionState_ctype = 0; #[repr(i32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum TErrorResult_UnionState { diff --git a/bindgen-tests/tests/expectations/tests/bitfield_align_2.rs b/bindgen-tests/tests/expectations/tests/bitfield_align_2.rs index b87af0c99c..1465c596a3 100644 --- a/bindgen-tests/tests/expectations/tests/bitfield_align_2.rs +++ b/bindgen-tests/tests/expectations/tests/bitfield_align_2.rs @@ -84,6 +84,11 @@ where } } } +pub type MyEnum_ctype = ::std::os::raw::c_uint; +pub const MyEnum_ONE: MyEnum_ctype = 0; +pub const MyEnum_TWO: MyEnum_ctype = 1; +pub const MyEnum_THREE: MyEnum_ctype = 2; +pub const MyEnum_FOUR: MyEnum_ctype = 3; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum MyEnum { diff --git a/bindgen-tests/tests/expectations/tests/class_with_inner_struct.rs b/bindgen-tests/tests/expectations/tests/class_with_inner_struct.rs index 710026c72f..4436b64803 100644 --- a/bindgen-tests/tests/expectations/tests/class_with_inner_struct.rs +++ b/bindgen-tests/tests/expectations/tests/class_with_inner_struct.rs @@ -107,6 +107,11 @@ const _: () = { ["Alignment of B"][::std::mem::align_of::() - 4usize]; ["Offset of field: B::d"][::std::mem::offset_of!(B, d) - 0usize]; }; +pub type StepSyntax_ctype = ::std::os::raw::c_int; +pub const StepSyntax_Keyword: StepSyntax_ctype = 0; +pub const StepSyntax_FunctionalWithoutKeyword: StepSyntax_ctype = 1; +pub const StepSyntax_FunctionalWithStartKeyword: StepSyntax_ctype = 2; +pub const StepSyntax_FunctionalWithEndKeyword: StepSyntax_ctype = 3; #[repr(i32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum StepSyntax { diff --git a/bindgen-tests/tests/expectations/tests/class_with_inner_struct_1_0.rs b/bindgen-tests/tests/expectations/tests/class_with_inner_struct_1_0.rs index 23afad6319..aff7ace03f 100644 --- a/bindgen-tests/tests/expectations/tests/class_with_inner_struct_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/class_with_inner_struct_1_0.rs @@ -211,6 +211,11 @@ impl Clone for B { *self } } +pub type StepSyntax_ctype = ::std::os::raw::c_int; +pub const StepSyntax_Keyword: StepSyntax_ctype = 0; +pub const StepSyntax_FunctionalWithoutKeyword: StepSyntax_ctype = 1; +pub const StepSyntax_FunctionalWithStartKeyword: StepSyntax_ctype = 2; +pub const StepSyntax_FunctionalWithEndKeyword: StepSyntax_ctype = 3; #[repr(i32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum StepSyntax { diff --git a/bindgen-tests/tests/expectations/tests/const_enum_unnamed.rs b/bindgen-tests/tests/expectations/tests/const_enum_unnamed.rs index f49d825224..bdb814d727 100644 --- a/bindgen-tests/tests/expectations/tests/const_enum_unnamed.rs +++ b/bindgen-tests/tests/expectations/tests/const_enum_unnamed.rs @@ -1,6 +1,9 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] pub const FOO_BAR: _bindgen_ty_1 = _bindgen_ty_1::FOO_BAR; pub const FOO_BAZ: _bindgen_ty_1 = _bindgen_ty_1::FOO_BAZ; +pub type _bindgen_ty_1_ctype = ::std::os::raw::c_uint; +pub const _bindgen_ty_1_FOO_BAR: _bindgen_ty_1_ctype = 0; +pub const _bindgen_ty_1_FOO_BAZ: _bindgen_ty_1_ctype = 1; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum _bindgen_ty_1 { @@ -13,6 +16,8 @@ pub struct Foo { pub _address: u8, } pub const Foo_FOO_BAR: Foo__bindgen_ty_1 = Foo__bindgen_ty_1::FOO_BAR; +pub type Foo__bindgen_ty_1_ctype = ::std::os::raw::c_uint; +pub const Foo__bindgen_ty_1_FOO_BAR: Foo__bindgen_ty_1_ctype = 10; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum Foo__bindgen_ty_1 { diff --git a/bindgen-tests/tests/expectations/tests/constant-evaluate.rs b/bindgen-tests/tests/expectations/tests/constant-evaluate.rs index bbcf6d5450..69f4a08b76 100644 --- a/bindgen-tests/tests/expectations/tests/constant-evaluate.rs +++ b/bindgen-tests/tests/expectations/tests/constant-evaluate.rs @@ -1,6 +1,9 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] pub const foo: _bindgen_ty_1 = _bindgen_ty_1::foo; pub const bar: _bindgen_ty_1 = _bindgen_ty_1::bar; +pub type _bindgen_ty_1_ctype = ::std::os::raw::c_uint; +pub const _bindgen_ty_1_foo: _bindgen_ty_1_ctype = 4; +pub const _bindgen_ty_1_bar: _bindgen_ty_1_ctype = 8; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum _bindgen_ty_1 { diff --git a/bindgen-tests/tests/expectations/tests/constify-enum.rs b/bindgen-tests/tests/expectations/tests/constify-enum.rs index 67d2749c51..8ba7ba0146 100644 --- a/bindgen-tests/tests/expectations/tests/constify-enum.rs +++ b/bindgen-tests/tests/expectations/tests/constify-enum.rs @@ -3,6 +3,13 @@ pub const nsCSSPropertyID_eCSSProperty_COUNT_unexistingVariantValue: nsCSSProper impl nsCSSPropertyID { pub const eCSSProperty_COUNT: nsCSSPropertyID = nsCSSPropertyID::eCSSPropertyAlias_aa; } +pub type nsCSSPropertyID_ctype = ::std::os::raw::c_uint; +pub const nsCSSPropertyID_eCSSProperty_a: nsCSSPropertyID_ctype = 0; +pub const nsCSSPropertyID_eCSSProperty_b: nsCSSPropertyID_ctype = 1; +pub const nsCSSPropertyID_eCSSPropertyAlias_aa: nsCSSPropertyID_ctype = 2; +pub const nsCSSPropertyID_eCSSPropertyAlias_bb: nsCSSPropertyID_ctype = 3; +///<
+pub const nsCSSPropertyID_eCSSProperty_COUNT_unexistingVariantValue: nsCSSPropertyID_ctype = 4; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum nsCSSPropertyID { diff --git a/bindgen-tests/tests/expectations/tests/derive-custom-cli.rs b/bindgen-tests/tests/expectations/tests/derive-custom-cli.rs index 59a3a76571..591d4ecd23 100644 --- a/bindgen-tests/tests/expectations/tests/derive-custom-cli.rs +++ b/bindgen-tests/tests/expectations/tests/derive-custom-cli.rs @@ -12,6 +12,8 @@ const _: () = { "Offset of field: foo_struct::inner", ][::std::mem::offset_of!(foo_struct, inner) - 0usize]; }; +pub type foo_enum_ctype = ::std::os::raw::c_uint; +pub const foo_enum_inner: foo_enum_ctype = 0; #[repr(u32)] #[derive(Clone, Hash, PartialEq, Eq, Copy)] pub enum foo_enum { diff --git a/bindgen-tests/tests/expectations/tests/dupe-enum-variant-in-namespace.rs b/bindgen-tests/tests/expectations/tests/dupe-enum-variant-in-namespace.rs index d521bcc17b..7448ba4b43 100644 --- a/bindgen-tests/tests/expectations/tests/dupe-enum-variant-in-namespace.rs +++ b/bindgen-tests/tests/expectations/tests/dupe-enum-variant-in-namespace.rs @@ -12,6 +12,9 @@ pub mod root { impl root::foo::Bar { pub const Foo3: root::foo::Bar = Bar::Foo2; } + pub type Bar_ctype = ::std::os::raw::c_uint; + pub const Bar_Foo: Bar_ctype = 0; + pub const Bar_Foo2: Bar_ctype = 1; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum Bar { diff --git a/bindgen-tests/tests/expectations/tests/empty-enum.rs b/bindgen-tests/tests/expectations/tests/empty-enum.rs index fda6f581df..4af61e09f2 100644 --- a/bindgen-tests/tests/expectations/tests/empty-enum.rs +++ b/bindgen-tests/tests/expectations/tests/empty-enum.rs @@ -1,5 +1,6 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] pub type EmptyConstified = ::std::os::raw::c_uint; +pub type EmptyRustified_ctype = ::std::os::raw::c_uint; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum EmptyRustified { @@ -8,6 +9,7 @@ pub enum EmptyRustified { pub mod EmptyModule { pub type Type = ::std::os::raw::c_uint; } +pub type EmptyClassRustified_ctype = ::std::os::raw::c_char; #[repr(i8)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum EmptyClassRustified { @@ -17,6 +19,7 @@ pub type EmptyClassConstified = ::std::os::raw::c_char; pub mod EmptyClassModule { pub type Type = ::std::os::raw::c_char; } +pub type ForwardClassRustified_ctype = ::std::os::raw::c_char; #[repr(i8)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum ForwardClassRustified { diff --git a/bindgen-tests/tests/expectations/tests/enum-default-rust.rs b/bindgen-tests/tests/expectations/tests/enum-default-rust.rs index f9a99166de..80f297fd2e 100644 --- a/bindgen-tests/tests/expectations/tests/enum-default-rust.rs +++ b/bindgen-tests/tests/expectations/tests/enum-default-rust.rs @@ -6,6 +6,9 @@ pub struct foo { } pub const foo_FOO_A: foo__bindgen_ty_1 = foo__bindgen_ty_1::FOO_A; pub const foo_FOO_B: foo__bindgen_ty_1 = foo__bindgen_ty_1::FOO_B; +pub type foo__bindgen_ty_1_ctype = ::std::os::raw::c_uint; +pub const foo__bindgen_ty_1_FOO_A: foo__bindgen_ty_1_ctype = 0; +pub const foo__bindgen_ty_1_FOO_B: foo__bindgen_ty_1_ctype = 1; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum foo__bindgen_ty_1 { @@ -27,6 +30,9 @@ impl Default for foo { } } } +pub type Foo_ctype = ::std::os::raw::c_uint; +pub const Foo_Bar: Foo_ctype = 0; +pub const Foo_Qux: Foo_ctype = 1; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum Foo { @@ -38,6 +44,9 @@ pub mod Neg { pub const MinusOne: Type = -1; pub const One: Type = 1; } +pub type NoDebug_ctype = ::std::os::raw::c_uint; +pub const NoDebug_NoDebug1: NoDebug_ctype = 0; +pub const NoDebug_NoDebug2: NoDebug_ctype = 1; #[repr(u32)] ///
#[derive(Copy, Clone, Hash, PartialEq, Eq)] @@ -45,6 +54,9 @@ pub enum NoDebug { NoDebug1 = 0, NoDebug2 = 1, } +pub type Debug_ctype = ::std::os::raw::c_uint; +pub const Debug_Debug1: Debug_ctype = 0; +pub const Debug_Debug2: Debug_ctype = 1; #[repr(u32)] ///
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] diff --git a/bindgen-tests/tests/expectations/tests/enum-doc-rusty.rs b/bindgen-tests/tests/expectations/tests/enum-doc-rusty.rs index 3eec0759c5..21435b0bd7 100644 --- a/bindgen-tests/tests/expectations/tests/enum-doc-rusty.rs +++ b/bindgen-tests/tests/expectations/tests/enum-doc-rusty.rs @@ -1,4 +1,20 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] +pub type B_ctype = ::std::os::raw::c_uint; +/// Document field with three slashes +pub const B_VAR_A: B_ctype = 0; +/// Document field with preceding star +pub const B_VAR_B: B_ctype = 1; +/// Document field with preceding exclamation +pub const B_VAR_C: B_ctype = 2; +///< Document field with following star +pub const B_VAR_D: B_ctype = 3; +///< Document field with following exclamation +pub const B_VAR_E: B_ctype = 4; +/** Document field with preceding star, with a loong long multiline + comment. + + Very interesting documentation, definitely.*/ +pub const B_VAR_F: B_ctype = 5; #[repr(u32)] /// Document enum #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] diff --git a/bindgen-tests/tests/expectations/tests/enum-no-debug-rust.rs b/bindgen-tests/tests/expectations/tests/enum-no-debug-rust.rs index b728dfc898..40fb1a8a63 100644 --- a/bindgen-tests/tests/expectations/tests/enum-no-debug-rust.rs +++ b/bindgen-tests/tests/expectations/tests/enum-no-debug-rust.rs @@ -6,6 +6,9 @@ pub struct foo { } pub const foo_FOO_A: foo__bindgen_ty_1 = foo__bindgen_ty_1::FOO_A; pub const foo_FOO_B: foo__bindgen_ty_1 = foo__bindgen_ty_1::FOO_B; +pub type foo__bindgen_ty_1_ctype = ::std::os::raw::c_uint; +pub const foo__bindgen_ty_1_FOO_A: foo__bindgen_ty_1_ctype = 0; +pub const foo__bindgen_ty_1_FOO_B: foo__bindgen_ty_1_ctype = 1; #[repr(u32)] #[derive(Copy, Clone, Hash, PartialEq, Eq)] pub enum foo__bindgen_ty_1 { @@ -27,6 +30,9 @@ impl Default for foo { } } } +pub type Foo_ctype = ::std::os::raw::c_uint; +pub const Foo_Bar: Foo_ctype = 0; +pub const Foo_Qux: Foo_ctype = 1; #[repr(u32)] #[derive(Copy, Clone, Hash, PartialEq, Eq)] pub enum Foo { @@ -38,6 +44,9 @@ pub mod Neg { pub const MinusOne: Type = -1; pub const One: Type = 1; } +pub type NoDebug_ctype = ::std::os::raw::c_uint; +pub const NoDebug_NoDebug1: NoDebug_ctype = 0; +pub const NoDebug_NoDebug2: NoDebug_ctype = 1; #[repr(u32)] ///
#[derive(Copy, Clone, Hash, PartialEq, Eq)] @@ -45,6 +54,9 @@ pub enum NoDebug { NoDebug1 = 0, NoDebug2 = 1, } +pub type Debug_ctype = ::std::os::raw::c_uint; +pub const Debug_Debug1: Debug_ctype = 0; +pub const Debug_Debug2: Debug_ctype = 1; #[repr(u32)] ///
#[derive(Copy, Clone, Hash, PartialEq, Eq, Debug)] diff --git a/bindgen-tests/tests/expectations/tests/enum-undefault.rs b/bindgen-tests/tests/expectations/tests/enum-undefault.rs index 01a7aa3cea..953a13790f 100644 --- a/bindgen-tests/tests/expectations/tests/enum-undefault.rs +++ b/bindgen-tests/tests/expectations/tests/enum-undefault.rs @@ -1,4 +1,7 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] +pub type Foo_ctype = ::std::os::raw::c_uint; +pub const Foo_Bar: Foo_ctype = 0; +pub const Foo_Qux: Foo_ctype = 1; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum Foo { diff --git a/bindgen-tests/tests/expectations/tests/enum_alias.rs b/bindgen-tests/tests/expectations/tests/enum_alias.rs index 0d29768837..ba516dec67 100644 --- a/bindgen-tests/tests/expectations/tests/enum_alias.rs +++ b/bindgen-tests/tests/expectations/tests/enum_alias.rs @@ -1,4 +1,6 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] +pub type Bar_ctype = u8; +pub const Bar_VAL: Bar_ctype = 0; #[repr(u8)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum Bar { diff --git a/bindgen-tests/tests/expectations/tests/enum_and_vtable_mangling.rs b/bindgen-tests/tests/expectations/tests/enum_and_vtable_mangling.rs index 18e1ad8e36..736f633c67 100644 --- a/bindgen-tests/tests/expectations/tests/enum_and_vtable_mangling.rs +++ b/bindgen-tests/tests/expectations/tests/enum_and_vtable_mangling.rs @@ -1,6 +1,9 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] pub const match_: _bindgen_ty_1 = _bindgen_ty_1::match_; pub const whatever_else: _bindgen_ty_1 = _bindgen_ty_1::whatever_else; +pub type _bindgen_ty_1_ctype = ::std::os::raw::c_uint; +pub const _bindgen_ty_1_match_: _bindgen_ty_1_ctype = 0; +pub const _bindgen_ty_1_whatever_else: _bindgen_ty_1_ctype = 1; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum _bindgen_ty_1 { diff --git a/bindgen-tests/tests/expectations/tests/enum_dupe.rs b/bindgen-tests/tests/expectations/tests/enum_dupe.rs index 3ee4c68cbd..8c02f30ec5 100644 --- a/bindgen-tests/tests/expectations/tests/enum_dupe.rs +++ b/bindgen-tests/tests/expectations/tests/enum_dupe.rs @@ -2,6 +2,8 @@ impl Foo { pub const Dupe: Foo = Foo::Bar; } +pub type Foo_ctype = ::std::os::raw::c_uint; +pub const Foo_Bar: Foo_ctype = 1; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum Foo { diff --git a/bindgen-tests/tests/expectations/tests/enum_explicit_type.rs b/bindgen-tests/tests/expectations/tests/enum_explicit_type.rs index 1f3dd36f96..979f8b45eb 100644 --- a/bindgen-tests/tests/expectations/tests/enum_explicit_type.rs +++ b/bindgen-tests/tests/expectations/tests/enum_explicit_type.rs @@ -1,43 +1,62 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] +pub type Foo_ctype = ::std::os::raw::c_uchar; +pub const Foo_Bar: Foo_ctype = 0; +pub const Foo_Qux: Foo_ctype = 1; #[repr(u8)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum Foo { Bar = 0, Qux = 1, } +pub type Neg_ctype = ::std::os::raw::c_schar; +pub const Neg_MinusOne: Neg_ctype = -1; +pub const Neg_One: Neg_ctype = 1; #[repr(i8)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum Neg { MinusOne = -1, One = 1, } +pub type Bigger_ctype = ::std::os::raw::c_ushort; +pub const Bigger_Much: Bigger_ctype = 255; +pub const Bigger_Larger: Bigger_ctype = 256; #[repr(u16)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum Bigger { Much = 255, Larger = 256, } +pub type MuchLong_ctype = ::std::os::raw::c_long; +pub const MuchLong_MuchLow: MuchLong_ctype = -4294967296; #[repr(i64)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum MuchLong { MuchLow = -4294967296, } +pub type MuchLongLong_ctype = ::std::os::raw::c_longlong; +pub const MuchLongLong_I64_MIN: MuchLongLong_ctype = -9223372036854775808; #[repr(i64)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum MuchLongLong { I64_MIN = -9223372036854775808, } +pub type MuchULongLong_ctype = ::std::os::raw::c_ulonglong; +pub const MuchULongLong_MuchHigh: MuchULongLong_ctype = 4294967296; #[repr(u64)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum MuchULongLong { MuchHigh = 4294967296, } +pub type BoolEnumsAreFun_ctype = bool; +pub const BoolEnumsAreFun_Value: BoolEnumsAreFun_ctype = 1 as BoolEnumsAreFun_ctype; #[repr(u8)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum BoolEnumsAreFun { Value = 1, } pub type MyType = bool; +pub type BoolEnumsAreFun2_ctype = MyType; +pub const BoolEnumsAreFun2_Value2: BoolEnumsAreFun2_ctype = 1 as BoolEnumsAreFun2_ctype; #[repr(u8)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum BoolEnumsAreFun2 { @@ -45,6 +64,9 @@ pub enum BoolEnumsAreFun2 { } pub const AnonymousVariantOne: _bindgen_ty_1 = _bindgen_ty_1::AnonymousVariantOne; pub const AnonymousVariantTwo: _bindgen_ty_1 = _bindgen_ty_1::AnonymousVariantTwo; +pub type _bindgen_ty_1_ctype = ::std::os::raw::c_uchar; +pub const _bindgen_ty_1_AnonymousVariantOne: _bindgen_ty_1_ctype = 0; +pub const _bindgen_ty_1_AnonymousVariantTwo: _bindgen_ty_1_ctype = 1; #[repr(u8)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum _bindgen_ty_1 { diff --git a/bindgen-tests/tests/expectations/tests/enum_in_template_with_typedef.rs b/bindgen-tests/tests/expectations/tests/enum_in_template_with_typedef.rs index b71923e10f..7ee152b075 100644 --- a/bindgen-tests/tests/expectations/tests/enum_in_template_with_typedef.rs +++ b/bindgen-tests/tests/expectations/tests/enum_in_template_with_typedef.rs @@ -8,6 +8,8 @@ pub type std_fbstring_core_category_type = u8; impl std_fbstring_core_Category { pub const Bar: std_fbstring_core_Category = std_fbstring_core_Category::Foo; } +pub type std_fbstring_core_Category_ctype = std_fbstring_core_category_type; +pub const std_fbstring_core_Category_Foo: std_fbstring_core_Category_ctype = 0; #[repr(u8)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum std_fbstring_core_Category { diff --git a/bindgen-tests/tests/expectations/tests/enum_negative.rs b/bindgen-tests/tests/expectations/tests/enum_negative.rs index f946e0f044..2a204f019a 100644 --- a/bindgen-tests/tests/expectations/tests/enum_negative.rs +++ b/bindgen-tests/tests/expectations/tests/enum_negative.rs @@ -1,4 +1,7 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] +pub type Foo_ctype = ::std::os::raw::c_int; +pub const Foo_Bar: Foo_ctype = -2; +pub const Foo_Qux: Foo_ctype = 1; #[repr(i32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum Foo { diff --git a/bindgen-tests/tests/expectations/tests/enum_packed.rs b/bindgen-tests/tests/expectations/tests/enum_packed.rs index d4ec2192d1..24b9cc5890 100644 --- a/bindgen-tests/tests/expectations/tests/enum_packed.rs +++ b/bindgen-tests/tests/expectations/tests/enum_packed.rs @@ -1,16 +1,25 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] +pub type Foo_ctype = ::std::os::raw::c_uchar; +pub const Foo_Bar: Foo_ctype = 0; +pub const Foo_Qux: Foo_ctype = 1; #[repr(u8)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum Foo { Bar = 0, Qux = 1, } +pub type Neg_ctype = ::std::os::raw::c_schar; +pub const Neg_MinusOne: Neg_ctype = -1; +pub const Neg_One: Neg_ctype = 1; #[repr(i8)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum Neg { MinusOne = -1, One = 1, } +pub type Bigger_ctype = ::std::os::raw::c_ushort; +pub const Bigger_Much: Bigger_ctype = 255; +pub const Bigger_Larger: Bigger_ctype = 256; #[repr(u16)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum Bigger { diff --git a/bindgen-tests/tests/expectations/tests/forward-enum-decl.rs b/bindgen-tests/tests/expectations/tests/forward-enum-decl.rs index 9949b39aae..de6c4d7cb9 100644 --- a/bindgen-tests/tests/expectations/tests/forward-enum-decl.rs +++ b/bindgen-tests/tests/expectations/tests/forward-enum-decl.rs @@ -1,4 +1,7 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] +pub type CSSPseudoClassType_ctype = ::std::os::raw::c_int; +pub const CSSPseudoClassType_empty: CSSPseudoClassType_ctype = 0; +pub const CSSPseudoClassType_link: CSSPseudoClassType_ctype = 1; #[repr(i32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum CSSPseudoClassType { diff --git a/bindgen-tests/tests/expectations/tests/func_ptr_in_struct.rs b/bindgen-tests/tests/expectations/tests/func_ptr_in_struct.rs index 308bb069e0..aa304e6197 100644 --- a/bindgen-tests/tests/expectations/tests/func_ptr_in_struct.rs +++ b/bindgen-tests/tests/expectations/tests/func_ptr_in_struct.rs @@ -1,4 +1,5 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] +pub type baz_ctype = i32; #[repr(i32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum baz { diff --git a/bindgen-tests/tests/expectations/tests/issue-1198-alias-rust-enum.rs b/bindgen-tests/tests/expectations/tests/issue-1198-alias-rust-enum.rs index f41d6e90ec..f86e28571b 100644 --- a/bindgen-tests/tests/expectations/tests/issue-1198-alias-rust-enum.rs +++ b/bindgen-tests/tests/expectations/tests/issue-1198-alias-rust-enum.rs @@ -2,6 +2,9 @@ impl MyDupeEnum { pub const A_alias: MyDupeEnum = MyDupeEnum::A; } +pub type MyDupeEnum_ctype = ::std::os::raw::c_uint; +pub const MyDupeEnum_A: MyDupeEnum_ctype = 0; +pub const MyDupeEnum_B: MyDupeEnum_ctype = 1; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum MyDupeEnum { @@ -11,6 +14,9 @@ pub enum MyDupeEnum { impl MyOtherDupeEnum { pub const C_alias: MyOtherDupeEnum = MyOtherDupeEnum::C; } +pub type MyOtherDupeEnum_ctype = ::std::os::raw::c_uint; +pub const MyOtherDupeEnum_C: MyOtherDupeEnum_ctype = 0; +pub const MyOtherDupeEnum_D: MyOtherDupeEnum_ctype = 1; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum MyOtherDupeEnum { diff --git a/bindgen-tests/tests/expectations/tests/issue-1488-enum-new-type.rs b/bindgen-tests/tests/expectations/tests/issue-1488-enum-new-type.rs index 60882da2d5..048f1cd030 100644 --- a/bindgen-tests/tests/expectations/tests/issue-1488-enum-new-type.rs +++ b/bindgen-tests/tests/expectations/tests/issue-1488-enum-new-type.rs @@ -13,6 +13,9 @@ pub mod Bar { #[repr(transparent)] #[derive(Debug, Copy, Clone)] pub struct BarAlias(pub Bar::Type); +pub type Qux_ctype = ::std::os::raw::c_uint; +pub const Qux_E: Qux_ctype = 0; +pub const Qux_F: Qux_ctype = 1; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum Qux { diff --git a/bindgen-tests/tests/expectations/tests/issue-1554.rs b/bindgen-tests/tests/expectations/tests/issue-1554.rs index 7b1a2cd410..3410e69a78 100644 --- a/bindgen-tests/tests/expectations/tests/issue-1554.rs +++ b/bindgen-tests/tests/expectations/tests/issue-1554.rs @@ -1,6 +1,9 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] #![cfg(feature = "nightly")] #![feature(non_exhaustive)] +pub type Planet_ctype = ::std::os::raw::c_uint; +pub const Planet_earth: Planet_ctype = 0; +pub const Planet_mars: Planet_ctype = 1; #[repr(u32)] #[non_exhaustive] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] diff --git a/bindgen-tests/tests/expectations/tests/issue-2646.rs b/bindgen-tests/tests/expectations/tests/issue-2646.rs new file mode 100644 index 0000000000..fcb74a7146 --- /dev/null +++ b/bindgen-tests/tests/expectations/tests/issue-2646.rs @@ -0,0 +1,101 @@ +#![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] +pub type Plain_ctype = ::std::os::raw::c_uint; +pub const Plain_Plain1: Plain_ctype = 0; +pub const Plain_Plain2: Plain_ctype = 1; +pub const Plain_Plain3: Plain_ctype = 2; +#[repr(u32)] +#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] +pub enum Plain { + Plain1 = 0, + Plain2 = 1, + Plain3 = 2, +} +pub type TryFromRaw_ctype = ::std::os::raw::c_int; +pub const TryFromRaw_TFR1: TryFromRaw_ctype = -1; +pub const TryFromRaw_TFR2: TryFromRaw_ctype = 5; +pub const TryFromRaw_TFR3: TryFromRaw_ctype = 6; +#[repr(i32)] +#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] +pub enum TryFromRaw { + TFR1 = -1, + TFR2 = 5, + TFR3 = 6, +} +pub struct TryFromRawError(TryFromRaw_ctype); +impl TryFromRawError { + #[must_use] + pub fn value(&self) -> TryFromRaw_ctype { + self.0 + } +} +impl std::convert::TryFrom for TryFromRaw { + type Error = TryFromRawError; + fn try_from(v: TryFromRaw_ctype) -> Result { + match v { + -1 => TryFromRaw::TFR1, + 5 => TryFromRaw::TFR2, + 6 => TryFromRaw::TFR3, + _ => TryFromRawError(v), + } + } +} +pub type FromRawUnchecked_ctype = ::std::os::raw::c_uint; +pub const FromRawUnchecked_FRU1: FromRawUnchecked_ctype = 6; +pub const FromRawUnchecked_FRU2: FromRawUnchecked_ctype = 10; +pub const FromRawUnchecked_FRU3: FromRawUnchecked_ctype = 11; +#[repr(u32)] +#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] +pub enum FromRawUnchecked { + FRU1 = 6, + FRU2 = 10, + FRU3 = 11, +} +impl FromRawUnchecked { + const unsafe fn from_ctype_unchecked(v: FromRawUnchecked_ctype) -> Self { + std::mem::transmute(v) + } +} +impl Both { + pub const Both3: Both = Both::Both1; +} +pub type Both_ctype = ::std::os::raw::c_int; +pub const Both_Both1: Both_ctype = 0; +pub const Both_Both2: Both_ctype = -1; +#[repr(i32)] +#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] +pub enum Both { + Both1 = 0, + Both2 = -1, +} +pub struct BothError(Both_ctype); +impl BothError { + #[must_use] + pub fn value(&self) -> Both_ctype { + self.0 + } +} +impl std::convert::TryFrom for Both { + type Error = BothError; + fn try_from(v: Both_ctype) -> Result { + match v { + 0 => Both::Both1, + -1 => Both::Both2, + _ => BothError(v), + } + } +} +impl Both { + const unsafe fn from_ctype_unchecked(v: Both_ctype) -> Self { + std::mem::transmute(v) + } +} +pub type NonExhaustive_ctype = ::std::os::raw::c_uint; +pub const NonExhaustive_Ex1: NonExhaustive_ctype = 0; +pub const NonExhaustive_Ex2: NonExhaustive_ctype = 1; +#[repr(u32)] +#[non_exhaustive] +#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] +pub enum NonExhaustive { + Ex1 = 0, + Ex2 = 1, +} diff --git a/bindgen-tests/tests/expectations/tests/issue-372.rs b/bindgen-tests/tests/expectations/tests/issue-372.rs index 80b8cbe8b6..b8e5b6917f 100644 --- a/bindgen-tests/tests/expectations/tests/issue-372.rs +++ b/bindgen-tests/tests/expectations/tests/issue-372.rs @@ -67,6 +67,19 @@ pub mod root { } } } + pub type n_ctype = ::std::os::raw::c_uint; + pub const n_o: n_ctype = 0; + pub const n_p: n_ctype = 1; + pub const n_q: n_ctype = 2; + pub const n_r: n_ctype = 3; + pub const n_s: n_ctype = 4; + pub const n_t: n_ctype = 5; + pub const n_b: n_ctype = 6; + pub const n_ae: n_ctype = 7; + pub const n_e: n_ctype = 8; + pub const n_ag: n_ctype = 9; + pub const n_ah: n_ctype = 10; + pub const n_ai: n_ctype = 11; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum n { diff --git a/bindgen-tests/tests/expectations/tests/issue-410.rs b/bindgen-tests/tests/expectations/tests/issue-410.rs index e52aa25a13..1307bd6267 100644 --- a/bindgen-tests/tests/expectations/tests/issue-410.rs +++ b/bindgen-tests/tests/expectations/tests/issue-410.rs @@ -27,6 +27,7 @@ pub mod root { } } } + pub type JSWhyMagic_ctype = ::std::os::raw::c_uint; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum JSWhyMagic { diff --git a/bindgen-tests/tests/expectations/tests/issue-493.rs b/bindgen-tests/tests/expectations/tests/issue-493.rs index d2e4ea5375..0c66679e2a 100644 --- a/bindgen-tests/tests/expectations/tests/issue-493.rs +++ b/bindgen-tests/tests/expectations/tests/issue-493.rs @@ -67,6 +67,8 @@ impl Default for basic_string___long { } } pub const basic_string___min_cap: basic_string__bindgen_ty_1 = basic_string__bindgen_ty_1::__min_cap; +pub type basic_string__bindgen_ty_1_ctype = i32; +pub const basic_string__bindgen_ty_1___min_cap: basic_string__bindgen_ty_1_ctype = 0; #[repr(i32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum basic_string__bindgen_ty_1 { @@ -117,6 +119,8 @@ impl Default for basic_string___ulx { } } pub const basic_string___n_words: basic_string__bindgen_ty_2 = basic_string__bindgen_ty_2::__n_words; +pub type basic_string__bindgen_ty_2_ctype = i32; +pub const basic_string__bindgen_ty_2___n_words: basic_string__bindgen_ty_2_ctype = 0; #[repr(i32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum basic_string__bindgen_ty_2 { diff --git a/bindgen-tests/tests/expectations/tests/issue-493_1_0.rs b/bindgen-tests/tests/expectations/tests/issue-493_1_0.rs index ff0c93428b..e46cb9ce25 100644 --- a/bindgen-tests/tests/expectations/tests/issue-493_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/issue-493_1_0.rs @@ -67,6 +67,8 @@ impl Default for basic_string___long { } } pub const basic_string___min_cap: basic_string__bindgen_ty_1 = basic_string__bindgen_ty_1::__min_cap; +pub type basic_string__bindgen_ty_1_ctype = i32; +pub const basic_string__bindgen_ty_1___min_cap: basic_string__bindgen_ty_1_ctype = 0; #[repr(i32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum basic_string__bindgen_ty_1 { @@ -102,6 +104,8 @@ pub struct basic_string___ulx { pub bindgen_union_field: [u8; 0usize], } pub const basic_string___n_words: basic_string__bindgen_ty_2 = basic_string__bindgen_ty_2::__n_words; +pub type basic_string__bindgen_ty_2_ctype = i32; +pub const basic_string__bindgen_ty_2___n_words: basic_string__bindgen_ty_2_ctype = 0; #[repr(i32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum basic_string__bindgen_ty_2 { diff --git a/bindgen-tests/tests/expectations/tests/issue-569-non-type-template-params-causing-layout-test-failures.rs b/bindgen-tests/tests/expectations/tests/issue-569-non-type-template-params-causing-layout-test-failures.rs index 567325b82d..4b9b789b3f 100644 --- a/bindgen-tests/tests/expectations/tests/issue-569-non-type-template-params-causing-layout-test-failures.rs +++ b/bindgen-tests/tests/expectations/tests/issue-569-non-type-template-params-causing-layout-test-failures.rs @@ -1,6 +1,9 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] pub const ENUM_VARIANT_1: _bindgen_ty_1 = _bindgen_ty_1::ENUM_VARIANT_1; pub const ENUM_VARIANT_2: _bindgen_ty_1 = _bindgen_ty_1::ENUM_VARIANT_2; +pub type _bindgen_ty_1_ctype = ::std::os::raw::c_uint; +pub const _bindgen_ty_1_ENUM_VARIANT_1: _bindgen_ty_1_ctype = 0; +pub const _bindgen_ty_1_ENUM_VARIANT_2: _bindgen_ty_1_ctype = 1; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum _bindgen_ty_1 { diff --git a/bindgen-tests/tests/expectations/tests/issue-888-enum-var-decl-jump.rs b/bindgen-tests/tests/expectations/tests/issue-888-enum-var-decl-jump.rs index 0a0d05f9f9..0afcb6bcf6 100644 --- a/bindgen-tests/tests/expectations/tests/issue-888-enum-var-decl-jump.rs +++ b/bindgen-tests/tests/expectations/tests/issue-888-enum-var-decl-jump.rs @@ -21,6 +21,7 @@ pub mod root { ["Alignment of Type"][::std::mem::align_of::() - 1usize]; }; } + pub type a_ctype = ::std::os::raw::c_uint; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum a { diff --git a/bindgen-tests/tests/expectations/tests/jsval_layout_opaque.rs b/bindgen-tests/tests/expectations/tests/jsval_layout_opaque.rs index e7cb9af39e..5b53bc54bc 100644 --- a/bindgen-tests/tests/expectations/tests/jsval_layout_opaque.rs +++ b/bindgen-tests/tests/expectations/tests/jsval_layout_opaque.rs @@ -86,6 +86,18 @@ where pub const JSVAL_TAG_SHIFT: u32 = 47; pub const JSVAL_PAYLOAD_MASK: u64 = 140737488355327; pub const JSVAL_TAG_MASK: i64 = -140737488355328; +pub type JSValueType_ctype = ::std::os::raw::c_uchar; +pub const JSValueType_JSVAL_TYPE_DOUBLE: JSValueType_ctype = 0; +pub const JSValueType_JSVAL_TYPE_INT32: JSValueType_ctype = 1; +pub const JSValueType_JSVAL_TYPE_UNDEFINED: JSValueType_ctype = 2; +pub const JSValueType_JSVAL_TYPE_BOOLEAN: JSValueType_ctype = 3; +pub const JSValueType_JSVAL_TYPE_MAGIC: JSValueType_ctype = 4; +pub const JSValueType_JSVAL_TYPE_STRING: JSValueType_ctype = 5; +pub const JSValueType_JSVAL_TYPE_SYMBOL: JSValueType_ctype = 6; +pub const JSValueType_JSVAL_TYPE_NULL: JSValueType_ctype = 7; +pub const JSValueType_JSVAL_TYPE_OBJECT: JSValueType_ctype = 8; +pub const JSValueType_JSVAL_TYPE_UNKNOWN: JSValueType_ctype = 32; +pub const JSValueType_JSVAL_TYPE_MISSING: JSValueType_ctype = 33; #[repr(u8)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum JSValueType { @@ -101,6 +113,16 @@ pub enum JSValueType { JSVAL_TYPE_UNKNOWN = 32, JSVAL_TYPE_MISSING = 33, } +pub type JSValueTag_ctype = ::std::os::raw::c_uint; +pub const JSValueTag_JSVAL_TAG_MAX_DOUBLE: JSValueTag_ctype = 131056; +pub const JSValueTag_JSVAL_TAG_INT32: JSValueTag_ctype = 131057; +pub const JSValueTag_JSVAL_TAG_UNDEFINED: JSValueTag_ctype = 131058; +pub const JSValueTag_JSVAL_TAG_STRING: JSValueTag_ctype = 131061; +pub const JSValueTag_JSVAL_TAG_SYMBOL: JSValueTag_ctype = 131062; +pub const JSValueTag_JSVAL_TAG_BOOLEAN: JSValueTag_ctype = 131059; +pub const JSValueTag_JSVAL_TAG_MAGIC: JSValueTag_ctype = 131060; +pub const JSValueTag_JSVAL_TAG_NULL: JSValueTag_ctype = 131063; +pub const JSValueTag_JSVAL_TAG_OBJECT: JSValueTag_ctype = 131064; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum JSValueTag { @@ -114,6 +136,16 @@ pub enum JSValueTag { JSVAL_TAG_NULL = 131063, JSVAL_TAG_OBJECT = 131064, } +pub type JSValueShiftedTag_ctype = ::std::os::raw::c_ulong; +pub const JSValueShiftedTag_JSVAL_SHIFTED_TAG_MAX_DOUBLE: JSValueShiftedTag_ctype = 18444492278190833663; +pub const JSValueShiftedTag_JSVAL_SHIFTED_TAG_INT32: JSValueShiftedTag_ctype = 18444633011384221696; +pub const JSValueShiftedTag_JSVAL_SHIFTED_TAG_UNDEFINED: JSValueShiftedTag_ctype = 18444773748872577024; +pub const JSValueShiftedTag_JSVAL_SHIFTED_TAG_STRING: JSValueShiftedTag_ctype = 18445195961337643008; +pub const JSValueShiftedTag_JSVAL_SHIFTED_TAG_SYMBOL: JSValueShiftedTag_ctype = 18445336698825998336; +pub const JSValueShiftedTag_JSVAL_SHIFTED_TAG_BOOLEAN: JSValueShiftedTag_ctype = 18444914486360932352; +pub const JSValueShiftedTag_JSVAL_SHIFTED_TAG_MAGIC: JSValueShiftedTag_ctype = 18445055223849287680; +pub const JSValueShiftedTag_JSVAL_SHIFTED_TAG_NULL: JSValueShiftedTag_ctype = 18445477436314353664; +pub const JSValueShiftedTag_JSVAL_SHIFTED_TAG_OBJECT: JSValueShiftedTag_ctype = 18445618173802708992; #[repr(u64)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum JSValueShiftedTag { @@ -127,6 +159,45 @@ pub enum JSValueShiftedTag { JSVAL_SHIFTED_TAG_NULL = 18445477436314353664, JSVAL_SHIFTED_TAG_OBJECT = 18445618173802708992, } +pub type JSWhyMagic_ctype = ::std::os::raw::c_uint; +/// a hole in a native object's elements +pub const JSWhyMagic_JS_ELEMENTS_HOLE: JSWhyMagic_ctype = 0; +/// there is not a pending iterator value +pub const JSWhyMagic_JS_NO_ITER_VALUE: JSWhyMagic_ctype = 1; +/// exception value thrown when closing a generator +pub const JSWhyMagic_JS_GENERATOR_CLOSING: JSWhyMagic_ctype = 2; +/// compiler sentinel value +pub const JSWhyMagic_JS_NO_CONSTANT: JSWhyMagic_ctype = 3; +/// used in debug builds to catch tracing errors +pub const JSWhyMagic_JS_THIS_POISON: JSWhyMagic_ctype = 4; +/// used in debug builds to catch tracing errors +pub const JSWhyMagic_JS_ARG_POISON: JSWhyMagic_ctype = 5; +/// an empty subnode in the AST serializer +pub const JSWhyMagic_JS_SERIALIZE_NO_NODE: JSWhyMagic_ctype = 6; +/// lazy arguments value on the stack +pub const JSWhyMagic_JS_LAZY_ARGUMENTS: JSWhyMagic_ctype = 7; +/// optimized-away 'arguments' value +pub const JSWhyMagic_JS_OPTIMIZED_ARGUMENTS: JSWhyMagic_ctype = 8; +/// magic value passed to natives to indicate construction +pub const JSWhyMagic_JS_IS_CONSTRUCTING: JSWhyMagic_ctype = 9; +/// arguments.callee has been overwritten +pub const JSWhyMagic_JS_OVERWRITTEN_CALLEE: JSWhyMagic_ctype = 10; +/// value of static block object slot +pub const JSWhyMagic_JS_BLOCK_NEEDS_CLONE: JSWhyMagic_ctype = 11; +/// see class js::HashableValue +pub const JSWhyMagic_JS_HASH_KEY_EMPTY: JSWhyMagic_ctype = 12; +/// error while running Ion code +pub const JSWhyMagic_JS_ION_ERROR: JSWhyMagic_ctype = 13; +/// missing recover instruction result +pub const JSWhyMagic_JS_ION_BAILOUT: JSWhyMagic_ctype = 14; +/// optimized out slot +pub const JSWhyMagic_JS_OPTIMIZED_OUT: JSWhyMagic_ctype = 15; +/// uninitialized lexical bindings that produce ReferenceError on touch. +pub const JSWhyMagic_JS_UNINITIALIZED_LEXICAL: JSWhyMagic_ctype = 16; +/// for local use +pub const JSWhyMagic_JS_GENERIC_MAGIC: JSWhyMagic_ctype = 17; +/// for local use +pub const JSWhyMagic_JS_WHY_MAGIC_COUNT: JSWhyMagic_ctype = 18; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum JSWhyMagic { diff --git a/bindgen-tests/tests/expectations/tests/jsval_layout_opaque_1_0.rs b/bindgen-tests/tests/expectations/tests/jsval_layout_opaque_1_0.rs index 7ae53bc40f..af2a6d3277 100644 --- a/bindgen-tests/tests/expectations/tests/jsval_layout_opaque_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/jsval_layout_opaque_1_0.rs @@ -129,6 +129,18 @@ impl ::std::cmp::Eq for __BindgenUnionField {} pub const JSVAL_TAG_SHIFT: u32 = 47; pub const JSVAL_PAYLOAD_MASK: u64 = 140737488355327; pub const JSVAL_TAG_MASK: i64 = -140737488355328; +pub type JSValueType_ctype = ::std::os::raw::c_uchar; +pub const JSValueType_JSVAL_TYPE_DOUBLE: JSValueType_ctype = 0; +pub const JSValueType_JSVAL_TYPE_INT32: JSValueType_ctype = 1; +pub const JSValueType_JSVAL_TYPE_UNDEFINED: JSValueType_ctype = 2; +pub const JSValueType_JSVAL_TYPE_BOOLEAN: JSValueType_ctype = 3; +pub const JSValueType_JSVAL_TYPE_MAGIC: JSValueType_ctype = 4; +pub const JSValueType_JSVAL_TYPE_STRING: JSValueType_ctype = 5; +pub const JSValueType_JSVAL_TYPE_SYMBOL: JSValueType_ctype = 6; +pub const JSValueType_JSVAL_TYPE_NULL: JSValueType_ctype = 7; +pub const JSValueType_JSVAL_TYPE_OBJECT: JSValueType_ctype = 8; +pub const JSValueType_JSVAL_TYPE_UNKNOWN: JSValueType_ctype = 32; +pub const JSValueType_JSVAL_TYPE_MISSING: JSValueType_ctype = 33; #[repr(u8)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum JSValueType { @@ -144,6 +156,16 @@ pub enum JSValueType { JSVAL_TYPE_UNKNOWN = 32, JSVAL_TYPE_MISSING = 33, } +pub type JSValueTag_ctype = ::std::os::raw::c_uint; +pub const JSValueTag_JSVAL_TAG_MAX_DOUBLE: JSValueTag_ctype = 131056; +pub const JSValueTag_JSVAL_TAG_INT32: JSValueTag_ctype = 131057; +pub const JSValueTag_JSVAL_TAG_UNDEFINED: JSValueTag_ctype = 131058; +pub const JSValueTag_JSVAL_TAG_STRING: JSValueTag_ctype = 131061; +pub const JSValueTag_JSVAL_TAG_SYMBOL: JSValueTag_ctype = 131062; +pub const JSValueTag_JSVAL_TAG_BOOLEAN: JSValueTag_ctype = 131059; +pub const JSValueTag_JSVAL_TAG_MAGIC: JSValueTag_ctype = 131060; +pub const JSValueTag_JSVAL_TAG_NULL: JSValueTag_ctype = 131063; +pub const JSValueTag_JSVAL_TAG_OBJECT: JSValueTag_ctype = 131064; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum JSValueTag { @@ -157,6 +179,16 @@ pub enum JSValueTag { JSVAL_TAG_NULL = 131063, JSVAL_TAG_OBJECT = 131064, } +pub type JSValueShiftedTag_ctype = ::std::os::raw::c_ulong; +pub const JSValueShiftedTag_JSVAL_SHIFTED_TAG_MAX_DOUBLE: JSValueShiftedTag_ctype = 18444492278190833663; +pub const JSValueShiftedTag_JSVAL_SHIFTED_TAG_INT32: JSValueShiftedTag_ctype = 18444633011384221696; +pub const JSValueShiftedTag_JSVAL_SHIFTED_TAG_UNDEFINED: JSValueShiftedTag_ctype = 18444773748872577024; +pub const JSValueShiftedTag_JSVAL_SHIFTED_TAG_STRING: JSValueShiftedTag_ctype = 18445195961337643008; +pub const JSValueShiftedTag_JSVAL_SHIFTED_TAG_SYMBOL: JSValueShiftedTag_ctype = 18445336698825998336; +pub const JSValueShiftedTag_JSVAL_SHIFTED_TAG_BOOLEAN: JSValueShiftedTag_ctype = 18444914486360932352; +pub const JSValueShiftedTag_JSVAL_SHIFTED_TAG_MAGIC: JSValueShiftedTag_ctype = 18445055223849287680; +pub const JSValueShiftedTag_JSVAL_SHIFTED_TAG_NULL: JSValueShiftedTag_ctype = 18445477436314353664; +pub const JSValueShiftedTag_JSVAL_SHIFTED_TAG_OBJECT: JSValueShiftedTag_ctype = 18445618173802708992; #[repr(u64)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum JSValueShiftedTag { @@ -170,6 +202,45 @@ pub enum JSValueShiftedTag { JSVAL_SHIFTED_TAG_NULL = 18445477436314353664, JSVAL_SHIFTED_TAG_OBJECT = 18445618173802708992, } +pub type JSWhyMagic_ctype = ::std::os::raw::c_uint; +/// a hole in a native object's elements +pub const JSWhyMagic_JS_ELEMENTS_HOLE: JSWhyMagic_ctype = 0; +/// there is not a pending iterator value +pub const JSWhyMagic_JS_NO_ITER_VALUE: JSWhyMagic_ctype = 1; +/// exception value thrown when closing a generator +pub const JSWhyMagic_JS_GENERATOR_CLOSING: JSWhyMagic_ctype = 2; +/// compiler sentinel value +pub const JSWhyMagic_JS_NO_CONSTANT: JSWhyMagic_ctype = 3; +/// used in debug builds to catch tracing errors +pub const JSWhyMagic_JS_THIS_POISON: JSWhyMagic_ctype = 4; +/// used in debug builds to catch tracing errors +pub const JSWhyMagic_JS_ARG_POISON: JSWhyMagic_ctype = 5; +/// an empty subnode in the AST serializer +pub const JSWhyMagic_JS_SERIALIZE_NO_NODE: JSWhyMagic_ctype = 6; +/// lazy arguments value on the stack +pub const JSWhyMagic_JS_LAZY_ARGUMENTS: JSWhyMagic_ctype = 7; +/// optimized-away 'arguments' value +pub const JSWhyMagic_JS_OPTIMIZED_ARGUMENTS: JSWhyMagic_ctype = 8; +/// magic value passed to natives to indicate construction +pub const JSWhyMagic_JS_IS_CONSTRUCTING: JSWhyMagic_ctype = 9; +/// arguments.callee has been overwritten +pub const JSWhyMagic_JS_OVERWRITTEN_CALLEE: JSWhyMagic_ctype = 10; +/// value of static block object slot +pub const JSWhyMagic_JS_BLOCK_NEEDS_CLONE: JSWhyMagic_ctype = 11; +/// see class js::HashableValue +pub const JSWhyMagic_JS_HASH_KEY_EMPTY: JSWhyMagic_ctype = 12; +/// error while running Ion code +pub const JSWhyMagic_JS_ION_ERROR: JSWhyMagic_ctype = 13; +/// missing recover instruction result +pub const JSWhyMagic_JS_ION_BAILOUT: JSWhyMagic_ctype = 14; +/// optimized out slot +pub const JSWhyMagic_JS_OPTIMIZED_OUT: JSWhyMagic_ctype = 15; +/// uninitialized lexical bindings that produce ReferenceError on touch. +pub const JSWhyMagic_JS_UNINITIALIZED_LEXICAL: JSWhyMagic_ctype = 16; +/// for local use +pub const JSWhyMagic_JS_GENERIC_MAGIC: JSWhyMagic_ctype = 17; +/// for local use +pub const JSWhyMagic_JS_WHY_MAGIC_COUNT: JSWhyMagic_ctype = 18; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum JSWhyMagic { diff --git a/bindgen-tests/tests/expectations/tests/layout_array_too_long.rs b/bindgen-tests/tests/expectations/tests/layout_array_too_long.rs index d6ce2883d7..d6aa5e125d 100644 --- a/bindgen-tests/tests/expectations/tests/layout_array_too_long.rs +++ b/bindgen-tests/tests/expectations/tests/layout_array_too_long.rs @@ -5,6 +5,14 @@ pub const IP_LAST_FRAG_IDX: _bindgen_ty_1 = _bindgen_ty_1::IP_LAST_FRAG_IDX; pub const IP_FIRST_FRAG_IDX: _bindgen_ty_1 = _bindgen_ty_1::IP_FIRST_FRAG_IDX; pub const IP_MIN_FRAG_NUM: _bindgen_ty_1 = _bindgen_ty_1::IP_MIN_FRAG_NUM; pub const IP_MAX_FRAG_NUM: _bindgen_ty_1 = _bindgen_ty_1::IP_MAX_FRAG_NUM; +pub type _bindgen_ty_1_ctype = ::std::os::raw::c_uint; +///< index of last fragment +pub const _bindgen_ty_1_IP_LAST_FRAG_IDX: _bindgen_ty_1_ctype = 0; +///< index of first fragment +pub const _bindgen_ty_1_IP_FIRST_FRAG_IDX: _bindgen_ty_1_ctype = 1; +///< minimum number of fragments +pub const _bindgen_ty_1_IP_MIN_FRAG_NUM: _bindgen_ty_1_ctype = 2; +pub const _bindgen_ty_1_IP_MAX_FRAG_NUM: _bindgen_ty_1_ctype = 4; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum _bindgen_ty_1 { diff --git a/bindgen-tests/tests/expectations/tests/layout_cmdline_token.rs b/bindgen-tests/tests/expectations/tests/layout_cmdline_token.rs index fb7b3bf584..ad2012bcf5 100644 --- a/bindgen-tests/tests/expectations/tests/layout_cmdline_token.rs +++ b/bindgen-tests/tests/expectations/tests/layout_cmdline_token.rs @@ -104,6 +104,15 @@ const _: () = { "Offset of field: cmdline_token_ops::get_help", ][::std::mem::offset_of!(cmdline_token_ops, get_help) - 24usize]; }; +pub type cmdline_numtype_ctype = ::std::os::raw::c_uint; +pub const cmdline_numtype_UINT8: cmdline_numtype_ctype = 0; +pub const cmdline_numtype_UINT16: cmdline_numtype_ctype = 1; +pub const cmdline_numtype_UINT32: cmdline_numtype_ctype = 2; +pub const cmdline_numtype_UINT64: cmdline_numtype_ctype = 3; +pub const cmdline_numtype_INT8: cmdline_numtype_ctype = 4; +pub const cmdline_numtype_INT16: cmdline_numtype_ctype = 5; +pub const cmdline_numtype_INT32: cmdline_numtype_ctype = 6; +pub const cmdline_numtype_INT64: cmdline_numtype_ctype = 7; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum cmdline_numtype { diff --git a/bindgen-tests/tests/expectations/tests/layout_eth_conf.rs b/bindgen-tests/tests/expectations/tests/layout_eth_conf.rs index 9b98bac376..e6e4c84a9d 100644 --- a/bindgen-tests/tests/expectations/tests/layout_eth_conf.rs +++ b/bindgen-tests/tests/expectations/tests/layout_eth_conf.rs @@ -115,6 +115,23 @@ pub const RTE_ETH_FLOW_VXLAN: u32 = 19; pub const RTE_ETH_FLOW_GENEVE: u32 = 20; pub const RTE_ETH_FLOW_NVGRE: u32 = 21; pub const RTE_ETH_FLOW_MAX: u32 = 22; +pub type rte_eth_rx_mq_mode_ctype = ::std::os::raw::c_uint; +/// None of DCB,RSS or VMDQ mode +pub const rte_eth_rx_mq_mode_ETH_MQ_RX_NONE: rte_eth_rx_mq_mode_ctype = 0; +/// For RX side, only RSS is on +pub const rte_eth_rx_mq_mode_ETH_MQ_RX_RSS: rte_eth_rx_mq_mode_ctype = 1; +/// For RX side,only DCB is on. +pub const rte_eth_rx_mq_mode_ETH_MQ_RX_DCB: rte_eth_rx_mq_mode_ctype = 2; +/// Both DCB and RSS enable +pub const rte_eth_rx_mq_mode_ETH_MQ_RX_DCB_RSS: rte_eth_rx_mq_mode_ctype = 3; +/// Only VMDQ, no RSS nor DCB +pub const rte_eth_rx_mq_mode_ETH_MQ_RX_VMDQ_ONLY: rte_eth_rx_mq_mode_ctype = 4; +/// RSS mode with VMDQ +pub const rte_eth_rx_mq_mode_ETH_MQ_RX_VMDQ_RSS: rte_eth_rx_mq_mode_ctype = 5; +/// Use VMDQ+DCB to route traffic to queues +pub const rte_eth_rx_mq_mode_ETH_MQ_RX_VMDQ_DCB: rte_eth_rx_mq_mode_ctype = 6; +/// Enable both VMDQ and DCB in VMDq +pub const rte_eth_rx_mq_mode_ETH_MQ_RX_VMDQ_DCB_RSS: rte_eth_rx_mq_mode_ctype = 7; #[repr(u32)] /** A set of values to identify what method is to be used to route packets to multiple queues.*/ @@ -400,6 +417,15 @@ impl rte_eth_rxmode { __bindgen_bitfield_unit } } +pub type rte_eth_tx_mq_mode_ctype = ::std::os::raw::c_uint; +///< It is in neither DCB nor VT mode. +pub const rte_eth_tx_mq_mode_ETH_MQ_TX_NONE: rte_eth_tx_mq_mode_ctype = 0; +///< For TX side,only DCB is on. +pub const rte_eth_tx_mq_mode_ETH_MQ_TX_DCB: rte_eth_tx_mq_mode_ctype = 1; +///< For TX side,both DCB and VT is on. +pub const rte_eth_tx_mq_mode_ETH_MQ_TX_VMDQ_DCB: rte_eth_tx_mq_mode_ctype = 2; +///< Only VT on, no DCB +pub const rte_eth_tx_mq_mode_ETH_MQ_TX_VMDQ_ONLY: rte_eth_tx_mq_mode_ctype = 3; #[repr(u32)] /** A set of values to identify what method is to be used to transmit packets using multi-TCs.*/ @@ -600,6 +626,11 @@ impl Default for rte_eth_rss_conf { } } } +pub type rte_eth_nb_tcs_ctype = ::std::os::raw::c_uint; +///< 4 TCs with DCB. +pub const rte_eth_nb_tcs_ETH_4_TCS: rte_eth_nb_tcs_ctype = 4; +///< 8 TCs with DCB. +pub const rte_eth_nb_tcs_ETH_8_TCS: rte_eth_nb_tcs_ctype = 8; #[repr(u32)] /** This enum indicates the possible number of traffic classes in DCB configratioins*/ @@ -610,6 +641,15 @@ pub enum rte_eth_nb_tcs { ///< 8 TCs with DCB. ETH_8_TCS = 8, } +pub type rte_eth_nb_pools_ctype = ::std::os::raw::c_uint; +///< 8 VMDq pools. +pub const rte_eth_nb_pools_ETH_8_POOLS: rte_eth_nb_pools_ctype = 8; +///< 16 VMDq pools. +pub const rte_eth_nb_pools_ETH_16_POOLS: rte_eth_nb_pools_ctype = 16; +///< 32 VMDq pools. +pub const rte_eth_nb_pools_ETH_32_POOLS: rte_eth_nb_pools_ctype = 32; +///< 64 VMDq pools. +pub const rte_eth_nb_pools_ETH_64_POOLS: rte_eth_nb_pools_ctype = 64; #[repr(u32)] /** This enum indicates the possible number of queue pools in VMDQ configurations.*/ @@ -1010,6 +1050,17 @@ impl Default for rte_eth_vmdq_rx_conf { } } } +pub type rte_fdir_mode_ctype = ::std::os::raw::c_uint; +///< Disable FDIR support. +pub const rte_fdir_mode_RTE_FDIR_MODE_NONE: rte_fdir_mode_ctype = 0; +///< Enable FDIR signature filter mode. +pub const rte_fdir_mode_RTE_FDIR_MODE_SIGNATURE: rte_fdir_mode_ctype = 1; +///< Enable FDIR perfect filter mode. +pub const rte_fdir_mode_RTE_FDIR_MODE_PERFECT: rte_fdir_mode_ctype = 2; +///< Enable FDIR filter mode - MAC VLAN. +pub const rte_fdir_mode_RTE_FDIR_MODE_PERFECT_MAC_VLAN: rte_fdir_mode_ctype = 3; +///< Enable FDIR filter mode - tunnel. +pub const rte_fdir_mode_RTE_FDIR_MODE_PERFECT_TUNNEL: rte_fdir_mode_ctype = 4; #[repr(u32)] /// Flow Director setting modes: none, signature or perfect. #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] @@ -1025,6 +1076,13 @@ pub enum rte_fdir_mode { ///< Enable FDIR filter mode - tunnel. RTE_FDIR_MODE_PERFECT_TUNNEL = 4, } +pub type rte_fdir_pballoc_type_ctype = ::std::os::raw::c_uint; +///< 64k. +pub const rte_fdir_pballoc_type_RTE_FDIR_PBALLOC_64K: rte_fdir_pballoc_type_ctype = 0; +///< 128k. +pub const rte_fdir_pballoc_type_RTE_FDIR_PBALLOC_128K: rte_fdir_pballoc_type_ctype = 1; +///< 256k. +pub const rte_fdir_pballoc_type_RTE_FDIR_PBALLOC_256K: rte_fdir_pballoc_type_ctype = 2; #[repr(u32)] /** Memory space that can be configured to store Flow Director filters in the board memory.*/ @@ -1037,6 +1095,13 @@ pub enum rte_fdir_pballoc_type { ///< 256k. RTE_FDIR_PBALLOC_256K = 2, } +pub type rte_fdir_status_mode_ctype = ::std::os::raw::c_uint; +///< Never report FDIR hash. +pub const rte_fdir_status_mode_RTE_FDIR_NO_REPORT_STATUS: rte_fdir_status_mode_ctype = 0; +///< Only report FDIR hash for matching pkts. +pub const rte_fdir_status_mode_RTE_FDIR_REPORT_STATUS: rte_fdir_status_mode_ctype = 1; +///< Always report FDIR hash. +pub const rte_fdir_status_mode_RTE_FDIR_REPORT_STATUS_ALWAYS: rte_fdir_status_mode_ctype = 2; #[repr(u32)] /// Select report mode of FDIR hash information in RX descriptors. #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] @@ -1239,6 +1304,13 @@ fn bindgen_test_layout_rte_eth_fdir_masks() { "Offset of field: rte_eth_fdir_masks::tunnel_type_mask", ); } +pub type rte_eth_payload_type_ctype = ::std::os::raw::c_uint; +pub const rte_eth_payload_type_RTE_ETH_PAYLOAD_UNKNOWN: rte_eth_payload_type_ctype = 0; +pub const rte_eth_payload_type_RTE_ETH_RAW_PAYLOAD: rte_eth_payload_type_ctype = 1; +pub const rte_eth_payload_type_RTE_ETH_L2_PAYLOAD: rte_eth_payload_type_ctype = 2; +pub const rte_eth_payload_type_RTE_ETH_L3_PAYLOAD: rte_eth_payload_type_ctype = 3; +pub const rte_eth_payload_type_RTE_ETH_L4_PAYLOAD: rte_eth_payload_type_ctype = 4; +pub const rte_eth_payload_type_RTE_ETH_PAYLOAD_MAX: rte_eth_payload_type_ctype = 8; #[repr(u32)] /// Payload type #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] diff --git a/bindgen-tests/tests/expectations/tests/layout_eth_conf_1_0.rs b/bindgen-tests/tests/expectations/tests/layout_eth_conf_1_0.rs index 34688b20b3..db331f9a36 100644 --- a/bindgen-tests/tests/expectations/tests/layout_eth_conf_1_0.rs +++ b/bindgen-tests/tests/expectations/tests/layout_eth_conf_1_0.rs @@ -158,6 +158,23 @@ pub const RTE_ETH_FLOW_VXLAN: u32 = 19; pub const RTE_ETH_FLOW_GENEVE: u32 = 20; pub const RTE_ETH_FLOW_NVGRE: u32 = 21; pub const RTE_ETH_FLOW_MAX: u32 = 22; +pub type rte_eth_rx_mq_mode_ctype = ::std::os::raw::c_uint; +/// None of DCB,RSS or VMDQ mode +pub const rte_eth_rx_mq_mode_ETH_MQ_RX_NONE: rte_eth_rx_mq_mode_ctype = 0; +/// For RX side, only RSS is on +pub const rte_eth_rx_mq_mode_ETH_MQ_RX_RSS: rte_eth_rx_mq_mode_ctype = 1; +/// For RX side,only DCB is on. +pub const rte_eth_rx_mq_mode_ETH_MQ_RX_DCB: rte_eth_rx_mq_mode_ctype = 2; +/// Both DCB and RSS enable +pub const rte_eth_rx_mq_mode_ETH_MQ_RX_DCB_RSS: rte_eth_rx_mq_mode_ctype = 3; +/// Only VMDQ, no RSS nor DCB +pub const rte_eth_rx_mq_mode_ETH_MQ_RX_VMDQ_ONLY: rte_eth_rx_mq_mode_ctype = 4; +/// RSS mode with VMDQ +pub const rte_eth_rx_mq_mode_ETH_MQ_RX_VMDQ_RSS: rte_eth_rx_mq_mode_ctype = 5; +/// Use VMDQ+DCB to route traffic to queues +pub const rte_eth_rx_mq_mode_ETH_MQ_RX_VMDQ_DCB: rte_eth_rx_mq_mode_ctype = 6; +/// Enable both VMDQ and DCB in VMDq +pub const rte_eth_rx_mq_mode_ETH_MQ_RX_VMDQ_DCB_RSS: rte_eth_rx_mq_mode_ctype = 7; #[repr(u32)] /** A set of values to identify what method is to be used to route packets to multiple queues.*/ @@ -448,6 +465,15 @@ impl rte_eth_rxmode { __bindgen_bitfield_unit } } +pub type rte_eth_tx_mq_mode_ctype = ::std::os::raw::c_uint; +///< It is in neither DCB nor VT mode. +pub const rte_eth_tx_mq_mode_ETH_MQ_TX_NONE: rte_eth_tx_mq_mode_ctype = 0; +///< For TX side,only DCB is on. +pub const rte_eth_tx_mq_mode_ETH_MQ_TX_DCB: rte_eth_tx_mq_mode_ctype = 1; +///< For TX side,both DCB and VT is on. +pub const rte_eth_tx_mq_mode_ETH_MQ_TX_VMDQ_DCB: rte_eth_tx_mq_mode_ctype = 2; +///< Only VT on, no DCB +pub const rte_eth_tx_mq_mode_ETH_MQ_TX_VMDQ_ONLY: rte_eth_tx_mq_mode_ctype = 3; #[repr(u32)] /** A set of values to identify what method is to be used to transmit packets using multi-TCs.*/ @@ -658,6 +684,11 @@ impl Default for rte_eth_rss_conf { } } } +pub type rte_eth_nb_tcs_ctype = ::std::os::raw::c_uint; +///< 4 TCs with DCB. +pub const rte_eth_nb_tcs_ETH_4_TCS: rte_eth_nb_tcs_ctype = 4; +///< 8 TCs with DCB. +pub const rte_eth_nb_tcs_ETH_8_TCS: rte_eth_nb_tcs_ctype = 8; #[repr(u32)] /** This enum indicates the possible number of traffic classes in DCB configratioins*/ @@ -668,6 +699,15 @@ pub enum rte_eth_nb_tcs { ///< 8 TCs with DCB. ETH_8_TCS = 8, } +pub type rte_eth_nb_pools_ctype = ::std::os::raw::c_uint; +///< 8 VMDq pools. +pub const rte_eth_nb_pools_ETH_8_POOLS: rte_eth_nb_pools_ctype = 8; +///< 16 VMDq pools. +pub const rte_eth_nb_pools_ETH_16_POOLS: rte_eth_nb_pools_ctype = 16; +///< 32 VMDq pools. +pub const rte_eth_nb_pools_ETH_32_POOLS: rte_eth_nb_pools_ctype = 32; +///< 64 VMDq pools. +pub const rte_eth_nb_pools_ETH_64_POOLS: rte_eth_nb_pools_ctype = 64; #[repr(u32)] /** This enum indicates the possible number of queue pools in VMDQ configurations.*/ @@ -1108,6 +1148,17 @@ impl Default for rte_eth_vmdq_rx_conf { } } } +pub type rte_fdir_mode_ctype = ::std::os::raw::c_uint; +///< Disable FDIR support. +pub const rte_fdir_mode_RTE_FDIR_MODE_NONE: rte_fdir_mode_ctype = 0; +///< Enable FDIR signature filter mode. +pub const rte_fdir_mode_RTE_FDIR_MODE_SIGNATURE: rte_fdir_mode_ctype = 1; +///< Enable FDIR perfect filter mode. +pub const rte_fdir_mode_RTE_FDIR_MODE_PERFECT: rte_fdir_mode_ctype = 2; +///< Enable FDIR filter mode - MAC VLAN. +pub const rte_fdir_mode_RTE_FDIR_MODE_PERFECT_MAC_VLAN: rte_fdir_mode_ctype = 3; +///< Enable FDIR filter mode - tunnel. +pub const rte_fdir_mode_RTE_FDIR_MODE_PERFECT_TUNNEL: rte_fdir_mode_ctype = 4; #[repr(u32)] /// Flow Director setting modes: none, signature or perfect. #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] @@ -1123,6 +1174,13 @@ pub enum rte_fdir_mode { ///< Enable FDIR filter mode - tunnel. RTE_FDIR_MODE_PERFECT_TUNNEL = 4, } +pub type rte_fdir_pballoc_type_ctype = ::std::os::raw::c_uint; +///< 64k. +pub const rte_fdir_pballoc_type_RTE_FDIR_PBALLOC_64K: rte_fdir_pballoc_type_ctype = 0; +///< 128k. +pub const rte_fdir_pballoc_type_RTE_FDIR_PBALLOC_128K: rte_fdir_pballoc_type_ctype = 1; +///< 256k. +pub const rte_fdir_pballoc_type_RTE_FDIR_PBALLOC_256K: rte_fdir_pballoc_type_ctype = 2; #[repr(u32)] /** Memory space that can be configured to store Flow Director filters in the board memory.*/ @@ -1135,6 +1193,13 @@ pub enum rte_fdir_pballoc_type { ///< 256k. RTE_FDIR_PBALLOC_256K = 2, } +pub type rte_fdir_status_mode_ctype = ::std::os::raw::c_uint; +///< Never report FDIR hash. +pub const rte_fdir_status_mode_RTE_FDIR_NO_REPORT_STATUS: rte_fdir_status_mode_ctype = 0; +///< Only report FDIR hash for matching pkts. +pub const rte_fdir_status_mode_RTE_FDIR_REPORT_STATUS: rte_fdir_status_mode_ctype = 1; +///< Always report FDIR hash. +pub const rte_fdir_status_mode_RTE_FDIR_REPORT_STATUS_ALWAYS: rte_fdir_status_mode_ctype = 2; #[repr(u32)] /// Select report mode of FDIR hash information in RX descriptors. #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] @@ -1352,6 +1417,13 @@ impl Clone for rte_eth_fdir_masks { *self } } +pub type rte_eth_payload_type_ctype = ::std::os::raw::c_uint; +pub const rte_eth_payload_type_RTE_ETH_PAYLOAD_UNKNOWN: rte_eth_payload_type_ctype = 0; +pub const rte_eth_payload_type_RTE_ETH_RAW_PAYLOAD: rte_eth_payload_type_ctype = 1; +pub const rte_eth_payload_type_RTE_ETH_L2_PAYLOAD: rte_eth_payload_type_ctype = 2; +pub const rte_eth_payload_type_RTE_ETH_L3_PAYLOAD: rte_eth_payload_type_ctype = 3; +pub const rte_eth_payload_type_RTE_ETH_L4_PAYLOAD: rte_eth_payload_type_ctype = 4; +pub const rte_eth_payload_type_RTE_ETH_PAYLOAD_MAX: rte_eth_payload_type_ctype = 8; #[repr(u32)] /// Payload type #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] diff --git a/bindgen-tests/tests/expectations/tests/layout_large_align_field.rs b/bindgen-tests/tests/expectations/tests/layout_large_align_field.rs index b72c221dca..6cb2a3dbde 100644 --- a/bindgen-tests/tests/expectations/tests/layout_large_align_field.rs +++ b/bindgen-tests/tests/expectations/tests/layout_large_align_field.rs @@ -35,6 +35,14 @@ pub const IP_LAST_FRAG_IDX: _bindgen_ty_1 = _bindgen_ty_1::IP_LAST_FRAG_IDX; pub const IP_FIRST_FRAG_IDX: _bindgen_ty_1 = _bindgen_ty_1::IP_FIRST_FRAG_IDX; pub const IP_MIN_FRAG_NUM: _bindgen_ty_1 = _bindgen_ty_1::IP_MIN_FRAG_NUM; pub const IP_MAX_FRAG_NUM: _bindgen_ty_1 = _bindgen_ty_1::IP_MAX_FRAG_NUM; +pub type _bindgen_ty_1_ctype = ::std::os::raw::c_uint; +///< index of last fragment +pub const _bindgen_ty_1_IP_LAST_FRAG_IDX: _bindgen_ty_1_ctype = 0; +///< index of first fragment +pub const _bindgen_ty_1_IP_FIRST_FRAG_IDX: _bindgen_ty_1_ctype = 1; +///< minimum number of fragments +pub const _bindgen_ty_1_IP_MIN_FRAG_NUM: _bindgen_ty_1_ctype = 2; +pub const _bindgen_ty_1_IP_MAX_FRAG_NUM: _bindgen_ty_1_ctype = 4; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum _bindgen_ty_1 { diff --git a/bindgen-tests/tests/expectations/tests/libclang-9/struct_typedef_ns.rs b/bindgen-tests/tests/expectations/tests/libclang-9/struct_typedef_ns.rs index d93a62e746..b836b74a8a 100644 --- a/bindgen-tests/tests/expectations/tests/libclang-9/struct_typedef_ns.rs +++ b/bindgen-tests/tests/expectations/tests/libclang-9/struct_typedef_ns.rs @@ -21,6 +21,8 @@ pub mod root { "Offset of field: typedef_struct::foo", ][::std::mem::offset_of!(typedef_struct, foo) - 0usize]; }; + pub type typedef_enum_ctype = ::std::os::raw::c_uint; + pub const typedef_enum_BAR: typedef_enum_ctype = 1; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum typedef_enum { @@ -47,6 +49,8 @@ pub mod root { }; pub type typedef_struct = root::_bindgen_mod_id_12::_bindgen_ty_1; pub const _bindgen_mod_id_12_BAR: root::_bindgen_mod_id_12::_bindgen_ty_2 = _bindgen_ty_2::BAR; + pub type _bindgen_ty_2_ctype = ::std::os::raw::c_uint; + pub const _bindgen_ty_2_BAR: _bindgen_ty_2_ctype = 1; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum _bindgen_ty_2 { diff --git a/bindgen-tests/tests/expectations/tests/nsStyleAutoArray.rs b/bindgen-tests/tests/expectations/tests/nsStyleAutoArray.rs index 3d8edcfdc1..b1a9c5e740 100644 --- a/bindgen-tests/tests/expectations/tests/nsStyleAutoArray.rs +++ b/bindgen-tests/tests/expectations/tests/nsStyleAutoArray.rs @@ -21,6 +21,8 @@ pub struct nsStyleAutoArray { pub mFirstElement: T, pub mOtherElements: nsTArray, } +pub type nsStyleAutoArray_WithSingleInitialElement_ctype = i32; +pub const nsStyleAutoArray_WithSingleInitialElement_WITH_SINGLE_INITIAL_ELEMENT: nsStyleAutoArray_WithSingleInitialElement_ctype = 0; #[repr(i32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum nsStyleAutoArray_WithSingleInitialElement { diff --git a/bindgen-tests/tests/expectations/tests/ord-enum.rs b/bindgen-tests/tests/expectations/tests/ord-enum.rs index aa2e67ab91..9fc80e9e0d 100644 --- a/bindgen-tests/tests/expectations/tests/ord-enum.rs +++ b/bindgen-tests/tests/expectations/tests/ord-enum.rs @@ -1,4 +1,9 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] +pub type A_ctype = ::std::os::raw::c_int; +pub const A_A0: A_ctype = 0; +pub const A_A1: A_ctype = 1; +pub const A_A2: A_ctype = 2; +pub const A_A3: A_ctype = -1; #[repr(i32)] #[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] pub enum A { @@ -7,6 +12,11 @@ pub enum A { A2 = 2, A3 = -1, } +pub type B_ctype = ::std::os::raw::c_int; +pub const B_B0: B_ctype = 1; +pub const B_B1: B_ctype = 4; +pub const B_B2: B_ctype = 3; +pub const B_B3: B_ctype = -1; #[repr(i32)] #[derive(Debug, Copy, Clone, Hash, PartialOrd, Ord, PartialEq, Eq)] pub enum B { diff --git a/bindgen-tests/tests/expectations/tests/overflowed_enum.rs b/bindgen-tests/tests/expectations/tests/overflowed_enum.rs index 2c67ba6903..c13c1fd8a7 100644 --- a/bindgen-tests/tests/expectations/tests/overflowed_enum.rs +++ b/bindgen-tests/tests/expectations/tests/overflowed_enum.rs @@ -1,4 +1,8 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] +pub type Foo_ctype = ::std::os::raw::c_uint; +pub const Foo_BAP_ARM: Foo_ctype = 9698489; +pub const Foo_BAP_X86: Foo_ctype = 11960045; +pub const Foo_BAP_X86_64: Foo_ctype = 3128633167; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum Foo { @@ -6,6 +10,9 @@ pub enum Foo { BAP_X86 = 11960045, BAP_X86_64 = 3128633167, } +pub type Bar_ctype = ::std::os::raw::c_ushort; +pub const Bar_One: Bar_ctype = 1; +pub const Bar_Big: Bar_ctype = 2; #[repr(u16)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum Bar { diff --git a/bindgen-tests/tests/expectations/tests/prepend-enum-constified-variant.rs b/bindgen-tests/tests/expectations/tests/prepend-enum-constified-variant.rs index ff49d684f1..986111353e 100644 --- a/bindgen-tests/tests/expectations/tests/prepend-enum-constified-variant.rs +++ b/bindgen-tests/tests/expectations/tests/prepend-enum-constified-variant.rs @@ -2,6 +2,8 @@ impl AVCodecID { pub const AV_CODEC_ID_TTF: AVCodecID = AVCodecID::AV_CODEC_ID_FIRST_UNKNOWN; } +pub type AVCodecID_ctype = ::std::os::raw::c_uint; +pub const AVCodecID_AV_CODEC_ID_FIRST_UNKNOWN: AVCodecID_ctype = 98304; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum AVCodecID { diff --git a/bindgen-tests/tests/expectations/tests/short-enums.rs b/bindgen-tests/tests/expectations/tests/short-enums.rs index 493bb5b419..d8acb6230f 100644 --- a/bindgen-tests/tests/expectations/tests/short-enums.rs +++ b/bindgen-tests/tests/expectations/tests/short-enums.rs @@ -1,14 +1,20 @@ #![allow(dead_code, non_snake_case, non_camel_case_types, non_upper_case_globals)] +pub type one_byte_t_ctype = ::std::os::raw::c_uchar; +pub const one_byte_t_SOME_VALUE: one_byte_t_ctype = 1; #[repr(u8)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum one_byte_t { SOME_VALUE = 1, } +pub type two_byte_t_ctype = ::std::os::raw::c_ushort; +pub const two_byte_t_SOME_OTHER_VALUE: two_byte_t_ctype = 256; #[repr(u16)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum two_byte_t { SOME_OTHER_VALUE = 256, } +pub type four_byte_t_ctype = ::std::os::raw::c_uint; +pub const four_byte_t_SOME_BIGGER_VALUE: four_byte_t_ctype = 16777216; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum four_byte_t { diff --git a/bindgen-tests/tests/expectations/tests/struct_typedef.rs b/bindgen-tests/tests/expectations/tests/struct_typedef.rs index bc12a1bce8..e70a366885 100644 --- a/bindgen-tests/tests/expectations/tests/struct_typedef.rs +++ b/bindgen-tests/tests/expectations/tests/struct_typedef.rs @@ -40,12 +40,16 @@ impl Default for _bindgen_ty_1 { } pub type struct_ptr_t = *mut _bindgen_ty_1; pub type struct_ptr_ptr_t = *mut *mut _bindgen_ty_1; +pub type typedef_named_enum_ctype = ::std::os::raw::c_uint; +pub const typedef_named_enum_ENUM_HAS_NAME: typedef_named_enum_ctype = 1; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum typedef_named_enum { ENUM_HAS_NAME = 1, } pub const ENUM_IS_ANON: _bindgen_ty_2 = _bindgen_ty_2::ENUM_IS_ANON; +pub type _bindgen_ty_2_ctype = ::std::os::raw::c_uint; +pub const _bindgen_ty_2_ENUM_IS_ANON: _bindgen_ty_2_ctype = 0; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum _bindgen_ty_2 { diff --git a/bindgen-tests/tests/expectations/tests/struct_typedef_ns.rs b/bindgen-tests/tests/expectations/tests/struct_typedef_ns.rs index 82f93dfd16..33cbdff5f4 100644 --- a/bindgen-tests/tests/expectations/tests/struct_typedef_ns.rs +++ b/bindgen-tests/tests/expectations/tests/struct_typedef_ns.rs @@ -21,6 +21,8 @@ pub mod root { "Offset of field: typedef_struct::foo", ][::std::mem::offset_of!(typedef_struct, foo) - 0usize]; }; + pub type typedef_enum_ctype = ::std::os::raw::c_uint; + pub const typedef_enum_BAR: typedef_enum_ctype = 1; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum typedef_enum { @@ -45,6 +47,8 @@ pub mod root { "Offset of field: typedef_struct::foo", ][::std::mem::offset_of!(typedef_struct, foo) - 0usize]; }; + pub type typedef_enum_ctype = ::std::os::raw::c_uint; + pub const typedef_enum_BAR: typedef_enum_ctype = 1; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum typedef_enum { diff --git a/bindgen-tests/tests/expectations/tests/weird_bitfields.rs b/bindgen-tests/tests/expectations/tests/weird_bitfields.rs index f76189eb74..078fc74807 100644 --- a/bindgen-tests/tests/expectations/tests/weird_bitfields.rs +++ b/bindgen-tests/tests/expectations/tests/weird_bitfields.rs @@ -83,6 +83,10 @@ where } } } +pub type nsStyleSVGOpacitySource_ctype = ::std::os::raw::c_uint; +pub const nsStyleSVGOpacitySource_eStyleSVGOpacitySource_Normal: nsStyleSVGOpacitySource_ctype = 0; +pub const nsStyleSVGOpacitySource_eStyleSVGOpacitySource_ContextFillOpacity: nsStyleSVGOpacitySource_ctype = 1; +pub const nsStyleSVGOpacitySource_eStyleSVGOpacitySource_ContextStrokeOpacity: nsStyleSVGOpacitySource_ctype = 2; #[repr(u32)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)] pub enum nsStyleSVGOpacitySource { diff --git a/bindgen-tests/tests/headers/issue-2646.h b/bindgen-tests/tests/headers/issue-2646.h new file mode 100644 index 0000000000..63068780eb --- /dev/null +++ b/bindgen-tests/tests/headers/issue-2646.h @@ -0,0 +1,30 @@ +// bindgen-flags: --rustified-enum 'Plain.*' --rustified-enum 'TryFromRaw.*=try_from_raw' --rustified-enum='FromRawUnchecked.*=from_raw_unchecked' --rustified-enum='Both.*=try_from_raw,from_raw_unchecked' --rustified-enum 'NonExhaustive.*=non_exhaustive' + +enum Plain { + Plain1, + Plain2, + Plain3 +}; + +enum TryFromRaw { + TFR1 = -1, + TFR2 = 5, + TFR3 +}; + +enum FromRawUnchecked { + FRU1 = 6, + FRU2 = 10, + FRU3 = 11, +}; + +enum Both { + Both1, + Both2 = -1, + Both3, +}; + +enum NonExhaustive { + Ex1, + Ex2, +}; diff --git a/bindgen/codegen/mod.rs b/bindgen/codegen/mod.rs index 741a3fbe43..e343a0f243 100644 --- a/bindgen/codegen/mod.rs +++ b/bindgen/codegen/mod.rs @@ -3020,6 +3020,12 @@ pub enum EnumVariation { Rust { /// Indicates whether the generated struct should be `#[non_exhaustive]` non_exhaustive: bool, + /// Indicates whether the generated struct should have a safe conversion from an integer + /// value. + safe_conversion: bool, + /// Indicates whether the generated struct should have an unsafe conversion from an integer + /// value. + unsafe_conversion: bool, }, /// The code for this enum will use a newtype NewType { @@ -3051,11 +3057,20 @@ impl fmt::Display for EnumVariation { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let s = match self { Self::Rust { - non_exhaustive: false, - } => "rust", - Self::Rust { - non_exhaustive: true, - } => "rust_non_exhaustive", + non_exhaustive, + safe_conversion, + unsafe_conversion, + .. + } => match (non_exhaustive, safe_conversion, unsafe_conversion) { + (false, false, false) => "rust", + (false, true, true) => "rust_conversions", + (false, true, _) => "rust_safe_conversion", + (false, _, true) => "rust_unsafe_conversion", + (true, false, false) => "rust_non_exhaustive", + (true, true, true) => "rust_non_exhaustive_conversions", + (true, true, _) => "rust_non_exhaustive_safe_conversion", + (true, _, true) => "rust_non_exhaustive_unsafe_conversion", + }, Self::NewType { is_bitfield: true, .. } => "bitfield", @@ -3084,10 +3099,46 @@ impl std::str::FromStr for EnumVariation { match s { "rust" => Ok(EnumVariation::Rust { non_exhaustive: false, + safe_conversion: false, + unsafe_conversion: false, + }), + "rust_conversions" => Ok(EnumVariation::Rust { + non_exhaustive: false, + safe_conversion: true, + unsafe_conversion: true, + }), + "rust_safe_conversion" => Ok(EnumVariation::Rust { + non_exhaustive: false, + safe_conversion: true, + unsafe_conversion: false, + }), + "rust_unsafe_conversion" => Ok(EnumVariation::Rust { + non_exhaustive: false, + safe_conversion: false, + unsafe_conversion: true, }), "rust_non_exhaustive" => Ok(EnumVariation::Rust { non_exhaustive: true, + safe_conversion: false, + unsafe_conversion: false, + }), + "rust_non_exhaustive_conversions" => Ok(EnumVariation::Rust { + non_exhaustive: true, + safe_conversion: true, + unsafe_conversion: true, + }), + "rust_non_exhaustive_safe_conversion" => Ok(EnumVariation::Rust { + non_exhaustive: true, + safe_conversion: true, + unsafe_conversion: false, }), + "rust_non_exhaustive_unsafe_conversion" => { + Ok(EnumVariation::Rust { + non_exhaustive: true, + safe_conversion: false, + unsafe_conversion: true, + }) + } "bitfield" => Ok(EnumVariation::NewType { is_bitfield: true, is_global: false, @@ -3119,8 +3170,13 @@ enum EnumBuilder<'a> { Rust { attrs: Vec, ident: Ident, - tokens: proc_macro2::TokenStream, + typedef: syn::Ident, + const_tokens: Vec, + rustified_tokens: Vec, + safe_conversion_tokens: Vec, emitted_any_variants: bool, + safe_conversion: bool, + unsafe_conversion: bool, }, NewType { canonical_name: &'a str, @@ -3148,7 +3204,7 @@ impl<'a> EnumBuilder<'a> { fn new( name: &'a str, mut attrs: Vec, - repr: syn::Type, + repr: EnumRepr, enum_variation: EnumVariation, has_typedef: bool, ) -> Self { @@ -3158,29 +3214,64 @@ impl<'a> EnumBuilder<'a> { EnumVariation::NewType { is_bitfield, is_global, - } => EnumBuilder::NewType { - canonical_name: name, - tokens: quote! { - #( #attrs )* - pub struct #ident (pub #repr); - }, - is_bitfield, - is_global, - }, + } => { + let repr = match repr { + EnumRepr::Other(r) => r, + _ => panic!( + "Should never get this variant for new type enum" + ), + }; + EnumBuilder::NewType { + canonical_name: name, + tokens: quote! { + #( #attrs )* + pub struct #ident (pub #repr); + }, + is_bitfield, + is_global, + } + } - EnumVariation::Rust { .. } => { + EnumVariation::Rust { + safe_conversion, + unsafe_conversion, + .. + } => { + let (untranslated_repr, translated_repr) = match repr { + EnumRepr::Rust(un_r, t_r) => (un_r, t_r), + _ => panic!( + "Should never get this variant for rustified enum" + ), + }; + let ctype = Ident::new( + format!("{}_ctype", ident.to_string()).as_str(), + Span::call_site(), + ); // `repr` is guaranteed to be Rustified in Enum::codegen - attrs.insert(0, quote! { #[repr( #repr )] }); - let tokens = quote!(); + attrs.insert(0, quote! { #[repr( #translated_repr)] }); EnumBuilder::Rust { attrs, + const_tokens: vec![quote! { + pub type #ctype = #untranslated_repr; + }], ident, - tokens, + typedef: ctype, + rustified_tokens: vec![], + safe_conversion_tokens: vec![], emitted_any_variants: false, + safe_conversion, + unsafe_conversion, } } EnumVariation::Consts => { + let repr = match repr { + EnumRepr::Other(r) => r, + _ => { + panic!("Should never get this variant for consts enum") + } + }; + let mut variants = Vec::new(); if !has_typedef { @@ -3194,6 +3285,13 @@ impl<'a> EnumBuilder<'a> { } EnumVariation::ModuleConsts => { + let repr = match repr { + EnumRepr::Other(r) => r, + _ => { + panic!("Should never get this variant for module consts enum") + } + }; + let ident = Ident::new( CONSTIFIED_ENUM_MODULE_REPR_NAME, Span::call_site(), @@ -3213,7 +3311,7 @@ impl<'a> EnumBuilder<'a> { /// Add a variant to this enum. fn with_variant( - self, + mut self, ctx: &BindgenContext, variant: &EnumVariant, mangling_prefix: Option<&str>, @@ -3223,13 +3321,17 @@ impl<'a> EnumBuilder<'a> { ) -> Self { let variant_name = ctx.rust_mangle(variant.name()); let is_rust_enum = self.is_rust_enum(); - let expr = match variant.val() { + let (is_bool, expr) = match variant.val() { EnumVariantValue::Boolean(v) if is_rust_enum => { - helpers::ast_ty::uint_expr(v as u64) + (true, helpers::ast_ty::uint_expr(v as u64)) + } + EnumVariantValue::Boolean(v) => (true, quote!(#v)), + EnumVariantValue::Signed(v) => { + (false, helpers::ast_ty::int_expr(v)) + } + EnumVariantValue::Unsigned(v) => { + (false, helpers::ast_ty::uint_expr(v)) } - EnumVariantValue::Boolean(v) => quote!(#v), - EnumVariantValue::Signed(v) => helpers::ast_ty::int_expr(v), - EnumVariantValue::Unsigned(v) => helpers::ast_ty::uint_expr(v), }; let mut doc = quote! {}; @@ -3242,22 +3344,40 @@ impl<'a> EnumBuilder<'a> { match self { EnumBuilder::Rust { - attrs, - ident, - tokens, - emitted_any_variants: _, + ref ident, + ref typedef, + ref mut const_tokens, + ref mut rustified_tokens, + ref mut safe_conversion_tokens, + ref mut emitted_any_variants, + .. } => { let name = ctx.rust_ident(variant_name); - EnumBuilder::Rust { - attrs, - ident, - tokens: quote! { - #tokens - #doc - #name = #expr, - }, - emitted_any_variants: true, - } + let const_name = Ident::new( + format!("{}_{}", ident.to_string(), name.to_string()) + .as_str(), + Span::call_site(), + ); + let add_as = if is_bool { + quote! { as #typedef } + } else { + quote!() + }; + const_tokens.push(quote! { + #doc + pub const #const_name: #typedef = #expr #add_as ; + }); + rustified_tokens.push(quote! { + #doc + #name = #expr , + }); + safe_conversion_tokens.push(quote! { + #expr => Ok(#ident::#name) , + }); + + *emitted_any_variants = true; + + self } EnumBuilder::NewType { @@ -3339,21 +3459,75 @@ impl<'a> EnumBuilder<'a> { EnumBuilder::Rust { attrs, ident, - tokens, + typedef, + const_tokens, + rustified_tokens, + safe_conversion_tokens, emitted_any_variants, + safe_conversion, + unsafe_conversion, .. } => { let variants = if !emitted_any_variants { - quote!(__bindgen_cannot_repr_c_on_empty_enum = 0) + vec![quote!(__bindgen_cannot_repr_c_on_empty_enum = 0)] + } else { + rustified_tokens + }; + + let safe_conversion = if safe_conversion { + let prefix = ctx.trait_prefix(); + let error_type = Ident::new( + format!("{}Error", ident.to_string()).as_str(), + Span::call_site(), + ); + quote! { + pub struct #error_type(#typedef); + + impl #error_type { + #[must_use] + pub fn value(&self) -> #typedef { + self.0 + } + } + + impl #prefix::convert::TryFrom<#typedef> for #ident { + type Error = #error_type; + + fn try_from(v: #typedef) -> Result { + match v { + #( #safe_conversion_tokens )* + _ => #error_type(v) + } + } + } + } } else { - tokens + quote!() + }; + + let unsafe_conversion = if unsafe_conversion { + quote! { + impl #ident { + const unsafe fn from_ctype_unchecked(v: #typedef) -> Self { + std::mem::transmute(v) + } + } + } + } else { + quote!() }; quote! { + #( #const_tokens )* + #( #attrs )* pub enum #ident { - #variants + #( #variants )* } + + #safe_conversion + + #unsafe_conversion } } EnumBuilder::NewType { @@ -3428,6 +3602,76 @@ impl<'a> EnumBuilder<'a> { } } +fn handle_tranlation<'a>( + ctx: &BindgenContext, + layout: Option<&Layout>, + item: &Item, + repr: Option<&Type>, + translate: bool, +) -> syn::Type { + match repr { + Some(repr) + if !ctx.options().translate_enum_integer_types && !translate => + { + repr.to_rust_ty_or_opaque(ctx, item) + } + repr => { + // An enum's integer type is translated to a native Rust + // integer type in 3 cases: + // * the enum is Rustified and we need a translated type for + // the repr attribute + // * the representation couldn't be determined from the C source + // * it was explicitly requested as a bindgen option + + let kind = match repr { + Some(repr) => match *repr.canonical_type(ctx).kind() { + TypeKind::Int(int_kind) => int_kind, + _ => panic!("Unexpected type as enum repr"), + }, + None => { + warn!( + "Guessing type of enum! Forward declarations of enums \ + shouldn't be legal!" + ); + IntKind::Int + } + }; + + let signed = kind.is_signed(); + let size = layout + .map(|l| l.size) + .or_else(|| kind.known_size()) + .unwrap_or(0); + + let translated = match (signed, size) { + (true, 1) => IntKind::I8, + (false, 1) => IntKind::U8, + (true, 2) => IntKind::I16, + (false, 2) => IntKind::U16, + (true, 4) => IntKind::I32, + (false, 4) => IntKind::U32, + (true, 8) => IntKind::I64, + (false, 8) => IntKind::U64, + _ => { + warn!( + "invalid enum decl: signed: {}, size: {}", + signed, size + ); + IntKind::I32 + } + }; + + Type::new(None, None, TypeKind::Int(translated), false) + .to_rust_ty_or_opaque(ctx, item) + } + } +} + +enum EnumRepr { + Rust(syn::Type, syn::Type), + Other(syn::Type), +} + impl CodeGenerator for Enum { type Extra = Item; type Return = (); @@ -3447,71 +3691,37 @@ impl CodeGenerator for Enum { let layout = enum_ty.layout(ctx); let variation = self.computed_enum_variation(ctx, item); - let repr_translated; - let repr = match self.repr().map(|repr| ctx.resolve_type(repr)) { - Some(repr) - if !ctx.options().translate_enum_integer_types && - !variation.is_rust() => - { - repr - } - repr => { - // An enum's integer type is translated to a native Rust - // integer type in 3 cases: - // * the enum is Rustified and we need a translated type for - // the repr attribute - // * the representation couldn't be determined from the C source - // * it was explicitly requested as a bindgen option - - let kind = match repr { - Some(repr) => match *repr.canonical_type(ctx).kind() { - TypeKind::Int(int_kind) => int_kind, - _ => panic!("Unexpected type as enum repr"), - }, - None => { - warn!( - "Guessing type of enum! Forward declarations of enums \ - shouldn't be legal!" - ); - IntKind::Int - } - }; - - let signed = kind.is_signed(); - let size = layout - .map(|l| l.size) - .or_else(|| kind.known_size()) - .unwrap_or(0); - - let translated = match (signed, size) { - (true, 1) => IntKind::I8, - (false, 1) => IntKind::U8, - (true, 2) => IntKind::I16, - (false, 2) => IntKind::U16, - (true, 4) => IntKind::I32, - (false, 4) => IntKind::U32, - (true, 8) => IntKind::I64, - (false, 8) => IntKind::U64, - _ => { - warn!( - "invalid enum decl: signed: {}, size: {}", - signed, size - ); - IntKind::I32 - } - }; - - repr_translated = - Type::new(None, None, TypeKind::Int(translated), false); - &repr_translated - } + let repr = if variation.is_rust() { + EnumRepr::Rust( + handle_tranlation( + ctx, + layout.as_ref(), + item, + self.repr().map(|repr| ctx.resolve_type(repr)), + false, + ), + handle_tranlation( + ctx, + layout.as_ref(), + item, + self.repr().map(|repr| ctx.resolve_type(repr)), + true, + ), + ) + } else { + EnumRepr::Other(handle_tranlation( + ctx, + layout.as_ref(), + item, + self.repr().map(|repr| ctx.resolve_type(repr)), + false, + )) }; - let mut attrs = vec![]; // TODO(emilio): Delegate this to the builders? match variation { - EnumVariation::Rust { non_exhaustive } => { + EnumVariation::Rust { non_exhaustive, .. } => { if non_exhaustive && ctx.options().rust_features().non_exhaustive { @@ -3603,7 +3813,6 @@ impl CodeGenerator for Enum { }); } - let repr = repr.to_rust_ty_or_opaque(ctx, item); let has_typedef = ctx.is_enum_typedef_combo(item.id()); let mut builder = diff --git a/bindgen/ir/enum_ty.rs b/bindgen/ir/enum_ty.rs index 70cf0eae88..c080668804 100644 --- a/bindgen/ir/enum_ty.rs +++ b/bindgen/ir/enum_ty.rs @@ -9,6 +9,75 @@ use crate::ir::annotations::Annotations; use crate::parse::ParseError; use crate::regex_set::RegexSet; +use std::fmt::{self, Display}; +use std::ops::Deref; +use std::str::FromStr; + +/// Represents option for rustified enum generation. +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub enum RustEnumOption { + /// Add non-exhaustive attribute to Rust enum. + NonExhaustive, + /// Add safe TryFrom conversion from integer value. + TryFromRaw, + /// Provide an unsafe wrapper for transmute from integer value. + FromRawUnchecked, +} + +impl FromStr for RustEnumOption { + type Err = String; + + fn from_str(s: &str) -> Result { + match s { + "non_exhaustive" => Ok(Self::NonExhaustive), + "try_from_raw" => Ok(Self::TryFromRaw), + "from_raw_unchecked" => Ok(Self::FromRawUnchecked), + _ => Err(format!( + "Invalid or unknown rustified struct option {:?}", + s + )), + } + } +} + +/// Collection of RustEnumOption values. +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub struct RustEnumOptions(Vec); + +impl Deref for RustEnumOptions { + type Target = Vec; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl FromStr for RustEnumOptions { + type Err = String; + + fn from_str(s: &str) -> Result { + Ok(RustEnumOptions( + s.split(",").filter(|s| s != &"").try_fold( + Vec::new(), + |mut vec, opt| { + vec.push(RustEnumOption::from_str(opt)?); + Result::<_, String>::Ok(vec) + }, + )?, + )) + } +} + +impl Display for RustEnumOption { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + RustEnumOption::NonExhaustive => write!(f, "non_exhaustive"), + RustEnumOption::TryFromRaw => write!(f, "try_from_raw"), + RustEnumOption::FromRawUnchecked => write!(f, "from_raw_unchecked"), + } + } +} + /// An enum representing custom handling that can be given to a variant. #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum EnumVariantCustomBehavior { @@ -209,22 +278,6 @@ impl Enum { is_bitfield: false, is_global: true, } - } else if self.is_matching_enum( - ctx, - &ctx.options().rustified_enums, - item, - ) { - EnumVariation::Rust { - non_exhaustive: false, - } - } else if self.is_matching_enum( - ctx, - &ctx.options().rustified_non_exhaustive_enums, - item, - ) { - EnumVariation::Rust { - non_exhaustive: true, - } } else if self.is_matching_enum( ctx, &ctx.options().constified_enums, @@ -232,7 +285,22 @@ impl Enum { ) { EnumVariation::Consts } else { - ctx.options().default_enum_style + let matches = ctx + .options() + .rustified_enums + .iter() + .find(|(_, regex)| self.is_matching_enum(ctx, regex, item)); + match matches { + Some((options, _)) => EnumVariation::Rust { + non_exhaustive: options + .contains(&RustEnumOption::NonExhaustive), + safe_conversion: options + .contains(&RustEnumOption::TryFromRaw), + unsafe_conversion: options + .contains(&RustEnumOption::FromRawUnchecked), + }, + None => ctx.options().default_enum_style, + } } } } diff --git a/bindgen/lib.rs b/bindgen/lib.rs index 3bf0bc3c1d..e1158f2911 100644 --- a/bindgen/lib.rs +++ b/bindgen/lib.rs @@ -54,6 +54,7 @@ pub use codegen::{ pub use features::RUST_TARGET_STRINGS; pub use features::{RustTarget, LATEST_STABLE_RUST}; pub use ir::annotations::FieldVisibilityKind; +pub use ir::enum_ty::RustEnumOptions; pub use ir::function::Abi; pub use regex_set::RegexSet; @@ -440,7 +441,7 @@ impl Builder { impl BindgenOptions { fn build(&mut self) { - const REGEX_SETS_LEN: usize = 29; + const REGEX_SETS_LEN: usize = 27; let regex_sets: [_; REGEX_SETS_LEN] = [ &mut self.blocklisted_types, @@ -459,8 +460,6 @@ impl BindgenOptions { &mut self.constified_enum_modules, &mut self.newtype_enums, &mut self.newtype_global_enums, - &mut self.rustified_enums, - &mut self.rustified_non_exhaustive_enums, &mut self.type_alias, &mut self.new_type_alias, &mut self.new_type_alias_deref, @@ -477,7 +476,9 @@ impl BindgenOptions { let record_matches = self.record_matches; #[cfg(feature = "experimental")] { - let sets_len = REGEX_SETS_LEN + self.abi_overrides.len(); + let sets_len = REGEX_SETS_LEN + + self.abi_overrides.len() + + self.rustified_enums.len(); let names = if self.emit_diagnostics { <[&str; REGEX_SETS_LEN]>::into_iter([ "--blocklist-type", @@ -494,8 +495,6 @@ impl BindgenOptions { "--bitfield-enum", "--newtype-enum", "--newtype-global-enum", - "--rustified-enum", - "--rustified-enum-non-exhaustive", "--constified-enum-module", "--constified-enum", "--type-alias", @@ -511,6 +510,9 @@ impl BindgenOptions { "--must-use", ]) .chain((0..self.abi_overrides.len()).map(|_| "--override-abi")) + .chain( + (0..self.rustified_enums.len()).map(|_| "--rustified-enum"), + ) .map(Some) .collect() } else { @@ -527,6 +529,9 @@ impl BindgenOptions { for regex_set in self.abi_overrides.values_mut().chain(regex_sets) { regex_set.build(record_matches); } + for regex_set in self.rustified_enums.values_mut() { + regex_set.build(record_matches); + } let rust_target = self.rust_target; #[allow(deprecated)] diff --git a/bindgen/options/mod.rs b/bindgen/options/mod.rs index e4c03ecd4d..727c91fc22 100644 --- a/bindgen/options/mod.rs +++ b/bindgen/options/mod.rs @@ -18,6 +18,7 @@ use crate::CodegenConfig; use crate::FieldVisibilityKind; use crate::Formatter; use crate::HashMap; +use crate::RustEnumOptions; use crate::DEFAULT_ANON_FIELDS_PREFIX; use std::env; @@ -454,7 +455,7 @@ options! { as_args: "--newtype-global-enum", }, /// `enum`s marked as Rust `enum`s. - rustified_enums: RegexSet { + rustified_enums: HashMap { methods: { regex_option! { /// Mark the given `enum` as a Rust `enum`. @@ -465,13 +466,21 @@ options! { /// **Use this with caution**, creating an instance of a Rust `enum` with an /// invalid value will cause undefined behaviour. To avoid this, use the /// [`Builder::newtype_enum`] style instead. - pub fn rustified_enum>(mut self, arg: T) -> Builder { - self.options.rustified_enums.insert(arg); + pub fn rustified_enum>(mut self, options: RustEnumOptions, arg: T) -> Builder { + self.options.rustified_enums.entry(options).or_default().insert(arg.into()); self } } }, - as_args: "--rustified-enum", + as_args: |overrides, args| { + for (options, set) in overrides { + let options = options.iter().map(|item| item.to_string()).collect::>(); + for item in set.get_items() { + args.push("--rustified-enum".to_owned()); + args.push(format!("{}={}", item, options.join(","))); + } + } + }, }, /// `enum`s marked as non-exhaustive Rust `enum`s. rustified_non_exhaustive_enums: RegexSet {