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
16 changes: 12 additions & 4 deletions cpp2rust/converter/models/converter_refcount.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2165,7 +2165,7 @@ bool ConverterRefCount::ConvertCXXOperatorCallExpr(
pending_deref_.set(std::format("({} as {}).offset({})",
ConvertObject(expr->getArg(0)),
ConvertPtrType(expr->getArg(0)->getType()),
ConvertRValue(expr->getArg(1))),
ConvertSubscriptIndex(expr->getArg(1))),
expr);
break;
}
Expand All @@ -2185,7 +2185,7 @@ bool ConverterRefCount::ConvertCXXOperatorCallExpr(
StrCat(std::format("({} as {}).offset({})",
ConvertObject(expr->getArg(0)),
ConvertPtrType(expr->getArg(0)->getType()),
ConvertRValue(expr->getArg(1))));
ConvertSubscriptIndex(expr->getArg(1))));

if (is_inner_boxed) {
StrCat(GetPointerDerefSuffix(expr->getType()), ".as_pointer()");
Expand Down Expand Up @@ -2220,6 +2220,14 @@ void ConverterRefCount::ConvertFunctionParameters(clang::FunctionDecl *decl) {
}
}

std::string ConverterRefCount::ConvertSubscriptIndex(clang::Expr *idx) {
auto str = ConvertRValue(idx);
if (idx->getType()->isEnumeralType()) {
return std::format("({}) as isize", str);
}
return str;
}

void ConverterRefCount::ConvertArraySubscript(clang::Expr *base,
clang::Expr *idx,
clang::QualType type) {
Expand All @@ -2236,12 +2244,12 @@ void ConverterRefCount::ConvertArraySubscript(clang::Expr *base,
if (IsStringLiteralExpr(base)) {
StrCat(std::format("Ptr::from_string_literal({}).offset({})",
ToString(base->IgnoreParens()->IgnoreImplicit()),
ConvertRValue(idx)));
ConvertSubscriptIndex(idx)));
} else {
StrCat(std::format("({} as {}).offset({})",
ToString(base->IgnoreImplicit()),
ConvertPtrType(base->IgnoreImplicit()->getType()),
ConvertRValue(idx)));
ConvertSubscriptIndex(idx)));
}

if (is_inner_boxed) {
Expand Down
2 changes: 2 additions & 0 deletions cpp2rust/converter/models/converter_refcount.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ class ConverterRefCount final : public Converter {
std::string ConvertPtrType(clang::QualType type);
std::string ConvertPointeeType(clang::QualType ptr_type) override;

std::string ConvertSubscriptIndex(clang::Expr *idx);

std::string GetSafeTypeAsString(clang::QualType qual_type) const;

/// The kind of conversion that should be performed.
Expand Down
11 changes: 11 additions & 0 deletions tests/unit/enum_int_interop_c.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,5 +140,16 @@ int main() {
assert(entries[2].color == BLUE);
assert(entries[2].opt == OPT_C);

const char *names[] = {"red", "green", "blue"};
enum Color idx = GREEN;
assert(names[idx][0] == 'g');
assert(entries[idx].opt == OPT_A);
assert(names[global_tag][0] == 'b');

const char **pp = &names[idx];
assert((*pp)[0] == 'g');
struct Entry *pe = &entries[idx];
assert(pe->opt == OPT_A);

return 0;
}
45 changes: 45 additions & 0 deletions tests/unit/out/refcount/enum_int_interop_c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,5 +319,50 @@ fn main_0() -> i32 {
== ((Option::OPT_C as i32) as u32)) as i32)
!= 0)
);
let names: Value<Box<[Ptr<u8>]>> = Rc::new(RefCell::new(Box::new([
Ptr::from_string_literal(b"red"),
Ptr::from_string_literal(b"green"),
Ptr::from_string_literal(b"blue"),
])));
let idx: Value<Color> = Rc::new(RefCell::new(Color::GREEN));
assert!(
((((((*names.borrow())[(*idx.borrow()) as usize]
.offset((0) as isize)
.read()) as i32)
== ('g' as i32)) as i32)
!= 0)
);
assert!(
(((((*(*entries_3.with(Value::clone).borrow())[(*idx.borrow()) as usize]
.opt
.borrow()) as u32)
== ((Option::OPT_A as i32) as u32)) as i32)
!= 0)
);
assert!(
((((((*names.borrow())[(*global_tag_2.with(Value::clone).borrow()) as usize]
.offset((0) as isize)
.read()) as i32)
== ('b' as i32)) as i32)
!= 0)
);
let pp: Value<Ptr<Ptr<u8>>> = Rc::new(RefCell::new(
((names.as_pointer() as Ptr<Ptr<u8>>).offset((*idx.borrow()) as isize)),
));
assert!(
(((((((*pp.borrow()).read()).offset((0) as isize).read()) as i32) == ('g' as i32)) as i32)
!= 0)
);
let pe: Value<Ptr<Entry>> = Rc::new(RefCell::new(
((entries_3.with(Value::clone).as_pointer() as Ptr<Entry>)
.offset((*idx.borrow()) as isize)),
));
assert!(
((({
let _lhs = ((*(*(*pe.borrow()).upgrade().deref()).opt.borrow()) as u32).clone();
_lhs == ((Option::OPT_A as i32) as u32)
}) as i32)
!= 0)
);
return 0;
}
21 changes: 21 additions & 0 deletions tests/unit/out/unsafe/enum_int_interop_c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,5 +220,26 @@ unsafe fn main_0() -> i32 {
assert!(
((((entries_3[(2) as usize].opt as u32) == ((Option::OPT_C as i32) as u32)) as i32) != 0)
);
let mut names: [*const libc::c_char; 3] = [
(c"red".as_ptr().cast_mut()).cast_const(),
(c"green".as_ptr().cast_mut()).cast_const(),
(c"blue".as_ptr().cast_mut()).cast_const(),
];
let mut idx: Color = Color::GREEN;
assert!(
(((((*names[(idx) as usize].offset((0) as isize)) as i32) == ('g' as i32)) as i32) != 0)
);
assert!(
((((entries_3[(idx) as usize].opt as u32) == ((Option::OPT_A as i32) as u32)) as i32) != 0)
);
assert!(
(((((*names[(global_tag_2) as usize].offset((0) as isize)) as i32) == ('b' as i32))
as i32)
!= 0)
);
let mut pp: *mut *const libc::c_char = (&mut names[(idx) as usize] as *mut *const libc::c_char);
assert!((((((*(*pp).offset((0) as isize)) as i32) == ('g' as i32)) as i32) != 0));
let mut pe: *mut Entry = (&mut entries_3[(idx) as usize] as *mut Entry);
assert!((((((*pe).opt as u32) == ((Option::OPT_A as i32) as u32)) as i32) != 0));
return 0;
}
Loading