Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1917,6 +1917,11 @@ bool Converter::ConvertSpecialCall(clang::CallExpr *expr) {
return true;
}

if (IsTransparentStdCall(ctx_, expr)) {
Convert(expr->getArg(0));
return true;
}

// The remaining calls are converted as rules say, if there is one.
if (Mapper::Contains(ctx_, expr)) {
return false;
Expand All @@ -1928,11 +1933,6 @@ bool Converter::ConvertSpecialCall(clang::CallExpr *expr) {
return true;
}

if (IsTransparentStdCall(expr)) {
Convert(expr->getArg(0));
return true;
}

if (auto *opcall = clang::dyn_cast<clang::CXXOperatorCallExpr>(expr);
opcall && !IsUserOperatorCall(opcall)) {
ConvertCXXOperatorCallExpr(opcall);
Expand Down Expand Up @@ -3090,7 +3090,8 @@ void Converter::ConvertGenericBinaryOperator(clang::BinaryOperator *expr) {
}

bool Converter::IsReferenceType(const clang::Expr *expr) const {
const auto *e = IgnoreTransparentStdCall(expr->IgnoreCasts())->IgnoreCasts();
const auto *e =
IgnoreTransparentStdCall(ctx_, expr->IgnoreCasts())->IgnoreCasts();
if (const auto *call = clang::dyn_cast<clang::CallExpr>(e)) {
return !clang::isa<clang::CXXOperatorCallExpr>(call) &&
GetReturnTypeOfFunction(call)->isReferenceType();
Expand Down
31 changes: 14 additions & 17 deletions cpp2rust/converter/converter_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2459,34 +2459,31 @@ std::optional<clang::QualType> GetOperandImplicitConversionTarget(
return std::nullopt;
}

bool IsTransparentStdCall(const clang::CallExpr *expr) {
const auto *callee = expr->getDirectCallee();
if (!callee) {
return false;
}
switch (callee->getBuiltinID()) {
case clang::Builtin::BImove:
case clang::Builtin::BImove_if_noexcept:
case clang::Builtin::BIforward:
case clang::Builtin::BIforward_like:
case clang::Builtin::BIas_const:
return true;
default:
bool IsTransparentStdCall(clang::ASTContext &ctx, const clang::CallExpr *expr) {
auto *rule = RuleRegistry::GetExprRule(ctx, expr);
if (!rule || rule->body.size() != 1 ||
!RuleRegistry::ReturnsReference(expr) ||
clang::isa<clang::CXXMemberCallExpr>(expr) || expr->getNumArgs() == 0) {
return false;
}
auto *ph = std::get_if<TranslationRule::PlaceholderFragment>(&rule->body[0]);
return ph && ph->n == 0 &&
ctx.hasSameUnqualifiedType(expr->getArg(0)->getType(),
expr->getType());
}

const clang::Expr *IgnoreTransparentStdCall(const clang::Expr *expr) {
const clang::Expr *IgnoreTransparentStdCall(clang::ASTContext &ctx,
const clang::Expr *expr) {
if (const auto *call =
clang::dyn_cast<clang::CallExpr>(expr->IgnoreParenImpCasts());
call && IsTransparentStdCall(call)) {
call && IsTransparentStdCall(ctx, call)) {
return call->getArg(0);
}
return expr;
}

bool IsTemporaryObject(const clang::Expr *expr) {
const auto *operand = IgnoreTransparentStdCall(expr);
bool IsTemporaryObject(clang::ASTContext &ctx, const clang::Expr *expr) {
const auto *operand = IgnoreTransparentStdCall(ctx, expr);
if (operand != expr) {
return !operand->isGLValue();
}
Expand Down
7 changes: 4 additions & 3 deletions cpp2rust/converter/converter_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,12 @@ bool IsConstantEvaluatedQuery(const clang::CallExpr *expr);
bool HasConstantEvaluatedInit(const clang::ASTContext &ctx,
const clang::VarDecl *decl);

bool IsTransparentStdCall(const clang::CallExpr *expr);
bool IsTransparentStdCall(clang::ASTContext &ctx, const clang::CallExpr *expr);

const clang::Expr *IgnoreTransparentStdCall(const clang::Expr *expr);
const clang::Expr *IgnoreTransparentStdCall(clang::ASTContext &ctx,
const clang::Expr *expr);

bool IsTemporaryObject(const clang::Expr *expr);
bool IsTemporaryObject(clang::ASTContext &ctx, const clang::Expr *expr);

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

Expand Down
8 changes: 6 additions & 2 deletions cpp2rust/converter/models/boxed_vars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,9 @@ class AddressTakenVisitor
}

bool VisitCallExpr(clang::CallExpr *expr) {
if (IsTransparentStdCall(ctx_, expr)) {
return true;
}
AddRuleArgs(expr, expr->getArgs(), expr->getNumArgs());
return true;
}
Expand Down Expand Up @@ -305,7 +308,7 @@ class AddressTakenVisitor
}

void VisitCallExpr(const clang::CallExpr *expr) {
if (IsTransparentStdCall(expr)) {
if (IsTransparentStdCall(owner_.ctx_, expr)) {
Traverse(expr->getArg(0));
} else {
VisitExpr(expr);
Expand Down Expand Up @@ -392,7 +395,8 @@ class AddressTakenVisitor
// An element of an array or vector, or a field of a struct, is accessed
// without making a pointer to it.
void AddValueUse(const clang::Expr *expr, bool write = false) {
expr = IgnoreNoOpCasts(IgnoreTransparentStdCall(expr)->IgnoreParens());
expr =
IgnoreNoOpCasts(IgnoreTransparentStdCall(ctx_, expr)->IgnoreParens());
if (auto *op = clang::dyn_cast<clang::UnaryOperator>(expr);
op && op->isPrefix() && op->isIncrementDecrementOp()) {
AddValueUse(op->getSubExpr(), /*write=*/true);
Expand Down
4 changes: 2 additions & 2 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3165,7 +3165,7 @@ std::string ConverterRefCount::ConvertMappedMethodCall(
auto arg_idx = receiver_ph->n;
auto *arg = args[arg_idx];
if (auto *call = clang::dyn_cast<clang::CallExpr>(arg->IgnoreCasts());
call && IsTransparentStdCall(call)) {
call && IsTransparentStdCall(ctx_, call)) {
arg = call->getArg(0);
}

Expand Down Expand Up @@ -3249,7 +3249,7 @@ void ConverterRefCount::SetUFCSReceiver(clang::Expr *base, bool is_arrow,
}
return;
}
if (IsTemporaryObject(base) && base->getType()->isRecordType() &&
if (IsTemporaryObject(ctx_, base) && base->getType()->isRecordType() &&
!IsReferenceType(base->IgnoreImplicit())) {
if (retain_bound_temporaries_ && full_expression_temporaries_) {
ufcs_receiver_ = token::kRef + ConvertTemporaryPointer(base, {});
Expand Down
16 changes: 16 additions & 0 deletions rules/move/src.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

#include <utility>

template <typename T1> T1 &&f2(typename std::remove_reference<T1>::type &&a0) {
return std::forward<T1>(std::move(a0));
}

template <typename T1> T1 &&f3(typename std::remove_reference<T1>::type &a0) {
return std::forward<T1>(a0);
}

template <typename T1> typename std::remove_reference<T1>::type &&f5(T1 &&a0) {
return std::move(std::forward<T1>(a0));
}
8 changes: 8 additions & 0 deletions rules/move/src_cxx14.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

#include <utility>

template <typename T1> const T1 &f6(T1 &a0) {
return std::move_if_noexcept(a0);
}
8 changes: 8 additions & 0 deletions rules/move/src_cxx23.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

#include <utility>

template <typename T1, typename T2> auto &&f7(T2 &&a0) {
return std::forward_like<T1>(std::forward<T2>(a0));
}
24 changes: 24 additions & 0 deletions rules/move/tgt_refcount.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

use libcc2rs::*;

fn f2<T1>(a0: Ptr<T1>) -> Ptr<T1> {
a0
}

fn f3<T1>(a0: Ptr<T1>) -> Ptr<T1> {
a0
}

fn f5<T1>(a0: Ptr<T1>) -> Ptr<T1> {
a0
}

fn f6<T1>(a0: Ptr<T1>) -> Ptr<T1> {
a0
}

fn f7<T1, T2>(a0: Ptr<T2>) -> Ptr<T2> {
a0
}
22 changes: 22 additions & 0 deletions rules/move/tgt_unsafe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

unsafe fn f2<T1>(a0: *mut T1) -> *mut T1 {
a0
}

unsafe fn f3<T1>(a0: *mut T1) -> *mut T1 {
a0
}

unsafe fn f5<T1>(a0: *mut T1) -> *mut T1 {
a0
}

unsafe fn f6<T1>(a0: *const T1) -> *const T1 {
a0
}

unsafe fn f7<T1, T2>(a0: *mut T2) -> *mut T2 {
a0
}
9 changes: 9 additions & 0 deletions rules/utility/src_cxx17.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

#include <type_traits>
#include <utility>

template <typename T1> std::add_const_t<T1> &f7(T1 &a0) {
return std::as_const(a0);
}
8 changes: 8 additions & 0 deletions rules/utility/tgt_refcount.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

use libcc2rs::*;

fn f7<T1>(a0: Ptr<T1>) -> Ptr<T1> {
a0
}
6 changes: 6 additions & 0 deletions rules/utility/tgt_unsafe.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

unsafe fn f7<T1>(a0: *const T1) -> *const T1 {
a0
}
136 changes: 136 additions & 0 deletions tests/std/utilities/utility/forward-helpers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
// ADDITIONAL_COMPILE_FLAGS: -std=c++23
#include <cassert>
#include <string>
#include <utility>
#include <vector>

// N4950 [forward]/3: forward<T>(t) returns static_cast<T&&>(t).
namespace p3_forward {
void test() {
int value = 1;
int forwarded = std::forward<int>(std::move(value));
assert(forwarded == 1);
int &lvalue = std::forward<int &>(value);
lvalue = 2;
assert(value == 2);
}
} // namespace p3_forward

// N4950 [forward]/6: forward_like<T>(x) forwards x as an rvalue when T is not
// an lvalue reference, and as an lvalue otherwise.
namespace p6_forward_like {
struct Movable {
int value;
Movable(int value) : value(value) {}
Movable(const Movable &other) : value(other.value) {}
Movable(Movable &&other) : value(other.value) { other.value = 0; }
};

struct Holder {
Movable member{1};
};

template <typename Self> Movable take(Self &&self) {
return Movable(std::forward_like<Self>(self.member));
}

void test() {
Holder holder;
Movable copied = take(holder);
assert(copied.value == 1 && holder.member.value == 1);
Movable moved = take(std::move(holder));
assert(moved.value == 1 && holder.member.value == 0);
}
} // namespace p6_forward_like

// N4950 [forward]/9: move(t) returns static_cast<remove_reference_t<T>&&>(t).
namespace p9_move {
struct Movable {
int value;
Movable(int value) : value(value) {}
Movable(Movable &&other) : value(other.value) { other.value = 0; }
};

void test() {
int number = 1;
int moved = std::move(number);
assert(moved == 1 && number == 1);

int target = 2;
int *pointer = &target;
int *moved_pointer = std::move(pointer);
assert(moved_pointer == &target && pointer == &target);

Movable source(3);
Movable destination = std::move(source);
assert(destination.value == 3 && source.value == 0);

std::vector<std::string> strings;
std::string text = "text";
strings.push_back(std::move(text));
assert(strings[0] == "text");
}
} // namespace p9_move

// N4950 [forward]/10: forward passes an lvalue to A(const A&), which copies,
// and an rvalue to A(A&&), which moves.
namespace p10_forward_in_template {
struct A {
int value;
int copies = 0;
int moves = 0;
A(int value) : value(value) {}
A(const A &other) : value(other.value), copies(other.copies + 1) {}
A(A &&other) : value(other.value), moves(other.moves + 1) { other.value = 0; }
};

template <typename T> A make(T &&source) { return A(std::forward<T>(source)); }

void test() {
A a(1);
A copied = make(a);
assert(copied.value == 1 && copied.copies == 1 && copied.moves == 0);
assert(a.value == 1);
A moved = make(std::move(a));
assert(moved.value == 1 && moved.copies == 0 && moved.moves == 1);
assert(a.value == 0);
}
} // namespace p10_forward_in_template

// N4950 [forward]/11: move_if_noexcept(x) returns std::move(x), as a const
// lvalue when the move constructor may throw and there is a copy constructor.
namespace p11_move_if_noexcept {
struct NoexceptMove {
int value;
NoexceptMove(int value) : value(value) {}
NoexceptMove(const NoexceptMove &other) : value(other.value) {}
NoexceptMove(NoexceptMove &&other) noexcept : value(other.value) {
other.value = 0;
}
};

struct ThrowingMove {
int value;
ThrowingMove(int value) : value(value) {}
ThrowingMove(const ThrowingMove &other) : value(other.value) {}
ThrowingMove(ThrowingMove &&other) : value(other.value) { other.value = 0; }
};

void test() {
NoexceptMove moved_from(1);
NoexceptMove moved(std::move_if_noexcept(moved_from));
assert(moved.value == 1 && moved_from.value == 0);
ThrowingMove copied_from(2);
ThrowingMove copied(std::move_if_noexcept(copied_from));
assert(copied.value == 2 && copied_from.value == 2);
}
} // namespace p11_move_if_noexcept

int main() {
p3_forward::test();
p6_forward_like::test();
p9_move::test();
p10_forward_in_template::test();
p11_move_if_noexcept::test();
return 0;
}
Loading
Loading