Skip to content

[CIR] Enable support for nested struct members in C++ #142205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 30, 2025
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
12 changes: 7 additions & 5 deletions clang/lib/CIR/CodeGen/CIRGenTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,13 @@ isSafeToConvert(const RecordDecl *rd, CIRGenTypes &cgt,
// out, don't do it. This includes virtual base classes which get laid out
// when a class is translated, even though they aren't embedded by-value into
// the class.
if (isa<CXXRecordDecl>(rd)) {
assert(!cir::MissingFeatures::cxxSupport());
cgt.getCGModule().errorNYI(rd->getSourceRange(),
"isSafeToConvert: CXXRecordDecl");
return false;
if (auto *crd = dyn_cast<CXXRecordDecl>(rd)) {
if (crd->getNumBases() > 0) {
assert(!cir::MissingFeatures::cxxSupport());
cgt.getCGModule().errorNYI(rd->getSourceRange(),
"isSafeToConvert: CXXRecordDecl with bases");
return false;
}
}

// If this type would require laying out members that are currently being laid
Expand Down
28 changes: 28 additions & 0 deletions clang/test/CIR/CodeGen/struct.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,31 @@ char f2(CompleteS &s) {
// OGCG: %[[S_REF:.*]] = load ptr, ptr %[[S_ADDR]]
// OGCG: %[[S_ADDR2:.*]] = getelementptr inbounds nuw %struct.CompleteS, ptr %[[S_REF]], i32 0, i32 1
// OGCG: %[[S_B:.*]] = load i8, ptr %[[S_ADDR2]]

struct Inner {
int n;
};

struct Outer {
Inner i;
};

void f3() {
Outer o;
o.i.n;
}

// CIR: cir.func @_Z2f3v()
// CIR: %[[O:.*]] = cir.alloca !rec_Outer, !cir.ptr<!rec_Outer>, ["o"]
// CIR: %[[O_I:.*]] = cir.get_member %[[O]][0] {name = "i"}
// CIR: %[[O_I_N:.*]] = cir.get_member %[[O_I]][0] {name = "n"}

// LLVM: define{{.*}} void @_Z2f3v()
// LLVM: %[[O:.*]] = alloca %struct.Outer, i64 1, align 4
// LLVM: %[[O_I:.*]] = getelementptr %struct.Outer, ptr %[[O]], i32 0, i32 0
// LLVM: %[[O_I_N:.*]] = getelementptr %struct.Inner, ptr %[[O_I]], i32 0, i32 0

// OGCG: define{{.*}} void @_Z2f3v()
// OGCG: %[[O:.*]] = alloca %struct.Outer, align 4
// OGCG: %[[O_I:.*]] = getelementptr inbounds nuw %struct.Outer, ptr %[[O]], i32 0, i32 0
// OGCG: %[[O_I_N:.*]] = getelementptr inbounds nuw %struct.Inner, ptr %[[O_I]], i32 0, i32 0
Loading