Skip to content

Commit 5b60ce6

Browse files
committed
Move IsMutatingCall in converter_lib
1 parent 4099462 commit 5b60ce6

3 files changed

Lines changed: 16 additions & 11 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,21 +2225,21 @@ bool Converter::VisitConditionalOperator(clang::ConditionalOperator *expr) {
22252225
bool branch_is_addr =
22262226
expr->isLValue() && !isRValue() && !expr->getType()->isFunctionType();
22272227
{
2228-
PushBrace brace(*this);
2228+
PushBrace then_brace(*this);
22292229
if (branch_is_addr) {
22302230
StrCat(token::kRef, keyword_mut_);
22312231
}
2232-
PushExplicitAutoref no_autoref(*this, branch_is_addr ? std::optional<bool>{}
2232+
PushExplicitAutoref no_autoref(*this, branch_is_addr ? std::nullopt
22332233
: autoref_mut_);
22342234
Convert(expr->getTrueExpr());
22352235
}
22362236
StrCat(keyword::kElse);
22372237
{
2238-
PushBrace brace(*this);
2238+
PushBrace else_brace(*this);
22392239
if (branch_is_addr) {
22402240
StrCat(token::kRef, keyword_mut_);
22412241
}
2242-
PushExplicitAutoref no_autoref(*this, branch_is_addr ? std::optional<bool>{}
2242+
PushExplicitAutoref no_autoref(*this, branch_is_addr ? std::nullopt
22432243
: autoref_mut_);
22442244
Convert(expr->getFalseExpr());
22452245
}
@@ -2369,13 +2369,7 @@ bool Converter::ConvertCXXOperatorCallExpr(clang::CXXOperatorCallExpr *expr) {
23692369
}
23702370
break;
23712371
case clang::OverloadedOperatorKind::OO_Subscript: {
2372-
bool is_mut = true;
2373-
if (auto *callee = expr->getDirectCallee()) {
2374-
if (auto *method = clang::dyn_cast<clang::CXXMethodDecl>(callee)) {
2375-
is_mut = !method->isConst();
2376-
}
2377-
}
2378-
PushExplicitAutoref autoref(*this, is_mut);
2372+
PushExplicitAutoref autoref(*this, IsMutatingCall(expr));
23792373
ConvertArraySubscript(expr->getArg(0), expr->getArg(1), expr->getType());
23802374
break;
23812375
}

cpp2rust/converter/converter_lib.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,15 @@ bool IsMut(clang::QualType qual_type) {
156156
qual_type->getPointeeType().isConstQualified());
157157
}
158158

159+
bool IsMutatingCall(const clang::CallExpr *expr) {
160+
if (auto *callee = expr->getDirectCallee()) {
161+
if (auto *method = clang::dyn_cast<clang::CXXMethodDecl>(callee)) {
162+
return !method->isConst();
163+
}
164+
}
165+
return true;
166+
}
167+
159168
bool IsOverloadedFunction(const clang::FunctionDecl *decl) {
160169
const auto *ctx = decl->getDeclContext();
161170
const auto decl_name = decl->getDeclName();

cpp2rust/converter/converter_lib.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ bool IsUnsignedArithOp(const clang::BinaryOperator *expr);
4545

4646
bool IsMut(clang::QualType qual_type);
4747

48+
bool IsMutatingCall(const clang::CallExpr *expr);
49+
4850
bool IsOverloadedFunction(const clang::FunctionDecl *decl);
4951

5052
bool IsOverloadedMethod(const clang::CXXMethodDecl *decl);

0 commit comments

Comments
 (0)