Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle constants of nested transparent types #958

Merged
merged 1 commit into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/bindgen/ir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,14 +663,13 @@ impl Constant {
Cow::Owned(format!("{}_{}", associated_name, self.export_name()))
};

let value = match self.value {
Literal::Struct {
ref fields,
ref path,
..
} if out.bindings().struct_is_transparent(path) => fields.iter().next().unwrap().1,
_ => &self.value,
};
let mut value = &self.value;
while let Literal::Struct { path, fields, .. } = value {
if !out.bindings().struct_is_transparent(path) {
break;
}
value = fields.iter().next().unwrap().1
}

language_backend.write_documentation(out, &self.documentation);

Expand Down
15 changes: 13 additions & 2 deletions tests/expectations/const_transparent.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
#include <stdint.h>
#include <stdlib.h>

typedef uint8_t Transparent;
typedef uint8_t TransparentStruct;
#define TransparentStruct_ASSOC_STRUCT_FOO 1
#define TransparentStruct_ASSOC_STRUCT_BAR 2


typedef uint8_t TransparentTupleStruct;

#define STRUCT_FOO 4

#define STRUCT_BAR 5




#define FOO 0
21 changes: 19 additions & 2 deletions tests/expectations/const_transparent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@
#include <ostream>
#include <new>

using Transparent = uint8_t;
template<typename T>
using Wrapper = T;

constexpr static const Transparent FOO = 0;
using TransparentStruct = uint8_t;
constexpr static const int64_t TransparentStruct_ASSOC_STRUCT_FOO = 1;
constexpr static const TransparentStruct TransparentStruct_ASSOC_STRUCT_BAR = 2;
constexpr static const Wrapper<TransparentStruct> TransparentStruct_ASSOC_STRUCT_BAZ = 3;

using TransparentTupleStruct = uint8_t;

template<typename T>
using TransparentStructWithErasedField = Wrapper<T>;

constexpr static const TransparentStruct STRUCT_FOO = 4;

constexpr static const TransparentTupleStruct STRUCT_BAR = 5;

constexpr static const Wrapper<TransparentStruct> STRUCT_BAZ = 6;

constexpr static const TransparentStructWithErasedField<TransparentStruct> COMPLEX = 7;
15 changes: 13 additions & 2 deletions tests/expectations/const_transparent.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@ cdef extern from *:

cdef extern from *:

ctypedef uint8_t Transparent;
ctypedef uint8_t TransparentStruct;
const int64_t TransparentStruct_ASSOC_STRUCT_FOO # = 1
const TransparentStruct TransparentStruct_ASSOC_STRUCT_BAR # = 2


ctypedef uint8_t TransparentTupleStruct;

const TransparentStruct STRUCT_FOO # = 4

const TransparentTupleStruct STRUCT_BAR # = 5




const Transparent FOO # = 0
32 changes: 30 additions & 2 deletions tests/rust/const_transparent.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,32 @@
#[repr(transparent)]
struct Transparent { field: u8 }
struct TransparentStruct { field: u8 }

pub const FOO: Transparent = Transparent { field: 0 };
impl TransparentStruct {
pub const ASSOC_STRUCT_FOO: i64 = 1;
pub const ASSOC_STRUCT_BAR: TransparentStruct = TransparentStruct { field: 2 };

// TODO: Only C++ supports template constants so far.
pub const ASSOC_STRUCT_BAZ: Wrapper<TransparentStruct> = Wrapper { field: TransparentStruct { field: 3 } };
}

#[repr(transparent)]
struct TransparentTupleStruct(u8);

#[repr(transparent)]
struct Wrapper<T> { field: T }

pub const STRUCT_FOO: TransparentStruct = TransparentStruct { field: 4 };
pub const STRUCT_BAR: TransparentTupleStruct = TransparentTupleStruct(5);

// TODO: Only C++ supports template constants so far.
pub const STRUCT_BAZ: Wrapper<TransparentStruct> = Wrapper { field: TransparentStruct { field: 6 } };

#[repr(transparent)]
struct TransparentStructWithErasedField<T> {
field: Wrapper<T>,
}

// TODO: Only C++ supports template constants so far.
pub const COMPLEX: TransparentStructWithErasedField<TransparentStruct> = TransparentStructWithErasedField {
field: Wrapper { field: TransparentStruct { field: 7 } }
};
Loading