Skip to content

Commit 68785f2

Browse files
committed
Fix address of static method
1 parent f71ddd0 commit 68785f2

5 files changed

Lines changed: 22 additions & 12 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1896,9 +1896,18 @@ void Converter::EmitFnPtrCall(clang::Expr *callee) {
18961896
StrCat(".unwrap()");
18971897
}
18981898

1899+
std::string Converter::GetFunctionRefName(const clang::FunctionDecl *fn_decl) {
1900+
if (auto *method = clang::dyn_cast<clang::CXXMethodDecl>(fn_decl);
1901+
method && method->isStatic()) {
1902+
return std::format("{}::{}", GetRecordName(method->getParent()),
1903+
GetMethodName(method));
1904+
}
1905+
return Mapper::MapFunctionName(fn_decl);
1906+
}
1907+
18991908
void Converter::ConvertFunctionToFunctionPointer(
19001909
const clang::FunctionDecl *fn_decl) {
1901-
StrCat(std::format("Some({})", Mapper::MapFunctionName(fn_decl)));
1910+
StrCat(std::format("Some({})", GetFunctionRefName(fn_decl)));
19021911
computed_expr_type_ = ComputedExprType::FreshPointer;
19031912
}
19041913

@@ -3015,8 +3024,7 @@ std::string Converter::ConvertDeclRefExpr(clang::DeclRefExpr *expr) {
30153024
if (auto *function = decl->getAsFunction()) {
30163025
if (auto method = clang::dyn_cast<clang::CXXMethodDecl>(function)) {
30173026
if (method->isStatic()) {
3018-
return std::format("{}::{}", GetRecordName(method->getParent()),
3019-
GetNamedDeclAsString(method));
3027+
return GetFunctionRefName(method);
30203028
}
30213029
}
30223030
return GetNamedDeclAsString(function->getCanonicalDecl());

‎cpp2rust/converter/converter.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,8 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
314314
virtual void
315315
ConvertFunctionToFunctionPointer(const clang::FunctionDecl *fn_decl);
316316

317+
std::string GetFunctionRefName(const clang::FunctionDecl *fn_decl);
318+
317319
std::string ConvertFnPtrCallee(clang::Expr *arg);
318320
virtual std::string ConvertFnPtrPlaceholder(clang::Expr *arg);
319321

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1352,7 +1352,7 @@ void ConverterRefCount::ConvertFunctionToFunctionPointer(
13521352
StrCat(std::format("FnPtr::<{}>::new({})",
13531353
ConvertFunctionPointerType(
13541354
fn_decl->getType()->getAs<clang::FunctionProtoType>()),
1355-
Mapper::MapFunctionName(fn_decl)));
1355+
GetFunctionRefName(fn_decl)));
13561356
computed_expr_type_ = ComputedExprType::FreshPointer;
13571357
}
13581358

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ pub fn main() {
7676
}
7777
fn main_0() -> i32 {
7878
let p1: Value<FnPtr<fn(i32) -> i32>> =
79-
Rc::new(RefCell::new((FnPtr::<fn(i32) -> i32>::new(pick))));
79+
Rc::new(RefCell::new((FnPtr::<fn(i32) -> i32>::new(S::pick_i32))));
8080
let p2: Value<FnPtr<fn(i32) -> i32>> =
81-
Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::new(solo)));
81+
Rc::new(RefCell::new(FnPtr::<fn(i32) -> i32>::new(S::solo)));
8282
assert!((({ (*(*p1.borrow()))(5,) }) == 6));
8383
assert!((({ (*(*p2.borrow()))(5,) }) == 8));
84-
assert!((({ S::pick(5_i64,) }) == 7));
84+
assert!((({ S::pick_i64(5_i64,) }) == 7));
8585
let h3: Value<Handler> = Rc::new(RefCell::new(Handler {
8686
tag: Rc::new(RefCell::new(3)),
87-
cb: Rc::new(RefCell::new((FnPtr::<fn(i32) -> i32>::new(pick)))),
87+
cb: Rc::new(RefCell::new((FnPtr::<fn(i32) -> i32>::new(S::pick_i32)))),
8888
}));
8989
assert!((({ (*(*(*h3.borrow()).cb.borrow()))(1,) }) == 2));
9090
let h1: Value<Handler> = Rc::new(RefCell::new(Handler {

‎tests/unit/out/unsafe/fn_ptr_struct.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ pub fn main() {
4747
}
4848
}
4949
unsafe fn main_0() -> i32 {
50-
let mut p1: Option<unsafe fn(i32) -> i32> = (Some(pick));
51-
let mut p2: Option<unsafe fn(i32) -> i32> = Some(solo);
50+
let mut p1: Option<unsafe fn(i32) -> i32> = (Some(S::pick_i32));
51+
let mut p2: Option<unsafe fn(i32) -> i32> = Some(S::solo);
5252
assert!(((unsafe { (p1).unwrap()(5,) }) == (6)));
5353
assert!(((unsafe { (p2).unwrap()(5,) }) == (8)));
54-
assert!(((unsafe { S::pick(5_i64,) }) == (7)));
54+
assert!(((unsafe { S::pick_i64(5_i64,) }) == (7)));
5555
let mut h3: Handler = Handler {
5656
tag: 3,
57-
cb: (Some(pick)),
57+
cb: (Some(S::pick_i32)),
5858
};
5959
assert!(((unsafe { (h3.cb).unwrap()(1,) }) == (2)));
6060
let mut h1: Handler = Handler {

0 commit comments

Comments
 (0)