Skip to content

Commit e7572a3

Browse files
committed
Use rule function name in AddrOf context
1 parent acdbd96 commit e7572a3

4 files changed

Lines changed: 44 additions & 13 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1381,6 +1381,25 @@ void Converter::EmitFnPtrCall(clang::Expr *callee) {
13811381
StrCat(").unwrap()");
13821382
}
13831383

1384+
void Converter::EmitFnAsValue(const clang::FunctionDecl *fn_decl) {
1385+
StrCat(std::format("Some({} as _)", Mapper::GetFnRefName(fn_decl)));
1386+
}
1387+
1388+
std::string Converter::GetPointeeRustType(clang::QualType ptr_type) {
1389+
auto pointee = ptr_type->getPointeeType();
1390+
if (pointee->isIntegerType()) {
1391+
return ToString(pointee);
1392+
}
1393+
auto str = ToString(ptr_type);
1394+
while (!str.empty() && std::isspace(str.back())) {
1395+
str.pop_back();
1396+
}
1397+
if (str.starts_with("Ptr<") && str.ends_with(">")) {
1398+
return str.substr(4, str.size() - 5);
1399+
}
1400+
return ToString(pointee);
1401+
}
1402+
13841403
void Converter::ConvertGenericCallExpr(clang::CallExpr *expr) {
13851404
clang::Expr *callee = expr->getCallee();
13861405
auto convert_param_ty = [&](clang::QualType param_type, clang::Expr *expr) {
@@ -2000,7 +2019,8 @@ std::string Converter::ConvertDeclRefExpr(clang::DeclRefExpr *expr) {
20002019
}
20012020

20022021
auto *decl = expr->getDecl();
2003-
if (Mapper::Contains(expr)) {
2022+
if (!(clang::isa<clang::FunctionDecl>(decl) && isAddrOf()) &&
2023+
Mapper::Contains(expr)) {
20042024
return GetMappedAsString(expr);
20052025
} else if (auto *function = decl->getAsFunction()) {
20062026
if (auto method = clang::dyn_cast<clang::CXXMethodDecl>(function)) {
@@ -2037,9 +2057,9 @@ bool Converter::VisitDeclRefExpr(clang::DeclRefExpr *expr) {
20372057
return false;
20382058
}
20392059

2040-
if (clang::isa<clang::FunctionDecl>(decl)) {
2060+
if (auto *fn_decl = clang::dyn_cast<clang::FunctionDecl>(decl)) {
20412061
if (isAddrOf()) {
2042-
StrCat(std::format("Some({} as _)", str));
2062+
EmitFnAsValue(fn_decl);
20432063
return false;
20442064
}
20452065
}

cpp2rust/converter/converter.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
201201

202202
virtual void EmitFnPtrCall(clang::Expr *callee);
203203

204+
virtual void EmitFnAsValue(const clang::FunctionDecl *fn_decl);
205+
206+
std::string GetPointeeRustType(clang::QualType ptr_type);
207+
204208
virtual void ConvertPrintf(clang::CallExpr *expr);
205209

206210
void ConvertVAArgCall(clang::CallExpr *expr);

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,15 @@ std::string ConverterRefCount::BuildFnAdapter(
192192
closure += "{ ";
193193

194194
// Build adapter body: src_fn(convert(a0), convert(a1), ...)
195-
closure += GetNamedDeclAsString(src_fn->getCanonicalDecl()) + "(";
195+
closure += Mapper::GetFnRefName(src_fn) + "(";
196196
for (unsigned i = 0; i < src_proto->getNumParams(); ++i) {
197197
auto src_pty = src_proto->getParamType(i);
198198
auto tgt_pty = target_proto->getParamType(i);
199199
if (ToString(src_pty) == ToString(tgt_pty)) {
200200
closure += std::format("a{}", i);
201201
} else if (src_pty->isPointerType() && tgt_pty->isVoidPointerType()) {
202202
closure += std::format("a{}.cast::<{}>().unwrap()", i,
203-
ToString(src_pty->getPointeeType()));
203+
GetPointeeRustType(src_pty));
204204
} else if (src_pty->isVoidPointerType() && tgt_pty->isPointerType()) {
205205
closure += std::format("a{}.to_any()", i);
206206
} else {
@@ -622,19 +622,18 @@ bool ConverterRefCount::VisitDeclRefExpr(clang::DeclRefExpr *expr) {
622622
}
623623
}
624624

625-
if (Mapper::Contains(expr)) {
625+
auto str = ConvertDeclRefExpr(expr);
626+
auto decl = expr->getDecl();
627+
628+
if (!(clang::isa<clang::FunctionDecl>(decl) && isAddrOf()) &&
629+
Mapper::Contains(expr)) {
626630
StrCat(GetMappedAsString(expr));
627631
return false;
628632
}
629633

630-
auto str = ConvertDeclRefExpr(expr);
631-
auto decl = expr->getDecl();
632-
633-
if (clang::isa<clang::FunctionDecl>(decl)) {
634+
if (auto *fn_decl = clang::dyn_cast<clang::FunctionDecl>(decl)) {
634635
if (isAddrOf()) {
635-
auto proto = decl->getType()->getAs<clang::FunctionProtoType>();
636-
auto fn_type = GetFnTypeString(proto);
637-
StrCat(std::format("fn_ptr!({}, {})", str, fn_type));
636+
EmitFnAsValue(fn_decl);
638637
} else {
639638
StrCat(str);
640639
}
@@ -1030,6 +1029,12 @@ void ConverterRefCount::EmitFnPtrCall(clang::Expr *callee) {
10301029
StrCat(".call()");
10311030
}
10321031

1032+
void ConverterRefCount::EmitFnAsValue(const clang::FunctionDecl *fn_decl) {
1033+
StrCat(std::format(
1034+
"fn_ptr!({}, {})", Mapper::GetFnRefName(fn_decl),
1035+
GetFnTypeString(fn_decl->getType()->getAs<clang::FunctionProtoType>())));
1036+
}
1037+
10331038
std::string ConverterRefCount::GetFunctionPointerDefaultAsString(
10341039
clang::QualType qual_type) {
10351040
return "FnPtr::null()";

cpp2rust/converter/models/converter_refcount.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class ConverterRefCount final : public Converter {
6161

6262
void EmitFnPtrCall(clang::Expr *callee) override;
6363

64+
void EmitFnAsValue(const clang::FunctionDecl *fn_decl) override;
65+
6466
bool VisitCallExpr(clang::CallExpr *expr) override;
6567

6668
bool VisitStringLiteral(clang::StringLiteral *expr) override;

0 commit comments

Comments
 (0)