]> granicus.if.org Git - clang/commitdiff
Add codegen support for block-level compound literals.
authorEli Friedman <eli.friedman@gmail.com>
Tue, 13 May 2008 23:18:27 +0000 (23:18 +0000)
committerEli Friedman <eli.friedman@gmail.com>
Tue, 13 May 2008 23:18:27 +0000 (23:18 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@51081 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGExpr.cpp
lib/CodeGen/CGExprScalar.cpp
lib/CodeGen/CodeGenFunction.h
test/CodeGen/compound-literal.c [new file with mode: 0644]

index addd93934856eda0579e609f8897444298030ea5..d6791074c7d72d452689f27bc9b5c9d977e1254b 100644 (file)
@@ -106,6 +106,8 @@ LValue CodeGenFunction::EmitLValue(const Expr *E) {
   case Expr::ExtVectorElementExprClass:
     return EmitExtVectorElementExpr(cast<ExtVectorElementExpr>(E));
   case Expr::MemberExprClass: return EmitMemberExpr(cast<MemberExpr>(E));
+  case Expr::CompoundLiteralExprClass:
+    return EmitCompoundLiteralLValue(cast<CompoundLiteralExpr>(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
 //===--------------------------------------------------------------------===//
index a501145a495836811e924ecff68625c80b8b0b95..4fb69d045e3ee452cad9bd571ab5c3c1800d9698 100644 (file)
@@ -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) { 
index f067a0e2250b67412e1ac7287ca4e1577683fe1d..0693781bce8f7364b80b8d6b800a4b1a17134c71 100644 (file)
@@ -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 (file)
index 0000000..bc185b2
--- /dev/null
@@ -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){};
+}