Skip to content

Commit fa5a463

Browse files
committed
Translate lambda TPOs
1 parent e2daca0 commit fa5a463

3 files changed

Lines changed: 180 additions & 0 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3232,6 +3232,13 @@ std::string Converter::ConvertDeclRef(clang::Expr *expr,
32323232
}
32333233

32343234
bool Converter::VisitDeclRefExpr(clang::DeclRefExpr *expr) {
3235+
if (auto param =
3236+
clang::dyn_cast<clang::TemplateParamObjectDecl>(expr->getDecl());
3237+
param && AsLambdaClass(param->getType())) {
3238+
StrCat(GetDefaultAsString(param->getType()));
3239+
computed_expr_type_ = ComputedExprType::FreshValue;
3240+
return false;
3241+
}
32353242
if (auto init = AsLambdaUncapturedConstant(curr_function_, expr)) {
32363243
Convert(init);
32373244
return false;
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
extern crate libcc2rs;
2+
use libcc2rs::*;
3+
use std::cell::RefCell;
4+
use std::collections::BTreeMap;
5+
use std::io::prelude::*;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::AsFd;
8+
use std::rc::{Rc, Weak};
9+
pub fn apply_0(mut x: i32, mut fn_: FnPtr<fn(i32) -> i32>) -> i32 {
10+
return ({ fn_.call(x) });
11+
}
12+
pub fn call_nttp_1(mut x: i32) -> i32 {
13+
return ({ (FnPtr::<fn(i32) -> i32>::new(int_operator___int__const::__invoke))(x) });
14+
}
15+
pub fn call_nttp_1(mut x: i32) -> i32 {
16+
return ({ (FnPtr::<fn(i32) -> i32>::new(int_operator___int__const::__invoke))(x) });
17+
}
18+
pub fn call_nttp_2(mut x: i32) -> i32 {
19+
return ({
20+
FnPtr::<fn(i32) -> i32>::new(|mut x: i32| -> i32 {
21+
{
22+
return (x + 100);
23+
}
24+
})
25+
.call(x)
26+
});
27+
}
28+
pub fn main() {
29+
__cpp2rust_init_globals();
30+
std::process::exit(main_0());
31+
}
32+
fn main_0() -> i32 {
33+
let mut fresh: FnPtr<fn(i32) -> i32> = FnPtr::<fn(i32) -> i32>::new(|mut x: i32| -> i32 {
34+
{
35+
return -x;
36+
}
37+
});
38+
assert!((({ fresh.call(5,) }) == -5_i32));
39+
let twice: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::new(
40+
|mut x: i32| -> i32 {
41+
{
42+
return (x * 2);
43+
}
44+
},
45+
)));
46+
let mut named: FnPtr<fn(i32) -> i32> = (*twice.borrow()).clone();
47+
assert!((({ named.call(5,) }) == 10));
48+
assert!((({ apply_0(5, (*twice.borrow()).clone(),) }) == 10));
49+
named = (fresh).clone();
50+
assert!((({ named.call(3,) }) == -3_i32));
51+
let mut p: FnPtr<fn(i32) -> i32> = FnPtr::<fn(i32) -> i32>::new(|mut x: i32| -> i32 {
52+
{
53+
return (x * 3);
54+
}
55+
});
56+
assert!((({ p.call(2,) }) == 6));
57+
assert!((({ call_nttp_1(5,) }) == 4));
58+
assert!((({ call_nttp_1(5,) }) == 50));
59+
assert!((({ call_nttp_2(5,) }) == 105));
60+
let sum: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::new(
61+
|n: i32| -> i32 {
62+
{
63+
let __args: Value<Box<[VaArg]>> = Rc::new(RefCell::new(__args.into()));
64+
let n: Value<i32> = Rc::new(RefCell::new(n));
65+
let ap: Value<Ptr<VaArg>> = Rc::new(RefCell::new(Ptr::<VaArg>::default()));
66+
(*ap.borrow_mut()) = __args.as_pointer();
67+
let s: Value<i32> = Rc::new(RefCell::new(0));
68+
let mut i: i32 = 0;
69+
'loop_: while (i < (*n.borrow())) {
70+
(*s.borrow_mut()) += (*ap.borrow_mut()).arg::<i32>();
71+
i.postfix_inc();
72+
}
73+
return (*s.borrow());
74+
}
75+
},
76+
)));
77+
assert!((({ (*sum.borrow()).call(2, &[(5).into(), (5).into(),]) }) == 10));
78+
assert!((({ (*sum.borrow()).call(3, &[(1).into(), (2).into(), (3).into(),]) }) == 6));
79+
return 0;
80+
}
81+
pub fn __cpp2rust_init_globals() {}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
extern crate libc;
2+
use libc::*;
3+
extern crate libcc2rs;
4+
use libcc2rs::*;
5+
use std::collections::BTreeMap;
6+
use std::io::{Read, Seek, Write};
7+
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
8+
use std::rc::Rc;
9+
pub unsafe fn apply_0(mut x: i32, mut fn_: Option<unsafe fn(i32) -> i32>) -> i32 {
10+
return (unsafe { (fn_).unwrap()(x) });
11+
}
12+
pub unsafe fn call_nttp_1(mut x: i32) -> i32 {
13+
return (unsafe { (Some(int_operator___int__const::__invoke))(x) });
14+
}
15+
pub unsafe fn call_nttp_1(mut x: i32) -> i32 {
16+
return (unsafe { (Some(int_operator___int__const::__invoke))(x) });
17+
}
18+
pub unsafe fn call_nttp_2(mut x: i32) -> i32 {
19+
return (unsafe {
20+
FnPtr::<fn(i32) -> i32>::new(|mut x: i32| -> i32 {
21+
unsafe {
22+
return ((x) + (100));
23+
}
24+
})
25+
.call(x)
26+
});
27+
}
28+
pub fn main() {
29+
unsafe {
30+
__cpp2rust_init_globals();
31+
std::process::exit(main_0() as i32);
32+
}
33+
}
34+
unsafe fn main_0() -> i32 {
35+
let mut fresh: Option<unsafe fn(i32) -> i32> = Some(|mut x: i32| -> i32 {
36+
unsafe {
37+
return -x;
38+
}
39+
});
40+
assert!(((unsafe { (fresh).unwrap()(5,) }) == (-5_i32)));
41+
let mut twice: FnPtr<fn(i32) -> i32> = FnPtr::<fn(i32) -> i32>::new(|mut x: i32| -> i32 {
42+
unsafe {
43+
return ((x) * (2));
44+
}
45+
});
46+
let mut named: Option<unsafe fn(i32) -> i32> = Some(|mut x: i32| -> i32 {
47+
unsafe {
48+
return ((x) * (2));
49+
}
50+
});
51+
assert!(((unsafe { (named).unwrap()(5,) }) == (10)));
52+
assert!(
53+
((unsafe {
54+
apply_0(
55+
5,
56+
Some(|mut x: i32| -> i32 {
57+
unsafe {
58+
return ((x) * (2));
59+
}
60+
}),
61+
)
62+
}) == (10))
63+
);
64+
named = fresh;
65+
assert!(((unsafe { (named).unwrap()(3,) }) == (-3_i32)));
66+
let mut p: Option<unsafe fn(i32) -> i32> = Some(|mut x: i32| -> i32 {
67+
unsafe {
68+
return ((x) * (3));
69+
}
70+
});
71+
assert!(((unsafe { (p).unwrap()(2,) }) == (6)));
72+
assert!(((unsafe { call_nttp_1(5,) }) == (4)));
73+
assert!(((unsafe { call_nttp_1(5,) }) == (50)));
74+
assert!(((unsafe { call_nttp_2(5,) }) == (105)));
75+
let mut sum: FnPtr<fn(i32) -> i32> = FnPtr::<fn(i32) -> i32>::new(|mut n: i32| -> i32 {
76+
unsafe {
77+
let mut ap: *const VaArg = ::std::ptr::null();
78+
ap = __args.as_ptr();
79+
let mut s: i32 = 0;
80+
let mut i: i32 = 0;
81+
'loop_: while ((i) < (n)) {
82+
s += ap.arg::<i32>();
83+
i.postfix_inc();
84+
}
85+
return s;
86+
}
87+
});
88+
assert!(((unsafe { sum.call(2, &[(5).into(), (5).into(),]) }) == (10)));
89+
assert!(((unsafe { sum.call(3, &[(1).into(), (2).into(), (3).into(),]) }) == (6)));
90+
return 0;
91+
}
92+
pub unsafe fn __cpp2rust_init_globals() {}

0 commit comments

Comments
 (0)