Skip to content

Commit 1cc7173

Browse files
committed
Unbox holders of std::unique_ptr
1 parent 76d56ee commit 1cc7173

10 files changed

Lines changed: 149 additions & 174 deletions

File tree

‎cpp2rust/converter/models/boxed_vars.cpp‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@
1414
namespace cpp2rust {
1515
namespace {
1616
// Whether decl may be stored without a Value, if its address is not taken:
17-
// scalars, arrays of non-arrays, and user-defined structs, unless they have a
18-
// destructor, which is called with a pointer to them.
17+
// scalars, arrays of non-arrays, user-defined structs and unique_ptrs, unless
18+
// they have a destructor, which is called with a pointer to them.
1919
bool CanUnbox(const clang::VarDecl *decl) {
2020
auto type = decl->getType();
2121
if (type->isConstantArrayType()) {
2222
if (type->getAsArrayTypeUnsafe()->getElementType()->isArrayType()) {
2323
return false;
2424
}
2525
} else if (type->isStructureOrClassType()) {
26-
if (!IsUserDefinedDecl(type->getAsRecordDecl()) ||
26+
if ((!IsUserDefinedDecl(type->getAsRecordDecl()) && !IsUniquePtr(type)) ||
2727
TypeNeedsDestruction(type)) {
2828
return false;
2929
}
@@ -118,6 +118,19 @@ class AddressTakenVisitor
118118
AddValueUse(expr->getArg(0));
119119
AddValueUse(IgnoreNoOpCasts(expr->getArg(1)));
120120
}
121+
// Dereferencing a unique_ptr reads the pointer it holds; the object it
122+
// points to is not stored in the unique_ptr.
123+
switch (expr->getOperator()) {
124+
case clang::OO_Subscript:
125+
case clang::OO_Star:
126+
case clang::OO_Arrow:
127+
if (IsUniquePtr(expr->getArg(0)->getType())) {
128+
AddValueUse(IgnoreNoOpCasts(expr->getArg(0)));
129+
}
130+
break;
131+
default:
132+
break;
133+
}
121134
return true;
122135
}
123136

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ pub fn All_0(arr: Ptr<Option<Value<Box<[i32]>>>>, mut N: i32, mut element: i32)
1919
}
2020
((arr).clone() as Ptr<Option<Value<Box<[i32]>>>>).write((*all.borrow_mut()).take());
2121
}
22-
pub fn Consume_1(arr: Option<Value<Box<[i32]>>>, mut N: i32) -> i32 {
23-
let arr: Value<Option<Value<Box<[i32]>>>> = Rc::new(RefCell::new(arr));
22+
pub fn Consume_1(mut arr: Option<Value<Box<[i32]>>>, mut N: i32) -> i32 {
2423
let mut sum: i32 = 0;
2524
let mut i: i32 = -1_i32;
2625
'loop_: while (i.prefix_inc() < N) {
27-
sum += (*arr.borrow()).as_ref().unwrap().borrow()[(i as usize) as usize];
26+
sum += arr.as_ref().unwrap().borrow()[(i as usize) as usize];
2827
}
2928
return sum;
3029
}

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

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -50,44 +50,39 @@ pub fn main() {
5050
std::process::exit(main_0());
5151
}
5252
fn main_0() -> i32 {
53-
let tree: Value<Option<Value<node_t>>> =
54-
Rc::new(RefCell::new(Some(Rc::new(RefCell::new(node_t {
55-
left: Ptr::<node_t>::null(),
56-
right: Ptr::<node_t>::null(),
57-
value: 0,
58-
})))));
59-
let n1: Value<Option<Value<node_t>>> =
60-
Rc::new(RefCell::new(Some(Rc::new(RefCell::new(node_t {
61-
left: Ptr::<node_t>::null(),
62-
right: Ptr::<node_t>::null(),
63-
value: 1,
64-
})))));
65-
let n2: Value<Option<Value<node_t>>> =
66-
Rc::new(RefCell::new(Some(Rc::new(RefCell::new(node_t {
67-
left: Ptr::<node_t>::null(),
68-
right: Ptr::<node_t>::null(),
69-
value: 2,
70-
})))));
71-
let n3: Value<Option<Value<node_t>>> =
72-
Rc::new(RefCell::new(Some(Rc::new(RefCell::new(node_t {
73-
left: Ptr::<node_t>::null(),
74-
right: Ptr::<node_t>::null(),
75-
value: 3,
76-
})))));
77-
let n4: Value<Option<Value<node_t>>> =
78-
Rc::new(RefCell::new(Some(Rc::new(RefCell::new(node_t {
79-
left: Ptr::<node_t>::null(),
80-
right: Ptr::<node_t>::null(),
81-
value: 4,
82-
})))));
83-
let mut ptr1: Ptr<node_t> = ((*tree.borrow()).as_pointer());
84-
let __rhs = ({ insert_1((ptr1).clone(), ((*n1.borrow()).as_pointer())) });
53+
let mut tree: Option<Value<node_t>> = Some(Rc::new(RefCell::new(node_t {
54+
left: Ptr::<node_t>::null(),
55+
right: Ptr::<node_t>::null(),
56+
value: 0,
57+
})));
58+
let mut n1: Option<Value<node_t>> = Some(Rc::new(RefCell::new(node_t {
59+
left: Ptr::<node_t>::null(),
60+
right: Ptr::<node_t>::null(),
61+
value: 1,
62+
})));
63+
let mut n2: Option<Value<node_t>> = Some(Rc::new(RefCell::new(node_t {
64+
left: Ptr::<node_t>::null(),
65+
right: Ptr::<node_t>::null(),
66+
value: 2,
67+
})));
68+
let mut n3: Option<Value<node_t>> = Some(Rc::new(RefCell::new(node_t {
69+
left: Ptr::<node_t>::null(),
70+
right: Ptr::<node_t>::null(),
71+
value: 3,
72+
})));
73+
let mut n4: Option<Value<node_t>> = Some(Rc::new(RefCell::new(node_t {
74+
left: Ptr::<node_t>::null(),
75+
right: Ptr::<node_t>::null(),
76+
value: 4,
77+
})));
78+
let mut ptr1: Ptr<node_t> = (tree.as_pointer());
79+
let __rhs = ({ insert_1((ptr1).clone(), (n1.as_pointer())) });
8580
ptr1 = __rhs;
86-
let __rhs = ({ insert_1((ptr1).clone(), ((*n2.borrow()).as_pointer())) });
81+
let __rhs = ({ insert_1((ptr1).clone(), (n2.as_pointer())) });
8782
ptr1 = __rhs;
88-
let __rhs = ({ insert_1((ptr1).clone(), ((*n3.borrow()).as_pointer())) });
83+
let __rhs = ({ insert_1((ptr1).clone(), (n3.as_pointer())) });
8984
ptr1 = __rhs;
90-
let __rhs = ({ insert_1((ptr1).clone(), ((*n4.borrow()).as_pointer())) });
85+
let __rhs = ({ insert_1((ptr1).clone(), (n4.as_pointer())) });
9186
ptr1 = __rhs;
9287
assert!(
9388
(((((({ find_0((ptr1).clone(), 0,) }).with(|__s| __s.value) == 0)

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

Lines changed: 39 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,19 @@ pub fn fft_3(a: Ptr<Option<Value<Box<[Complex]>>>>, mut N: i32) -> Option<Value<
5050
(*y.borrow()).as_ref().unwrap().borrow_mut()[(0_usize) as usize] = __rhs;
5151
return (*y.borrow_mut()).take();
5252
}
53-
let w: Value<Option<Value<Box<[Complex]>>>> =
54-
Rc::new(RefCell::new(Some(Rc::new(RefCell::new(
55-
(0..(N as usize))
56-
.map(|_| <Complex>::default())
57-
.collect::<Box<[_]>>(),
58-
)))));
53+
let mut w: Option<Value<Box<[Complex]>>> = Some(Rc::new(RefCell::new(
54+
(0..(N as usize))
55+
.map(|_| <Complex>::default())
56+
.collect::<Box<[_]>>(),
57+
)));
5958
let mut i: i32 = 0;
6059
'loop_: while (i < N) {
6160
let mut alpha: f64 = ((((-2_i32 as f64) * 3.141592654E+0) * (i as f64)) / (N as f64));
6261
let __rhs = Complex {
6362
re: alpha.cos(),
6463
img: alpha.sin(),
6564
};
66-
(*w.borrow()).as_ref().unwrap().borrow_mut()[(i as usize) as usize] = __rhs;
65+
w.as_ref().unwrap().borrow_mut()[(i as usize) as usize] = __rhs;
6766
i.postfix_inc();
6867
}
6968
let A0: Value<Option<Value<Box<[Complex]>>>> =
@@ -102,20 +101,15 @@ pub fn fft_3(a: Ptr<Option<Value<Box<[Complex]>>>>, mut N: i32) -> Option<Value<
102101
(*A1.borrow()).as_ref().unwrap().borrow_mut()[(i as usize) as usize] = __rhs;
103102
i.postfix_inc();
104103
}
105-
let y0: Value<Option<Value<Box<[Complex]>>>> =
106-
Rc::new(RefCell::new(({ fft_3(A0.as_pointer(), (N / 2)) })));
107-
let y1: Value<Option<Value<Box<[Complex]>>>> =
108-
Rc::new(RefCell::new(({ fft_3(A1.as_pointer(), (N / 2)) })));
104+
let mut y0: Option<Value<Box<[Complex]>>> = ({ fft_3(A0.as_pointer(), (N / 2)) });
105+
let mut y1: Option<Value<Box<[Complex]>>> = ({ fft_3(A1.as_pointer(), (N / 2)) });
109106
let mut k: i32 = 0;
110107
'loop_: while (k < (N / 2)) {
111108
let mut yk: Complex = ({
112-
let _z1: Complex =
113-
((*y0.borrow()).as_ref().unwrap().borrow()[(k as usize) as usize]).clone();
109+
let _z1: Complex = (y0.as_ref().unwrap().borrow()[(k as usize) as usize]).clone();
114110
let _z2: Complex = ({
115-
let _z1: Complex =
116-
((*w.borrow()).as_ref().unwrap().borrow()[(k as usize) as usize]).clone();
117-
let _z2: Complex =
118-
((*y1.borrow()).as_ref().unwrap().borrow()[(k as usize) as usize]).clone();
111+
let _z1: Complex = (w.as_ref().unwrap().borrow()[(k as usize) as usize]).clone();
112+
let _z2: Complex = (y1.as_ref().unwrap().borrow()[(k as usize) as usize]).clone();
119113
Product_0(_z1, _z2)
120114
});
121115
Sum_1(_z1, _z2)
@@ -125,17 +119,14 @@ pub fn fft_3(a: Ptr<Option<Value<Box<[Complex]>>>>, mut N: i32) -> Option<Value<
125119
img: yk.img,
126120
};
127121
let mut yk_n2: Complex = ({
128-
let _z1: Complex =
129-
((*y0.borrow()).as_ref().unwrap().borrow()[(k as usize) as usize]).clone();
122+
let _z1: Complex = (y0.as_ref().unwrap().borrow()[(k as usize) as usize]).clone();
130123
let _z2: Complex = ({
131124
Neg_2(
132125
({
133-
let _z1: Complex = ((*w.borrow()).as_ref().unwrap().borrow()
134-
[(k as usize) as usize])
135-
.clone();
136-
let _z2: Complex = ((*y1.borrow()).as_ref().unwrap().borrow()
137-
[(k as usize) as usize])
138-
.clone();
126+
let _z1: Complex =
127+
(w.as_ref().unwrap().borrow()[(k as usize) as usize]).clone();
128+
let _z2: Complex =
129+
(y1.as_ref().unwrap().borrow()[(k as usize) as usize]).clone();
139130
Product_0(_z1, _z2)
140131
}),
141132
)
@@ -171,39 +162,34 @@ fn main_0() -> i32 {
171162
(*a.borrow()).as_ref().unwrap().borrow_mut()[(i as usize) as usize] = __rhs;
172163
i.postfix_inc();
173164
}
174-
let b: Value<Option<Value<Box<[Complex]>>>> =
175-
Rc::new(RefCell::new(({ fft_3(a.as_pointer(), N) })));
176-
let reals: Value<Option<Value<Box<[i32]>>>> =
177-
Rc::new(RefCell::new(Some(Rc::new(RefCell::new(
178-
(0..(N as usize))
179-
.map(|_| <i32>::default())
180-
.collect::<Box<[_]>>(),
181-
)))));
182-
let imgs: Value<Option<Value<Box<[i32]>>>> =
183-
Rc::new(RefCell::new(Some(Rc::new(RefCell::new(
184-
(0..(N as usize))
185-
.map(|_| <i32>::default())
186-
.collect::<Box<[_]>>(),
187-
)))));
165+
let mut b: Option<Value<Box<[Complex]>>> = ({ fft_3(a.as_pointer(), N) });
166+
let mut reals: Option<Value<Box<[i32]>>> = Some(Rc::new(RefCell::new(
167+
(0..(N as usize))
168+
.map(|_| <i32>::default())
169+
.collect::<Box<[_]>>(),
170+
)));
171+
let mut imgs: Option<Value<Box<[i32]>>> = Some(Rc::new(RefCell::new(
172+
(0..(N as usize))
173+
.map(|_| <i32>::default())
174+
.collect::<Box<[_]>>(),
175+
)));
188176
let mut i: i32 = 0;
189177
'loop_: while (i < N) {
190-
let __rhs =
191-
({ (*b.borrow()).as_ref().unwrap().borrow()[(i as usize) as usize].re }.round() as i32);
192-
(*reals.borrow()).as_ref().unwrap().borrow_mut()[(i as usize) as usize] = __rhs;
193-
let __rhs = ({ (*b.borrow()).as_ref().unwrap().borrow()[(i as usize) as usize].img }.round()
194-
as i32);
195-
(*imgs.borrow()).as_ref().unwrap().borrow_mut()[(i as usize) as usize] = __rhs;
178+
let __rhs = ({ b.as_ref().unwrap().borrow()[(i as usize) as usize].re }.round() as i32);
179+
reals.as_ref().unwrap().borrow_mut()[(i as usize) as usize] = __rhs;
180+
let __rhs = ({ b.as_ref().unwrap().borrow()[(i as usize) as usize].img }.round() as i32);
181+
imgs.as_ref().unwrap().borrow_mut()[(i as usize) as usize] = __rhs;
196182
i.prefix_inc();
197183
}
198184
assert!(
199-
((((((((*reals.borrow()).as_ref().unwrap().borrow()[(0_usize) as usize] == 10)
200-
&& ((*imgs.borrow()).as_ref().unwrap().borrow()[(0_usize) as usize] == 0))
201-
&& ((*reals.borrow()).as_ref().unwrap().borrow()[(1_usize) as usize] == -2_i32))
202-
&& ((*imgs.borrow()).as_ref().unwrap().borrow()[(1_usize) as usize] == 2))
203-
&& ((*reals.borrow()).as_ref().unwrap().borrow()[(2_usize) as usize] == -2_i32))
204-
&& ((*imgs.borrow()).as_ref().unwrap().borrow()[(2_usize) as usize] == 0))
205-
&& ((*reals.borrow()).as_ref().unwrap().borrow()[(3_usize) as usize] == -2_i32))
206-
&& ((*imgs.borrow()).as_ref().unwrap().borrow()[(3_usize) as usize] == -2_i32)
185+
(((((((reals.as_ref().unwrap().borrow()[(0_usize) as usize] == 10)
186+
&& (imgs.as_ref().unwrap().borrow()[(0_usize) as usize] == 0))
187+
&& (reals.as_ref().unwrap().borrow()[(1_usize) as usize] == -2_i32))
188+
&& (imgs.as_ref().unwrap().borrow()[(1_usize) as usize] == 2))
189+
&& (reals.as_ref().unwrap().borrow()[(2_usize) as usize] == -2_i32))
190+
&& (imgs.as_ref().unwrap().borrow()[(2_usize) as usize] == 0))
191+
&& (reals.as_ref().unwrap().borrow()[(3_usize) as usize] == -2_i32))
192+
&& (imgs.as_ref().unwrap().borrow()[(3_usize) as usize] == -2_i32)
207193
);
208194
return 0;
209195
}

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

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,13 @@ pub fn HuffmanCodes_5(
194194
freq: Ptr<Option<Value<Box<[i32]>>>>,
195195
mut size: i32,
196196
) -> Option<Value<Box<[i32]>>> {
197-
let minHeap: Value<Option<Value<MinHeap>>> = Rc::new(RefCell::new(
198-
({
199-
let _data: Ptr<Option<Value<Box<[i8]>>>> = (data).clone();
200-
let _freq: Ptr<Option<Value<Box<[i32]>>>> = (freq).clone();
201-
let _size: i32 = size;
202-
Huffman_2(_data, _freq, _size)
203-
}),
204-
));
205-
let mut root: Ptr<MinHeapNode> =
206-
({ MinHeapImpl::ExtractMin(&((*minHeap.borrow()).as_pointer())) });
197+
let mut minHeap: Option<Value<MinHeap>> = ({
198+
let _data: Ptr<Option<Value<Box<[i8]>>>> = (data).clone();
199+
let _freq: Ptr<Option<Value<Box<[i32]>>>> = (freq).clone();
200+
let _size: i32 = size;
201+
Huffman_2(_data, _freq, _size)
202+
});
203+
let mut root: Ptr<MinHeapNode> = ({ MinHeapImpl::ExtractMin(&(minHeap.as_pointer())) });
207204
let arr: Value<Option<Value<Box<[i32]>>>> = Rc::new(RefCell::new(Some(Rc::new(RefCell::new(
208205
(0..100_usize)
209206
.map(|_| <i32>::default())
@@ -261,16 +258,15 @@ fn main_0() -> i32 {
261258
(*freq.borrow()).as_ref().unwrap().borrow_mut()[(i as usize) as usize] = __rhs;
262259
i.prefix_inc();
263260
}
264-
let out: Value<Option<Value<Box<[i32]>>>> = Rc::new(RefCell::new(
265-
({ HuffmanCodes_5(data.as_pointer(), freq.as_pointer(), size) }),
266-
));
261+
let mut out: Option<Value<Box<[i32]>>> =
262+
({ HuffmanCodes_5(data.as_pointer(), freq.as_pointer(), size) });
267263
assert!(
268-
((((((*out.borrow()).as_ref().unwrap().borrow()[(0_usize) as usize] == 0)
269-
&& ((*out.borrow()).as_ref().unwrap().borrow()[(1_usize) as usize] == 100))
270-
&& ((*out.borrow()).as_ref().unwrap().borrow()[(2_usize) as usize] == 101))
271-
&& ((*out.borrow()).as_ref().unwrap().borrow()[(3_usize) as usize] == 1100))
272-
&& ((*out.borrow()).as_ref().unwrap().borrow()[(4_usize) as usize] == 1101))
273-
&& ((*out.borrow()).as_ref().unwrap().borrow()[(5_usize) as usize] == 111)
264+
(((((out.as_ref().unwrap().borrow()[(0_usize) as usize] == 0)
265+
&& (out.as_ref().unwrap().borrow()[(1_usize) as usize] == 100))
266+
&& (out.as_ref().unwrap().borrow()[(2_usize) as usize] == 101))
267+
&& (out.as_ref().unwrap().borrow()[(3_usize) as usize] == 1100))
268+
&& (out.as_ref().unwrap().borrow()[(4_usize) as usize] == 1101))
269+
&& (out.as_ref().unwrap().borrow()[(5_usize) as usize] == 111)
274270
);
275271
return 0;
276272
}

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

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,13 @@ pub fn matalloc_0(
4646
return (*m.borrow_mut()).take();
4747
}
4848
pub fn matmul_1(
49-
m1: Option<Value<Box<[Option<Value<Box<[i32]>>>]>>>,
49+
mut m1: Option<Value<Box<[Option<Value<Box<[i32]>>>]>>>,
5050
mut n1: i32,
5151
mut p1: i32,
52-
m2: Option<Value<Box<[Option<Value<Box<[i32]>>>]>>>,
52+
mut m2: Option<Value<Box<[Option<Value<Box<[i32]>>>]>>>,
5353
mut n2: i32,
5454
mut p2: i32,
5555
) -> Option<Value<Box<[Option<Value<Box<[i32]>>>]>>> {
56-
let m1: Value<Option<Value<Box<[Option<Value<Box<[i32]>>>]>>>> = Rc::new(RefCell::new(m1));
57-
let m2: Value<Option<Value<Box<[Option<Value<Box<[i32]>>>]>>>> = Rc::new(RefCell::new(m2));
5856
let m3: Value<Option<Value<Box<[Option<Value<Box<[i32]>>>]>>>> =
5957
Rc::new(RefCell::new(({ matalloc_0(n1, p2, 0) })));
6058
let mut i: i32 = 0;
@@ -64,11 +62,11 @@ pub fn matmul_1(
6462
'loop_: while (j < p2) {
6563
let mut k: i32 = 0;
6664
'loop_: while (k < p1) {
67-
sum += ((*m1.borrow()).as_ref().unwrap().borrow()[(i as usize) as usize]
65+
sum += (m1.as_ref().unwrap().borrow()[(i as usize) as usize]
6866
.as_ref()
6967
.unwrap()
7068
.borrow()[(k as usize) as usize]
71-
* (*m2.borrow()).as_ref().unwrap().borrow()[(k as usize) as usize]
69+
* m2.as_ref().unwrap().borrow()[(k as usize) as usize]
7270
.as_ref()
7371
.unwrap()
7472
.borrow()[(j as usize) as usize]);
@@ -95,19 +93,17 @@ fn main_0() -> i32 {
9593
Rc::new(RefCell::new(({ matalloc_0(n, p, 1) })));
9694
let m2: Value<Option<Value<Box<[Option<Value<Box<[i32]>>>]>>>> =
9795
Rc::new(RefCell::new(({ matalloc_0(p, n, 2) })));
98-
let m3: Value<Option<Value<Box<[Option<Value<Box<[i32]>>>]>>>> = Rc::new(RefCell::new(
99-
({
100-
let _m1: Option<Value<Box<[Option<Value<Box<[i32]>>>]>>> = (*m1.borrow_mut()).take();
101-
let _n1: i32 = n;
102-
let _p1: i32 = p;
103-
let _m2: Option<Value<Box<[Option<Value<Box<[i32]>>>]>>> = (*m2.borrow_mut()).take();
104-
let _n2: i32 = p;
105-
let _p2: i32 = n;
106-
matmul_1(_m1, _n1, _p1, _m2, _n2, _p2)
107-
}),
108-
));
96+
let mut m3: Option<Value<Box<[Option<Value<Box<[i32]>>>]>>> = ({
97+
let _m1: Option<Value<Box<[Option<Value<Box<[i32]>>>]>>> = (*m1.borrow_mut()).take();
98+
let _n1: i32 = n;
99+
let _p1: i32 = p;
100+
let _m2: Option<Value<Box<[Option<Value<Box<[i32]>>>]>>> = (*m2.borrow_mut()).take();
101+
let _n2: i32 = p;
102+
let _p2: i32 = n;
103+
matmul_1(_m1, _n1, _p1, _m2, _n2, _p2)
104+
});
109105
assert!(
110-
((*m3.borrow()).as_ref().unwrap().borrow()[(0_usize) as usize]
106+
(m3.as_ref().unwrap().borrow()[(0_usize) as usize]
111107
.as_ref()
112108
.unwrap()
113109
.borrow()[(0_usize) as usize]

0 commit comments

Comments
 (0)