Skip to content

Commit f7222a2

Browse files
authored
Update derives for unions and opaque records (#471)
1 parent 27a113b commit f7222a2

39 files changed

Lines changed: 139 additions & 48 deletions

‎cpp2rust/converter/converter.cpp‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,14 @@ void Converter::EmitGlobalInits(Model model, std::string &out) {
9292
out += "}\n";
9393
}
9494

95-
void Converter::EmitOpaqueRecords(std::string &out) {
95+
void Converter::EmitOpaqueRecords(Model model, std::string &out) {
9696
record_decls_.ForEachUndefined([&](const std::string &name) {
97-
out += "#[derive(Clone, Copy, Default, ByteRepr, VaArg, FnPtrArg)]";
98-
out += "#[byte_size(1)]";
97+
if (model == Model::kRefCount) {
98+
out += "#[derive(Clone, Copy, Default, ByteRepr, VaArg, FnPtrArg)]";
99+
out += "#[byte_size(1)]";
100+
} else {
101+
out += "#[derive(Clone, Copy, Default, VaArg, FnPtrArg)]";
102+
}
99103
out += "pub struct ";
100104
out += name;
101105
out += ";\n";
@@ -4250,7 +4254,7 @@ std::string Converter::GetRecordName(const clang::NamedDecl *decl) const {
42504254
std::vector<const char *>
42514255
Converter::GetStructAttributes(const clang::RecordDecl *decl) {
42524256
if (decl->isUnion()) {
4253-
return {"Copy", "Clone", "FnPtrArg"};
4257+
return {"Copy", "Clone", "FnPtrArg", "VaArg"};
42544258
}
42554259

42564260
std::vector<const char *> struct_attrs;

‎cpp2rust/converter/converter.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
5252

5353
virtual void EmitFilePreamble();
5454

55-
static void EmitOpaqueRecords(std::string &out);
55+
static void EmitOpaqueRecords(Model model, std::string &out);
5656
static void EmitGlobalInits(Model model, std::string &out);
5757

5858
static void EmitVirtualMethods(std::string &out);

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,11 @@ void ConverterRefCount::EmitRustUnion(clang::RecordDecl *decl) {
703703
RuleRegistry::SetDerives(
704704
ctx_, ctx_.getCanonicalTagType(decl),
705705
std::vector<std::string>(attrs.begin(), attrs.end()));
706+
StrCat("#[derive(");
707+
for (auto *attr : attrs) {
708+
StrCat(attr, ',');
709+
}
710+
StrCat(")]");
706711

707712
auto size = ctx_.getTypeSizeInChars(ctx_.getCanonicalTagType(decl));
708713
StrCat(std::format(
@@ -2503,7 +2508,7 @@ ConverterRefCount::GetStructAttributes(const clang::RecordDecl *decl) {
25032508
std::vector<const char *> attrs;
25042509

25052510
if (decl->isUnion()) {
2506-
return attrs;
2511+
return {"VaArg", "FnPtrArg"};
25072512
}
25082513

25092514
if (RecordDerivesClone(decl)) {

‎cpp2rust/cpp2rust_lib.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ std::string TranspileSrc(std::string_view cc_code, Model model,
3131
rules_dir),
3232
cc_code, tool_args, std::filesystem::path(filename).filename().string(),
3333
filename.ends_with(".c") ? CLANG_C_COMPILER : CLANG_CXX_COMPILER);
34-
Converter::EmitOpaqueRecords(rs_code);
34+
Converter::EmitOpaqueRecords(model, rs_code);
3535
Converter::EmitVirtualMethods(rs_code);
3636
if (model == Model::kRefCount) {
3737
ConverterRefCount::EmitMethodsOnPtr(rs_code);
@@ -76,7 +76,7 @@ std::string TranspileDir(std::string_view build_dir, Model model,
7676
std::string rs_code;
7777
FrontendActionFactory factory(rs_code, model, rules_dir);
7878
Tool.run(&factory);
79-
Converter::EmitOpaqueRecords(rs_code);
79+
Converter::EmitOpaqueRecords(model, rs_code);
8080
Converter::EmitVirtualMethods(rs_code);
8181
if (model == Model::kRefCount) {
8282
ConverterRefCount::EmitMethodsOnPtr(rs_code);

‎tests/multi-file/opaque_forward_decl/out/unsafe/opaque_forward_decl.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ unsafe fn main_0() -> i32 {
3131
pub unsafe fn touch_0(mut c: *mut container) {
3232
&((*c).p);
3333
}
34-
#[derive(Clone, Copy, Default, ByteRepr, VaArg, FnPtrArg)]
35-
#[byte_size(1)]
34+
#[derive(Clone, Copy, Default, VaArg, FnPtrArg)]
3635
pub struct opaque;
3736
pub unsafe fn __cpp2rust_init_globals() {}

‎tests/unit/out/refcount/tag_vs_identifier_collision.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub struct point_struct {
2626
#[offset(4)]
2727
pub y: i32,
2828
}
29-
#[derive(ByteRepr, DeepClone)]
29+
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
3030
#[byte_size(4)]
3131
pub struct point {
3232
#[offset(0)]
@@ -48,7 +48,7 @@ impl Default for point {
4848
}
4949
}
5050
}
51-
#[derive(ByteRepr, DeepClone)]
51+
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
5252
#[byte_size(4)]
5353
pub struct slot_union {
5454
#[offset(0)]

‎tests/unit/out/refcount/union_addrof_external.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ impl Default for record {
2929
}
3030
}
3131
}
32-
#[derive(ByteRepr, DeepClone)]
32+
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
3333
#[byte_size(128)]
3434
pub struct anon_0 {
3535
#[offset(0)]

‎tests/unit/out/refcount/union_basic.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::io::prelude::*;
66
use std::io::{Read, Seek, Write};
77
use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
9-
#[derive(ByteRepr, DeepClone)]
9+
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
1010
#[byte_size(4)]
1111
pub struct basic {
1212
#[offset(0)]
@@ -28,7 +28,7 @@ impl Default for basic {
2828
}
2929
}
3030
}
31-
#[derive(ByteRepr, DeepClone)]
31+
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
3232
#[byte_size(1)]
3333
pub struct empty {
3434
#[offset(0)]

‎tests/unit/out/refcount/union_cross_arm_cast.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl Default for shape_b {
4949
}
5050
}
5151
}
52-
#[derive(ByteRepr, DeepClone)]
52+
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
5353
#[byte_size(64)]
5454
pub struct anon_0 {
5555
#[offset(0)]

‎tests/unit/out/refcount/union_field_alignment.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::io::prelude::*;
66
use std::io::{Read, Seek, Write};
77
use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
9-
#[derive(ByteRepr, DeepClone)]
9+
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
1010
#[byte_size(8)]
1111
pub struct anon_0 {
1212
#[offset(0)]

0 commit comments

Comments
 (0)