Skip to content

Commit 83a7658

Browse files
committed
Don't pass addr to FnPtr::new
1 parent b9c7409 commit 83a7658

14 files changed

Lines changed: 75 additions & 58 deletions

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,10 +1029,10 @@ void ConverterRefCount::EmitFnPtrCall(clang::Expr *callee) {
10291029

10301030
void ConverterRefCount::ConvertFunctionToFunctionPointer(
10311031
const clang::FunctionDecl *fn_decl) {
1032-
StrCat(
1033-
std::format("fn_ptr!({}, {})", GetNamedDeclAsString(fn_decl),
1034-
ConvertFunctionPointerType(
1035-
fn_decl->getType()->getAs<clang::FunctionProtoType>())));
1032+
StrCat(std::format("FnPtr::<{}>::new({})",
1033+
ConvertFunctionPointerType(
1034+
fn_decl->getType()->getAs<clang::FunctionProtoType>()),
1035+
GetNamedDeclAsString(fn_decl)));
10361036
}
10371037

10381038
void ConverterRefCount::ConvertEqualsNullPtr(clang::Expr *expr) {
@@ -1614,9 +1614,9 @@ void ConverterRefCount::ConvertVarInit(clang::QualType qual_type,
16141614
Buffer buf(*this);
16151615
PushConversionKind push(*this, ConversionKind::Unboxed);
16161616
if (qual_type->isFunctionPointerType() && lambda->capture_size() == 0) {
1617-
StrCat("FnPtr::new((");
1617+
StrCat("FnPtr::new(");
16181618
VisitLambdaExpr(lambda);
1619-
StrCat("), 0)");
1619+
StrCat(")");
16201620
} else {
16211621
VisitLambdaExpr(lambda);
16221622
}

libcc2rs/src/fn_ptr.rs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
// Copyright (c) 2022-present INESC-ID.
22
// Distributed under the MIT license that can be found in the LICENSE file.
33

4-
#[macro_export]
5-
macro_rules! fn_ptr {
6-
($f:expr, $ty:ty) => {
7-
$crate::FnPtr::new($f as $ty, $f as *const () as usize)
8-
};
9-
}
10-
114
use std::any::{Any, TypeId};
125
use std::marker::PhantomData;
136
use std::ops::Deref;
@@ -16,6 +9,28 @@ use std::rc::Rc;
169
use crate::rc::{AnyPtr, ErasedPtr};
1710
use crate::reinterpret::ByteRepr;
1811

12+
pub trait FnAddr {
13+
fn fn_addr(&self) -> usize;
14+
}
15+
16+
macro_rules! impl_fn_addr {
17+
() => {
18+
impl_fn_addr!(@gen A B C D E F G H I J);
19+
};
20+
(@gen $($a:ident)*) => {
21+
impl<R $(, $a)*> FnAddr for fn($($a,)*) -> R {
22+
#[inline]
23+
fn fn_addr(&self) -> usize { *self as *const () as usize }
24+
}
25+
impl_fn_addr!(@peel $($a)*);
26+
};
27+
(@peel) => {};
28+
(@peel $head:ident $($tail:ident)*) => {
29+
impl_fn_addr!(@gen $($tail)*);
30+
};
31+
}
32+
impl_fn_addr!();
33+
1934
#[derive(Clone)]
2035
pub(crate) struct FnState {
2136
addr: usize,
@@ -43,17 +58,19 @@ impl<T> FnPtr<T> {
4358
}
4459
}
4560

46-
impl<T: 'static> FnPtr<T> {
47-
pub fn new(f: T, addr: usize) -> Self {
61+
impl<T: FnAddr + 'static> FnPtr<T> {
62+
pub fn new(f: T) -> Self {
4863
FnPtr {
4964
state: Some(Rc::new(FnState {
50-
addr,
65+
addr: f.fn_addr(),
5166
cast_history: vec![Some(Rc::new(f))],
5267
})),
5368
_marker: PhantomData,
5469
}
5570
}
71+
}
5672

73+
impl<T: 'static> FnPtr<T> {
5774
pub fn cast<U: 'static>(&self, adapter: Option<U>) -> FnPtr<U> {
5875
let state = self.state.as_ref().expect("ub: null fn pointer cast");
5976

tests/unit/out/refcount/fn_ptr.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ fn main_0() -> i32 {
2727
assert!((*fn_.borrow()).is_null());
2828
assert!({
2929
let _lhs = (*fn_.borrow()).clone();
30-
_lhs != fn_ptr!(my_foo_0, fn(AnyPtr) -> i32)
30+
_lhs != FnPtr::<fn(AnyPtr) -> i32>::new(my_foo_0)
3131
});
32-
(*fn_.borrow_mut()) = fn_ptr!(my_foo_0, fn(AnyPtr) -> i32);
32+
(*fn_.borrow_mut()) = FnPtr::<fn(AnyPtr) -> i32>::new(my_foo_0);
3333
assert!(!((*fn_.borrow()).is_null()));
3434
assert!({
3535
let _lhs = (*fn_.borrow()).clone();
36-
_lhs == fn_ptr!(my_foo_0, fn(AnyPtr) -> i32)
36+
_lhs == FnPtr::<fn(AnyPtr) -> i32>::new(my_foo_0)
3737
});
3838
let a: Value<i32> = Rc::new(RefCell::new(10));
3939
assert!({

tests/unit/out/refcount/fn_ptr_array.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ pub fn main() {
2727
}
2828
fn main_0() -> i32 {
2929
let ops: Value<Box<[FnPtr<fn(i32, i32) -> i32>]>> = Rc::new(RefCell::new(Box::new([
30-
fn_ptr!(add_0, fn(i32, i32) -> i32),
31-
fn_ptr!(sub_1, fn(i32, i32) -> i32),
32-
fn_ptr!(mul_2, fn(i32, i32) -> i32),
30+
FnPtr::<fn(i32, i32) -> i32>::new(add_0),
31+
FnPtr::<fn(i32, i32) -> i32>::new(sub_1),
32+
FnPtr::<fn(i32, i32) -> i32>::new(mul_2),
3333
])));
3434
assert!(
3535
(({
@@ -53,7 +53,7 @@ fn main_0() -> i32 {
5353
}) == 30)
5454
);
5555
assert!(!(((*ops.borrow())[(0) as usize]).is_null()));
56-
assert!(((*ops.borrow())[(0) as usize] == fn_ptr!(add_0, fn(i32, i32) -> i32)));
57-
assert!(((*ops.borrow())[(0) as usize] != fn_ptr!(sub_1, fn(i32, i32) -> i32)));
56+
assert!(((*ops.borrow())[(0) as usize] == FnPtr::<fn(i32, i32) -> i32>::new(add_0)));
57+
assert!(((*ops.borrow())[(0) as usize] != FnPtr::<fn(i32, i32) -> i32>::new(sub_1)));
5858
return 0;
5959
}

tests/unit/out/refcount/fn_ptr_as_condition.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub fn main() {
3131
fn main_0() -> i32 {
3232
let a: Value<i32> = Rc::new(RefCell::new(5));
3333
({
34-
let _cb: FnPtr<fn(Ptr<i32>)> = fn_ptr!(double_it_0, fn(Ptr::<i32>));
34+
let _cb: FnPtr<fn(Ptr<i32>)> = FnPtr::<fn(Ptr<i32>)>::new(double_it_0);
3535
let _x: Ptr<i32> = (a.as_pointer());
3636
maybe_call_1(_cb, _x)
3737
});
@@ -45,7 +45,7 @@ fn main_0() -> i32 {
4545
assert!(((*b.borrow()) == 5));
4646
let fn_: Value<FnPtr<fn(Ptr<i32>)>> = Rc::new(RefCell::new(FnPtr::null()));
4747
if !!(*fn_.borrow()).is_null() {
48-
(*fn_.borrow_mut()) = (fn_ptr!(double_it_0, fn(Ptr::<i32>))).clone();
48+
(*fn_.borrow_mut()) = (FnPtr::<fn(Ptr<i32>)>::new(double_it_0)).clone();
4949
}
5050
let c: Value<i32> = Rc::new(RefCell::new(3));
5151
if !(*fn_.borrow()).is_null() {

tests/unit/out/refcount/fn_ptr_cast.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn double_it_0(x: i32) -> i32 {
1313
}
1414
pub fn test_roundtrip_1() {
1515
let fn_: Value<FnPtr<fn(i32) -> i32>> =
16-
Rc::new(RefCell::new(fn_ptr!(double_it_0, fn(i32) -> i32)));
16+
Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::new(double_it_0)));
1717
assert!(
1818
(({
1919
let _arg0: i32 = 5;
@@ -39,7 +39,7 @@ pub fn test_roundtrip_1() {
3939
}
4040
pub fn test_double_cast_2() {
4141
let fn_: Value<FnPtr<fn(i32) -> i32>> =
42-
Rc::new(RefCell::new(fn_ptr!(double_it_0, fn(i32) -> i32)));
42+
Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::new(double_it_0)));
4343
let fn2: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(
4444
((*fn_.borrow())
4545
.cast::<fn()>(None)
@@ -72,7 +72,7 @@ impl Clone for Command {
7272
impl ByteRepr for Command {}
7373
pub fn test_void_ptr_to_fn_3() {
7474
let cmd: Value<Command> = Rc::new(RefCell::new(<Command>::default()));
75-
(*(*cmd.borrow()).data.borrow_mut()) = fn_ptr!(double_it_0, fn(i32) -> i32).to_any();
75+
(*(*cmd.borrow()).data.borrow_mut()) = FnPtr::<fn(i32) -> i32>::new(double_it_0).to_any();
7676
let fn_: Value<FnPtr<fn(i32) -> i32>> = Rc::new(RefCell::new(
7777
((*(*cmd.borrow()).data.borrow())
7878
.cast_fn::<fn(i32) -> i32>()
@@ -96,7 +96,7 @@ pub fn add_offset_4(base: Ptr<i32>, offset: i32) -> i32 {
9696
}
9797
pub fn test_call_through_cast_5() {
9898
let gfn: Value<FnPtr<fn(AnyPtr, i32) -> i32>> = Rc::new(RefCell::new(
99-
fn_ptr!(add_offset_4, fn(Ptr::<i32>, i32) -> i32).cast::<fn(AnyPtr, i32) -> i32>(Some(
99+
FnPtr::<fn(Ptr<i32>, i32) -> i32>::new(add_offset_4).cast::<fn(AnyPtr, i32) -> i32>(Some(
100100
(|a0: AnyPtr, a1: i32| -> i32 { add_offset_4(a0.cast::<i32>().unwrap(), a1) })
101101
as fn(AnyPtr, i32) -> i32,
102102
)),

tests/unit/out/refcount/fn_ptr_conditional.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ pub fn identity_2(x: i32) -> i32 {
2222
pub fn pick_3(mode: i32) -> FnPtr<fn(i32) -> i32> {
2323
let mode: Value<i32> = Rc::new(RefCell::new(mode));
2424
return if ((*mode.borrow()) > 0) {
25-
fn_ptr!(inc_0, fn(i32) -> i32)
25+
FnPtr::<fn(i32) -> i32>::new(inc_0)
2626
} else {
2727
if ((*mode.borrow()) < 0) {
28-
fn_ptr!(dec_1, fn(i32) -> i32)
28+
FnPtr::<fn(i32) -> i32>::new(dec_1)
2929
} else {
30-
fn_ptr!(identity_2, fn(i32) -> i32)
30+
FnPtr::<fn(i32) -> i32>::new(identity_2)
3131
}
3232
};
3333
}
@@ -38,7 +38,7 @@ pub fn apply_4(fn_: FnPtr<fn(i32) -> i32>, x: i32) -> i32 {
3838
Rc::new(RefCell::new(if !(*fn_.borrow()).is_null() {
3939
(*fn_.borrow()).clone()
4040
} else {
41-
fn_ptr!(identity_2, fn(i32) -> i32)
41+
FnPtr::<fn(i32) -> i32>::new(identity_2)
4242
}));
4343
return ({
4444
let _arg0: i32 = (*x.borrow());
@@ -78,7 +78,7 @@ fn main_0() -> i32 {
7878
);
7979
assert!(
8080
(({
81-
let _fn: FnPtr<fn(i32) -> i32> = fn_ptr!(inc_0, fn(i32) -> i32);
81+
let _fn: FnPtr<fn(i32) -> i32> = FnPtr::<fn(i32) -> i32>::new(inc_0);
8282
let _x: i32 = 5;
8383
apply_4(_fn, _x)
8484
}) == 6)

tests/unit/out/refcount/fn_ptr_default_arg.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn main_0() -> i32 {
4343
assert!(
4444
(({
4545
let _x: i32 = 5;
46-
let _fn: FnPtr<fn(i32) -> i32> = fn_ptr!(identity_0, fn(i32) -> i32);
46+
let _fn: FnPtr<fn(i32) -> i32> = FnPtr::<fn(i32) -> i32>::new(identity_0);
4747
apply_1(_x, Some(_fn))
4848
}) == 5)
4949
);
@@ -52,7 +52,6 @@ fn main_0() -> i32 {
5252
let x: Value<i32> = Rc::new(RefCell::new(x));
5353
return -(*x.borrow());
5454
}),
55-
0,
5655
)));
5756
assert!(
5857
(({

tests/unit/out/refcount/fn_ptr_global.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ fn main_0() -> i32 {
4343
}) == 5)
4444
);
4545
({
46-
let _fn: FnPtr<fn(i32) -> i32> = fn_ptr!(double_it_0, fn(i32) -> i32);
46+
let _fn: FnPtr<fn(i32) -> i32> = FnPtr::<fn(i32) -> i32>::new(double_it_0);
4747
set_op_2(_fn)
4848
});
4949
assert!(!((*g_op.with(Value::clone).borrow()).is_null()));
5050
assert!({
5151
let _lhs = (*g_op.with(Value::clone).borrow()).clone();
52-
_lhs == fn_ptr!(double_it_0, fn(i32) -> i32)
52+
_lhs == FnPtr::<fn(i32) -> i32>::new(double_it_0)
5353
});
5454
assert!(
5555
(({
@@ -58,12 +58,12 @@ fn main_0() -> i32 {
5858
}) == 10)
5959
);
6060
({
61-
let _fn: FnPtr<fn(i32) -> i32> = fn_ptr!(triple_it_1, fn(i32) -> i32);
61+
let _fn: FnPtr<fn(i32) -> i32> = FnPtr::<fn(i32) -> i32>::new(triple_it_1);
6262
set_op_2(_fn)
6363
});
6464
assert!({
6565
let _lhs = (*g_op.with(Value::clone).borrow()).clone();
66-
_lhs == fn_ptr!(triple_it_1, fn(i32) -> i32)
66+
_lhs == FnPtr::<fn(i32) -> i32>::new(triple_it_1)
6767
});
6868
assert!(
6969
(({

tests/unit/out/refcount/fn_ptr_reassign.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,23 @@ pub fn main() {
2727
}
2828
fn main_0() -> i32 {
2929
let fn_: Value<FnPtr<fn(i32, i32) -> i32>> =
30-
Rc::new(RefCell::new(fn_ptr!(add_0, fn(i32, i32) -> i32)));
30+
Rc::new(RefCell::new(FnPtr::<fn(i32, i32) -> i32>::new(add_0)));
3131
assert!(
3232
(({
3333
let _arg0: i32 = 3;
3434
let _arg1: i32 = 4;
3535
(*(*fn_.borrow()))(_arg0, _arg1)
3636
}) == 7)
3737
);
38-
(*fn_.borrow_mut()) = fn_ptr!(sub_1, fn(i32, i32) -> i32);
38+
(*fn_.borrow_mut()) = FnPtr::<fn(i32, i32) -> i32>::new(sub_1);
3939
assert!(
4040
(({
4141
let _arg0: i32 = 10;
4242
let _arg1: i32 = 3;
4343
(*(*fn_.borrow()))(_arg0, _arg1)
4444
}) == 7)
4545
);
46-
(*fn_.borrow_mut()) = fn_ptr!(mul_2, fn(i32, i32) -> i32);
46+
(*fn_.borrow_mut()) = FnPtr::<fn(i32, i32) -> i32>::new(mul_2);
4747
assert!(
4848
(({
4949
let _arg0: i32 = 6;
@@ -53,7 +53,7 @@ fn main_0() -> i32 {
5353
);
5454
(*fn_.borrow_mut()) = FnPtr::null();
5555
assert!((*fn_.borrow()).is_null());
56-
(*fn_.borrow_mut()) = fn_ptr!(add_0, fn(i32, i32) -> i32);
56+
(*fn_.borrow_mut()) = FnPtr::<fn(i32, i32) -> i32>::new(add_0);
5757
assert!(!((*fn_.borrow()).is_null()));
5858
assert!(
5959
(({

0 commit comments

Comments
 (0)