From 06e863f2902b8ba55b056308875c19f7ba3dab25 Mon Sep 17 00:00:00 2001 From: Eli Friedman Date: Tue, 13 May 2008 23:18:27 +0000 Subject: [PATCH] Add codegen support for block-level compound literals. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@51081 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/CGExpr.cpp | 20 ++++++++++++++++++++ lib/CodeGen/CGExprScalar.cpp | 5 +---- lib/CodeGen/CodeGenFunction.h | 2 ++ test/CodeGen/compound-literal.c | 13 +++++++++++++ 4 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 test/CodeGen/compound-literal.c diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index addd939348..d6791074c7 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/lib/CodeGen/CGExpr.cpp @@ -106,6 +106,8 @@ LValue CodeGenFunction::EmitLValue(const Expr *E) { case Expr::ExtVectorElementExprClass: return EmitExtVectorElementExpr(cast(E)); case Expr::MemberExprClass: return EmitMemberExpr(cast(E)); + case Expr::CompoundLiteralExprClass: + return EmitCompoundLiteralLValue(cast(E)); } } @@ -563,6 +565,24 @@ LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue, Field->getType()->isSignedIntegerType()); } +LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E) { + const llvm::Type *LTy = ConvertType(E->getType()); + llvm::Value *DeclPtr = CreateTempAlloca(LTy, ".compoundliteral"); + + const Expr* InitExpr = E->getInitializer(); + LValue Result = LValue::MakeAddr(DeclPtr); + + if (E->getType()->isComplexType()) { + EmitComplexExprIntoAddr(InitExpr, DeclPtr, false); + } else if (hasAggregateLLVMType(E->getType())) { + EmitAnyExpr(InitExpr, DeclPtr, false); + } else { + EmitStoreThroughLValue(EmitAnyExpr(InitExpr), Result, E->getType()); + } + + return Result; +} + //===--------------------------------------------------------------------===// // Expression Emission //===--------------------------------------------------------------------===// diff --git a/lib/CodeGen/CGExprScalar.cpp b/lib/CodeGen/CGExprScalar.cpp index a501145a49..4fb69d045e 100644 --- a/lib/CodeGen/CGExprScalar.cpp +++ b/lib/CodeGen/CGExprScalar.cpp @@ -129,6 +129,7 @@ public: Value *VisitArraySubscriptExpr(ArraySubscriptExpr *E); Value *VisitMemberExpr(Expr *E) { return EmitLoadOfLValue(E); } Value *VisitExtVectorElementExpr(Expr *E) { return EmitLoadOfLValue(E); } + Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { return EmitLoadOfLValue(E); } Value *VisitStringLiteral(Expr *E) { return EmitLValue(E).getAddress(); } Value *VisitPreDefinedExpr(Expr *E) { return EmitLValue(E).getAddress(); } @@ -165,10 +166,6 @@ public: return V; } - - Value *VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { - return Visit(E->getInitializer()); - } Value *VisitImplicitCastExpr(const ImplicitCastExpr *E); Value *VisitCastExpr(const CastExpr *E) { diff --git a/lib/CodeGen/CodeGenFunction.h b/lib/CodeGen/CodeGenFunction.h index f067a0e225..0693781bce 100644 --- a/lib/CodeGen/CodeGenFunction.h +++ b/lib/CodeGen/CodeGenFunction.h @@ -69,6 +69,7 @@ namespace clang { class ObjCStringLiteral; class ObjCIvarRefExpr; class MemberExpr; + class CompoundLiteralExpr; class VarDecl; class EnumConstantDecl; @@ -431,6 +432,7 @@ public: LValue EmitArraySubscriptExpr(const ArraySubscriptExpr *E); LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E); LValue EmitMemberExpr(const MemberExpr *E); + LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E); LValue EmitLValueForField(llvm::Value* Base, FieldDecl* Field, bool isUnion); diff --git a/test/CodeGen/compound-literal.c b/test/CodeGen/compound-literal.c new file mode 100644 index 0000000000..bc185b24dc --- /dev/null +++ b/test/CodeGen/compound-literal.c @@ -0,0 +1,13 @@ +// RUN: clang < %s -emit-llvm + +int* a = &(int){1}; +struct s {int a, b, c;} * b = &(struct s) {1, 2, 3}; +// Not working; complex constants are broken +// _Complex double * x = &(_Complex double){1.0f}; + +int xxx() { +int* a = &(int){1}; +struct s {int a, b, c;} * b = &(struct s) {1, 2, 3}; +_Complex double * x = &(_Complex double){1.0f}; +_Complex double * y = &(_Complex double){}; +} -- 2.50.1