Skip to content

Commit 38ca68d

Browse files
YazZz1klanza
authored andcommitted
[CIR][CIRGen] Support for compound literal lvalue (#515)
This change is taken from the original codegen.
1 parent a5b9d6e commit 38ca68d

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,33 @@ LValue CIRGenFunction::buildLValueForFieldInitialization(
411411
return makeAddrLValue(V, FieldType, FieldBaseInfo);
412412
}
413413

414+
LValue
415+
CIRGenFunction::buildCompoundLiteralLValue(const CompoundLiteralExpr *E) {
416+
if (E->isFileScope()) {
417+
llvm_unreachable("NYI");
418+
}
419+
420+
if (E->getType()->isVariablyModifiedType()) {
421+
llvm_unreachable("NYI");
422+
}
423+
424+
Address DeclPtr = CreateMemTemp(E->getType(), getLoc(E->getSourceRange()),
425+
".compoundliteral");
426+
const Expr *InitExpr = E->getInitializer();
427+
LValue Result = makeAddrLValue(DeclPtr, E->getType(), AlignmentSource::Decl);
428+
429+
buildAnyExprToMem(InitExpr, DeclPtr, E->getType().getQualifiers(),
430+
/*Init*/ true);
431+
432+
// Block-scope compound literals are destroyed at the end of the enclosing
433+
// scope in C.
434+
if (!getLangOpts().CPlusPlus)
435+
if (QualType::DestructionKind DtorKind = E->getType().isDestructedType())
436+
llvm_unreachable("NYI");
437+
438+
return Result;
439+
}
440+
414441
// Detect the unusual situation where an inline version is shadowed by a
415442
// non-inline version. In that case we should pick the external one
416443
// everywhere. That's GCC behavior too.
@@ -2241,6 +2268,8 @@ LValue CIRGenFunction::buildLValue(const Expr *E) {
22412268
return buildStringLiteralLValue(cast<StringLiteral>(E));
22422269
case Expr::MemberExprClass:
22432270
return buildMemberExpr(cast<MemberExpr>(E));
2271+
case Expr::CompoundLiteralExprClass:
2272+
return buildCompoundLiteralLValue(cast<CompoundLiteralExpr>(E));
22442273
case Expr::PredefinedExprClass:
22452274
return buildPredefinedLValue(cast<PredefinedExpr>(E));
22462275
case Expr::CXXFunctionalCastExprClass:

clang/lib/CIR/CodeGen/CIRGenFunction.h

+1
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,7 @@ class CIRGenFunction : public CIRGenTypeCache {
15291529

15301530
LValue buildCheckedLValue(const Expr *E, TypeCheckKind TCK);
15311531
LValue buildMemberExpr(const MemberExpr *E);
1532+
LValue buildCompoundLiteralLValue(const CompoundLiteralExpr *E);
15321533

15331534
/// Specifies which type of sanitizer check to apply when handling a
15341535
/// particular builtin.

clang/test/CIR/CodeGen/compound-literal.c

+18
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,21 @@ S b = {
2727

2828
// LLVM: @.compoundLiteral.1 = internal global [1 x i32] [i32 1]
2929
// LLVM: @b = global %struct.S { ptr @.compoundLiteral.1 }
30+
31+
int foo() {
32+
return (struct {
33+
int i;
34+
}){1}
35+
.i;
36+
}
37+
38+
// CIR: cir.func no_proto @foo() -> !s32i
39+
// CIR: [[RET_MEM:%.*]] = cir.alloca !s32i, cir.ptr <!s32i>, ["__retval"] {alignment = 4 : i64}
40+
// CIR: [[COMPLITERAL_MEM:%.*]] = cir.alloca !ty_22anon2E122, cir.ptr <!ty_22anon2E122>, [".compoundliteral"] {alignment = 4 : i64}
41+
// CIR: [[FIELD:%.*]] = cir.get_member [[COMPLITERAL_MEM]][0] {name = "i"} : !cir.ptr<!ty_22anon2E122> -> !cir.ptr<!s32i>
42+
// CIR: [[ONE:%.*]] = cir.const(#cir.int<1> : !s32i) : !s32i
43+
// CIR: cir.store [[ONE]], [[FIELD]] : !s32i, cir.ptr <!s32i>
44+
// CIR: [[ONE:%.*]] = cir.const(#cir.int<1> : !s32i) : !s32i
45+
// CIR: cir.store [[ONE]], [[RET_MEM]] : !s32i, cir.ptr <!s32i>
46+
// CIR: [[RET:%.*]] = cir.load [[RET_MEM]] : cir.ptr <!s32i>, !s32i
47+
// CIR: cir.return [[RET]] : !s32i

0 commit comments

Comments
 (0)