Skip to content

Commit 34627ea

Browse files
committed
Codegen for union accessors
1 parent 6da60bd commit 34627ea

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

cpp2rust/converter/models/converter_refcount.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,8 @@ void ConverterRefCount::EmitRustUnion(clang::RecordDecl *decl) {
490490
}
491491
StrCat(")]");
492492

493-
StrCat(
494-
std::format("pub struct {} {{ __store: libcc2rs::UnionStorage, }}", name));
493+
StrCat(std::format("pub struct {} {{ __store: libcc2rs::UnionStorage, }}",
494+
name));
495495

496496
StrCat(std::format("impl {}", name));
497497
{
@@ -1487,6 +1487,32 @@ bool ConverterRefCount::VisitMemberExpr(clang::MemberExpr *expr) {
14871487
return false;
14881488
}
14891489

1490+
if (auto *parent =
1491+
clang::dyn_cast<clang::RecordDecl>(member->getDeclContext());
1492+
parent && parent->isUnion() && clang::isa<clang::FieldDecl>(member)) {
1493+
std::string str;
1494+
{
1495+
Buffer buf(*this);
1496+
PushExprKind push(*this,
1497+
isLValue() ? ExprKind::LValue : ExprKind::RValue);
1498+
Converter::ConvertMemberExpr(expr); // e.g. (*u.borrow()).i
1499+
str = std::move(buf).str();
1500+
}
1501+
str += "()";
1502+
1503+
if (isAddrOf()) {
1504+
StrCat(str);
1505+
computed_expr_type_ = ComputedExprType::Pointer;
1506+
return false;
1507+
}
1508+
if (isLValue()) {
1509+
pending_deref_.set(str);
1510+
return false;
1511+
}
1512+
StrCat(DerefPtrExpr(str, member->getType()));
1513+
return false;
1514+
}
1515+
14901516
std::string str;
14911517
if (known) {
14921518
str = GetMappedAsString(expr);

0 commit comments

Comments
 (0)