Fix crash when synthesizing property setters when the property type and ivar
type have mismatched Objective-C types.
  - <rdar://problem/7336352> [irgen] crash in synthesized property construction

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@85275 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/CodeGen/CGObjC.cpp b/lib/CodeGen/CGObjC.cpp
index cadba32..2fe3f5b 100644
--- a/lib/CodeGen/CGObjC.cpp
+++ b/lib/CodeGen/CGObjC.cpp
@@ -280,17 +280,29 @@
     EmitCall(Types.getFunctionInfo(getContext().VoidTy, Args),
              SetPropertyFn, Args);
   } else {
+    // FIXME: Find a clean way to avoid AST node creation.
     SourceLocation Loc = PD->getLocation();
     ValueDecl *Self = OMD->getSelfDecl();
     ObjCIvarDecl *Ivar = PID->getPropertyIvarDecl();
     DeclRefExpr Base(Self, Self->getType(), Loc);
     ParmVarDecl *ArgDecl = *OMD->param_begin();
     DeclRefExpr Arg(ArgDecl, ArgDecl->getType(), Loc);
-    ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base,
-                            true, true);
-    BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
-                          Ivar->getType(), Loc);
-    EmitStmt(&Assign);
+    ObjCIvarRefExpr IvarRef(Ivar, Ivar->getType(), Loc, &Base, true, true);
+    
+    // The property type can differ from the ivar type in some situations with
+    // Objective-C pointer types, we can always bit cast the RHS in these cases.
+    if (getContext().getCanonicalType(Ivar->getType()) !=
+        getContext().getCanonicalType(ArgDecl->getType())) {
+      ImplicitCastExpr ArgCasted(Ivar->getType(), CastExpr::CK_BitCast, &Arg,
+                                 false);
+      BinaryOperator Assign(&IvarRef, &ArgCasted, BinaryOperator::Assign,
+                            Ivar->getType(), Loc);
+      EmitStmt(&Assign);
+    } else {
+      BinaryOperator Assign(&IvarRef, &Arg, BinaryOperator::Assign,
+                            Ivar->getType(), Loc);
+      EmitStmt(&Assign);
+    }
   }
 
   FinishFunction();