Skip to content

Commit 67b0540

Browse files
committed
Read global variables without clonning the Rc
Part of #377. Needs to fix the writing as well.
1 parent 1ba24cb commit 67b0540

35 files changed

Lines changed: 214 additions & 187 deletions

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -853,9 +853,14 @@ bool ConverterRefCount::VisitDeclRefExpr(clang::DeclRefExpr *expr) {
853853
}
854854

855855
const auto decl_t = decl->getType();
856+
bool is_global_value = false, is_global_ptr = false;
856857
if (IsGlobalVar(expr)) {
857-
auto tp = decl_t->isReferenceType() ? "Ptr" : "Value";
858-
str = std::format("{}.with({}::clone)", str, std::move(tp));
858+
if (decl_t->isReferenceType()) {
859+
str += ".with(Ptr::clone)";
860+
is_global_ptr = true;
861+
} else {
862+
is_global_value = true;
863+
}
859864
}
860865

861866
if (auto *ref = decl_t->getAs<clang::ReferenceType>()) {
@@ -896,14 +901,32 @@ bool ConverterRefCount::VisitDeclRefExpr(clang::DeclRefExpr *expr) {
896901
}
897902

898903
if (isAddrOf()) {
899-
StrCat(str, ".as_pointer()");
904+
if (is_global_value) {
905+
StrCat(std::format("{}.with(|v| v.as_pointer())", std::move(str)));
906+
} else if (is_global_ptr) {
907+
StrCat(str);
908+
} else {
909+
StrCat(str, ".as_pointer()");
910+
}
900911
computed_expr_type_ = ComputedExprType::FreshPointer;
901912
return false;
902913
}
903914

915+
bool fresh = false;
904916
if (isRValue()) {
905-
StrCat(std::format("(*{}.borrow())", std::move(str)));
917+
if (is_global_value) {
918+
StrCat(str, ".with(|rc| rc.borrow().clone())");
919+
fresh = true;
920+
} else if (is_global_ptr) {
921+
StrCat(str);
922+
fresh = true;
923+
} else {
924+
StrCat(std::format("(*{}.borrow())", std::move(str)));
925+
}
906926
} else {
927+
if (is_global_value) {
928+
str += ".with(Value::clone)";
929+
}
907930
StrCat(std::format("(*{}.borrow_mut())", std::move(str)));
908931
}
909932

@@ -913,7 +936,11 @@ bool ConverterRefCount::VisitDeclRefExpr(clang::DeclRefExpr *expr) {
913936
return false;
914937
}
915938
}
916-
SetValueFreshness(expr->getType());
939+
if (fresh) {
940+
SetFreshType(expr->getType());
941+
} else {
942+
SetValueFreshness(expr->getType());
943+
}
917944
return false;
918945
}
919946

