[cfe-commits] r62278 - in /cfe/trunk: lib/CodeGen/CGExprAgg.cpp test/CodeGen/cast-to-union.c

Nuno Lopes nunoplopes at sapo.pt
Thu Jan 15 12:14:34 PST 2009


Author: nlopes
Date: Thu Jan 15 14:14:33 2009
New Revision: 62278

URL: http://llvm.org/viewvc/llvm-project?rev=62278&view=rev
Log:
add codegen support to union casts
init of static vars still not working. I'll get back to it tomorrow or so

Added:
    cfe/trunk/test/CodeGen/cast-to-union.c
Modified:
    cfe/trunk/lib/CodeGen/CGExprAgg.cpp

Modified: cfe/trunk/lib/CodeGen/CGExprAgg.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExprAgg.cpp?rev=62278&r1=62277&r2=62278&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprAgg.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprAgg.cpp Thu Jan 15 14:14:33 2009
@@ -74,6 +74,7 @@
   // Operators.
   //  case Expr::UnaryOperatorClass:
   //  case Expr::CastExprClass: 
+  void VisitCStyleCastExpr(CStyleCastExpr *E);
   void VisitImplicitCastExpr(ImplicitCastExpr *E);
   void VisitCallExpr(const CallExpr *E);
   void VisitStmtExpr(const StmtExpr *E);
@@ -127,6 +128,18 @@
 //                            Visitor Methods
 //===----------------------------------------------------------------------===//
 
+void AggExprEmitter::VisitCStyleCastExpr(CStyleCastExpr *E) {
+  // GCC union extension
+  if (E->getType()->isUnionType()) {
+    RecordDecl *SD = E->getType()->getAsRecordType()->getDecl();
+    LValue FieldLoc = CGF.EmitLValueForField(DestPtr, *SD->field_begin(), true, 0);
+    EmitInitializationToLValue(E->getSubExpr(), FieldLoc);
+    return;
+  }
+
+  Visit(E->getSubExpr());
+}
+
 void AggExprEmitter::VisitImplicitCastExpr(ImplicitCastExpr *E) {
   assert(CGF.getContext().typesAreCompatible(
                           E->getSubExpr()->getType().getUnqualifiedType(),
@@ -467,7 +480,7 @@
     // Unions only initialize one field.
     // (things can get weird with designators, but they aren't
     // supported yet.)
-    if (E->getType()->isUnionType())
+    if (isUnion)
       break;
   }
 }

Added: cfe/trunk/test/CodeGen/cast-to-union.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/cast-to-union.c?rev=62278&view=auto

==============================================================================
--- cfe/trunk/test/CodeGen/cast-to-union.c (added)
+++ cfe/trunk/test/CodeGen/cast-to-union.c Thu Jan 15 14:14:33 2009
@@ -0,0 +1,10 @@
+// RUN: clang -emit-llvm < %s | grep "store i32 351, i32*"
+
+union u { int i; };
+
+void foo() {
+  union u ola = (union u) 351;
+}
+
+// FIXME: not working yet
+// union u w = (union u)2;





More information about the cfe-commits mailing list