diff options
| author | Chris Lattner <sabre@nondot.org> | 2009-03-18 18:28:57 +0000 |
|---|---|---|
| committer | Chris Lattner <sabre@nondot.org> | 2009-03-18 18:28:57 +0000 |
| commit | 75dfedaf702822c9e51a4f3c5d3ecf2d2ad99272 (patch) | |
| tree | 60d2415ff88388fdfb1f6fe5a50d991d058d7447 /lib/CodeGen/CGExpr.cpp | |
| parent | af2e4baa2a75471e820030701225a1ca02c7308f (diff) | |
| download | clang-75dfedaf702822c9e51a4f3c5d3ecf2d2ad99272.tar.gz clang-75dfedaf702822c9e51a4f3c5d3ecf2d2ad99272.tar.bz2 clang-75dfedaf702822c9e51a4f3c5d3ecf2d2ad99272.tar.xz | |
add codegen support for casting an element to a union.
There are some more complex cases (_Complex and structs)
that I'm still working on.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67218 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/CodeGen/CGExpr.cpp')
| -rw-r--r-- | lib/CodeGen/CGExpr.cpp | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/lib/CodeGen/CGExpr.cpp b/lib/CodeGen/CGExpr.cpp index b3cf921bc5..93b2382166 100644 --- a/lib/CodeGen/CGExpr.cpp +++ b/lib/CodeGen/CGExpr.cpp @@ -186,12 +186,7 @@ LValue CodeGenFunction::EmitLValue(const Expr *E) { case Expr::CXXDynamicCastExprClass: case Expr::CXXReinterpretCastExprClass: case Expr::CXXConstCastExprClass: - // Casts are only lvalues when the source and destination types are the - // same. - assert(getContext().hasSameUnqualifiedType(E->getType(), - cast<CastExpr>(E)->getSubExpr()->getType()) && - "Type changing cast is not an lvalue"); - return EmitLValue(cast<CastExpr>(E)->getSubExpr()); + return EmitCastLValue(cast<CastExpr>(E)); } } @@ -994,8 +989,7 @@ LValue CodeGenFunction::EmitLValueForField(llvm::Value* BaseValue, return LV; } -LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E) -{ +LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E){ const llvm::Type *LTy = ConvertType(E->getType()); llvm::Value *DeclPtr = CreateTempAlloca(LTy, ".compoundliteral"); @@ -1013,6 +1007,29 @@ LValue CodeGenFunction::EmitCompoundLiteralLValue(const CompoundLiteralExpr* E) return Result; } +/// EmitCastLValue - Casts are never lvalues. If a cast is needed by the code +/// generator in an lvalue context, then it must mean that we need the address +/// of an aggregate in order to access one of its fields. This can happen for +/// all the reasons that casts are permitted with aggregate result, including +/// noop aggregate casts, and cast from scalar to union. +LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) { + // If this is an aggregate-to-aggregate cast, just use the input's address as + // the lvalue. + if (getContext().hasSameUnqualifiedType(E->getType(), + E->getSubExpr()->getType())) + return EmitLValue(E->getSubExpr()); + + // Otherwise, we must have a cast from scalar to union. + assert(E->getType()->isUnionType() && "Expected scalar-to-union cast"); + + // Casts are only lvalues when the source and destination types are the same. + llvm::Value *Temp = CreateTempAlloca(ConvertType(E->getType())); + EmitAnyExpr(E, Temp, false); + + return LValue::MakeAddr(Temp, E->getType().getCVRQualifiers(), + getContext().getObjCGCAttrKind(E->getType())); +} + //===--------------------------------------------------------------------===// // Expression Emission //===--------------------------------------------------------------------===// |