‎tests/multi-file/static_name_collision/out/refcount/static_name_collision.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ thread_local!(
1313
pub static same_name_same_type_1: Value<i32> = Rc::new(RefCell::new(5));
1414
);
1515
pub fn a_foo_2() -> i32 {
16-
return (*same_name_different_type_0.with(Value::clone).borrow());
16+
return same_name_different_type_0.with(|rc| rc.borrow().clone());
1717
}
1818
pub fn a_bar_3() -> i32 {
19-
return (*same_name_same_type_1.with(Value::clone).borrow());
19+
return same_name_same_type_1.with(|rc| rc.borrow().clone());
2020
}
2121
pub fn main() {
2222
__cpp2rust_init_globals();
@@ -36,10 +36,10 @@ thread_local!(
3636
pub static same_name_same_type_7: Value<i32> = Rc::new(RefCell::new(6));
3737
);
3838
pub fn b_foo_4() -> f32 {
39-
return (*same_name_different_type_6.with(Value::clone).borrow());
39+
return same_name_different_type_6.with(|rc| rc.borrow().clone());
4040
}
4141
pub fn b_bar_5() -> i32 {
42-
return (*same_name_same_type_7.with(Value::clone).borrow());
42+
return same_name_same_type_7.with(|rc| rc.borrow().clone());
4343
}
4444
pub fn __cpp2rust_init_globals() {
4545
let _ = same_name_different_type_0.with(|_| ());

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ thread_local!(
7575
);
7676
thread_local!(
7777
pub static items_3: Value<Box<[Ptr<Inner>]>> = Rc::new(RefCell::new(Box::new([
78-
(alpha_0.with(Value::clone).as_pointer()),
79-
(beta_1.with(Value::clone).as_pointer()),
78+
(alpha_0.with(|v| v.as_pointer())),
79+
(beta_1.with(|v| v.as_pointer())),
8080
])));
8181
);
8282
thread_local!(
8383
pub static obj_4: Value<Outer> = Rc::new(RefCell::new(Outer {
84-
p: Rc::new(RefCell::new((shared_2.with(Value::clone).as_pointer()))),
84+
p: Rc::new(RefCell::new((shared_2.with(|v| v.as_pointer())))),
8585
}));
8686
);
8787
pub fn main() {
@@ -90,23 +90,23 @@ pub fn main() {
9090
}
9191
fn main_0() -> i32 {
9292
assert!(
93-
((*(*(*items_3.with(Value::clone).borrow())[(0) as usize]
93+
((*(*items_3.with(|rc| rc.borrow().clone())[(0) as usize]
9494
.upgrade()
9595
.deref())
9696
.value
9797
.borrow())
9898
== 1)
9999
);
100100
assert!(
101-
((*(*(*items_3.with(Value::clone).borrow())[(1) as usize]
101+
((*(*items_3.with(|rc| rc.borrow().clone())[(1) as usize]
102102
.upgrade()
103103
.deref())
104104
.value
105105
.borrow())
106106
== 2)
107107
);
108108
assert!(
109-
((*(*(*(*obj_4.with(Value::clone).borrow()).p.borrow())
109+
((*(*(*obj_4.with(|rc| rc.borrow().clone()).p.borrow())
110110
.upgrade()
111111
.deref())
112112
.value
@@ -115,20 +115,20 @@ fn main_0() -> i32 {
115115
);
116116
thread_local!(
117117
static cache_5: Value<Box<[Ptr<Inner>]>> = Rc::new(RefCell::new(Box::new([
118-
(alpha_0.with(Value::clone).as_pointer()),
119-
(beta_1.with(Value::clone).as_pointer()),
118+
(alpha_0.with(|v| v.as_pointer())),
119+
(beta_1.with(|v| v.as_pointer())),
120120
])));
121121
);
122122
assert!(
123-
((*(*(*cache_5.with(Value::clone).borrow())[(0) as usize]
123+
((*(*cache_5.with(|rc| rc.borrow().clone())[(0) as usize]
124124
.upgrade()
125125
.deref())
126126
.value
127127
.borrow())
128128
== 1)
129129
);
130130
assert!(
131-
((*(*(*cache_5.with(Value::clone).borrow())[(1) as usize]
131+
((*(*cache_5.with(|rc| rc.borrow().clone())[(1) as usize]
132132
.upgrade()
133133
.deref())
134134
.value

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ pub fn main() {
6363
std::process::exit(main_0());
6464
}
6565
fn main_0() -> i32 {
66-
assert!(((((*(*s_0.with(Value::clone).borrow()).head.borrow()) == 5) as i32) != 0));
66+
assert!(((((*s_0.with(|rc| rc.borrow().clone()).head.borrow()) == 5) as i32) != 0));
6767
let i: Value<i32> = Rc::new(RefCell::new(0));
6868
'loop_: while ((((*i.borrow()) < 3) as i32) != 0) {
6969
assert!(
70-
((((*(*s_0.with(Value::clone).borrow()).tail.borrow())[(*i.borrow()) as usize] == 0)
70+
((((*s_0.with(|rc| rc.borrow().clone()).tail.borrow())[(*i.borrow()) as usize] == 0)
7171
as i32)
7272
!= 0)
7373
);
@@ -76,7 +76,7 @@ fn main_0() -> i32 {
7676
let i: Value<i32> = Rc::new(RefCell::new(0));
7777
'loop_: while ((((*i.borrow()) < 4) as i32) != 0) {
7878
assert!(
79-
(((((*(*s_0.with(Value::clone).borrow()).buf.borrow())[(*i.borrow()) as usize] as i32)
79+
(((((*s_0.with(|rc| rc.borrow().clone()).buf.borrow())[(*i.borrow()) as usize] as i32)
8080
== 0) as i32)
8181
!= 0)
8282
);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ fn main_0() -> i32 {
5757
if ((*zero.borrow()) != 0) && (({ observe_1(1) }) != 0) {
5858
assert!(false);
5959
}
60-
assert!(((*side_effect_0.with(Value::clone).borrow()) == 0));
60+
assert!((side_effect_0.with(|rc| rc.borrow().clone()) == 0));
6161
if ((*n.borrow()) != 0) || (({ observe_1(1) }) != 0) {
6262
assert!(true);
6363
}
64-
assert!(((*side_effect_0.with(Value::clone).borrow()) == 0));
64+
assert!((side_effect_0.with(|rc| rc.borrow().clone()) == 0));
6565
let x: Value<i32> = Rc::new(RefCell::new(5));
6666
let y: Value<i32> = Rc::new(RefCell::new(3));
6767
let flags: Value<u32> = Rc::new(RefCell::new(2_u32));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ fn main_0() -> i32 {
6161
if (((((*zero.borrow()) != 0) && (({ observe_1(1) }) != 0)) as i32) != 0) {
6262
assert!((0 != 0));
6363
}
64-
assert!(((((*side_effect_0.with(Value::clone).borrow()) == 0) as i32) != 0));
64+
assert!((((side_effect_0.with(|rc| rc.borrow().clone()) == 0) as i32) != 0));
6565
if (((((*n.borrow()) != 0) || (({ observe_1(1) }) != 0)) as i32) != 0) {
6666
assert!((1 != 0));
6767
}
68-
assert!(((((*side_effect_0.with(Value::clone).borrow()) == 0) as i32) != 0));
68+
assert!((((side_effect_0.with(|rc| rc.borrow().clone()) == 0) as i32) != 0));
6969
let x: Value<i32> = Rc::new(RefCell::new(5));
7070
let y: Value<i32> = Rc::new(RefCell::new(3));
7171
let flags: Value<u32> = Rc::new(RefCell::new(2_u32));

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ fn main_0() -> i32 {
5656
let s: Value<S> = Rc::new(RefCell::new(S::S({ 3 })));
5757
let _dtor_s = ScopedDestructor::new(&s, |__p| __p.destructor());
5858
assert!(((*(*s.borrow()).v.borrow()) == 4));
59-
assert!(((*total_0.with(Value::clone).borrow()) == 8));
59+
assert!((total_0.with(|rc| rc.borrow().clone()) == 8));
6060
}
61-
assert!(((*total_0.with(Value::clone).borrow()) == 18));
61+
assert!((total_0.with(|rc| rc.borrow().clone()) == 18));
6262
return 0;
6363
}
6464
pub trait SImpl {

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,26 +198,26 @@ fn main_0() -> i32 {
198198
let c: Value<Partial> = Rc::new(RefCell::new(Partial::Partial({ 3 }, { 300 })));
199199
({ PartialImpl::operator_assign(&a.as_pointer(), b.as_pointer()) });
200200
assert!(((*(*a.borrow()).v.borrow()) == 2) && ((*(*a.borrow()).keep.borrow()) == 100));
201-
assert!(((*assigns_0.with(Value::clone).borrow()) == 1));
201+
assert!((assigns_0.with(|rc| rc.borrow().clone()) == 1));
202202
({
203203
PartialImpl::operator_assign(
204204
&c.as_pointer(),
205205
({ PartialImpl::operator_assign(&a.as_pointer(), b.as_pointer()) }),
206206
)
207207
});
208208
assert!(((*(*c.borrow()).v.borrow()) == 2) && ((*(*c.borrow()).keep.borrow()) == 300));
209-
assert!(((*assigns_0.with(Value::clone).borrow()) == 3));
209+
assert!((assigns_0.with(|rc| rc.borrow().clone()) == 3));
210210
({
211211
let _o: Ptr<Partial> = a.as_pointer();
212212
PartialImpl::operator_assign(&a.as_pointer(), _o)
213213
});
214-
assert!(((*assigns_0.with(Value::clone).borrow()) == 3));
214+
assert!((assigns_0.with(|rc| rc.borrow().clone()) == 3));
215215
({
216216
let _o: Value<Partial> = Rc::new(RefCell::new(Partial::Partial({ 9 }, { 900 })));
217217
PartialImpl::operator_assign(&a.as_pointer(), _o.as_pointer())
218218
});
219219
assert!(((*(*a.borrow()).v.borrow()) == 9) && ((*(*a.borrow()).keep.borrow()) == 100));
220-
assert!(((*assigns_0.with(Value::clone).borrow()) == 4));
220+
assert!((assigns_0.with(|rc| rc.borrow().clone()) == 4));
221221
let ra: Ptr<Partial> = a.as_pointer();
222222
({
223223
let _o: Ptr<Partial> = c.as_pointer();
@@ -230,7 +230,7 @@ fn main_0() -> i32 {
230230
PartialImpl::operator_assign(&(*pa.borrow()), _o)
231231
});
232232
assert!(((*(*a.borrow()).v.borrow()) == 2));
233-
assert!(((*assigns_0.with(Value::clone).borrow()) == 6));
233+
assert!((assigns_0.with(|rc| rc.borrow().clone()) == 6));
234234
let h: Value<Holder> = Rc::new(RefCell::new(Holder {
235235
p: Rc::new(RefCell::new(Partial::Partial({ 4 }, { 40 }))),
236236
arr: Rc::new(RefCell::new(Box::new([
@@ -253,7 +253,7 @@ fn main_0() -> i32 {
253253
((*(*(*h.borrow()).arr.borrow())[(1) as usize].v.borrow()) == 2)
254254
&& ((*(*(*h.borrow()).arr.borrow())[(1) as usize].keep.borrow()) == 60)
255255
);
256-
assert!(((*assigns_0.with(Value::clone).borrow()) == 8));
256+
assert!((assigns_0.with(|rc| rc.borrow().clone()) == 8));
257257
let n: Value<NonConstAssign> = Rc::new(RefCell::new(NonConstAssign::NonConstAssign()));
258258
let n1: Value<NonConstAssign> = Rc::new(RefCell::new(NonConstAssign::NonConstAssign()));
259259
let n2: Value<NonConstAssign> = Rc::new(RefCell::new(NonConstAssign::NonConstAssign()));

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,25 +177,25 @@ fn main_0() -> i32 {
177177
let d: Value<Counted> = Rc::new(RefCell::new(Counted::Counted_pconstCounted({
178178
a.as_pointer()
179179
})));
180-
assert!(((*copies_0.with(Value::clone).borrow()) == 3));
180+
assert!((copies_0.with(|rc| rc.borrow().clone()) == 3));
181181
assert!(
182182
(((*(*b.borrow()).v.borrow()) == 1) && ((*(*c.borrow()).v.borrow()) == 1))
183183
&& ((*(*d.borrow()).v.borrow()) == 1)
184184
);
185185
assert!((({ by_value_1(Counted::Counted_pconstCounted({ a.as_pointer() },),) }) == 1));
186-
assert!(((*copies_0.with(Value::clone).borrow()) == 4));
186+
assert!((copies_0.with(|rc| rc.borrow().clone()) == 4));
187187
let e: Value<Counted> = Rc::new(RefCell::new(({ make_2(5) })));
188188
assert!(((*(*e.borrow()).v.borrow()) == 5));
189-
assert!(((*copies_0.with(Value::clone).borrow()) == 5));
189+
assert!((copies_0.with(|rc| rc.borrow().clone()) == 5));
190190
let f: Value<Counted> = Rc::new(RefCell::new(Counted::Counted({ 6 })));
191191
assert!(((*(*f.borrow()).v.borrow()) == 6));
192-
assert!(((*copies_0.with(Value::clone).borrow()) == 5));
192+
assert!((copies_0.with(|rc| rc.borrow().clone()) == 5));
193193
let g: Value<Counted> = Rc::new(RefCell::new(Counted::Counted({ 7 })));
194194
let h: Value<Counted> = Rc::new(RefCell::new(Counted::Counted_pconstCounted({
195195
g.as_pointer()
196196
})));
197197
assert!(((*(*h.borrow()).v.borrow()) == 7));
198-
assert!(((*copies_0.with(Value::clone).borrow()) == 6));
198+
assert!((copies_0.with(|rc| rc.borrow().clone()) == 6));
199199
let hold: Value<Holder> = Rc::new(RefCell::new(Holder {
200200
c: Rc::new(RefCell::new(Counted::Counted({ 8 }))),
201201
arr: Rc::new(RefCell::new(Box::new([
@@ -209,7 +209,7 @@ fn main_0() -> i32 {
209209
&& ((*(*(*hold2.borrow()).arr.borrow())[(0) as usize].v.borrow()) == 9))
210210
&& ((*(*(*hold2.borrow()).arr.borrow())[(1) as usize].v.borrow()) == 10)
211211
);
212-
assert!(((*copies_0.with(Value::clone).borrow()) == 9));
212+
assert!((copies_0.with(|rc| rc.borrow().clone()) == 9));
213213
let vec_: Value<Vec<Counted>> = Rc::new(RefCell::new(Vec::new()));
214214
{
215215
let a0_clone = (*a.borrow()).clone();
@@ -224,7 +224,7 @@ fn main_0() -> i32 {
224224
.borrow())
225225
== 1)
226226
);
227-
assert!(((*copies_0.with(Value::clone).borrow()) == 10));
227+
assert!((copies_0.with(|rc| rc.borrow().clone()) == 10));
228228
let n: Value<NonConst> = Rc::new(RefCell::new(NonConst::NonConst()));
229229
let n1: Value<NonConst> = Rc::new(RefCell::new(NonConst::NonConst_pmutNonConst({
230230
n.as_pointer()

0 commit comments

Comments
 (0)