diff --git a/src/bindgen/ir/enumeration.rs b/src/bindgen/ir/enumeration.rs index a739da3..65f7454 100644 --- a/src/bindgen/ir/enumeration.rs +++ b/src/bindgen/ir/enumeration.rs @@ -684,15 +684,33 @@ impl Source for Enum { write!(out, "{} result;", self.export_name); if let Some((ref variant_name, ref body)) = variant.body { - for &(ref field_name, ..) in body.fields.iter().skip(skip_fields) { + for &(ref field_name, ref ty, ..) in body.fields.iter().skip(skip_fields) { out.new_line(); - write!( - out, - "result.{}.{} = {};", - variant_name, - field_name, - arg_renamer(field_name) - ); + match ty { + Type::Array(ref _ty, ref length) => { + // arrays are not assignable in C++ so we + // need to manually copy the elements + write!( + out, + "for (int i = 0; i < {}; i++) {{\ + result.{}.{}[i] = {}[i];\ + }}", + length.as_str(), + variant_name, + field_name, + arg_renamer(field_name) + ); + } + _ => { + write!( + out, + "result.{}.{} = {};", + variant_name, + field_name, + arg_renamer(field_name) + ); + } + } } } diff --git a/tests/expectations/array.c b/tests/expectations/array.c new file mode 100644 index 0000000..7944b4e --- /dev/null +++ b/tests/expectations/array.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include + +typedef enum { + A, +} Foo_Tag; + +typedef struct { + float _0[20]; +} A_Body; + +typedef struct { + Foo_Tag tag; + union { + A_Body a; + }; +} Foo; + +void root(Foo a); diff --git a/tests/expectations/array.cpp b/tests/expectations/array.cpp new file mode 100644 index 0000000..fd7efb0 --- /dev/null +++ b/tests/expectations/array.cpp @@ -0,0 +1,35 @@ +#include +#include +#include + +struct Foo { + enum class Tag { + A, + }; + + struct A_Body { + float _0[20]; + }; + + Tag tag; + union { + A_Body a; + }; + + static Foo A(const float (&a0)[20]) { + Foo result; + for (int i = 0; i < 20; i++) {result.a._0[i] = a0[i];} + result.tag = Tag::A; + return result; + } + + bool IsA() const { + return tag == Tag::A; + } +}; + +extern "C" { + +void root(Foo a); + +} // extern "C" diff --git a/tests/expectations/both/array.c b/tests/expectations/both/array.c new file mode 100644 index 0000000..0ff4b99 --- /dev/null +++ b/tests/expectations/both/array.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include + +typedef enum Foo_Tag { + A, +} Foo_Tag; + +typedef struct A_Body { + float _0[20]; +} A_Body; + +typedef struct Foo { + Foo_Tag tag; + union { + A_Body a; + }; +} Foo; + +void root(Foo a); diff --git a/tests/expectations/tag/array.c b/tests/expectations/tag/array.c new file mode 100644 index 0000000..69c50f7 --- /dev/null +++ b/tests/expectations/tag/array.c @@ -0,0 +1,21 @@ +#include +#include +#include +#include + +enum Foo_Tag { + A, +}; + +struct A_Body { + float _0[20]; +}; + +struct Foo { + enum Foo_Tag tag; + union { + struct A_Body a; + }; +}; + +void root(struct Foo a); diff --git a/tests/rust/array.rs b/tests/rust/array.rs new file mode 100644 index 0000000..fb33537 --- /dev/null +++ b/tests/rust/array.rs @@ -0,0 +1,7 @@ +#[repr(C)] +enum Foo { + A([f32; 20]) +} + +#[no_mangle] +pub extern "C" fn root(a: Foo) {} diff --git a/tests/rust/array.toml b/tests/rust/array.toml new file mode 100644 index 0000000..ea03916 --- /dev/null +++ b/tests/rust/array.toml @@ -0,0 +1,2 @@ +[enum] +derive_helper_methods = true