Skip to content

Commit feaaa9f

Browse files
committed
Translate pointer diff
1 parent 9a23dc6 commit feaaa9f

3 files changed

Lines changed: 29 additions & 41 deletions

File tree

cpp2rust/converter/converter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2028,7 +2028,7 @@ bool Converter::VisitBinaryOperator(clang::BinaryOperator *expr) {
20282028
std::format("::std::mem::size_of::<{}>()", pointee_type_as_string);
20292029
StrCat(size_of_as_string);
20302030
}
2031-
StrCat(keyword::kAs, "u64");
2031+
ConvertCast(expr->getType());
20322032
computed_expr_type_ = ComputedExprType::FreshValue;
20332033
} else {
20342034
ConvertGenericBinaryOperator(expr);

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,8 +1247,12 @@ bool ConverterRefCount::VisitBinaryOperator(clang::BinaryOperator *expr) {
12471247
// fresh pointers
12481248
if (expr->isAdditiveOp() && lhs_type->isPointerType() &&
12491249
rhs_type->isPointerType()) {
1250-
StrCat(ConvertFreshPointer(lhs), expr->getOpcodeStr(),
1251-
ConvertFreshPointer(rhs));
1250+
{
1251+
PushParen paren(*this);
1252+
StrCat(ConvertFreshPointer(lhs), expr->getOpcodeStr(),
1253+
ConvertFreshPointer(rhs));
1254+
}
1255+
ConvertCast(expr->getType());
12521256
computed_expr_type_ = ComputedExprType::FreshValue;
12531257
return false;
12541258
}

libcc2rs/src/rc.rs

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -675,45 +675,29 @@ impl<T> Sub for Ptr<T> {
675675
}
676676
}
677677

678-
impl<T> std::ops::AddAssign<u64> for Ptr<T> {
679-
#[inline]
680-
fn add_assign(&mut self, other: u64) {
681-
let step = self.elem_step();
682-
self.offset = self
683-
.offset
684-
.wrapping_add((other as usize).wrapping_mul(step));
685-
}
686-
}
687-
688-
impl<T> std::ops::AddAssign<i32> for Ptr<T> {
689-
#[inline]
690-
fn add_assign(&mut self, other: i32) {
691-
let step = self.elem_step();
692-
self.offset = self
693-
.offset
694-
.wrapping_add(((other as isize).wrapping_mul(step as isize)) as usize);
695-
}
696-
}
697-
698-
impl<T> std::ops::AddAssign<u32> for Ptr<T> {
699-
#[inline]
700-
fn add_assign(&mut self, other: u32) {
701-
let step = self.elem_step();
702-
self.offset = self
703-
.offset
704-
.wrapping_add((other as usize).wrapping_mul(step));
705-
}
706-
}
707-
708-
impl<T> std::ops::AddAssign<isize> for Ptr<T> {
709-
#[inline]
710-
fn add_assign(&mut self, other: isize) {
711-
let step = self.elem_step();
712-
self.offset = self
713-
.offset
714-
.wrapping_add((other.wrapping_mul(step as isize)) as usize);
715-
}
678+
macro_rules! impl_ptr_add_sub_assign {
679+
($($rhs:ty),+) => { $(
680+
impl<T> std::ops::AddAssign<$rhs> for Ptr<T> {
681+
#[inline]
682+
fn add_assign(&mut self, other: $rhs) {
683+
let step = self.elem_step();
684+
self.offset = self.offset.wrapping_add(
685+
((other as isize).wrapping_mul(step as isize)) as usize,
686+
);
687+
}
688+
}
689+
impl<T> std::ops::SubAssign<$rhs> for Ptr<T> {
690+
#[inline]
691+
fn sub_assign(&mut self, other: $rhs) {
692+
let step = self.elem_step();
693+
self.offset = self.offset.wrapping_sub(
694+
((other as isize).wrapping_mul(step as isize)) as usize,
695+
);
696+
}
697+
}
698+
)+ }
716699
}
700+
impl_ptr_add_sub_assign!(i32, u32, u64, isize);
717701

718702
macro_rules! impl_ptr_add_sub {
719703
($($rhs:ty),+) => { $(

0 commit comments

Comments
 (0)