Skip to content

Commit 1d97acd

Browse files
committed
Refactor into a single GetComparisonCall
1 parent 9bc912a commit 1d97acd

4 files changed

Lines changed: 54 additions & 28 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4492,18 +4492,34 @@ std::string Converter::GetComparisonCall(const clang::FunctionDecl *op,
44924492
const clang::CXXRecordDecl *decl,
44934493
std::string_view lhs,
44944494
std::string_view rhs) {
4495-
auto record = GetRecordName(decl);
4496-
auto arg = std::format("{} as *const {}", rhs, record);
4495+
auto arg = [&](unsigned i, std::string_view value) {
4496+
if (op->getParamDecl(i)->getType()->isReferenceType()) {
4497+
return GetComparisonReferenceArg(decl, value);
4498+
}
4499+
return std::format("{}.clone()", value);
4500+
};
44974501
if (const auto *method = clang::dyn_cast<clang::CXXMethodDecl>(op)) {
4498-
auto recv = !MethodNeedsMutableReceiver(method)
4499-
? std::string(lhs)
4500-
: std::format("&mut *(&raw const *{}).cast_mut()", lhs);
45014502
return std::format("{}::{}({}, {})", GetUFCSName(method),
4502-
GetMethodName(method), recv, arg);
4503+
GetMethodName(method),
4504+
GetComparisonReceiver(method, decl, lhs), arg(0, rhs));
4505+
}
4506+
return std::format("{}({}, {})", GetNamedDeclAsString(op->getCanonicalDecl()),
4507+
arg(0, lhs), arg(1, rhs));
4508+
}
4509+
4510+
std::string
4511+
Converter::GetComparisonReferenceArg(const clang::CXXRecordDecl *decl,
4512+
std::string_view value) {
4513+
return std::format("{} as *const {}", value, GetRecordName(decl));
4514+
}
4515+
4516+
std::string Converter::GetComparisonReceiver(const clang::CXXMethodDecl *method,
4517+
const clang::CXXRecordDecl *,
4518+
std::string_view lhs) {
4519+
if (!MethodNeedsMutableReceiver(method)) {
4520+
return std::string(lhs);
45034521
}
4504-
return std::format("{}({} as *const {}, {})",
4505-
GetNamedDeclAsString(op->getCanonicalDecl()), lhs, record,
4506-
arg);
4522+
return std::format("&mut *(&raw const *{}).cast_mut()", lhs);
45074523
}
45084524

45094525
void Converter::ConvertOrdAndPartialOrdTraits(const clang::CXXRecordDecl *decl,

‎cpp2rust/converter/converter.h‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -612,10 +612,17 @@ class Converter : public clang::RecursiveASTVisitor<Converter> {
612612
std::string_view eq_body,
613613
std::string_view record_name);
614614

615-
virtual std::string GetComparisonCall(const clang::FunctionDecl *op,
616-
const clang::CXXRecordDecl *decl,
617-
std::string_view lhs,
618-
std::string_view rhs);
615+
std::string GetComparisonCall(const clang::FunctionDecl *op,
616+
const clang::CXXRecordDecl *decl,
617+
std::string_view lhs, std::string_view rhs);
618+
619+
virtual std::string
620+
GetComparisonReferenceArg(const clang::CXXRecordDecl *decl,
621+
std::string_view value);
622+
623+
virtual std::string GetComparisonReceiver(const clang::CXXMethodDecl *method,
624+
const clang::CXXRecordDecl *decl,
625+
std::string_view lhs);
619626

620627
virtual void AddCloneTrait(const clang::RecordDecl *decl);
621628

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -443,18 +443,19 @@ bool ConverterRefCount::VisitOffsetOfExpr(clang::OffsetOfExpr *expr) {
443443
return false;
444444
}
445445

446-
std::string ConverterRefCount::GetComparisonCall(
447-
const clang::FunctionDecl *op, const clang::CXXRecordDecl *decl,
448-
std::string_view lhs, std::string_view rhs) {
446+
std::string
447+
ConverterRefCount::GetComparisonReferenceArg(const clang::CXXRecordDecl *decl,
448+
std::string_view value) {
449+
PushConversionKind push(*this, ConversionKind::FullRefCount);
450+
return BoxValue(GetShallowCopy(decl, value)) + ".as_pointer()";
451+
}
452+
453+
std::string
454+
ConverterRefCount::GetComparisonReceiver(const clang::CXXMethodDecl *,
455+
const clang::CXXRecordDecl *decl,
456+
std::string_view lhs) {
449457
PushConversionKind push(*this, ConversionKind::FullRefCount);
450-
auto lhs_ptr = BoxValue(GetShallowCopy(decl, lhs)) + ".as_pointer()";
451-
auto rhs_ptr = BoxValue(GetShallowCopy(decl, rhs)) + ".as_pointer()";
452-
if (const auto *method = clang::dyn_cast<clang::CXXMethodDecl>(op)) {
453-
return std::format("{}::{}(&{}, {})", GetUFCSName(method),
454-
GetMethodName(method), lhs_ptr, rhs_ptr);
455-
}
456-
return std::format("{}({}, {})", GetNamedDeclAsString(op->getCanonicalDecl()),
457-
lhs_ptr, rhs_ptr);
458+
return std::format("&{}.as_pointer()", BoxValue(GetShallowCopy(decl, lhs)));
458459
}
459460

460461
std::string ConverterRefCount::GetShallowCopy(const clang::RecordDecl *decl,

‎cpp2rust/converter/models/converter_refcount.h‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,12 @@ class ConverterRefCount final : public Converter {
3636

3737
const char *CharRustType() const override { return "u8"; }
3838

39-
std::string GetComparisonCall(const clang::FunctionDecl *op,
40-
const clang::CXXRecordDecl *decl,
41-
std::string_view lhs,
42-
std::string_view rhs) override;
39+
std::string GetComparisonReferenceArg(const clang::CXXRecordDecl *decl,
40+
std::string_view value) override;
41+
42+
std::string GetComparisonReceiver(const clang::CXXMethodDecl *method,
43+
const clang::CXXRecordDecl *decl,
44+
std::string_view lhs) override;
4345

4446
std::string GetShallowCopy(const clang::RecordDecl *decl,
4547
std::string_view src);

0 commit comments

Comments
 (0)