Skip to content

Commit df8875f

Browse files
authored
Move transparent calls in rules (#651)
std::move, std::forward, etc
1 parent 29ffbab commit df8875f

20 files changed

Lines changed: 999 additions & 30 deletions

‎cpp2rust/converter/converter.cpp‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,6 +1917,11 @@ bool Converter::ConvertSpecialCall(clang::CallExpr *expr) {
19171917
return true;
19181918
}
19191919

1920+
if (IsTransparentStdCall(ctx_, expr)) {
1921+
Convert(expr->getArg(0));
1922+
return true;
1923+
}
1924+
19201925
// The remaining calls are converted as rules say, if there is one.
19211926
if (Mapper::Contains(ctx_, expr)) {
19221927
return false;
@@ -1928,11 +1933,6 @@ bool Converter::ConvertSpecialCall(clang::CallExpr *expr) {
19281933
return true;
19291934
}
19301935

1931-
if (IsTransparentStdCall(expr)) {
1932-
Convert(expr->getArg(0));
1933-
return true;
1934-
}
1935-
19361936
if (auto *opcall = clang::dyn_cast<clang::CXXOperatorCallExpr>(expr);
19371937
opcall && !IsUserOperatorCall(opcall)) {
19381938
ConvertCXXOperatorCallExpr(opcall);
@@ -3090,7 +3090,8 @@ void Converter::ConvertGenericBinaryOperator(clang::BinaryOperator *expr) {
30903090
}
30913091

30923092
bool Converter::IsReferenceType(const clang::Expr *expr) const {
3093-
const auto *e = IgnoreTransparentStdCall(expr->IgnoreCasts())->IgnoreCasts();
3093+
const auto *e =
3094+
IgnoreTransparentStdCall(ctx_, expr->IgnoreCasts())->IgnoreCasts();
30943095
if (const auto *call = clang::dyn_cast<clang::CallExpr>(e)) {
30953096
return !clang::isa<clang::CXXOperatorCallExpr>(call) &&
30963097
GetReturnTypeOfFunction(call)->isReferenceType();

‎cpp2rust/converter/converter_lib.cpp‎

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2477,34 +2477,31 @@ std::optional<clang::QualType> GetOperandImplicitConversionTarget(
24772477
return std::nullopt;
24782478
}
24792479

2480-
bool IsTransparentStdCall(const clang::CallExpr *expr) {
2481-
const auto *callee = expr->getDirectCallee();
2482-
if (!callee) {
2483-
return false;
2484-
}
2485-
switch (callee->getBuiltinID()) {
2486-
case clang::Builtin::BImove:
2487-
case clang::Builtin::BImove_if_noexcept:
2488-
case clang::Builtin::BIforward:
2489-
case clang::Builtin::BIforward_like:
2490-
case clang::Builtin::BIas_const:
2491-
return true;
2492-
default:
2480+
bool IsTransparentStdCall(clang::ASTContext &ctx, const clang::CallExpr *expr) {
2481+
auto *rule = RuleRegistry::GetExprRule(ctx, expr);
2482+
if (!rule || rule->body.size() != 1 ||
2483+
!RuleRegistry::ReturnsReference(expr) ||
2484+
clang::isa<clang::CXXMemberCallExpr>(expr) || expr->getNumArgs() == 0) {
24932485
return false;
24942486
}
2487+
auto *ph = std::get_if<TranslationRule::PlaceholderFragment>(&rule->body[0]);
2488+
return ph && ph->n == 0 &&
2489+
ctx.hasSameUnqualifiedType(expr->getArg(0)->getType(),
2490+
expr->getType());
24952491
}
24962492

2497-
const clang::Expr *IgnoreTransparentStdCall(const clang::Expr *expr) {
2493+
const clang::Expr *IgnoreTransparentStdCall(clang::ASTContext &ctx,
2494+
const clang::Expr *expr) {
24982495
if (const auto *call =
24992496
clang::dyn_cast<clang::CallExpr>(expr->IgnoreParenImpCasts());
2500-
call && IsTransparentStdCall(call)) {
2497+
call && IsTransparentStdCall(ctx, call)) {
25012498
return call->getArg(0);
25022499
}
25032500
return expr;
25042501
}
25052502

2506-
bool IsTemporaryObject(const clang::Expr *expr) {
2507-
const auto *operand = IgnoreTransparentStdCall(expr);
2503+
bool IsTemporaryObject(clang::ASTContext &ctx, const clang::Expr *expr) {
2504+
const auto *operand = IgnoreTransparentStdCall(ctx, expr);
25082505
if (operand != expr) {
25092506
return !operand->isGLValue();
25102507
}

‎cpp2rust/converter/converter_lib.h‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,12 @@ bool IsConstantEvaluatedQuery(const clang::CallExpr *expr);
405405
bool HasConstantEvaluatedInit(const clang::ASTContext &ctx,
406406
const clang::VarDecl *decl);
407407

408-
bool IsTransparentStdCall(const clang::CallExpr *expr);
408+
bool IsTransparentStdCall(clang::ASTContext &ctx, const clang::CallExpr *expr);
409409

410-
const clang::Expr *IgnoreTransparentStdCall(const clang::Expr *expr);
410+
const clang::Expr *IgnoreTransparentStdCall(clang::ASTContext &ctx,
411+
const clang::Expr *expr);
411412

412-
bool IsTemporaryObject(const clang::Expr *expr);
413+
bool IsTemporaryObject(clang::ASTContext &ctx, const clang::Expr *expr);
413414

414415
bool ContainsVAArgExpr(const clang::Stmt *stmt);
415416

‎cpp2rust/converter/models/boxed_vars.cpp‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,9 @@ class AddressTakenVisitor
192192
}
193193

194194
bool VisitCallExpr(clang::CallExpr *expr) {
195+
if (IsTransparentStdCall(ctx_, expr)) {
196+
return true;
197+
}
195198
AddRuleArgs(expr, expr->getArgs(), expr->getNumArgs());
196199
return true;
197200
}
@@ -305,7 +308,7 @@ class AddressTakenVisitor
305308
}
306309

307310
void VisitCallExpr(const clang::CallExpr *expr) {
308-
if (IsTransparentStdCall(expr)) {
311+
if (IsTransparentStdCall(owner_.ctx_, expr)) {
309312
Traverse(expr->getArg(0));
310313
} else {
311314
VisitExpr(expr);
@@ -392,7 +395,8 @@ class AddressTakenVisitor
392395
// An element of an array or vector, or a field of a struct, is accessed
393396
// without making a pointer to it.
394397
void AddValueUse(const clang::Expr *expr, bool write = false) {
395-
expr = IgnoreNoOpCasts(IgnoreTransparentStdCall(expr)->IgnoreParens());
398+
expr =
399+
IgnoreNoOpCasts(IgnoreTransparentStdCall(ctx_, expr)->IgnoreParens());
396400
if (auto *op = clang::dyn_cast<clang::UnaryOperator>(expr);
397401
op && op->isPrefix() && op->isIncrementDecrementOp()) {
398402
AddValueUse(op->getSubExpr(), /*write=*/true);

‎cpp2rust/converter/models/converter_refcount.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3165,7 +3165,7 @@ std::string ConverterRefCount::ConvertMappedMethodCall(
31653165
auto arg_idx = receiver_ph->n;
31663166
auto *arg = args[arg_idx];
31673167
if (auto *call = clang::dyn_cast<clang::CallExpr>(arg->IgnoreCasts());
3168-
call && IsTransparentStdCall(call)) {
3168+
call && IsTransparentStdCall(ctx_, call)) {
31693169
arg = call->getArg(0);
31703170
}
31713171

@@ -3249,7 +3249,7 @@ void ConverterRefCount::SetUFCSReceiver(clang::Expr *base, bool is_arrow,
32493249
}
32503250
return;
32513251
}
3252-
if (IsTemporaryObject(base) && base->getType()->isRecordType() &&
3252+
if (IsTemporaryObject(ctx_, base) && base->getType()->isRecordType() &&
32533253
!IsReferenceType(base->IgnoreImplicit())) {
32543254
if (retain_bound_temporaries_ && full_expression_temporaries_) {
32553255
ufcs_receiver_ = token::kRef + ConvertTemporaryPointer(base, {});

‎rules/move/src.cpp‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
#include <utility>
5+
6+
template <typename T1> T1 &&f2(typename std::remove_reference<T1>::type &&a0) {
7+
return std::forward<T1>(std::move(a0));
8+
}
9+
10+
template <typename T1> T1 &&f3(typename std::remove_reference<T1>::type &a0) {
11+
return std::forward<T1>(a0);
12+
}
13+
14+
template <typename T1> typename std::remove_reference<T1>::type &&f5(T1 &&a0) {
15+
return std::move(std::forward<T1>(a0));
16+
}

‎rules/move/src_cxx14.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
#include <utility>
5+
6+
template <typename T1> const T1 &f6(T1 &a0) {
7+
return std::move_if_noexcept(a0);
8+
}

‎rules/move/src_cxx23.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
#include <utility>
5+
6+
template <typename T1, typename T2> auto &&f7(T2 &&a0) {
7+
return std::forward_like<T1>(std::forward<T2>(a0));
8+
}

‎rules/move/tgt_refcount.rs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
use libcc2rs::*;
5+
6+
fn f2<T1>(a0: Ptr<T1>) -> Ptr<T1> {
7+
a0
8+
}
9+
10+
fn f3<T1>(a0: Ptr<T1>) -> Ptr<T1> {
11+
a0
12+
}
13+
14+
fn f5<T1>(a0: Ptr<T1>) -> Ptr<T1> {
15+
a0
16+
}
17+
18+
fn f6<T1>(a0: Ptr<T1>) -> Ptr<T1> {
19+
a0
20+
}
21+
22+
fn f7<T1, T2>(a0: Ptr<T2>) -> Ptr<T2> {
23+
a0
24+
}

‎rules/move/tgt_unsafe.rs‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
unsafe fn f2<T1>(a0: *mut T1) -> *mut T1 {
5+
a0
6+
}
7+
8+
unsafe fn f3<T1>(a0: *mut T1) -> *mut T1 {
9+
a0
10+
}
11+
12+
unsafe fn f5<T1>(a0: *mut T1) -> *mut T1 {
13+
a0
14+
}
15+
16+
unsafe fn f6<T1>(a0: *const T1) -> *const T1 {
17+
a0
18+
}
19+
20+
unsafe fn f7<T1, T2>(a0: *mut T2) -> *mut T2 {
21+
a0
22+
}

0 commit comments

Comments
 (0)