diff --git a/docs.md b/docs.md index ad5dcc18..bd66f5d1 100644 --- a/docs.md +++ b/docs.md @@ -133,10 +133,12 @@ You can learn about all of the different repr attributes [by reading Rust's refe * `#[repr(C)]`: give this struct/union/enum the same layout and ABI C would * `#[repr(u8, u16, ... etc)]`: give this enum the same layout and ABI as the given integer type -* `#[repr(transparent)]`: give this single-field struct the same ABI as its field (useful for newtyping integers but keeping the integer ABI) +* `#[repr(transparent)]`: give this single-field struct or enum the same ABI as its field (useful for newtyping integers but keeping the integer ABI) cbindgen supports the `#[repr(align(N))]` and `#[repr(packed)]` attributes, but currently does not support `#[repr(packed(N))]`. +cbindgen supports using `repr(transparent)` on single-field structs and single-variant enums with fields. Transparent structs and enums are exported as typedefs that alias the underlying single field's type. + cbindgen also supports using `repr(C)`/`repr(u8)` on non-C-like enums (enums with fields). This gives a C-compatible tagged union layout, as [defined by this RFC 2195][really-tagged-unions]. `repr(C)` will give a simpler layout that is perhaps more intuitive, while `repr(u8)` will produce a more compact layout. If you ensure everything has a guaranteed repr, then cbindgen will generate definitions for: @@ -407,9 +409,17 @@ The rest are just local overrides for the same options found in the cbindgen.tom ### Enum Annotations -* enum-trailing-values=\[variant1, variant2, ...\] -- add the following fieldless enum variants to the end of the enum's definition. These variant names *will* have the enum's renaming rules applied. +* enum-trailing-values=\[variant1, variant2, ...\] -- add the following fieldless enum variants to + the end of the enum's definition. These variant names *will* have the enum's renaming rules + applied. + + WARNING: if any of these values are ever passed into Rust, behaviour will be Undefined. Rust does + not know about them, and will assume they cannot happen. -WARNING: if any of these values are ever passed into Rust, behaviour will be Undefined. Rust does not know about them, and will assume they cannot happen. +* transparent-typedef -- when emitting the typedef for a transparent enum, mark it as + transparent. All references to the enum will be replaced with the type of its underlying NZST + variant field, effectively making the enum invisible on the FFI side. For exmaples of how this + works, see [Struct Annotations](#struct-annotations). The rest are just local overrides for the same options found in the cbindgen.toml: diff --git a/src/bindgen/ir/enumeration.rs b/src/bindgen/ir/enumeration.rs index f5a0bbee..2f8a2c72 100644 --- a/src/bindgen/ir/enumeration.rs +++ b/src/bindgen/ir/enumeration.rs @@ -12,7 +12,7 @@ use crate::bindgen::dependencies::Dependencies; use crate::bindgen::ir::{ AnnotationSet, AnnotationValue, Cfg, ConditionWrite, DeprecatedNoteKind, Documentation, Field, GenericArgument, GenericParams, GenericPath, Item, ItemContainer, Literal, Path, Repr, - ReprStyle, Struct, ToCondition, TransparentTypeEraser, Type, + ReprStyle, Struct, ToCondition, TransparentTypeEraser, Type, Typedef, }; use crate::bindgen::language_backend::LanguageBackend; use crate::bindgen::library::Library; @@ -311,6 +311,10 @@ impl Enum { repr.style != ReprStyle::C } + pub fn is_transparent(&self) -> bool { + self.repr.style == ReprStyle::Transparent + } + pub fn add_monomorphs(&self, library: &Library, out: &mut Monomorphs) { if self.is_generic() { return; @@ -438,6 +442,31 @@ impl Enum { documentation, } } + + /// Attempts to convert this enum to a typedef (only works for transparent enums). + pub fn as_typedef(&self) -> Option { + if self.is_transparent() { + // NOTE: A `#[repr(transparent)]` enum fails to compile unless it has + // exactly one NZT variant. So we can safely assume the variant exists. + if let Some(EnumVariant { + body: VariantBody::Body { ref body, .. }, + .. + }) = self.variants.first() + { + // NOTE: Inline tagged enum has the tag field first, ignore it. + if let Some(field) = body.fields.last() { + return Some(Typedef::new_from_item_field(self, field)); + } + } + } + None + } + + // Transparent enums become typedefs, so try converting to typedef and recurse on that. + pub fn as_transparent_alias(&self, generics: &[GenericArgument]) -> Option { + self.as_typedef() + .and_then(|t| t.as_transparent_alias(generics)) + } } impl Item for Enum { @@ -470,7 +499,9 @@ impl Item for Enum { } fn collect_declaration_types(&self, resolver: &mut DeclarationTypeResolver) { - if self.tag.is_some() { + if self.repr.style == ReprStyle::Transparent { + resolver.add_none(&self.path); + } else if self.tag.is_some() { if self.repr.style == ReprStyle::C { resolver.add_struct(&self.path); } else { diff --git a/src/bindgen/ir/structure.rs b/src/bindgen/ir/structure.rs index aaa7a3e0..7ed1d241 100644 --- a/src/bindgen/ir/structure.rs +++ b/src/bindgen/ir/structure.rs @@ -150,7 +150,7 @@ impl Struct { // NOTE: A `#[repr(transparent)]` struct with 2+ NZT fields fails to compile, but 0 // fields is allowed for some strange reason. Don't emit the typedef in that case. if let Some(field) = self.fields.first() { - return Some(Typedef::new_from_struct_field(self, field)); + return Some(Typedef::new_from_item_field(self, field)); } else { error!( "Cannot convert empty transparent struct {} to typedef", diff --git a/src/bindgen/ir/ty.rs b/src/bindgen/ir/ty.rs index c396eeda..73393702 100644 --- a/src/bindgen/ir/ty.rs +++ b/src/bindgen/ir/ty.rs @@ -569,6 +569,7 @@ impl Type { ItemContainer::OpaqueItem(o) => o.as_transparent_alias(&generics), ItemContainer::Typedef(t) => t.as_transparent_alias(&generics), ItemContainer::Struct(s) => s.as_transparent_alias(&generics), + ItemContainer::Enum(e) => e.as_transparent_alias(&generics), _ => None, }; if let Some(mut ty) = aliased_ty { diff --git a/src/bindgen/ir/typedef.rs b/src/bindgen/ir/typedef.rs index 580cea33..becfefe6 100644 --- a/src/bindgen/ir/typedef.rs +++ b/src/bindgen/ir/typedef.rs @@ -11,7 +11,7 @@ use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver; use crate::bindgen::dependencies::Dependencies; use crate::bindgen::ir::{ AnnotationSet, Cfg, Documentation, Field, GenericArgument, GenericParams, Item, ItemContainer, - Path, Struct, TransparentTypeEraser, Type, + Path, TransparentTypeEraser, Type, }; use crate::bindgen::library::Library; use crate::bindgen::mangle; @@ -69,12 +69,12 @@ impl Typedef { } } - // Used to convert a transparent Struct to a Typedef. - pub fn new_from_struct_field(item: &Struct, field: &Field) -> Self { + // Used to convert a transparent Struct or Enum to a Typedef. + pub fn new_from_item_field(item: &impl Item, field: &Field) -> Self { Self { path: item.path().clone(), export_name: item.export_name().to_string(), - generic_params: item.generic_params.clone(), + generic_params: item.generic_params().clone(), aliased: field.ty.clone(), cfg: item.cfg().cloned(), annotations: item.annotations().clone(), diff --git a/src/bindgen/language_backend/mod.rs b/src/bindgen/language_backend/mod.rs index 065bade2..6d9e5454 100644 --- a/src/bindgen/language_backend/mod.rs +++ b/src/bindgen/language_backend/mod.rs @@ -157,6 +157,27 @@ pub trait LanguageBackend: Sized { } } + /// If the enum is transparent, emit a typedef of its NZST field type instead. + fn write_enum_or_typedef( + &mut self, + out: &mut SourceWriter, + e: &Enum, + _b: &Bindings, + ) { + if let Some(typedef) = e.as_typedef() { + self.write_type_def(out, &typedef); + // TODO: Associated constants are not supported for enums. Should they be? Rust + // enum exports as a union, and C/C++ at least supports static union members? + // + //for constant in &e.associated_constants { + // out.new_line(); + // constant.write(b.config, self, out, Some(e)); + //} + } else { + self.write_enum(out, e); + } + } + fn write_items(&mut self, out: &mut SourceWriter, b: &Bindings) { for item in &b.items { if item @@ -172,7 +193,7 @@ pub trait LanguageBackend: Sized { match *item { ItemContainer::Constant(..) => unreachable!(), ItemContainer::Static(..) => unreachable!(), - ItemContainer::Enum(ref x) => self.write_enum(out, x), + ItemContainer::Enum(ref x) => self.write_enum_or_typedef(out, x, b), ItemContainer::Struct(ref x) => self.write_struct_or_typedef(out, x, b), ItemContainer::Union(ref x) => self.write_union(out, x), ItemContainer::OpaqueItem(ref x) => self.write_opaque_item(out, x), diff --git a/tests/expectations/const_transparent.compat.c b/tests/expectations/const_transparent.compat.c index 62100f77..8d296ae8 100644 --- a/tests/expectations/const_transparent.compat.c +++ b/tests/expectations/const_transparent.compat.c @@ -3,6 +3,8 @@ #include #include +#define TransparentEnum_ASSOC_ENUM_FOO 8 + typedef uint8_t TransparentStruct; #define TransparentStruct_ASSOC_STRUCT_FOO 1 #define TransparentStruct_ASSOC_STRUCT_BAR 2 @@ -10,6 +12,8 @@ typedef uint8_t TransparentStruct; typedef uint8_t TransparentTupleStruct; +typedef uint8_t TransparentEnum; + #define STRUCT_FOO 4 #define STRUCT_BAR 5 @@ -17,3 +21,11 @@ typedef uint8_t TransparentTupleStruct; + + + + + + + + diff --git a/tests/expectations/const_transparent.cpp b/tests/expectations/const_transparent.cpp index 285fd8ea..6a483045 100644 --- a/tests/expectations/const_transparent.cpp +++ b/tests/expectations/const_transparent.cpp @@ -4,6 +4,8 @@ #include #include +constexpr static const int64_t TransparentEnum_ASSOC_ENUM_FOO = 8; + template using Wrapper = T; @@ -17,6 +19,11 @@ using TransparentTupleStruct = uint8_t; template using TransparentStructWithErasedField = T; +using TransparentEnum = uint8_t; + +template +using TransparentWrapperEnum = T; + constexpr static const TransparentStruct STRUCT_FOO = 4; constexpr static const TransparentTupleStruct STRUCT_BAR = 5; @@ -24,3 +31,11 @@ constexpr static const TransparentTupleStruct STRUCT_BAR = 5; constexpr static const TransparentStruct STRUCT_BAZ = 6; constexpr static const TransparentStructWithErasedField COMPLEX = 7; + + + + + + + + diff --git a/tests/expectations/const_transparent.pyx b/tests/expectations/const_transparent.pyx index c0993a3a..c1b5c838 100644 --- a/tests/expectations/const_transparent.pyx +++ b/tests/expectations/const_transparent.pyx @@ -6,6 +6,8 @@ cdef extern from *: cdef extern from *: + const int64_t TransparentEnum_ASSOC_ENUM_FOO # = 8 + ctypedef uint8_t TransparentStruct; const int64_t TransparentStruct_ASSOC_STRUCT_FOO # = 1 const TransparentStruct TransparentStruct_ASSOC_STRUCT_BAR # = 2 @@ -13,6 +15,8 @@ cdef extern from *: ctypedef uint8_t TransparentTupleStruct; + ctypedef uint8_t TransparentEnum; + const TransparentStruct STRUCT_FOO # = 4 const TransparentTupleStruct STRUCT_BAR # = 5 @@ -20,3 +24,11 @@ cdef extern from *: + + + + + + + + diff --git a/tests/expectations/transparent.c b/tests/expectations/transparent.c index 95ed6b20..9f968f52 100644 --- a/tests/expectations/transparent.c +++ b/tests/expectations/transparent.c @@ -13,48 +13,77 @@ typedef DummyStruct TransparentComplexWrappingStructTuple; typedef uint32_t TransparentPrimitiveWrappingStructTuple; -typedef DummyStruct TransparentComplexWrappingStructure; +typedef DummyStruct TransparentComplexWrappingStruct; -typedef uint32_t TransparentPrimitiveWrappingStructure; +typedef uint32_t TransparentPrimitiveWrappingStruct; -typedef DummyStruct TransparentComplexWrapper_i32; +typedef DummyStruct TransparentComplexWrapperStruct_i32; -typedef uint32_t TransparentPrimitiveWrapper_i32; +typedef uint32_t TransparentPrimitiveWrapperStruct_i32; -typedef uint32_t TransparentPrimitiveWithAssociatedConstants; -#define TransparentPrimitiveWithAssociatedConstants_ZERO 0 -#define TransparentPrimitiveWithAssociatedConstants_ONE 1 +typedef uint32_t TransparentPrimitiveStructWithAssociatedConstants; +#define TransparentPrimitiveStructWithAssociatedConstants_STRUCT_ZERO 0 +#define TransparentPrimitiveStructWithAssociatedConstants_STRUCT_ONE 1 -typedef const uint32_t *TransparentPointerWrappingStructure; +typedef const uint32_t *TransparentPointerWrappingStruct; typedef int32_t TransparentIntStruct; typedef DummyStruct TransparentComplexStruct; -typedef TransparentPrimitiveWrappingStructure TransparentTransparentStruct; +typedef TransparentPrimitiveWrappingStruct TransparentTransparentStruct; typedef uint32_t *TransparentNonNullStruct; typedef uint32_t *TransparentOptionNonNullStruct; +typedef DummyStruct TransparentComplexWrappingEnumTuple; + +typedef uint32_t TransparentPrimitiveWrappingEnumTuple; + +typedef DummyStruct TransparentComplexWrappingEnum; + +typedef uint32_t TransparentPrimitiveWrappingEnum; + +typedef DummyStruct TransparentComplexWrapperEnum_i32; + +typedef uint32_t TransparentPrimitiveWrapperEnum_i32; + +typedef uint32_t TransparentPrimitiveEnumWithAssociatedConstants; + +typedef const uint32_t *TransparentPointerWrappingEnum; + +typedef int32_t TransparentIntEnum; + +typedef DummyStruct TransparentComplexEnum; + +typedef TransparentPrimitiveWrappingEnum TransparentTransparentEnum; + +typedef uint32_t *TransparentNonNullEnum; + +typedef uint32_t *TransparentOptionNonNullEnum; + #define StructWithAssociatedConstantInImpl_STRUCT_TEN 10 + + + + #define EnumWithAssociatedConstantInImpl_ENUM_TEN 10 -void root(TransparentComplexWrappingStructTuple a, - TransparentPrimitiveWrappingStructTuple b, - TransparentComplexWrappingStructure c, - TransparentPrimitiveWrappingStructure d, - TransparentComplexWrapper_i32 e, - TransparentPrimitiveWrapper_i32 f, - TransparentPrimitiveWithAssociatedConstants g, - TransparentPointerWrappingStructure h, - StructWithAssociatedConstantInImpl i, - EnumWithAssociatedConstantInImpl j); +void struct_root(TransparentComplexWrappingStructTuple a, + TransparentPrimitiveWrappingStructTuple b, + TransparentComplexWrappingStruct c, + TransparentPrimitiveWrappingStruct d, + TransparentComplexWrapperStruct_i32 e, + TransparentPrimitiveWrapperStruct_i32 f, + TransparentPrimitiveStructWithAssociatedConstants g, + TransparentPointerWrappingStruct h, + StructWithAssociatedConstantInImpl i); void erased_root(uint32_t *a, uint32_t *b, - TransparentPrimitiveWrappingStructure c, + TransparentPrimitiveWrappingStruct c, uint32_t *d, TransparentIntStruct e, int32_t f, @@ -66,3 +95,28 @@ void erased_root(uint32_t *a, TransparentTransparentStruct l, TransparentNonNullStruct m, TransparentOptionNonNullStruct n); + +void enum_root(TransparentComplexWrappingEnumTuple a, + TransparentPrimitiveWrappingEnumTuple b, + TransparentComplexWrappingEnum c, + TransparentPrimitiveWrappingEnum d, + TransparentComplexWrapperEnum_i32 e, + TransparentPrimitiveWrapperEnum_i32 f, + TransparentPrimitiveEnumWithAssociatedConstants g, + TransparentPointerWrappingEnum h, + EnumWithAssociatedConstantInImpl i); + +void erased_enum_root(uint32_t *a, + uint32_t *b, + TransparentPrimitiveWrappingEnum c, + uint32_t *d, + TransparentIntEnum e, + int32_t f, + DummyStruct g, + uint32_t *h, + int32_t i, + TransparentIntEnum j, + TransparentComplexEnum k, + TransparentTransparentEnum l, + TransparentNonNullEnum m, + TransparentOptionNonNullEnum n); diff --git a/tests/expectations/transparent.compat.c b/tests/expectations/transparent.compat.c index 9ee8f750..945cbd3e 100644 --- a/tests/expectations/transparent.compat.c +++ b/tests/expectations/transparent.compat.c @@ -13,52 +13,81 @@ typedef DummyStruct TransparentComplexWrappingStructTuple; typedef uint32_t TransparentPrimitiveWrappingStructTuple; -typedef DummyStruct TransparentComplexWrappingStructure; +typedef DummyStruct TransparentComplexWrappingStruct; -typedef uint32_t TransparentPrimitiveWrappingStructure; +typedef uint32_t TransparentPrimitiveWrappingStruct; -typedef DummyStruct TransparentComplexWrapper_i32; +typedef DummyStruct TransparentComplexWrapperStruct_i32; -typedef uint32_t TransparentPrimitiveWrapper_i32; +typedef uint32_t TransparentPrimitiveWrapperStruct_i32; -typedef uint32_t TransparentPrimitiveWithAssociatedConstants; -#define TransparentPrimitiveWithAssociatedConstants_ZERO 0 -#define TransparentPrimitiveWithAssociatedConstants_ONE 1 +typedef uint32_t TransparentPrimitiveStructWithAssociatedConstants; +#define TransparentPrimitiveStructWithAssociatedConstants_STRUCT_ZERO 0 +#define TransparentPrimitiveStructWithAssociatedConstants_STRUCT_ONE 1 -typedef const uint32_t *TransparentPointerWrappingStructure; +typedef const uint32_t *TransparentPointerWrappingStruct; typedef int32_t TransparentIntStruct; typedef DummyStruct TransparentComplexStruct; -typedef TransparentPrimitiveWrappingStructure TransparentTransparentStruct; +typedef TransparentPrimitiveWrappingStruct TransparentTransparentStruct; typedef uint32_t *TransparentNonNullStruct; typedef uint32_t *TransparentOptionNonNullStruct; +typedef DummyStruct TransparentComplexWrappingEnumTuple; + +typedef uint32_t TransparentPrimitiveWrappingEnumTuple; + +typedef DummyStruct TransparentComplexWrappingEnum; + +typedef uint32_t TransparentPrimitiveWrappingEnum; + +typedef DummyStruct TransparentComplexWrapperEnum_i32; + +typedef uint32_t TransparentPrimitiveWrapperEnum_i32; + +typedef uint32_t TransparentPrimitiveEnumWithAssociatedConstants; + +typedef const uint32_t *TransparentPointerWrappingEnum; + +typedef int32_t TransparentIntEnum; + +typedef DummyStruct TransparentComplexEnum; + +typedef TransparentPrimitiveWrappingEnum TransparentTransparentEnum; + +typedef uint32_t *TransparentNonNullEnum; + +typedef uint32_t *TransparentOptionNonNullEnum; + #define StructWithAssociatedConstantInImpl_STRUCT_TEN 10 + + + + #define EnumWithAssociatedConstantInImpl_ENUM_TEN 10 #ifdef __cplusplus extern "C" { #endif // __cplusplus -void root(TransparentComplexWrappingStructTuple a, - TransparentPrimitiveWrappingStructTuple b, - TransparentComplexWrappingStructure c, - TransparentPrimitiveWrappingStructure d, - TransparentComplexWrapper_i32 e, - TransparentPrimitiveWrapper_i32 f, - TransparentPrimitiveWithAssociatedConstants g, - TransparentPointerWrappingStructure h, - StructWithAssociatedConstantInImpl i, - EnumWithAssociatedConstantInImpl j); +void struct_root(TransparentComplexWrappingStructTuple a, + TransparentPrimitiveWrappingStructTuple b, + TransparentComplexWrappingStruct c, + TransparentPrimitiveWrappingStruct d, + TransparentComplexWrapperStruct_i32 e, + TransparentPrimitiveWrapperStruct_i32 f, + TransparentPrimitiveStructWithAssociatedConstants g, + TransparentPointerWrappingStruct h, + StructWithAssociatedConstantInImpl i); void erased_root(uint32_t *a, uint32_t *b, - TransparentPrimitiveWrappingStructure c, + TransparentPrimitiveWrappingStruct c, uint32_t *d, TransparentIntStruct e, int32_t f, @@ -71,6 +100,31 @@ void erased_root(uint32_t *a, TransparentNonNullStruct m, TransparentOptionNonNullStruct n); +void enum_root(TransparentComplexWrappingEnumTuple a, + TransparentPrimitiveWrappingEnumTuple b, + TransparentComplexWrappingEnum c, + TransparentPrimitiveWrappingEnum d, + TransparentComplexWrapperEnum_i32 e, + TransparentPrimitiveWrapperEnum_i32 f, + TransparentPrimitiveEnumWithAssociatedConstants g, + TransparentPointerWrappingEnum h, + EnumWithAssociatedConstantInImpl i); + +void erased_enum_root(uint32_t *a, + uint32_t *b, + TransparentPrimitiveWrappingEnum c, + uint32_t *d, + TransparentIntEnum e, + int32_t f, + DummyStruct g, + uint32_t *h, + int32_t i, + TransparentIntEnum j, + TransparentComplexEnum k, + TransparentTransparentEnum l, + TransparentNonNullEnum m, + TransparentOptionNonNullEnum n); + #ifdef __cplusplus } // extern "C" #endif // __cplusplus diff --git a/tests/expectations/transparent.cpp b/tests/expectations/transparent.cpp index 45b66efa..87881043 100644 --- a/tests/expectations/transparent.cpp +++ b/tests/expectations/transparent.cpp @@ -14,52 +14,83 @@ using TransparentComplexWrappingStructTuple = DummyStruct; using TransparentPrimitiveWrappingStructTuple = uint32_t; -using TransparentComplexWrappingStructure = DummyStruct; +using TransparentComplexWrappingStruct = DummyStruct; -using TransparentPrimitiveWrappingStructure = uint32_t; +using TransparentPrimitiveWrappingStruct = uint32_t; template -using TransparentComplexWrapper = DummyStruct; +using TransparentComplexWrapperStruct = DummyStruct; template -using TransparentPrimitiveWrapper = uint32_t; +using TransparentPrimitiveWrapperStruct = uint32_t; -using TransparentPrimitiveWithAssociatedConstants = uint32_t; -constexpr static const TransparentPrimitiveWithAssociatedConstants TransparentPrimitiveWithAssociatedConstants_ZERO = 0; -constexpr static const TransparentPrimitiveWithAssociatedConstants TransparentPrimitiveWithAssociatedConstants_ONE = 1; +using TransparentPrimitiveStructWithAssociatedConstants = uint32_t; +constexpr static const TransparentPrimitiveStructWithAssociatedConstants TransparentPrimitiveStructWithAssociatedConstants_STRUCT_ZERO = 0; +constexpr static const TransparentPrimitiveStructWithAssociatedConstants TransparentPrimitiveStructWithAssociatedConstants_STRUCT_ONE = 1; -using TransparentPointerWrappingStructure = const uint32_t*; +using TransparentPointerWrappingStruct = const uint32_t*; using TransparentIntStruct = int32_t; using TransparentComplexStruct = DummyStruct; -using TransparentTransparentStruct = TransparentPrimitiveWrappingStructure; +using TransparentTransparentStruct = TransparentPrimitiveWrappingStruct; using TransparentNonNullStruct = uint32_t*; using TransparentOptionNonNullStruct = uint32_t*; -constexpr static const TransparentPrimitiveWrappingStructure StructWithAssociatedConstantInImpl_STRUCT_TEN = 10; +using TransparentComplexWrappingEnumTuple = DummyStruct; -constexpr static const TransparentPrimitiveWrappingStructure EnumWithAssociatedConstantInImpl_ENUM_TEN = 10; +using TransparentPrimitiveWrappingEnumTuple = uint32_t; + +using TransparentComplexWrappingEnum = DummyStruct; + +using TransparentPrimitiveWrappingEnum = uint32_t; + +template +using TransparentComplexWrapperEnum = DummyStruct; + +template +using TransparentPrimitiveWrapperEnum = uint32_t; + +using TransparentPrimitiveEnumWithAssociatedConstants = uint32_t; + +using TransparentPointerWrappingEnum = const uint32_t*; + +using TransparentIntEnum = int32_t; + +using TransparentComplexEnum = DummyStruct; + +using TransparentTransparentEnum = TransparentPrimitiveWrappingEnum; + +using TransparentNonNullEnum = uint32_t*; + +using TransparentOptionNonNullEnum = uint32_t*; + +constexpr static const TransparentPrimitiveWrappingStruct StructWithAssociatedConstantInImpl_STRUCT_TEN = 10; + + + + + +constexpr static const TransparentPrimitiveWrappingStruct EnumWithAssociatedConstantInImpl_ENUM_TEN = 10; extern "C" { -void root(TransparentComplexWrappingStructTuple a, - TransparentPrimitiveWrappingStructTuple b, - TransparentComplexWrappingStructure c, - TransparentPrimitiveWrappingStructure d, - TransparentComplexWrapper e, - TransparentPrimitiveWrapper f, - TransparentPrimitiveWithAssociatedConstants g, - TransparentPointerWrappingStructure h, - StructWithAssociatedConstantInImpl i, - EnumWithAssociatedConstantInImpl j); +void struct_root(TransparentComplexWrappingStructTuple a, + TransparentPrimitiveWrappingStructTuple b, + TransparentComplexWrappingStruct c, + TransparentPrimitiveWrappingStruct d, + TransparentComplexWrapperStruct e, + TransparentPrimitiveWrapperStruct f, + TransparentPrimitiveStructWithAssociatedConstants g, + TransparentPointerWrappingStruct h, + StructWithAssociatedConstantInImpl i); void erased_root(uint32_t *a, uint32_t *b, - TransparentPrimitiveWrappingStructure c, + TransparentPrimitiveWrappingStruct c, uint32_t *d, TransparentIntStruct e, int32_t f, @@ -72,4 +103,29 @@ void erased_root(uint32_t *a, TransparentNonNullStruct m, TransparentOptionNonNullStruct n); +void enum_root(TransparentComplexWrappingEnumTuple a, + TransparentPrimitiveWrappingEnumTuple b, + TransparentComplexWrappingEnum c, + TransparentPrimitiveWrappingEnum d, + TransparentComplexWrapperEnum e, + TransparentPrimitiveWrapperEnum f, + TransparentPrimitiveEnumWithAssociatedConstants g, + TransparentPointerWrappingEnum h, + EnumWithAssociatedConstantInImpl i); + +void erased_enum_root(uint32_t *a, + uint32_t *b, + TransparentPrimitiveWrappingEnum c, + uint32_t *d, + TransparentIntEnum e, + int32_t f, + DummyStruct g, + uint32_t *h, + int32_t i, + TransparentIntEnum j, + TransparentComplexEnum k, + TransparentTransparentEnum l, + TransparentNonNullEnum m, + TransparentOptionNonNullEnum n); + } // extern "C" diff --git a/tests/expectations/transparent.pyx b/tests/expectations/transparent.pyx index 8eee38f2..b328919e 100644 --- a/tests/expectations/transparent.pyx +++ b/tests/expectations/transparent.pyx @@ -19,48 +19,77 @@ cdef extern from *: ctypedef uint32_t TransparentPrimitiveWrappingStructTuple; - ctypedef DummyStruct TransparentComplexWrappingStructure; + ctypedef DummyStruct TransparentComplexWrappingStruct; - ctypedef uint32_t TransparentPrimitiveWrappingStructure; + ctypedef uint32_t TransparentPrimitiveWrappingStruct; - ctypedef DummyStruct TransparentComplexWrapper_i32; + ctypedef DummyStruct TransparentComplexWrapperStruct_i32; - ctypedef uint32_t TransparentPrimitiveWrapper_i32; + ctypedef uint32_t TransparentPrimitiveWrapperStruct_i32; - ctypedef uint32_t TransparentPrimitiveWithAssociatedConstants; - const TransparentPrimitiveWithAssociatedConstants TransparentPrimitiveWithAssociatedConstants_ZERO # = 0 - const TransparentPrimitiveWithAssociatedConstants TransparentPrimitiveWithAssociatedConstants_ONE # = 1 + ctypedef uint32_t TransparentPrimitiveStructWithAssociatedConstants; + const TransparentPrimitiveStructWithAssociatedConstants TransparentPrimitiveStructWithAssociatedConstants_STRUCT_ZERO # = 0 + const TransparentPrimitiveStructWithAssociatedConstants TransparentPrimitiveStructWithAssociatedConstants_STRUCT_ONE # = 1 - ctypedef const uint32_t *TransparentPointerWrappingStructure; + ctypedef const uint32_t *TransparentPointerWrappingStruct; ctypedef int32_t TransparentIntStruct; ctypedef DummyStruct TransparentComplexStruct; - ctypedef TransparentPrimitiveWrappingStructure TransparentTransparentStruct; + ctypedef TransparentPrimitiveWrappingStruct TransparentTransparentStruct; ctypedef uint32_t *TransparentNonNullStruct; ctypedef uint32_t *TransparentOptionNonNullStruct; - const TransparentPrimitiveWrappingStructure StructWithAssociatedConstantInImpl_STRUCT_TEN # = 10 + ctypedef DummyStruct TransparentComplexWrappingEnumTuple; - const TransparentPrimitiveWrappingStructure EnumWithAssociatedConstantInImpl_ENUM_TEN # = 10 + ctypedef uint32_t TransparentPrimitiveWrappingEnumTuple; - void root(TransparentComplexWrappingStructTuple a, - TransparentPrimitiveWrappingStructTuple b, - TransparentComplexWrappingStructure c, - TransparentPrimitiveWrappingStructure d, - TransparentComplexWrapper_i32 e, - TransparentPrimitiveWrapper_i32 f, - TransparentPrimitiveWithAssociatedConstants g, - TransparentPointerWrappingStructure h, - StructWithAssociatedConstantInImpl i, - EnumWithAssociatedConstantInImpl j); + ctypedef DummyStruct TransparentComplexWrappingEnum; + + ctypedef uint32_t TransparentPrimitiveWrappingEnum; + + ctypedef DummyStruct TransparentComplexWrapperEnum_i32; + + ctypedef uint32_t TransparentPrimitiveWrapperEnum_i32; + + ctypedef uint32_t TransparentPrimitiveEnumWithAssociatedConstants; + + ctypedef const uint32_t *TransparentPointerWrappingEnum; + + ctypedef int32_t TransparentIntEnum; + + ctypedef DummyStruct TransparentComplexEnum; + + ctypedef TransparentPrimitiveWrappingEnum TransparentTransparentEnum; + + ctypedef uint32_t *TransparentNonNullEnum; + + ctypedef uint32_t *TransparentOptionNonNullEnum; + + const TransparentPrimitiveWrappingStruct StructWithAssociatedConstantInImpl_STRUCT_TEN # = 10 + + + + + + const TransparentPrimitiveWrappingStruct EnumWithAssociatedConstantInImpl_ENUM_TEN # = 10 + + void struct_root(TransparentComplexWrappingStructTuple a, + TransparentPrimitiveWrappingStructTuple b, + TransparentComplexWrappingStruct c, + TransparentPrimitiveWrappingStruct d, + TransparentComplexWrapperStruct_i32 e, + TransparentPrimitiveWrapperStruct_i32 f, + TransparentPrimitiveStructWithAssociatedConstants g, + TransparentPointerWrappingStruct h, + StructWithAssociatedConstantInImpl i); void erased_root(uint32_t *a, uint32_t *b, - TransparentPrimitiveWrappingStructure c, + TransparentPrimitiveWrappingStruct c, uint32_t *d, TransparentIntStruct e, int32_t f, @@ -72,3 +101,28 @@ cdef extern from *: TransparentTransparentStruct l, TransparentNonNullStruct m, TransparentOptionNonNullStruct n); + + void enum_root(TransparentComplexWrappingEnumTuple a, + TransparentPrimitiveWrappingEnumTuple b, + TransparentComplexWrappingEnum c, + TransparentPrimitiveWrappingEnum d, + TransparentComplexWrapperEnum_i32 e, + TransparentPrimitiveWrapperEnum_i32 f, + TransparentPrimitiveEnumWithAssociatedConstants g, + TransparentPointerWrappingEnum h, + EnumWithAssociatedConstantInImpl i); + + void erased_enum_root(uint32_t *a, + uint32_t *b, + TransparentPrimitiveWrappingEnum c, + uint32_t *d, + TransparentIntEnum e, + int32_t f, + DummyStruct g, + uint32_t *h, + int32_t i, + TransparentIntEnum j, + TransparentComplexEnum k, + TransparentTransparentEnum l, + TransparentNonNullEnum m, + TransparentOptionNonNullEnum n); diff --git a/tests/expectations/transparent_both.c b/tests/expectations/transparent_both.c index 2891106c..59d46429 100644 --- a/tests/expectations/transparent_both.c +++ b/tests/expectations/transparent_both.c @@ -13,48 +13,77 @@ typedef struct DummyStruct TransparentComplexWrappingStructTuple; typedef uint32_t TransparentPrimitiveWrappingStructTuple; -typedef struct DummyStruct TransparentComplexWrappingStructure; +typedef struct DummyStruct TransparentComplexWrappingStruct; -typedef uint32_t TransparentPrimitiveWrappingStructure; +typedef uint32_t TransparentPrimitiveWrappingStruct; -typedef struct DummyStruct TransparentComplexWrapper_i32; +typedef struct DummyStruct TransparentComplexWrapperStruct_i32; -typedef uint32_t TransparentPrimitiveWrapper_i32; +typedef uint32_t TransparentPrimitiveWrapperStruct_i32; -typedef uint32_t TransparentPrimitiveWithAssociatedConstants; -#define TransparentPrimitiveWithAssociatedConstants_ZERO 0 -#define TransparentPrimitiveWithAssociatedConstants_ONE 1 +typedef uint32_t TransparentPrimitiveStructWithAssociatedConstants; +#define TransparentPrimitiveStructWithAssociatedConstants_STRUCT_ZERO 0 +#define TransparentPrimitiveStructWithAssociatedConstants_STRUCT_ONE 1 -typedef const uint32_t *TransparentPointerWrappingStructure; +typedef const uint32_t *TransparentPointerWrappingStruct; typedef int32_t TransparentIntStruct; typedef struct DummyStruct TransparentComplexStruct; -typedef TransparentPrimitiveWrappingStructure TransparentTransparentStruct; +typedef TransparentPrimitiveWrappingStruct TransparentTransparentStruct; typedef uint32_t *TransparentNonNullStruct; typedef uint32_t *TransparentOptionNonNullStruct; +typedef struct DummyStruct TransparentComplexWrappingEnumTuple; + +typedef uint32_t TransparentPrimitiveWrappingEnumTuple; + +typedef struct DummyStruct TransparentComplexWrappingEnum; + +typedef uint32_t TransparentPrimitiveWrappingEnum; + +typedef struct DummyStruct TransparentComplexWrapperEnum_i32; + +typedef uint32_t TransparentPrimitiveWrapperEnum_i32; + +typedef uint32_t TransparentPrimitiveEnumWithAssociatedConstants; + +typedef const uint32_t *TransparentPointerWrappingEnum; + +typedef int32_t TransparentIntEnum; + +typedef struct DummyStruct TransparentComplexEnum; + +typedef TransparentPrimitiveWrappingEnum TransparentTransparentEnum; + +typedef uint32_t *TransparentNonNullEnum; + +typedef uint32_t *TransparentOptionNonNullEnum; + #define StructWithAssociatedConstantInImpl_STRUCT_TEN 10 + + + + #define EnumWithAssociatedConstantInImpl_ENUM_TEN 10 -void root(TransparentComplexWrappingStructTuple a, - TransparentPrimitiveWrappingStructTuple b, - TransparentComplexWrappingStructure c, - TransparentPrimitiveWrappingStructure d, - TransparentComplexWrapper_i32 e, - TransparentPrimitiveWrapper_i32 f, - TransparentPrimitiveWithAssociatedConstants g, - TransparentPointerWrappingStructure h, - struct StructWithAssociatedConstantInImpl i, - struct EnumWithAssociatedConstantInImpl j); +void struct_root(TransparentComplexWrappingStructTuple a, + TransparentPrimitiveWrappingStructTuple b, + TransparentComplexWrappingStruct c, + TransparentPrimitiveWrappingStruct d, + TransparentComplexWrapperStruct_i32 e, + TransparentPrimitiveWrapperStruct_i32 f, + TransparentPrimitiveStructWithAssociatedConstants g, + TransparentPointerWrappingStruct h, + struct StructWithAssociatedConstantInImpl i); void erased_root(uint32_t *a, uint32_t *b, - TransparentPrimitiveWrappingStructure c, + TransparentPrimitiveWrappingStruct c, uint32_t *d, TransparentIntStruct e, int32_t f, @@ -66,3 +95,28 @@ void erased_root(uint32_t *a, TransparentTransparentStruct l, TransparentNonNullStruct m, TransparentOptionNonNullStruct n); + +void enum_root(TransparentComplexWrappingEnumTuple a, + TransparentPrimitiveWrappingEnumTuple b, + TransparentComplexWrappingEnum c, + TransparentPrimitiveWrappingEnum d, + TransparentComplexWrapperEnum_i32 e, + TransparentPrimitiveWrapperEnum_i32 f, + TransparentPrimitiveEnumWithAssociatedConstants g, + TransparentPointerWrappingEnum h, + struct EnumWithAssociatedConstantInImpl i); + +void erased_enum_root(uint32_t *a, + uint32_t *b, + TransparentPrimitiveWrappingEnum c, + uint32_t *d, + TransparentIntEnum e, + int32_t f, + struct DummyStruct g, + uint32_t *h, + int32_t i, + TransparentIntEnum j, + TransparentComplexEnum k, + TransparentTransparentEnum l, + TransparentNonNullEnum m, + TransparentOptionNonNullEnum n); diff --git a/tests/expectations/transparent_both.compat.c b/tests/expectations/transparent_both.compat.c index a72c16b9..a6216c21 100644 --- a/tests/expectations/transparent_both.compat.c +++ b/tests/expectations/transparent_both.compat.c @@ -13,52 +13,81 @@ typedef struct DummyStruct TransparentComplexWrappingStructTuple; typedef uint32_t TransparentPrimitiveWrappingStructTuple; -typedef struct DummyStruct TransparentComplexWrappingStructure; +typedef struct DummyStruct TransparentComplexWrappingStruct; -typedef uint32_t TransparentPrimitiveWrappingStructure; +typedef uint32_t TransparentPrimitiveWrappingStruct; -typedef struct DummyStruct TransparentComplexWrapper_i32; +typedef struct DummyStruct TransparentComplexWrapperStruct_i32; -typedef uint32_t TransparentPrimitiveWrapper_i32; +typedef uint32_t TransparentPrimitiveWrapperStruct_i32; -typedef uint32_t TransparentPrimitiveWithAssociatedConstants; -#define TransparentPrimitiveWithAssociatedConstants_ZERO 0 -#define TransparentPrimitiveWithAssociatedConstants_ONE 1 +typedef uint32_t TransparentPrimitiveStructWithAssociatedConstants; +#define TransparentPrimitiveStructWithAssociatedConstants_STRUCT_ZERO 0 +#define TransparentPrimitiveStructWithAssociatedConstants_STRUCT_ONE 1 -typedef const uint32_t *TransparentPointerWrappingStructure; +typedef const uint32_t *TransparentPointerWrappingStruct; typedef int32_t TransparentIntStruct; typedef struct DummyStruct TransparentComplexStruct; -typedef TransparentPrimitiveWrappingStructure TransparentTransparentStruct; +typedef TransparentPrimitiveWrappingStruct TransparentTransparentStruct; typedef uint32_t *TransparentNonNullStruct; typedef uint32_t *TransparentOptionNonNullStruct; +typedef struct DummyStruct TransparentComplexWrappingEnumTuple; + +typedef uint32_t TransparentPrimitiveWrappingEnumTuple; + +typedef struct DummyStruct TransparentComplexWrappingEnum; + +typedef uint32_t TransparentPrimitiveWrappingEnum; + +typedef struct DummyStruct TransparentComplexWrapperEnum_i32; + +typedef uint32_t TransparentPrimitiveWrapperEnum_i32; + +typedef uint32_t TransparentPrimitiveEnumWithAssociatedConstants; + +typedef const uint32_t *TransparentPointerWrappingEnum; + +typedef int32_t TransparentIntEnum; + +typedef struct DummyStruct TransparentComplexEnum; + +typedef TransparentPrimitiveWrappingEnum TransparentTransparentEnum; + +typedef uint32_t *TransparentNonNullEnum; + +typedef uint32_t *TransparentOptionNonNullEnum; + #define StructWithAssociatedConstantInImpl_STRUCT_TEN 10 + + + + #define EnumWithAssociatedConstantInImpl_ENUM_TEN 10 #ifdef __cplusplus extern "C" { #endif // __cplusplus -void root(TransparentComplexWrappingStructTuple a, - TransparentPrimitiveWrappingStructTuple b, - TransparentComplexWrappingStructure c, - TransparentPrimitiveWrappingStructure d, - TransparentComplexWrapper_i32 e, - TransparentPrimitiveWrapper_i32 f, - TransparentPrimitiveWithAssociatedConstants g, - TransparentPointerWrappingStructure h, - struct StructWithAssociatedConstantInImpl i, - struct EnumWithAssociatedConstantInImpl j); +void struct_root(TransparentComplexWrappingStructTuple a, + TransparentPrimitiveWrappingStructTuple b, + TransparentComplexWrappingStruct c, + TransparentPrimitiveWrappingStruct d, + TransparentComplexWrapperStruct_i32 e, + TransparentPrimitiveWrapperStruct_i32 f, + TransparentPrimitiveStructWithAssociatedConstants g, + TransparentPointerWrappingStruct h, + struct StructWithAssociatedConstantInImpl i); void erased_root(uint32_t *a, uint32_t *b, - TransparentPrimitiveWrappingStructure c, + TransparentPrimitiveWrappingStruct c, uint32_t *d, TransparentIntStruct e, int32_t f, @@ -71,6 +100,31 @@ void erased_root(uint32_t *a, TransparentNonNullStruct m, TransparentOptionNonNullStruct n); +void enum_root(TransparentComplexWrappingEnumTuple a, + TransparentPrimitiveWrappingEnumTuple b, + TransparentComplexWrappingEnum c, + TransparentPrimitiveWrappingEnum d, + TransparentComplexWrapperEnum_i32 e, + TransparentPrimitiveWrapperEnum_i32 f, + TransparentPrimitiveEnumWithAssociatedConstants g, + TransparentPointerWrappingEnum h, + struct EnumWithAssociatedConstantInImpl i); + +void erased_enum_root(uint32_t *a, + uint32_t *b, + TransparentPrimitiveWrappingEnum c, + uint32_t *d, + TransparentIntEnum e, + int32_t f, + struct DummyStruct g, + uint32_t *h, + int32_t i, + TransparentIntEnum j, + TransparentComplexEnum k, + TransparentTransparentEnum l, + TransparentNonNullEnum m, + TransparentOptionNonNullEnum n); + #ifdef __cplusplus } // extern "C" #endif // __cplusplus diff --git a/tests/expectations/transparent_tag.c b/tests/expectations/transparent_tag.c index d6caa18b..0db0a049 100644 --- a/tests/expectations/transparent_tag.c +++ b/tests/expectations/transparent_tag.c @@ -13,48 +13,77 @@ typedef struct DummyStruct TransparentComplexWrappingStructTuple; typedef uint32_t TransparentPrimitiveWrappingStructTuple; -typedef struct DummyStruct TransparentComplexWrappingStructure; +typedef struct DummyStruct TransparentComplexWrappingStruct; -typedef uint32_t TransparentPrimitiveWrappingStructure; +typedef uint32_t TransparentPrimitiveWrappingStruct; -typedef struct DummyStruct TransparentComplexWrapper_i32; +typedef struct DummyStruct TransparentComplexWrapperStruct_i32; -typedef uint32_t TransparentPrimitiveWrapper_i32; +typedef uint32_t TransparentPrimitiveWrapperStruct_i32; -typedef uint32_t TransparentPrimitiveWithAssociatedConstants; -#define TransparentPrimitiveWithAssociatedConstants_ZERO 0 -#define TransparentPrimitiveWithAssociatedConstants_ONE 1 +typedef uint32_t TransparentPrimitiveStructWithAssociatedConstants; +#define TransparentPrimitiveStructWithAssociatedConstants_STRUCT_ZERO 0 +#define TransparentPrimitiveStructWithAssociatedConstants_STRUCT_ONE 1 -typedef const uint32_t *TransparentPointerWrappingStructure; +typedef const uint32_t *TransparentPointerWrappingStruct; typedef int32_t TransparentIntStruct; typedef struct DummyStruct TransparentComplexStruct; -typedef TransparentPrimitiveWrappingStructure TransparentTransparentStruct; +typedef TransparentPrimitiveWrappingStruct TransparentTransparentStruct; typedef uint32_t *TransparentNonNullStruct; typedef uint32_t *TransparentOptionNonNullStruct; +typedef struct DummyStruct TransparentComplexWrappingEnumTuple; + +typedef uint32_t TransparentPrimitiveWrappingEnumTuple; + +typedef struct DummyStruct TransparentComplexWrappingEnum; + +typedef uint32_t TransparentPrimitiveWrappingEnum; + +typedef struct DummyStruct TransparentComplexWrapperEnum_i32; + +typedef uint32_t TransparentPrimitiveWrapperEnum_i32; + +typedef uint32_t TransparentPrimitiveEnumWithAssociatedConstants; + +typedef const uint32_t *TransparentPointerWrappingEnum; + +typedef int32_t TransparentIntEnum; + +typedef struct DummyStruct TransparentComplexEnum; + +typedef TransparentPrimitiveWrappingEnum TransparentTransparentEnum; + +typedef uint32_t *TransparentNonNullEnum; + +typedef uint32_t *TransparentOptionNonNullEnum; + #define StructWithAssociatedConstantInImpl_STRUCT_TEN 10 + + + + #define EnumWithAssociatedConstantInImpl_ENUM_TEN 10 -void root(TransparentComplexWrappingStructTuple a, - TransparentPrimitiveWrappingStructTuple b, - TransparentComplexWrappingStructure c, - TransparentPrimitiveWrappingStructure d, - TransparentComplexWrapper_i32 e, - TransparentPrimitiveWrapper_i32 f, - TransparentPrimitiveWithAssociatedConstants g, - TransparentPointerWrappingStructure h, - struct StructWithAssociatedConstantInImpl i, - struct EnumWithAssociatedConstantInImpl j); +void struct_root(TransparentComplexWrappingStructTuple a, + TransparentPrimitiveWrappingStructTuple b, + TransparentComplexWrappingStruct c, + TransparentPrimitiveWrappingStruct d, + TransparentComplexWrapperStruct_i32 e, + TransparentPrimitiveWrapperStruct_i32 f, + TransparentPrimitiveStructWithAssociatedConstants g, + TransparentPointerWrappingStruct h, + struct StructWithAssociatedConstantInImpl i); void erased_root(uint32_t *a, uint32_t *b, - TransparentPrimitiveWrappingStructure c, + TransparentPrimitiveWrappingStruct c, uint32_t *d, TransparentIntStruct e, int32_t f, @@ -66,3 +95,28 @@ void erased_root(uint32_t *a, TransparentTransparentStruct l, TransparentNonNullStruct m, TransparentOptionNonNullStruct n); + +void enum_root(TransparentComplexWrappingEnumTuple a, + TransparentPrimitiveWrappingEnumTuple b, + TransparentComplexWrappingEnum c, + TransparentPrimitiveWrappingEnum d, + TransparentComplexWrapperEnum_i32 e, + TransparentPrimitiveWrapperEnum_i32 f, + TransparentPrimitiveEnumWithAssociatedConstants g, + TransparentPointerWrappingEnum h, + struct EnumWithAssociatedConstantInImpl i); + +void erased_enum_root(uint32_t *a, + uint32_t *b, + TransparentPrimitiveWrappingEnum c, + uint32_t *d, + TransparentIntEnum e, + int32_t f, + struct DummyStruct g, + uint32_t *h, + int32_t i, + TransparentIntEnum j, + TransparentComplexEnum k, + TransparentTransparentEnum l, + TransparentNonNullEnum m, + TransparentOptionNonNullEnum n); diff --git a/tests/expectations/transparent_tag.compat.c b/tests/expectations/transparent_tag.compat.c index eb5f2523..ec507f4e 100644 --- a/tests/expectations/transparent_tag.compat.c +++ b/tests/expectations/transparent_tag.compat.c @@ -13,52 +13,81 @@ typedef struct DummyStruct TransparentComplexWrappingStructTuple; typedef uint32_t TransparentPrimitiveWrappingStructTuple; -typedef struct DummyStruct TransparentComplexWrappingStructure; +typedef struct DummyStruct TransparentComplexWrappingStruct; -typedef uint32_t TransparentPrimitiveWrappingStructure; +typedef uint32_t TransparentPrimitiveWrappingStruct; -typedef struct DummyStruct TransparentComplexWrapper_i32; +typedef struct DummyStruct TransparentComplexWrapperStruct_i32; -typedef uint32_t TransparentPrimitiveWrapper_i32; +typedef uint32_t TransparentPrimitiveWrapperStruct_i32; -typedef uint32_t TransparentPrimitiveWithAssociatedConstants; -#define TransparentPrimitiveWithAssociatedConstants_ZERO 0 -#define TransparentPrimitiveWithAssociatedConstants_ONE 1 +typedef uint32_t TransparentPrimitiveStructWithAssociatedConstants; +#define TransparentPrimitiveStructWithAssociatedConstants_STRUCT_ZERO 0 +#define TransparentPrimitiveStructWithAssociatedConstants_STRUCT_ONE 1 -typedef const uint32_t *TransparentPointerWrappingStructure; +typedef const uint32_t *TransparentPointerWrappingStruct; typedef int32_t TransparentIntStruct; typedef struct DummyStruct TransparentComplexStruct; -typedef TransparentPrimitiveWrappingStructure TransparentTransparentStruct; +typedef TransparentPrimitiveWrappingStruct TransparentTransparentStruct; typedef uint32_t *TransparentNonNullStruct; typedef uint32_t *TransparentOptionNonNullStruct; +typedef struct DummyStruct TransparentComplexWrappingEnumTuple; + +typedef uint32_t TransparentPrimitiveWrappingEnumTuple; + +typedef struct DummyStruct TransparentComplexWrappingEnum; + +typedef uint32_t TransparentPrimitiveWrappingEnum; + +typedef struct DummyStruct TransparentComplexWrapperEnum_i32; + +typedef uint32_t TransparentPrimitiveWrapperEnum_i32; + +typedef uint32_t TransparentPrimitiveEnumWithAssociatedConstants; + +typedef const uint32_t *TransparentPointerWrappingEnum; + +typedef int32_t TransparentIntEnum; + +typedef struct DummyStruct TransparentComplexEnum; + +typedef TransparentPrimitiveWrappingEnum TransparentTransparentEnum; + +typedef uint32_t *TransparentNonNullEnum; + +typedef uint32_t *TransparentOptionNonNullEnum; + #define StructWithAssociatedConstantInImpl_STRUCT_TEN 10 + + + + #define EnumWithAssociatedConstantInImpl_ENUM_TEN 10 #ifdef __cplusplus extern "C" { #endif // __cplusplus -void root(TransparentComplexWrappingStructTuple a, - TransparentPrimitiveWrappingStructTuple b, - TransparentComplexWrappingStructure c, - TransparentPrimitiveWrappingStructure d, - TransparentComplexWrapper_i32 e, - TransparentPrimitiveWrapper_i32 f, - TransparentPrimitiveWithAssociatedConstants g, - TransparentPointerWrappingStructure h, - struct StructWithAssociatedConstantInImpl i, - struct EnumWithAssociatedConstantInImpl j); +void struct_root(TransparentComplexWrappingStructTuple a, + TransparentPrimitiveWrappingStructTuple b, + TransparentComplexWrappingStruct c, + TransparentPrimitiveWrappingStruct d, + TransparentComplexWrapperStruct_i32 e, + TransparentPrimitiveWrapperStruct_i32 f, + TransparentPrimitiveStructWithAssociatedConstants g, + TransparentPointerWrappingStruct h, + struct StructWithAssociatedConstantInImpl i); void erased_root(uint32_t *a, uint32_t *b, - TransparentPrimitiveWrappingStructure c, + TransparentPrimitiveWrappingStruct c, uint32_t *d, TransparentIntStruct e, int32_t f, @@ -71,6 +100,31 @@ void erased_root(uint32_t *a, TransparentNonNullStruct m, TransparentOptionNonNullStruct n); +void enum_root(TransparentComplexWrappingEnumTuple a, + TransparentPrimitiveWrappingEnumTuple b, + TransparentComplexWrappingEnum c, + TransparentPrimitiveWrappingEnum d, + TransparentComplexWrapperEnum_i32 e, + TransparentPrimitiveWrapperEnum_i32 f, + TransparentPrimitiveEnumWithAssociatedConstants g, + TransparentPointerWrappingEnum h, + struct EnumWithAssociatedConstantInImpl i); + +void erased_enum_root(uint32_t *a, + uint32_t *b, + TransparentPrimitiveWrappingEnum c, + uint32_t *d, + TransparentIntEnum e, + int32_t f, + struct DummyStruct g, + uint32_t *h, + int32_t i, + TransparentIntEnum j, + TransparentComplexEnum k, + TransparentTransparentEnum l, + TransparentNonNullEnum m, + TransparentOptionNonNullEnum n); + #ifdef __cplusplus } // extern "C" #endif // __cplusplus diff --git a/tests/expectations/transparent_tag.pyx b/tests/expectations/transparent_tag.pyx index dc1f49e8..050727fa 100644 --- a/tests/expectations/transparent_tag.pyx +++ b/tests/expectations/transparent_tag.pyx @@ -19,48 +19,77 @@ cdef extern from *: ctypedef uint32_t TransparentPrimitiveWrappingStructTuple; - ctypedef DummyStruct TransparentComplexWrappingStructure; + ctypedef DummyStruct TransparentComplexWrappingStruct; - ctypedef uint32_t TransparentPrimitiveWrappingStructure; + ctypedef uint32_t TransparentPrimitiveWrappingStruct; - ctypedef DummyStruct TransparentComplexWrapper_i32; + ctypedef DummyStruct TransparentComplexWrapperStruct_i32; - ctypedef uint32_t TransparentPrimitiveWrapper_i32; + ctypedef uint32_t TransparentPrimitiveWrapperStruct_i32; - ctypedef uint32_t TransparentPrimitiveWithAssociatedConstants; - const TransparentPrimitiveWithAssociatedConstants TransparentPrimitiveWithAssociatedConstants_ZERO # = 0 - const TransparentPrimitiveWithAssociatedConstants TransparentPrimitiveWithAssociatedConstants_ONE # = 1 + ctypedef uint32_t TransparentPrimitiveStructWithAssociatedConstants; + const TransparentPrimitiveStructWithAssociatedConstants TransparentPrimitiveStructWithAssociatedConstants_STRUCT_ZERO # = 0 + const TransparentPrimitiveStructWithAssociatedConstants TransparentPrimitiveStructWithAssociatedConstants_STRUCT_ONE # = 1 - ctypedef const uint32_t *TransparentPointerWrappingStructure; + ctypedef const uint32_t *TransparentPointerWrappingStruct; ctypedef int32_t TransparentIntStruct; ctypedef DummyStruct TransparentComplexStruct; - ctypedef TransparentPrimitiveWrappingStructure TransparentTransparentStruct; + ctypedef TransparentPrimitiveWrappingStruct TransparentTransparentStruct; ctypedef uint32_t *TransparentNonNullStruct; ctypedef uint32_t *TransparentOptionNonNullStruct; - const TransparentPrimitiveWrappingStructure StructWithAssociatedConstantInImpl_STRUCT_TEN # = 10 + ctypedef DummyStruct TransparentComplexWrappingEnumTuple; - const TransparentPrimitiveWrappingStructure EnumWithAssociatedConstantInImpl_ENUM_TEN # = 10 + ctypedef uint32_t TransparentPrimitiveWrappingEnumTuple; - void root(TransparentComplexWrappingStructTuple a, - TransparentPrimitiveWrappingStructTuple b, - TransparentComplexWrappingStructure c, - TransparentPrimitiveWrappingStructure d, - TransparentComplexWrapper_i32 e, - TransparentPrimitiveWrapper_i32 f, - TransparentPrimitiveWithAssociatedConstants g, - TransparentPointerWrappingStructure h, - StructWithAssociatedConstantInImpl i, - EnumWithAssociatedConstantInImpl j); + ctypedef DummyStruct TransparentComplexWrappingEnum; + + ctypedef uint32_t TransparentPrimitiveWrappingEnum; + + ctypedef DummyStruct TransparentComplexWrapperEnum_i32; + + ctypedef uint32_t TransparentPrimitiveWrapperEnum_i32; + + ctypedef uint32_t TransparentPrimitiveEnumWithAssociatedConstants; + + ctypedef const uint32_t *TransparentPointerWrappingEnum; + + ctypedef int32_t TransparentIntEnum; + + ctypedef DummyStruct TransparentComplexEnum; + + ctypedef TransparentPrimitiveWrappingEnum TransparentTransparentEnum; + + ctypedef uint32_t *TransparentNonNullEnum; + + ctypedef uint32_t *TransparentOptionNonNullEnum; + + const TransparentPrimitiveWrappingStruct StructWithAssociatedConstantInImpl_STRUCT_TEN # = 10 + + + + + + const TransparentPrimitiveWrappingStruct EnumWithAssociatedConstantInImpl_ENUM_TEN # = 10 + + void struct_root(TransparentComplexWrappingStructTuple a, + TransparentPrimitiveWrappingStructTuple b, + TransparentComplexWrappingStruct c, + TransparentPrimitiveWrappingStruct d, + TransparentComplexWrapperStruct_i32 e, + TransparentPrimitiveWrapperStruct_i32 f, + TransparentPrimitiveStructWithAssociatedConstants g, + TransparentPointerWrappingStruct h, + StructWithAssociatedConstantInImpl i); void erased_root(uint32_t *a, uint32_t *b, - TransparentPrimitiveWrappingStructure c, + TransparentPrimitiveWrappingStruct c, uint32_t *d, TransparentIntStruct e, int32_t f, @@ -72,3 +101,28 @@ cdef extern from *: TransparentTransparentStruct l, TransparentNonNullStruct m, TransparentOptionNonNullStruct n); + + void enum_root(TransparentComplexWrappingEnumTuple a, + TransparentPrimitiveWrappingEnumTuple b, + TransparentComplexWrappingEnum c, + TransparentPrimitiveWrappingEnum d, + TransparentComplexWrapperEnum_i32 e, + TransparentPrimitiveWrapperEnum_i32 f, + TransparentPrimitiveEnumWithAssociatedConstants g, + TransparentPointerWrappingEnum h, + EnumWithAssociatedConstantInImpl i); + + void erased_enum_root(uint32_t *a, + uint32_t *b, + TransparentPrimitiveWrappingEnum c, + uint32_t *d, + TransparentIntEnum e, + int32_t f, + DummyStruct g, + uint32_t *h, + int32_t i, + TransparentIntEnum j, + TransparentComplexEnum k, + TransparentTransparentEnum l, + TransparentNonNullEnum m, + TransparentOptionNonNullEnum n); diff --git a/tests/rust/const_transparent.rs b/tests/rust/const_transparent.rs index 1e2e81c9..c6c50d67 100644 --- a/tests/rust/const_transparent.rs +++ b/tests/rust/const_transparent.rs @@ -31,3 +31,35 @@ struct TransparentStructWithErasedField { pub const COMPLEX: TransparentStructWithErasedField = TransparentStructWithErasedField { field: Wrapper { field: TransparentStruct { field: 7 } } }; + +#[repr(transparent)] +enum TransparentEnum { + A { field: u8 }, +} + +impl TransparentEnum { + pub const ASSOC_ENUM_FOO: i64 = 8; + + // TODO: Transparent enum constants are not supported yet. + pub const ASSOC_ENUM_BAR: TransparentEnum = TransparentEnum::A { field: 9 }; + pub const ASSOC_ENUM_BAZ: TransparentWrapperEnum = TransparentWrapperEnum::A { + field: TransparentEnum::A { field: 10 } + }; +} + +#[repr(transparent)] +enum TransparentTupleEnum { + A(u8), +} + +#[repr(transparent)] +enum TransparentWrapperEnum { + A { field: T }, +} + +// TODO: Transparent enum constants are not supported yet. +pub const ENUM_FOO: TransparentEnum = TransparentEnum::A { field: 11 }; +pub const ENUM_BAR: TransparentTupleEnum = TransparentTupleEnum::A(12); +pub const ENUM_BAZ: TransparentWrapperEnum = TransparentWrapperEnum::A { + field: TransparentEnum::A { field: 13 } +}; diff --git a/tests/rust/transparent.rs b/tests/rust/transparent.rs index cda76ceb..61293f50 100644 --- a/tests/rust/transparent.rs +++ b/tests/rust/transparent.rs @@ -8,76 +8,70 @@ struct TransparentComplexWrappingStructTuple(DummyStruct); #[repr(transparent)] struct TransparentPrimitiveWrappingStructTuple(u32); -// Transparent structure wrapping a struct. +// Transparent struct wrapping a struct. #[repr(transparent)] -struct TransparentComplexWrappingStructure { only_field: DummyStruct } +struct TransparentComplexWrappingStruct { only_field: DummyStruct } -// Transparent structure wrapping a primitive. +// Transparent struct wrapping a primitive. #[repr(transparent)] -struct TransparentPrimitiveWrappingStructure { only_field: u32 } +struct TransparentPrimitiveWrappingStruct { only_field: u32 } // Transparent struct wrapping a pointer #[repr(transparent)] -struct TransparentPointerWrappingStructure { only_field: *const u32 } +struct TransparentPointerWrappingStruct { only_field: *const u32 } + +// Transparent struct wrapping a pointer +#[repr(transparent)] +struct TransparentPointerWrappingStruct { only_field: *const u32 } // Transparent struct wrapper with a marker wrapping a struct. #[repr(transparent)] -struct TransparentComplexWrapper { +struct TransparentComplexWrapperStruct { only_non_zero_sized_field: DummyStruct, marker: PhantomData, } // Transparent struct wrapper with a marker wrapping a primitive. #[repr(transparent)] -struct TransparentPrimitiveWrapper { +struct TransparentPrimitiveWrapperStruct { only_non_zero_sized_field: u32, marker: PhantomData, } // Associated constant declared before struct declaration. -impl TransparentPrimitiveWithAssociatedConstants { - pub const ZERO: TransparentPrimitiveWithAssociatedConstants = TransparentPrimitiveWithAssociatedConstants { - bits: 0 - }; +impl TransparentPrimitiveStructWithAssociatedConstants { + pub const STRUCT_ZERO: TransparentPrimitiveStructWithAssociatedConstants = + TransparentPrimitiveStructWithAssociatedConstants { bits: 0 }; } -// Transparent structure wrapping a primitive with associated constants. +// Transparent struct wrapping a primitive with associated constants. #[repr(transparent)] -struct TransparentPrimitiveWithAssociatedConstants { bits: u32 } +struct TransparentPrimitiveStructWithAssociatedConstants { bits: u32 } // Associated constant declared after struct declaration. -impl TransparentPrimitiveWithAssociatedConstants { - pub const ONE: TransparentPrimitiveWithAssociatedConstants = TransparentPrimitiveWithAssociatedConstants { - bits: 1 - }; +impl TransparentPrimitiveStructWithAssociatedConstants { + pub const STRUCT_ONE: TransparentPrimitiveStructWithAssociatedConstants = + TransparentPrimitiveStructWithAssociatedConstants { bits: 1 }; } struct StructWithAssociatedConstantInImpl { } impl StructWithAssociatedConstantInImpl { - pub const STRUCT_TEN: TransparentPrimitiveWrappingStructure = - TransparentPrimitiveWrappingStructure { only_field: 10 }; -} - -enum EnumWithAssociatedConstantInImpl { A } - -impl EnumWithAssociatedConstantInImpl { - pub const ENUM_TEN: TransparentPrimitiveWrappingStructure = - TransparentPrimitiveWrappingStructure { only_field: 10 }; + pub const STRUCT_TEN: TransparentPrimitiveWrappingStruct = + TransparentPrimitiveWrappingStruct { only_field: 10 }; } #[no_mangle] -pub extern "C" fn root( +pub extern "C" fn struct_root( a: TransparentComplexWrappingStructTuple, b: TransparentPrimitiveWrappingStructTuple, - c: TransparentComplexWrappingStructure, - d: TransparentPrimitiveWrappingStructure, - e: TransparentComplexWrapper, - f: TransparentPrimitiveWrapper, - g: TransparentPrimitiveWithAssociatedConstants, - h: TransparentPointerWrappingStructure, + c: TransparentComplexWrappingStruct, + d: TransparentPrimitiveWrappingStruct, + e: TransparentComplexWrapperStruct, + f: TransparentPrimitiveWrapperStruct, + g: TransparentPrimitiveStructWithAssociatedConstants, + h: TransparentPointerWrappingStruct, i: StructWithAssociatedConstantInImpl, - j: EnumWithAssociatedConstantInImpl, ) { } #[repr(transparent)] @@ -90,7 +84,7 @@ struct ErasedTransparentOptionalNonNullPointerWrappingStruct { only_field: Optio #[repr(transparent)] /// cbindgen:transparent-typedef -struct ErasedTransparentWrappingAnotherTransparentStruct { only_field: TransparentPrimitiveWrappingStructure } +struct ErasedTransparentWrappingAnotherTransparentStruct { only_field: TransparentPrimitiveWrappingStruct } /// cbindgen:transparent-typedef #[repr(transparent)] @@ -103,7 +97,7 @@ struct ErasedTransparentStructWrappingAnotherType { only_field: T } type TransparentIntStruct = ErasedTransparentStructWrappingAnotherType; type TransparentComplexStruct = ErasedTransparentStructWrappingAnotherType; -type TransparentTransparentStruct = ErasedTransparentStructWrappingAnotherType; +type TransparentTransparentStruct = ErasedTransparentStructWrappingAnotherType; type TransparentNonNullStruct = ErasedTransparentStructWrappingAnotherType>; type TransparentOptionNonNullStruct = ErasedTransparentStructWrappingAnotherType>>; @@ -131,3 +125,146 @@ pub extern "C" fn erased_root( m: TransparentNonNullStruct, n: TransparentOptionNonNullStruct, ) { } + +// Transparent enum tuple wrapping a struct. +#[repr(transparent)] +enum TransparentComplexWrappingEnumTuple { + A(DummyStruct), +} + +// Transparent enum tuple wrapping a primitive. +#[repr(transparent)] +enum TransparentPrimitiveWrappingEnumTuple { + A(u32), +} + +// Transparent enum wrapping a struct. +#[repr(transparent)] +enum TransparentComplexWrappingEnum { + A { only_field: DummyStruct }, +} + +// Transparent enum wrapping a primitive. +#[repr(transparent)] +enum TransparentPrimitiveWrappingEnum { + A { only_field: u32 }, +} + +// Transparent enum wrapping a pointer +#[repr(transparent)] +enum TransparentPointerWrappingEnum { + A { only_field: *const u32 }, +} + +// Transparent enum wrapper with a marker wrapping a struct. +#[repr(transparent)] +enum TransparentComplexWrapperEnum { + A { only_non_zero_sized_field: DummyStruct }, + B { marker: PhantomData }, +} + +// Transparent enum wrapper with a marker wrapping a primitive. +#[repr(transparent)] +enum TransparentPrimitiveWrapperEnum { + C { only_non_zero_sized_field: u32 }, + D { marker: PhantomData }, +} + +// Associated constant declared before enum declaration. +impl TransparentPrimitiveEnumWithAssociatedConstants { + // TODO: Transparent enum constants are not supported yet. + pub const ENUM_ZERO: TransparentPrimitiveEnumWithAssociatedConstants = + TransparentPrimitiveEnumWithAssociatedConstants::A { bits: 0 }; +} + +// Transparent enum wrapping a primitive with associated constants. +#[repr(transparent)] +enum TransparentPrimitiveEnumWithAssociatedConstants { + A { bits: u32 }, +} + +// Associated constant declared after enum declaration. +impl TransparentPrimitiveEnumWithAssociatedConstants { + // TODO: Transparent enum constants are not supported yet. + pub const ENUM_ONE: TransparentPrimitiveEnumWithAssociatedConstants = + TransparentPrimitiveEnumWithAssociatedConstants::A { bits: 1 }; +} + +struct StructWithAssociatedConstantInImpl { } + +impl StructWithAssociatedConstantInImpl { + pub const STRUCT_TEN: TransparentPrimitiveWrappingStruct = + TransparentPrimitiveWrappingStruct { only_field: 10 }; +} + +enum EnumWithAssociatedConstantInImpl { A } + +impl EnumWithAssociatedConstantInImpl { + pub const ENUM_TEN: TransparentPrimitiveWrappingStruct = + TransparentPrimitiveWrappingStruct { only_field: 10 }; +} + +#[no_mangle] +pub extern "C" fn enum_root( + a: TransparentComplexWrappingEnumTuple, + b: TransparentPrimitiveWrappingEnumTuple, + c: TransparentComplexWrappingEnum, + d: TransparentPrimitiveWrappingEnum, + e: TransparentComplexWrapperEnum, + f: TransparentPrimitiveWrapperEnum, + g: TransparentPrimitiveEnumWithAssociatedConstants, + h: TransparentPointerWrappingEnum, + i: EnumWithAssociatedConstantInImpl, +) { } + +#[repr(transparent)] +/// cbindgen:transparent-typedef +enum ErasedTransparentNonNullPointerWrappingEnum { A(NonNull) } + +#[repr(transparent)] +/// cbindgen:transparent-typedef +enum ErasedTransparentOptionalNonNullPointerWrappingEnum { A(Option>) } + +#[repr(transparent)] +/// cbindgen:transparent-typedef +enum ErasedTransparentWrappingAnotherTransparentEnum { A(TransparentPrimitiveWrappingEnum) } + +/// cbindgen:transparent-typedef +#[repr(transparent)] +enum ErasedTransparentWrappingTransparentNonNullPointerEnum { A(ErasedTransparentNonNullPointerWrappingEnum) } + +// Transparent enumure wrapping another type +#[repr(transparent)] +/// cbindgen:transparent-typedef +enum ErasedTransparentEnumWrappingAnotherType { A(T) } + +type TransparentIntEnum = ErasedTransparentEnumWrappingAnotherType; +type TransparentComplexEnum = ErasedTransparentEnumWrappingAnotherType; +type TransparentTransparentEnum = ErasedTransparentEnumWrappingAnotherType; +type TransparentNonNullEnum = ErasedTransparentEnumWrappingAnotherType>; +type TransparentOptionNonNullEnum = ErasedTransparentEnumWrappingAnotherType>>; + +/// cbindgen:transparent-typedef +type ErasedTransparentIntEnum = ErasedTransparentEnumWrappingAnotherType; +/// cbindgen:transparent-typedef +type ErasedTransparentComplexEnum = ErasedTransparentEnumWrappingAnotherType; +/// cbindgen:transparent-typedef +type ErasedTransparentOptionNonNullEnum = ErasedTransparentEnumWrappingAnotherType>>; + +#[no_mangle] +pub extern "C" fn erased_enum_root( + a: ErasedTransparentNonNullPointerWrappingEnum, + b: ErasedTransparentOptionalNonNullPointerWrappingEnum, + c: ErasedTransparentWrappingAnotherTransparentEnum, + d: ErasedTransparentWrappingTransparentNonNullPointerEnum, + e: ErasedTransparentEnumWrappingAnotherType, + f: ErasedTransparentEnumWrappingAnotherType, + g: ErasedTransparentEnumWrappingAnotherType, + h: ErasedTransparentEnumWrappingAnotherType, + i: ErasedTransparentIntEnum, + j: TransparentIntEnum, + k: TransparentComplexEnum, + l: TransparentTransparentEnum, + m: TransparentNonNullEnum, + n: TransparentOptionNonNullEnum, +) { }