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
10 changes: 8 additions & 2 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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"));
});
}

Expand Down
5 changes: 4 additions & 1 deletion libcc2rs-macros/src/cpp_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Source> for S with each converting constructor
Expand Down Expand Up @@ -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))
Expand Down
23 changes: 23 additions & 0 deletions tests/std/class/class.mem/class.conv/class.conv.ctor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <cassert>

// 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 <class U> 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;
}
24 changes: 24 additions & 0 deletions tests/std/class/class.mem/class.conv/class.conv.fct.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <cassert>

// 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;
}
Original file line number Diff line number Diff line change
@@ -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<p1_template_constructor_from_own_type_Probe>) -> 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<p1_template_constructor_from_own_type_Probe> = 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() {}
Original file line number Diff line number Diff line change
@@ -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<p2_non_const_conversion_function_Gauge> =
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<p2_non_const_conversion_function_Gauge> {
fn to_i64(&self) -> i64 {
return field!((*self), value).with_mut(|__v| __v.prefix_inc().clone());
}
}
pub fn __cpp2rust_init_globals() {}
46 changes: 46 additions & 0 deletions tests/std/class/class.mem/class.conv/out/unsafe/class.conv.ctor.rs
Original file line number Diff line number Diff line change
@@ -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() {}
38 changes: 38 additions & 0 deletions tests/std/class/class.mem/class.conv/out/unsafe/class.conv.fct.rs
Original file line number Diff line number Diff line change
@@ -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() {}
Loading