[cfe-commits] r77735 - /cfe/trunk/lib/CodeGen/CGExprConstant.cpp

Anders Carlsson andersca at mac.com
Fri Jul 31 14:38:40 PDT 2009


Author: andersca
Date: Fri Jul 31 16:38:39 2009
New Revision: 77735

URL: http://llvm.org/viewvc/llvm-project?rev=77735&view=rev
Log:
Move code from EmitUnion directly into the function that handles cast-to-union.

Modified:
    cfe/trunk/lib/CodeGen/CGExprConstant.cpp

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

==============================================================================
--- cfe/trunk/lib/CodeGen/CGExprConstant.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExprConstant.cpp Fri Jul 31 16:38:39 2009
@@ -378,9 +378,35 @@
     if (E->getType()->isUnionType()) {
       const llvm::Type *Ty = ConvertType(E->getType());
       Expr *SubExpr = E->getSubExpr();
-      return EmitUnion(CGM.EmitConstantExpr(SubExpr, SubExpr->getType(), CGF), 
-                       Ty);
+      
+      llvm::Constant *C = 
+        CGM.EmitConstantExpr(SubExpr, SubExpr->getType(), CGF);
+      if (!C)
+        return 0;
+      
+      // Build a struct with the union sub-element as the first member,
+      // and padded to the appropriate size
+      std::vector<llvm::Constant*> Elts;
+      std::vector<const llvm::Type*> Types;
+      Elts.push_back(C);
+      Types.push_back(C->getType());
+      unsigned CurSize = CGM.getTargetData().getTypeAllocSize(C->getType());
+      unsigned TotalSize = CGM.getTargetData().getTypeAllocSize(Ty);
+      
+      assert(CurSize <= TotalSize && "Union size mismatch!");
+      if (unsigned NumPadBytes = TotalSize - CurSize) {
+        const llvm::Type *Ty = llvm::Type::Int8Ty;
+        if (NumPadBytes > 1)
+          Ty = llvm::ArrayType::get(Ty, NumPadBytes);
+        
+        Elts.push_back(llvm::Constant::getNullValue(Ty));
+        Types.push_back(Ty);
+      }
+      
+      llvm::StructType* STy = llvm::StructType::get(Types, false);
+      return llvm::ConstantStruct::get(STy, Elts);
     }
+    
     // Explicit and implicit no-op casts
     QualType Ty = E->getType(), SubTy = E->getSubExpr()->getType();
     if (CGM.getContext().hasSameUnqualifiedType(Ty, SubTy)) {





More information about the cfe-commits mailing list