Skip to content
Snippets Groups Projects
Commit fe2419aa authored by Devang Patel's avatar Devang Patel
Browse files

Match union field type when member expression is u->x

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@44879 91177308-0d34-0410-b5e6-96231b3b80d8
parent 7a9d49fd
No related branches found
No related tags found
No related merge requests found
...@@ -387,16 +387,24 @@ EmitOCUVectorElementExpr(const OCUVectorElementExpr *E) { ...@@ -387,16 +387,24 @@ EmitOCUVectorElementExpr(const OCUVectorElementExpr *E) {
LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) { LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
bool isUnion = false;
Expr *BaseExpr = E->getBase(); Expr *BaseExpr = E->getBase();
llvm::Value *BaseValue = NULL; llvm::Value *BaseValue = NULL;
// If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar. // If this is s.x, emit s as an lvalue. If it is s->x, emit s as a scalar.
if (E->isArrow()) if (E->isArrow()) {
BaseValue = EmitScalarExpr(BaseExpr); BaseValue = EmitScalarExpr(BaseExpr);
const PointerType *PTy =
cast<PointerType>(BaseExpr->getType().getCanonicalType());
if (PTy->getPointeeType()->isUnionType())
isUnion = true;
}
else { else {
LValue BaseLV = EmitLValue(BaseExpr); LValue BaseLV = EmitLValue(BaseExpr);
// FIXME: this isn't right for bitfields. // FIXME: this isn't right for bitfields.
BaseValue = BaseLV.getAddress(); BaseValue = BaseLV.getAddress();
if (BaseExpr->getType()->isUnionType())
isUnion = true;
} }
FieldDecl *Field = E->getMemberDecl(); FieldDecl *Field = E->getMemberDecl();
...@@ -409,7 +417,7 @@ LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) { ...@@ -409,7 +417,7 @@ LValue CodeGenFunction::EmitMemberExpr(const MemberExpr *E) {
llvm::Value *V = Builder.CreateGEP(BaseValue,Idxs, Idxs + 2, "tmp"); llvm::Value *V = Builder.CreateGEP(BaseValue,Idxs, Idxs + 2, "tmp");
// Match union field type. // Match union field type.
if (BaseExpr->getType()->isUnionType()) { if (isUnion) {
const llvm::Type * FieldTy = ConvertType(Field->getType()); const llvm::Type * FieldTy = ConvertType(Field->getType());
const llvm::PointerType * BaseTy = const llvm::PointerType * BaseTy =
cast<llvm::PointerType>(BaseValue->getType()); cast<llvm::PointerType>(BaseValue->getType());
......
// RUN: clang %s -emit-llvm // RUN: clang %s -emit-llvm
union { union u_tag {
int a; int a;
float b; float b;
} u; } u;
...@@ -9,6 +9,10 @@ void f() { ...@@ -9,6 +9,10 @@ void f() {
u.b = 11; u.b = 11;
} }
float get_b(union u_tag *my_u) {
return my_u->b;
}
int f2( float __x ) { int f2( float __x ) {
union{ union{
float __f; float __f;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment