diff --git a/cpp2rust/converter/converter.cpp b/cpp2rust/converter/converter.cpp index 244826bf55..84414c6a83 100644 --- a/cpp2rust/converter/converter.cpp +++ b/cpp2rust/converter/converter.cpp @@ -1009,6 +1009,11 @@ void Converter::EmitConversionAttrs(clang::CXXRecordDecl *decl) { ForEachConvertingConstructor(decl, [&](clang::CXXConstructorDecl *ctor) { auto *param = ctor->getParamDecl(0); auto type = param->getType(); + if (ctx_.hasSameUnqualifiedType(type.getNonReferenceType(), + ctx_.getCanonicalTagType(decl)) && + RecordImplementsClone(decl)) { + return; + } auto source = GetUnboxedTypeAsString(type.getNonReferenceType().getUnqualifiedType()); bool is_move = type->isRValueReferenceType(); @@ -1034,8 +1039,9 @@ void Converter::EmitConversionAttrs(clang::CXXRecordDecl *decl) { if (!into_types.insert(target).second) { return; } - StrCat( - std::format("#[cpp_into({}, {})]", target, GetMethodName(conversion))); + StrCat(std::format("#[cpp_into({}, {}{})]", target, + GetMethodName(conversion), + conversion->isConst() ? "" : ", mut_ptr")); }); } diff --git a/libcc2rs-macros/src/cpp_from.rs b/libcc2rs-macros/src/cpp_from.rs index aed5ec94d1..36b30db1a3 100644 --- a/libcc2rs-macros/src/cpp_from.rs +++ b/libcc2rs-macros/src/cpp_from.rs @@ -14,6 +14,7 @@ use syn::{DeriveInput, Ident, Token, Type, parse_macro_input}; // #[cpp_from(T, new_3, mut_ptr)] // S(T &) // #[cpp_from_move(T, new_4, ptr)] // S(T &&) // #[cpp_into(i32, to_i32)] // operator int() const +// #[cpp_into(i64, to_i64, mut_ptr)] // operator long() // pub struct S { ... } // // Implements libcc2rs::CppFrom for S with each converting constructor @@ -199,7 +200,9 @@ pub fn expand(input: TokenStream, is_unsafe: bool) -> TokenStream { let op = &conversion.method; let self_ty = quote!(#name #ty_generics); let src = pointer(&syn::parse_quote!(#self_ty), false); - let body = if is_unsafe { + let body = if is_unsafe && matches!(conversion.kind, Kind::MutPtr) { + quote!((*src.cast_mut()).#op()) + } else if is_unsafe { quote!((*src).#op()) } else { quote!(#impl_trait::#op(&src)) diff --git a/tests/std/class/class.mem/class.conv/class.conv.ctor.cpp b/tests/std/class/class.mem/class.conv/class.conv.ctor.cpp new file mode 100644 index 0000000000..ca0d542c43 --- /dev/null +++ b/tests/std/class/class.mem/class.conv/class.conv.ctor.cpp @@ -0,0 +1,23 @@ +#include + +// N4950 [class.conv.ctor]/1: A constructor that is not explicit (9.2.3) +// specifies a conversion from the types of its parameters (if any) to the type +// of its class. +namespace p1_template_constructor_from_own_type { +struct Probe { + int value; + Probe(int v) : value(v) {} + template Probe(U &u) : value(u.value + 1) {} +}; + +void test() { + Probe probe(22); + Probe converted(probe); + assert(converted.value == 23); +} +} // namespace p1_template_constructor_from_own_type + +int main() { + p1_template_constructor_from_own_type::test(); + return 0; +} diff --git a/tests/std/class/class.mem/class.conv/class.conv.fct.cpp b/tests/std/class/class.mem/class.conv/class.conv.fct.cpp new file mode 100644 index 0000000000..4c083748a9 --- /dev/null +++ b/tests/std/class/class.mem/class.conv/class.conv.fct.cpp @@ -0,0 +1,24 @@ +#include + +// N4950 [class.conv.fct]/2: A conversion function shall have no non-object +// parameters and shall be a non-static member function of a class or class +// template X; it specifies a conversion from X to the type specified by the +// conversion-type-id +namespace p2_non_const_conversion_function { +struct Gauge { + long value; + operator long() { return ++value; } +}; + +void test() { + Gauge gauge{20}; + long converted = gauge; + assert(converted == 21); + assert(gauge.value == 21); +} +} // namespace p2_non_const_conversion_function + +int main() { + p2_non_const_conversion_function::test(); + return 0; +} diff --git a/tests/std/class/class.mem/class.conv/out/refcount/class.conv.ctor.rs b/tests/std/class/class.mem/class.conv/out/refcount/class.conv.ctor.rs new file mode 100644 index 0000000000..bbf5600882 --- /dev/null +++ b/tests/std/class/class.mem/class.conv/out/refcount/class.conv.ctor.rs @@ -0,0 +1,44 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +impl p1_template_constructor_from_own_type_Probe { + pub fn new_1(u: Ptr) -> Self { + Self { + value: (u.with(|__s| __s.value) + 1), + } + } +} +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Default)] +#[cpp_from(i32, new_2, value)] +#[byte_size(4)] +pub struct p1_template_constructor_from_own_type_Probe { + #[offset(0)] + pub value: i32, +} +impl p1_template_constructor_from_own_type_Probe { + pub fn new_2(mut v: i32) -> Self { + Self { value: v } + } +} +pub fn test_0() { + let probe: Value = Rc::new(RefCell::new( + p1_template_constructor_from_own_type_Probe::new_2({ 22 }), + )); + let mut converted: p1_template_constructor_from_own_type_Probe = + p1_template_constructor_from_own_type_Probe::new_1({ probe.as_pointer() }); + assert!((converted.value == 23)); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_0() }); + return 0; +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/class/class.mem/class.conv/out/refcount/class.conv.fct.rs b/tests/std/class/class.mem/class.conv/out/refcount/class.conv.fct.rs new file mode 100644 index 0000000000..f0d331931e --- /dev/null +++ b/tests/std/class/class.mem/class.conv/out/refcount/class.conv.fct.rs @@ -0,0 +1,42 @@ +extern crate libcc2rs; +use libcc2rs::*; +use std::cell::RefCell; +use std::collections::BTreeMap; +use std::io::prelude::*; +use std::io::{Read, Seek, Write}; +use std::os::fd::AsFd; +use std::rc::{Rc, Weak}; +#[derive(Clone, Record, ByteRepr, VaArg, FnPtrArg, CppFrom, Default)] +#[cpp_into(i64, to_i64, mut_ptr)] +#[byte_size(8)] +pub struct p2_non_const_conversion_function_Gauge { + #[offset(0)] + pub value: i64, +} +pub fn test_0() { + let gauge: Value = + Rc::new(RefCell::new(p2_non_const_conversion_function_Gauge { + value: 20_i64, + })); + let mut converted: i64 = + ({ p2_non_const_conversion_function_GaugeImpl::to_i64(&gauge.as_pointer()) }); + assert!((converted == 21_i64)); + assert!(({ ({ (*gauge.borrow()).value } == 21_i64) })); +} +pub fn main() { + __cpp2rust_init_globals(); + std::process::exit(main_0()); +} +fn main_0() -> i32 { + ({ test_0() }); + return 0; +} +pub trait p2_non_const_conversion_function_GaugeImpl { + fn to_i64(&self) -> i64; +} +impl p2_non_const_conversion_function_GaugeImpl for Ptr { + fn to_i64(&self) -> i64 { + return field!((*self), value).with_mut(|__v| __v.prefix_inc().clone()); + } +} +pub fn __cpp2rust_init_globals() {} diff --git a/tests/std/class/class.mem/class.conv/out/unsafe/class.conv.ctor.rs b/tests/std/class/class.mem/class.conv/out/unsafe/class.conv.ctor.rs new file mode 100644 index 0000000000..c57544300b --- /dev/null +++ b/tests/std/class/class.mem/class.conv/out/unsafe/class.conv.ctor.rs @@ -0,0 +1,46 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +impl p1_template_constructor_from_own_type_Probe { + pub unsafe fn new_1(u: *mut p1_template_constructor_from_own_type_Probe) -> Self { + let mut this = Self { + value: (((*u).value) + (1)), + }; + this + } +} +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, CppFromUnsafe, Default)] +#[cpp_from(i32, new_2, value)] +pub struct p1_template_constructor_from_own_type_Probe { + pub value: i32, +} +impl p1_template_constructor_from_own_type_Probe { + pub unsafe fn new_2(mut v: i32) -> Self { + let mut this = Self { value: v }; + this + } +} +pub unsafe fn test_0() { + let mut probe: p1_template_constructor_from_own_type_Probe = + p1_template_constructor_from_own_type_Probe::new_2({ 22 }); + let mut converted: p1_template_constructor_from_own_type_Probe = + p1_template_constructor_from_own_type_Probe::new_1({ &mut probe }); + assert!(((converted.value) == (23))); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_0() }); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {} diff --git a/tests/std/class/class.mem/class.conv/out/unsafe/class.conv.fct.rs b/tests/std/class/class.mem/class.conv/out/unsafe/class.conv.fct.rs new file mode 100644 index 0000000000..8cbeae9e48 --- /dev/null +++ b/tests/std/class/class.mem/class.conv/out/unsafe/class.conv.fct.rs @@ -0,0 +1,38 @@ +extern crate libc; +use libc::*; +extern crate libcc2rs; +use libcc2rs::*; +use std::collections::BTreeMap; +use std::io::{Read, Seek, Write}; +use std::os::fd::{AsFd, FromRawFd, IntoRawFd}; +use std::rc::Rc; +#[repr(C)] +#[derive(Copy, Clone, VaArg, FnPtrArg, CppFromUnsafe, Default)] +#[cpp_into(i64, to_i64, mut_ptr)] +pub struct p2_non_const_conversion_function_Gauge { + pub value: i64, +} +impl p2_non_const_conversion_function_Gauge { + pub unsafe fn to_i64(&mut self) -> i64 { + return (*self.value.prefix_inc()); + } +} +pub unsafe fn test_0() { + let mut gauge: p2_non_const_conversion_function_Gauge = + p2_non_const_conversion_function_Gauge { value: 20_i64 }; + let mut converted: i64 = + (unsafe { p2_non_const_conversion_function_Gauge::to_i64(&mut gauge) }); + assert!(((converted) == (21_i64))); + assert!(((gauge.value) == (21_i64))); +} +pub fn main() { + unsafe { + __cpp2rust_init_globals(); + std::process::exit(main_0() as i32); + } +} +unsafe fn main_0() -> i32 { + (unsafe { test_0() }); + return 0; +} +pub unsafe fn __cpp2rust_init_globals() {}