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
25 changes: 20 additions & 5 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,12 +496,19 @@ ConverterRefCount::MaterializeTemp(const std::string &binding_name,
clang::QualType param_type,
clang::Expr *expr) {
auto pointee = param_type.getNonReferenceType();
auto value = ConvertFreshRValue(expr, pointee);
auto type_str = ToStringBase(pointee);
const auto *decl = in_const_initializer_ ? keyword::kStatic : keyword::kLet;

auto binding = std::format("{} {} : Value<{}> = Rc::new(RefCell::new({}));",
decl, binding_name, type_str, value);
std::string binding;
if (pointee->isConstantArrayType()) {
PushConversionKind push(*this, ConversionKind::FullRefCount);
binding =
std::format("{} {} : {} = {};", decl, binding_name, ToString(pointee),
BoxValue(ConvertVarInitValue(pointee, expr)));
} else {
auto value = ConvertFreshRValue(expr, pointee);
auto type_str = ToStringBase(pointee);
binding = std::format("{} {} : Value<{}> = Rc::new(RefCell::new({}));",
decl, binding_name, type_str, value);
}
auto ref =
in_const_initializer_ ? ".with(Value::as_pointer)" : ".as_pointer()";
return {binding, binding_name + ref};
Expand Down Expand Up @@ -3032,6 +3039,9 @@ void ConverterRefCount::ConvertPointerSubscript(
ConvertPointerElem(base, idx);
} else {
ConvertPointerOffset(base, idx);
if (expr->getType()->isArrayType()) {
StrCat(GetPointerDerefSuffix(expr->getType()), ".as_pointer()");
}
}
if (deref) {
StrCat(GetPointerDerefSuffix(expr->getType()));
Expand Down Expand Up @@ -3113,6 +3123,11 @@ void ConverterRefCount::ConvertDeref(clang::Expr *expr) {
str = std::move(buf).str();
}

if (isAddrOf() && pointee_type->isArrayType()) {
str = std::format("({}){}.as_pointer()", std::move(str),
GetPointerDerefSuffix(pointee_type));
}

if (isObject() && WantsElementPtr() &&
(IsBoxedType(ctx_, pointee_type) || pointee_type->isArrayType())) {
StrCat(std::format("Ptr::<{}>::decay(&({}))", ToString(pointee_type),
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/array_reference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ int main() {
assert(len(buf) == 4);
int arr[3] = {1, 2, 3};
assert(sum(arr) == 6);
assert(sum({1, 2, 3}) == 6);
fill(arr, 7);
assert(sum(arr) == 21);
assert(sum_twice(arr) == 42);
Expand All @@ -88,5 +89,17 @@ int main() {

const char *names[2] = {"ab", "cde"};
assert(total_len(names) == 5);

int rows[2][3] = {{1, 2, 3}, {4, 5, 6}};
int (*p)[3] = rows;
++p;
assert(*p == &rows[1][0]);
int (&r)[3] = *p;
assert(r[0] == 4);
assert(sum(r) == 15);
assert(sum(*p) == 15);
int (*q)[3] = rows;
assert(sum(q[1]) == 15);
assert(q[1] == &rows[1][0]);
return 0;
}
36 changes: 36 additions & 0 deletions tests/unit/out/refcount/array_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,12 @@ fn main_0() -> i32 {
assert!((({ len_0(buf.as_pointer(),) }) == 4));
let arr: Value<Box<[i32]>> = Rc::new(RefCell::new(Box::new([1, 2, 3])));
assert!((({ sum_2(arr.as_pointer(),) }) == 6));
assert!(
(({
let _a: Value<Box<[i32]>> = Rc::new(RefCell::new(Box::new([1, 2, 3])));
sum_2(_a.as_pointer())
}) == 6)
);
({ fill_3(arr.as_pointer(), 7) });
assert!((({ sum_2(arr.as_pointer(),) }) == 21));
assert!((({ sum_twice_4(arr.as_pointer(),) }) == 42));
Expand Down Expand Up @@ -130,6 +136,36 @@ fn main_0() -> i32 {
Ptr::<i8>::from_string_literal(b"cde"),
])));
assert!((({ total_len_13(names.as_pointer(),) }) == 5));
let rows: Value<Box<[Value<Box<[i32]>>]>> = Rc::new(RefCell::new(Box::new([
Rc::new(RefCell::new(Box::new([1, 2, 3]))),
Rc::new(RefCell::new(Box::new([4, 5, 6]))),
])));
let mut p: Ptr<Value<Box<[i32]>>> = (rows.as_pointer() as Ptr<Value<Box<[i32]>>>);
p.prefix_inc();
assert!(
({ ((p).read().as_pointer() as Ptr::<i32>).clone() } == {
((((rows.as_pointer() as Ptr<Value<Box<[i32]>>>)
.offset(1)
.read()
.as_pointer()) as Ptr<i32>)
.offset(0))
})
);
let r: Ptr<i32> = ((p).read().as_pointer()).clone();
assert!(((elem!((r), 0).read()) == 4));
assert!((({ sum_2((r).clone(),) }) == 15));
assert!((({ sum_2(((p).read().as_pointer()).clone(),) }) == 15));
let mut q: Ptr<Value<Box<[i32]>>> = (rows.as_pointer() as Ptr<Value<Box<[i32]>>>);
assert!((({ sum_2(q.offset((1) as isize).read().as_pointer(),) }) == 15));
assert!(
({ (q.offset((1) as isize).read().as_pointer() as Ptr::<i32>) } == {
((((rows.as_pointer() as Ptr<Value<Box<[i32]>>>)
.offset(1)
.read()
.as_pointer()) as Ptr<i32>)
.offset(0))
})
);
return 0;
}
pub fn __cpp2rust_init_globals() {}
20 changes: 20 additions & 0 deletions tests/unit/out/unsafe/array_reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ unsafe fn main_0() -> i32 {
assert!(((unsafe { len_0(&buf,) }) == (4)));
let mut arr: [i32; 3] = [1, 2, 3];
assert!(((unsafe { sum_2(&arr,) }) == (6)));
assert!(
((unsafe {
let mut _a: [i32; 3] = [1, 2, 3];
sum_2(&mut _a)
}) == (6))
);
(unsafe { fill_3(&mut arr, 7) });
assert!(((unsafe { sum_2(&arr,) }) == (21)));
assert!(((unsafe { sum_twice_4(&arr,) }) == (42)));
Expand All @@ -111,6 +117,20 @@ unsafe fn main_0() -> i32 {
assert!(((arr[(0) as usize]) == (3)));
let mut names: [*const libc::c_char; 2] = [c"ab".as_ptr(), c"cde".as_ptr()];
assert!(((unsafe { total_len_13(&mut names,) }) == (5)));
let mut rows: [[i32; 3]; 2] = [[1, 2, 3], [4, 5, 6]];
let mut p: *mut [i32; 3] = rows.as_mut_ptr();
p.prefix_inc();
assert!((((*p).as_mut_ptr()) == (&mut rows[(1) as usize][(0) as usize] as *mut i32)));
let r: *mut [i32; 3] = &mut (*p);
assert!((((*r)[(0) as usize]) == (4)));
assert!(((unsafe { sum_2(r,) }) == (15)));
assert!(((unsafe { sum_2(&(*p),) }) == (15)));
let mut q: *mut [i32; 3] = rows.as_mut_ptr();
assert!(((unsafe { sum_2(&(*q.offset((1) as isize)),) }) == (15)));
assert!(
(((*q.offset((1) as isize)).as_mut_ptr())
== (&mut rows[(1) as usize][(0) as usize] as *mut i32))
);
return 0;
}
pub unsafe fn __cpp2rust_init_globals() {}
Loading