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
5 changes: 5 additions & 0 deletions cpp2rust/converter/converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3136,6 +3136,11 @@ bool Converter::ConvertIncAndDec(clang::UnaryOperator *expr) {
MarkFresh();
return true;
}
bool deref_prefix = expr->isPrefix() && !isVoid();
PushParen paren(*this, deref_prefix);
if (deref_prefix) {
StrCat(token::kStar);
}
{
PushExprKind push(*this, ExprKind::LValue);
Convert(expr->getSubExpr());
Expand Down
16 changes: 15 additions & 1 deletion cpp2rust/converter/models/boxed_vars.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ class AddressTakenVisitor
}

bool VisitUnaryOperator(clang::UnaryOperator *expr) {
if (expr->isIncrementDecrementOp()) {
if (expr->isIncrementDecrementOp() &&
(expr->isPostfix() || expr->isPRValue())) {
AddValueUse(expr->getSubExpr(), /*write=*/true);
}
return true;
Expand Down Expand Up @@ -392,6 +393,11 @@ class AddressTakenVisitor
// without making a pointer to it.
void AddValueUse(const clang::Expr *expr, bool write = false) {
expr = IgnoreNoOpCasts(IgnoreTransparentStdCall(expr)->IgnoreParens());
if (auto *op = clang::dyn_cast<clang::UnaryOperator>(expr);
op && op->isPrefix() && op->isIncrementDecrementOp()) {
AddValueUse(op->getSubExpr(), /*write=*/true);
return;
}
// The value of a conditional lvalue is the value of either operand; it is
// written through a pointer to the operand.
if (auto *cond = clang::dyn_cast<clang::ConditionalOperator>(expr)) {
Expand Down Expand Up @@ -423,6 +429,14 @@ class AddressTakenVisitor
if (auto *cleanups = clang::dyn_cast<clang::ExprWithCleanups>(expr)) {
expr = cleanups->getSubExpr()->IgnoreParens();
}
if (auto *op = clang::dyn_cast<clang::UnaryOperator>(expr);
op && op->isIncrementDecrementOp()) {
AddValueUse(op->getSubExpr(), /*write=*/true);
} else if (auto *comma = clang::dyn_cast<clang::BinaryOperator>(expr);
comma && comma->getOpcode() == clang::BO_Comma) {
AddDiscardedUse(comma->getRHS());
}

auto *binary = clang::dyn_cast<clang::BinaryOperator>(expr);
if (!binary) {
return;
Expand Down
39 changes: 34 additions & 5 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -925,22 +925,51 @@ bool ConverterRefCount::ConvertIncAndDec(clang::UnaryOperator *expr) {
if (expr->isPrefix() && isAddrOf()) {
auto ptr = ConvertFreshObject(sub_expr);
auto name = NewTempName();
StrCat(
std::format("{{ let {0} = {1}; {0}.with_mut(|__v| __v.{2}()); {0} }}",
name, ptr, method));
StrCat(std::format(
"{{ let {0} = {1}; {0}.with_mut(|__v| {{ __v.{2}(); }}); {0} }}", name,
ptr, method));
MarkFresh();
return true;
}

bool is_prefix = expr->isPrefix();
auto str = ConvertLValue(sub_expr);
if (!pending_deref_.empty()) {
StrCat(pending_deref_.take(), ".with_mut(|__v| __v.", method, "())");
bool fresh = pending_deref_.is_fresh();
auto ptr = pending_deref_.take();
if (!is_prefix) {
StrCat(ptr, ".with_mut(|__v| __v.", method, "())");
} else if (isVoid()) {
StrCat(ptr, ".with_mut(|__v| { __v.", method, "(); })");
} else if (isLValue()) {
auto name = NewTempName();
pending_deref_.set(std::format("{{ let {0} = {1}{2}; {0}.with_mut(|__v| "
"{{ __v.{3}(); }}); {0} }}",
name, ptr, fresh ? "" : ".clone()",
method),
/*fresh=*/true);
return true;
} else {
StrCat(ptr, ".with_mut(|__v| __v.", method, "().clone())");
}
} else if (!isVoid() &&
clang::isa<clang::MemberExpr>(sub_expr->IgnoreParens()) &&
!IsUnboxedPlace(sub_expr)) {
// The struct is borrowed in a block, which ends the borrow before the
// result is used, e.g., to write through a pointer into the struct.
StrCat(std::format("{{ {}.{}() }}", str, method));
if (!is_prefix) {
StrCat(std::format("{{ {}.{}() }}", str, method));
} else if (isLValue()) {
StrCat(std::format("(*{}.{}())", str, method));
} else {
StrCat(std::format("{{ {}.{}().clone() }}", str, method));
}
} else if (is_prefix && !isVoid()) {
StrCat(std::format("(*{}.{}())", str, method));
if (!isLValue()) {
SetValueFreshness(expr->getType());
return true;
}
} else {
StrCat(str, '.', method, "()");
}
Expand Down
6 changes: 3 additions & 3 deletions docs/src/runtime/inc-dec.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ statement, so the `inc` and `dec` modules define one trait per operator form:

```rust
pub trait PostfixInc { fn postfix_inc(&mut self) -> Self; }
pub trait PrefixInc { fn prefix_inc(&mut self) -> Self; }
pub trait PrefixInc { fn prefix_inc(&mut self) -> &mut Self; }
pub trait PostfixDec { fn postfix_dec(&mut self) -> Self; }
pub trait PrefixDec { fn prefix_dec(&mut self) -> Self; }
pub trait PrefixDec { fn prefix_dec(&mut self) -> &mut Self; }
```

Each method updates the value in place and returns what the C expression
evaluates to: the postfix forms return a copy of the old value, the prefix forms
the new one.
a mutable reference to the updated operand, which is an lvalue in C++.

```c
int x = 0;
Expand Down
24 changes: 12 additions & 12 deletions libcc2rs/src/dec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ macro_rules! postfix_wrap_dec_impl {
}

pub trait PrefixDec {
fn prefix_dec(&mut self) -> Self;
fn prefix_dec(&mut self) -> &mut Self;
}

macro_rules! prefix_nowrap_dec_impl {
($($type:ty),*) => {
$(impl PrefixDec for $type {
#[inline]
fn prefix_dec(&mut self) -> Self {
fn prefix_dec(&mut self) -> &mut Self {
*self -= 1 as $type;
*self
self
}
})*
};
Expand All @@ -51,9 +51,9 @@ macro_rules! prefix_wrap_dec_impl {
($($type:ty),*) => {
$(impl PrefixDec for $type {
#[inline]
fn prefix_dec(&mut self) -> Self {
fn prefix_dec(&mut self) -> &mut Self {
*self = self.wrapping_sub(1 as $type);
*self
self
}
})*
};
Expand Down Expand Up @@ -98,22 +98,22 @@ pub trait UnsafePrefixDec {
/// # Safety
/// This function decrements a pointer and returns the new value.
/// The caller must ensure the pointer is valid and doesn't underflow.
unsafe fn prefix_dec(&mut self) -> Self;
unsafe fn prefix_dec(&mut self) -> &mut Self;
}

impl<T> UnsafePrefixDec for *const T {
#[inline]
unsafe fn prefix_dec(&mut self) -> Self {
unsafe fn prefix_dec(&mut self) -> &mut Self {
unsafe { *self = self.offset(-1) }
*self
self
}
}

impl<T> UnsafePrefixDec for *mut T {
#[inline]
unsafe fn prefix_dec(&mut self) -> Self {
unsafe fn prefix_dec(&mut self) -> &mut Self {
unsafe { *self = self.offset(-1) }
*self
self
}
}

Expand All @@ -131,7 +131,7 @@ mod tests {
#[test]
fn test_prefix_dec() {
let mut x = 10;
assert_eq!(x.prefix_dec(), 9);
assert_eq!(*x.prefix_dec(), 9);
assert_eq!(x, 9);
}

Expand All @@ -152,7 +152,7 @@ mod tests {
let y = &x as *const i32;
let mut z = &x as *const i32;
unsafe {
assert_eq!(z.prefix_dec(), y.offset(-1));
assert_eq!(*z.prefix_dec(), y.offset(-1));
assert_eq!(z, y.offset(-1));
}
}
Expand Down
24 changes: 12 additions & 12 deletions libcc2rs/src/inc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ macro_rules! postfix_wrap_inc_impl {
}

pub trait PrefixInc {
fn prefix_inc(&mut self) -> Self;
fn prefix_inc(&mut self) -> &mut Self;
}

macro_rules! prefix_nowrap_inc_impl {
($($type:ty),*) => {
$(impl PrefixInc for $type {
#[inline]
fn prefix_inc(&mut self) -> Self {
fn prefix_inc(&mut self) -> &mut Self {
*self += 1 as $type;
*self
self
}
})*
};
Expand All @@ -51,9 +51,9 @@ macro_rules! prefix_wrap_inc_impl {
($($type:ty),*) => {
$(impl PrefixInc for $type {
#[inline]
fn prefix_inc(&mut self) -> Self {
fn prefix_inc(&mut self) -> &mut Self {
*self = self.wrapping_add(1 as $type);
*self
self
}
})*
};
Expand Down Expand Up @@ -98,22 +98,22 @@ pub trait UnsafePrefixInc {
/// # Safety
/// This function increments a pointer and returns the new value.
/// The caller must ensure the pointer is valid and doesn't overflow.
unsafe fn prefix_inc(&mut self) -> Self;
unsafe fn prefix_inc(&mut self) -> &mut Self;
}

impl<T> UnsafePrefixInc for *const T {
#[inline]
unsafe fn prefix_inc(&mut self) -> Self {
unsafe fn prefix_inc(&mut self) -> &mut Self {
unsafe { *self = self.offset(1) }
*self
self
}
}

impl<T> UnsafePrefixInc for *mut T {
#[inline]
unsafe fn prefix_inc(&mut self) -> Self {
unsafe fn prefix_inc(&mut self) -> &mut Self {
unsafe { *self = self.offset(1) }
*self
self
}
}

Expand All @@ -131,7 +131,7 @@ mod tests {
#[test]
fn test_prefix_inc() {
let mut x = 10;
assert_eq!(x.prefix_inc(), 11);
assert_eq!(*x.prefix_inc(), 11);
assert_eq!(x, 11);
}

Expand All @@ -152,7 +152,7 @@ mod tests {
let y = &x as *const i32;
let mut z = &x as *const i32;
unsafe {
assert_eq!(z.prefix_inc(), y.offset(1));
assert_eq!(*z.prefix_inc(), y.offset(1));
assert_eq!(z, y.offset(1));
}
}
Expand Down
8 changes: 4 additions & 4 deletions libcc2rs/src/rc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1444,17 +1444,17 @@ impl<T> PostfixDec for Ptr<T> {

impl<T> PrefixInc for Ptr<T> {
#[inline]
fn prefix_inc(&mut self) -> Self {
fn prefix_inc(&mut self) -> &mut Self {
self.offset = self.offset.wrapping_add(self.elem_step());
self.clone()
self
}
}

impl<T> PrefixDec for Ptr<T> {
#[inline]
fn prefix_dec(&mut self) -> Self {
fn prefix_dec(&mut self) -> &mut Self {
self.offset = self.offset.wrapping_sub(self.elem_step());
self.clone()
self
}
}

Expand Down
Loading
Loading