Skip to content
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
12 changes: 8 additions & 4 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,14 @@ void Converter::EmitGlobalInits(Model model, std::string &out) {
out += "}\n";
}

void Converter::EmitOpaqueRecords(std::string &out) {
void Converter::EmitOpaqueRecords(Model model, std::string &out) {
record_decls_.ForEachUndefined([&](const std::string &name) {
out += "#[derive(Clone, Copy, Default, ByteRepr, VaArg, FnPtrArg)]";
out += "#[byte_size(1)]";
if (model == Model::kRefCount) {
out += "#[derive(Clone, Copy, Default, ByteRepr, VaArg, FnPtrArg)]";
out += "#[byte_size(1)]";
} else {
out += "#[derive(Clone, Copy, Default, VaArg, FnPtrArg)]";
}
out += "pub struct ";
out += name;
out += ";\n";
Expand Down Expand Up @@ -4213,7 +4217,7 @@ std::string Converter::GetRecordName(const clang::NamedDecl *decl) const {
std::vector<const char *>
Converter::GetStructAttributes(const clang::RecordDecl *decl) {
if (decl->isUnion()) {
return {"Copy", "Clone", "FnPtrArg"};
return {"Copy", "Clone", "FnPtrArg", "VaArg"};
}

std::vector<const char *> struct_attrs;
Expand Down
2 changes: 1 addition & 1 deletion cpp2rust/converter/converter.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {

virtual void EmitFilePreamble();

static void EmitOpaqueRecords(std::string &out);
static void EmitOpaqueRecords(Model model, std::string &out);
static void EmitGlobalInits(Model model, std::string &out);

static void EmitVirtualMethods(std::string &out);
Expand Down
7 changes: 6 additions & 1 deletion cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,11 @@ void ConverterRefCount::EmitRustUnion(clang::RecordDecl *decl) {
RuleRegistry::SetDerives(
ctx_, ctx_.getCanonicalTagType(decl),
std::vector<std::string>(attrs.begin(), attrs.end()));
StrCat("#[derive(");
for (auto *attr : attrs) {
StrCat(attr, ',');
}
StrCat(")]");

auto size = ctx_.getTypeSizeInChars(ctx_.getCanonicalTagType(decl));
StrCat(std::format(
Expand Down Expand Up @@ -2470,7 +2475,7 @@ ConverterRefCount::GetStructAttributes(const clang::RecordDecl *decl) {
std::vector<const char *> attrs;

if (decl->isUnion()) {
return attrs;
return {"VaArg", "FnPtrArg"};
}

if (RecordDerivesClone(decl)) {
Expand Down
4 changes: 2 additions & 2 deletions cpp2rust/cpp2rust_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ std::string TranspileSrc(std::string_view cc_code, Model model,
rules_dir),
cc_code, tool_args, std::filesystem::path(filename).filename().string(),
filename.ends_with(".c") ? CLANG_C_COMPILER : CLANG_CXX_COMPILER);
Converter::EmitOpaqueRecords(rs_code);
Converter::EmitOpaqueRecords(model, rs_code);
Converter::EmitVirtualMethods(rs_code);
if (model == Model::kRefCount) {
ConverterRefCount::EmitMethodsOnPtr(rs_code);
Expand Down Expand Up @@ -76,7 +76,7 @@ std::string TranspileDir(std::string_view build_dir, Model model,
std::string rs_code;
FrontendActionFactory factory(rs_code, model, rules_dir);
Tool.run(&factory);
Converter::EmitOpaqueRecords(rs_code);
Converter::EmitOpaqueRecords(model, rs_code);
Converter::EmitVirtualMethods(rs_code);
if (model == Model::kRefCount) {
ConverterRefCount::EmitMethodsOnPtr(rs_code);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ unsafe fn main_0() -> i32 {
pub unsafe fn touch_0(mut c: *mut container) {
&((*c).p);
}
#[derive(Clone, Copy, Default, ByteRepr, VaArg, FnPtrArg)]
#[byte_size(1)]
#[derive(Clone, Copy, Default, VaArg, FnPtrArg)]
pub struct opaque;
pub unsafe fn __cpp2rust_init_globals() {}
4 changes: 2 additions & 2 deletions tests/unit/out/refcount/tag_vs_identifier_collision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct point_struct {
#[offset(4)]
pub y: i32,
}
#[derive(ByteRepr, DeepClone)]
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
#[byte_size(4)]
pub struct point {
#[offset(0)]
Expand All @@ -48,7 +48,7 @@ impl Default for point {
}
}
}
#[derive(ByteRepr, DeepClone)]
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
#[byte_size(4)]
pub struct slot_union {
#[offset(0)]
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/refcount/union_addrof_external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Default for record {
}
}
}
#[derive(ByteRepr, DeepClone)]
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
#[byte_size(128)]
pub struct anon_0 {
#[offset(0)]
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/out/refcount/union_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
#[derive(ByteRepr, DeepClone)]
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
#[byte_size(4)]
pub struct basic {
#[offset(0)]
Expand All @@ -28,7 +28,7 @@ impl Default for basic {
}
}
}
#[derive(ByteRepr, DeepClone)]
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
#[byte_size(1)]
pub struct empty {
#[offset(0)]
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/refcount/union_cross_arm_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl Default for shape_b {
}
}
}
#[derive(ByteRepr, DeepClone)]
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
#[byte_size(64)]
pub struct anon_0 {
#[offset(0)]
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/refcount/union_field_alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
#[derive(ByteRepr, DeepClone)]
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
#[byte_size(8)]
pub struct anon_0 {
#[offset(0)]
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/refcount/union_flex_array_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::io::prelude::*;
use std::io::{Read, Seek, Write};
use std::os::fd::AsFd;
use std::rc::{Rc, Weak};
#[derive(ByteRepr, DeepClone)]
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
#[byte_size(8)]
pub struct anon_0 {
#[offset(0)]
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/refcount/union_memset_memcpy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Default for shape_b {
}
}
}
#[derive(ByteRepr, DeepClone)]
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
#[byte_size(256)]
pub struct anon_0 {
#[offset(0)]
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/out/refcount/union_nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Default for record {
}
}
}
#[derive(ByteRepr, DeepClone)]
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
#[byte_size(128)]
pub struct anon_0 {
#[offset(0)]
Expand Down Expand Up @@ -52,7 +52,7 @@ pub struct inner {
#[byte_size(128)]
pub view: anon_0,
}
#[derive(ByteRepr, DeepClone)]
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
#[byte_size(128)]
pub struct anon_1 {
#[offset(0)]
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/refcount/union_pointer_pun_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn main_0() -> i32 {
assert!(((({ (out).to_any() } == { (a.as_pointer()).to_any() }) as i32) != 0));
return 0;
}
#[derive(ByteRepr, DeepClone)]
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
#[byte_size(8)]
pub struct anon_0 {
#[offset(0)]
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/refcount/union_pointer_pun_writethrough.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main_0() -> i32 {
assert!(((((*x.borrow()) == 42_i64) as i32) != 0));
return 0;
}
#[derive(ByteRepr, DeepClone)]
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
#[byte_size(8)]
pub struct anon_0 {
#[offset(0)]
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/refcount/union_struct_dual_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Inner {
pub fn sum_inner_0(mut i: Ptr<Inner>) -> i32 {
return ({ i.with(|__s| __s.a) } + { i.with(|__s| __s.b) });
}
#[derive(ByteRepr, DeepClone)]
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
#[byte_size(16)]
pub struct anon_1 {
#[offset(0)]
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/refcount/union_tagged_many_arms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub const Tag_enum_T_NUM_U: Tag_enum = 1;
pub const Tag_enum_T_TEXT: Tag_enum = 2;
pub const Tag_enum_T_FLOAT: Tag_enum = 3;
pub const Tag_enum_T_REF: Tag_enum = 4;
#[derive(ByteRepr, DeepClone)]
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
#[byte_size(8)]
pub struct anon_0 {
#[offset(0)]
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/refcount/union_tagged_simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::rc::{Rc, Weak};
pub type Kind_enum = u32;
pub const Kind_enum_KIND_NONE: Kind_enum = 0;
pub const Kind_enum_KIND_DONE: Kind_enum = 1;
#[derive(ByteRepr, DeepClone)]
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
#[byte_size(8)]
pub struct anon_0 {
#[offset(0)]
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/refcount/union_tagged_struct_arms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub struct anon_3 {
#[offset(32)]
pub width: i32,
}
#[derive(ByteRepr, DeepClone)]
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
#[byte_size(40)]
pub struct anon_0 {
#[offset(0)]
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/refcount/union_void_ptr_sized_deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub type Width_enum = u32;
pub const Width_enum_W_64: Width_enum = 0;
pub const Width_enum_W_32: Width_enum = 1;
pub const Width_enum_W_16: Width_enum = 2;
#[derive(ByteRepr, DeepClone)]
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
#[byte_size(8)]
pub struct anon_0 {
#[offset(0)]
Expand Down
37 changes: 37 additions & 0 deletions tests/unit/out/refcount/va_arg_struct_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,38 @@ pub fn set_error_0(mut ctx: Ptr<context>, fmt: Ptr<i8>, __args: &[VaArg]) {
field!(ctx, last_error).write(__rhs);
}
}
#[derive(VaArg, FnPtrArg, ByteRepr, DeepClone)]
#[byte_size(8)]
pub struct value {
#[offset(0)]
#[byte_size(8)]
__bytes: Value<Box<[u8]>>,
}
impl value {
pub fn i(&self) -> Ptr<i32> {
(self.__bytes.as_pointer() as Ptr<u8>).reinterpret_cast()
}
pub fn l(&self) -> Ptr<i64> {
(self.__bytes.as_pointer() as Ptr<u8>).reinterpret_cast()
}
}
impl Default for value {
fn default() -> Self {
value {
__bytes: Rc::new(RefCell::new(Box::from([0u8; 8]))),
}
}
}
pub fn pick_1(use_long: i32, __args: &[VaArg]) -> i64 {
let use_long: Value<i32> = Rc::new(RefCell::new(use_long));
let ap: Value<VaList> = Rc::new(RefCell::new(VaList::default()));
(*ap.borrow_mut()) = VaList::new(__args);
let v: Value<value> = Rc::new(RefCell::new((*ap.borrow_mut()).arg::<value>()));
if ((*use_long.borrow()) != 0) {
return ((*v.borrow()).l().read());
}
return (((*v.borrow()).i().read()) as i64);
}
pub fn main() {
__cpp2rust_init_globals();
std::process::exit(main_0());
Expand All @@ -48,6 +80,11 @@ fn main_0() -> i32 {
)
});
assert!(((({ (*ctx.borrow()).last_error } == 42) as i32) != 0));
let v: Value<value> = <Value<value>>::default();
(*v.borrow_mut()).l().write((1_i64 << 40));
assert!((((({ pick_1(1, &[((*v.borrow()).clone()).into(),]) }) == (1_i64 << 40)) as i32) != 0));
(*v.borrow_mut()).i().write(7);
assert!((((({ pick_1(0, &[((*v.borrow()).clone()).into(),]) }) == 7_i64) as i32) != 0));
return 0;
}
pub fn __cpp2rust_init_globals() {}
3 changes: 1 addition & 2 deletions tests/unit/out/unsafe/opaque_forward_decl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ unsafe fn main_0() -> i32 {
&(c.p);
return ((c.x) - (42));
}
#[derive(Clone, Copy, Default, ByteRepr, VaArg, FnPtrArg)]
#[byte_size(1)]
#[derive(Clone, Copy, Default, VaArg, FnPtrArg)]
pub struct opaque;
pub unsafe fn __cpp2rust_init_globals() {}
4 changes: 2 additions & 2 deletions tests/unit/out/unsafe/tag_vs_identifier_collision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct point_struct {
pub y: i32,
}
#[repr(C)]
#[derive(Copy, Clone, FnPtrArg)]
#[derive(Copy, Clone, FnPtrArg, VaArg)]
pub union point {
pub whole: i32,
pub half: i16,
Expand All @@ -34,7 +34,7 @@ impl Default for point {
}
}
#[repr(C)]
#[derive(Copy, Clone, FnPtrArg)]
#[derive(Copy, Clone, FnPtrArg, VaArg)]
pub union slot_union {
pub i: i32,
pub u: u32,
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/unsafe/union_addrof_external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct record {
pub pad: [libc::c_char; 8],
}
#[repr(C)]
#[derive(Copy, Clone, FnPtrArg)]
#[derive(Copy, Clone, FnPtrArg, VaArg)]
pub union anon_0 {
pub h: record,
pub raw_: [libc::c_char; 128],
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/out/unsafe/union_basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
#[repr(C)]
#[derive(Copy, Clone, FnPtrArg)]
#[derive(Copy, Clone, FnPtrArg, VaArg)]
pub union basic {
pub i: i32,
pub f: f32,
Expand All @@ -18,7 +18,7 @@ impl Default for basic {
}
}
#[repr(C)]
#[derive(Copy, Clone, FnPtrArg)]
#[derive(Copy, Clone, FnPtrArg, VaArg)]
pub union empty {
__empty: u8,
}
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/unsafe/union_cross_arm_cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct shape_b {
pub tail: u32,
}
#[repr(C)]
#[derive(Copy, Clone, FnPtrArg)]
#[derive(Copy, Clone, FnPtrArg, VaArg)]
pub union anon_0 {
pub a: shape_a,
pub b: shape_b,
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/unsafe/union_field_alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
#[repr(C)]
#[derive(Copy, Clone, FnPtrArg)]
#[derive(Copy, Clone, FnPtrArg, VaArg)]
pub union anon_0 {
pub bytes: [u8; 1],
pub aligner: *mut ::libc::c_void,
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/unsafe/union_flex_array_member.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::io::{Read, Seek, Write};
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
use std::rc::Rc;
#[repr(C)]
#[derive(Copy, Clone, FnPtrArg)]
#[derive(Copy, Clone, FnPtrArg, VaArg)]
pub union anon_0 {
pub bytes: [u8; 1],
pub aligner: *mut ::libc::c_void,
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/out/unsafe/union_memset_memcpy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct shape_b {
pub fill: [libc::c_char; 8],
}
#[repr(C)]
#[derive(Copy, Clone, FnPtrArg)]
#[derive(Copy, Clone, FnPtrArg, VaArg)]
pub union anon_0 {
pub a: shape_a,
pub b: shape_b,
Expand Down
Loading
Loading