diff options
| author | Anders Carlsson <andersca@mac.com> | 2009-07-31 21:38:39 +0000 | 
|---|---|---|
| committer | Anders Carlsson <andersca@mac.com> | 2009-07-31 21:38:39 +0000 | 
| commit | d65ab045e84c265e39e5cab9606c4489c9d21304 (patch) | |
| tree | 3952a7539fc1581f6e6bfab5a597f602f7881255 | |
| parent | 2358732164ec94a4e80c69e2e1a3ba6eb3efcbce (diff) | |
| download | bcm5719-llvm-d65ab045e84c265e39e5cab9606c4489c9d21304.tar.gz bcm5719-llvm-d65ab045e84c265e39e5cab9606c4489c9d21304.zip  | |
Move code from EmitUnion directly into the function that handles cast-to-union.
llvm-svn: 77735
| -rw-r--r-- | clang/lib/CodeGen/CGExprConstant.cpp | 30 | 
1 files changed, 28 insertions, 2 deletions
diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp index bf8123648bb..0e9d20928c7 100644 --- a/clang/lib/CodeGen/CGExprConstant.cpp +++ b/clang/lib/CodeGen/CGExprConstant.cpp @@ -378,9 +378,35 @@ public:      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)) {  | 

