Manually copy arrays

C++ arrays can't be assigned. We detect this situation and manually copy
over the elements.
This commit is contained in:
Jeff Muizelaar
2019-01-03 13:45:12 -05:00
committed by Ryan Hunt
parent b718662520
commit 42b4290c64
7 changed files with 133 additions and 8 deletions
+26 -8
View File
@@ -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)
);
}
}
}
}
+21
View File
@@ -0,0 +1,21 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
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);
+35
View File
@@ -0,0 +1,35 @@
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
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"
+21
View File
@@ -0,0 +1,21 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
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);
+21
View File
@@ -0,0 +1,21 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
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);
+7
View File
@@ -0,0 +1,7 @@
#[repr(C)]
enum Foo {
A([f32; 20])
}
#[no_mangle]
pub extern "C" fn root(a: Foo) {}
+2
View File
@@ -0,0 +1,2 @@
[enum]
derive_helper_methods = true