[cfe-commits] r67218 - in /cfe/trunk: lib/CodeGen/CGExpr.cpp lib/CodeGen/CodeGenFunction.h test/CodeGen/exprs.c
Chris Lattner
sabre at nondot.org
Wed Mar 18 11:28:59 PDT 2009
Author: lattner
Date: Wed Mar 18 13:28:57 2009
New Revision: 67218
URL: http://llvm.org/viewvc/llvm-project?rev=67218&view=rev
Log:
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.
Modified:
cfe/trunk/lib/CodeGen/CGExpr.cpp
cfe/trunk/lib/CodeGen/CodeGenFunction.h
cfe/trunk/test/CodeGen/exprs.c
Modified: cfe/trunk/lib/CodeGen/CGExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGExpr.cpp?rev=67218&r1=67217&r2=67218&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CGExpr.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGExpr.cpp Wed Mar 18 13:28:57 2009
@@ -186,12 +186,7 @@
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 @@
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 @@
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
//===--------------------------------------------------------------------===//
Modified: cfe/trunk/lib/CodeGen/CodeGenFunction.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenFunction.h?rev=67218&r1=67217&r2=67218&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenFunction.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenFunction.h Wed Mar 18 13:28:57 2009
@@ -605,6 +605,7 @@
LValue EmitExtVectorElementExpr(const ExtVectorElementExpr *E);
LValue EmitMemberExpr(const MemberExpr *E);
LValue EmitCompoundLiteralLValue(const CompoundLiteralExpr *E);
+ LValue EmitCastLValue(const CastExpr *E);
llvm::Value *EmitIvarOffset(ObjCInterfaceDecl *Interface,
const ObjCIvarDecl *Ivar);
Modified: cfe/trunk/test/CodeGen/exprs.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/exprs.c?rev=67218&r1=67217&r2=67218&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/exprs.c (original)
+++ cfe/trunk/test/CodeGen/exprs.c Wed Mar 18 13:28:57 2009
@@ -79,3 +79,6 @@
return (unsigned) fp;
}
+union f3_x {int x; float y;};
+int f3() {return ((union f3_x)2).x;}
+
More information about the cfe-commits
mailing list