Skip to content

Commit c1c8c4f

Browse files
committed
Do the same for copy/move assignment
1 parent e42c15f commit c1c8c4f

3 files changed

Lines changed: 50 additions & 23 deletions

File tree

‎cpp2rust/converter/converter.cpp‎

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,10 @@ std::string Converter::GetMethodName(const clang::CXXMethodDecl *decl) {
10951095
if (clang::isa<clang::CXXDestructorDecl>(decl)) {
10961096
return kDestructorName;
10971097
}
1098+
if (const char *name = GetCopyOrMoveName(decl);
1099+
name && CanUseCopyOrMoveName(decl, name)) {
1100+
return name;
1101+
}
10981102
if (IsOverloadedMethod(decl)) {
10991103
return GetOverloadedFunctionName(decl);
11001104
}
@@ -1138,30 +1142,10 @@ std::string Converter::GetSelfMaybeWithMut(const clang::CXXMethodDecl *decl) {
11381142
return MethodNeedsMutableReceiver(decl) ? "&mut self" : "&self";
11391143
}
11401144

1141-
static bool CanUseShortCopyOrMoveCtorName(clang::CXXConstructorDecl *decl,
1142-
const std::string &name) {
1143-
const auto *record = decl->getParent();
1144-
bool is_unique_ctor =
1145-
std::count_if(record->ctor_begin(), record->ctor_end(),
1146-
[decl](const clang::CXXConstructorDecl *ctor) {
1147-
return !ctor->isDeleted() &&
1148-
(decl->isCopyConstructor()
1149-
? ctor->isCopyConstructor()
1150-
: ctor->isMoveConstructor());
1151-
}) == 1;
1152-
bool is_unique_name =
1153-
std::none_of(record->method_begin(), record->method_end(),
1154-
[&name](const clang::CXXMethodDecl *method) {
1155-
return method->getDeclName().isIdentifier() &&
1156-
method->getName() == name;
1157-
});
1158-
return is_unique_ctor && is_unique_name;
1159-
}
1160-
11611145
std::string Converter::GetCtorName(clang::CXXConstructorDecl *decl) {
11621146
if (decl->isCopyOrMoveConstructor()) {
1163-
std::string name = decl->isCopyConstructor() ? "copy_from" : "move_from";
1164-
if (CanUseShortCopyOrMoveCtorName(decl, name)) {
1147+
if (const char *name = GetCopyOrMoveName(decl);
1148+
CanUseCopyOrMoveName(decl, name)) {
11651149
return name;
11661150
}
11671151
return GetOverloadedFunctionName(decl);
@@ -3358,7 +3342,7 @@ void Converter::ConvertMemberExpr(clang::MemberExpr *expr) {
33583342
if (auto *method = clang::dyn_cast<clang::CXXMethodDecl>(member);
33593343
method && IsOverloadedMethod(method)) {
33603344
StrCat(token::kDot);
3361-
StrCat(GetOverloadedFunctionName(method));
3345+
StrCat(GetMethodName(method));
33623346
} else if (!name_override.empty()) {
33633347
StrCat(token::kDot, name_override);
33643348
} else if (member->getDeclName().isIdentifier()) {

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,44 @@ bool IsOverloadedMethod(const clang::CXXMethodDecl *decl) {
286286
}) > 1;
287287
}
288288

289+
const char *GetCopyOrMoveName(const clang::CXXMethodDecl *method) {
290+
if (auto *ctor = clang::dyn_cast<clang::CXXConstructorDecl>(method)) {
291+
if (ctor->isCopyConstructor()) {
292+
return "copy_from";
293+
}
294+
if (ctor->isMoveConstructor()) {
295+
return "move_from";
296+
}
297+
return nullptr;
298+
}
299+
if (method->isCopyAssignmentOperator()) {
300+
return "copy_assign";
301+
}
302+
if (method->isMoveAssignmentOperator()) {
303+
return "move_assign";
304+
}
305+
return nullptr;
306+
}
307+
308+
bool CanUseCopyOrMoveName(const clang::CXXMethodDecl *decl,
309+
const std::string &name) {
310+
const auto *record = decl->getParent();
311+
bool is_unique_member =
312+
std::count_if(record->method_begin(), record->method_end(),
313+
[&name](const clang::CXXMethodDecl *method) {
314+
const char *method_name = GetCopyOrMoveName(method);
315+
return !method->isDeleted() && method_name &&
316+
method_name == name;
317+
}) == 1;
318+
bool is_unique_name =
319+
std::none_of(record->method_begin(), record->method_end(),
320+
[&name](const clang::CXXMethodDecl *method) {
321+
return method->getDeclName().isIdentifier() &&
322+
method->getName() == name;
323+
});
324+
return is_unique_member && is_unique_name;
325+
}
326+
289327
bool IsUserDefinedCopyConstructor(const clang::CXXConstructorDecl *ctor) {
290328
return ctor->isCopyConstructor() && ctor->isUserProvided() &&
291329
IsUserDefinedDecl(ctor);

‎cpp2rust/converter/converter_lib.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ void ForEachTemplateInstantiatedMethod(
7070

7171
bool IsOverloadedMethod(const clang::CXXMethodDecl *decl);
7272

73+
const char *GetCopyOrMoveName(const clang::CXXMethodDecl *method);
74+
75+
bool CanUseCopyOrMoveName(const clang::CXXMethodDecl *decl,
76+
const std::string &name);
77+
7378
bool IsUserDefinedCopyConstructor(const clang::CXXConstructorDecl *ctor);
7479

7580
bool IsConvertibleCopyOrMoveConstructor(const clang::CXXConstructorDecl *ctor);

0 commit comments

Comments
 (0)