Build AST for copy-construction of copied-in
class object in blocks and carry it to IRGen.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@105487 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp
index e1a6a04..5655a25 100644
--- a/lib/Sema/SemaExpr.cpp
+++ b/lib/Sema/SemaExpr.cpp
@@ -1746,8 +1746,28 @@
     // Variable will be bound by-copy, make it const within the closure.
 
     ExprTy.addConst();
-    return Owned(new (Context) BlockDeclRefExpr(VD, ExprTy, Loc, false,
-                                                constAdded));
+    BlockDeclRefExpr *BDRE = new (Context) BlockDeclRefExpr(VD, 
+                                                            ExprTy, Loc, false,
+                                                            constAdded);
+    QualType T = VD->getType();
+    if (getLangOptions().CPlusPlus && !T->isDependentType() &&
+        !T->isReferenceType()) {
+      Expr *E = new (Context) 
+                  DeclRefExpr(const_cast<ValueDecl*>(BDRE->getDecl()), T,
+                                         SourceLocation());
+      
+      OwningExprResult Res = PerformCopyInitialization(
+                      InitializedEntity::InitializeResult(SourceLocation(), 
+                                                          T, false),
+                      SourceLocation(),
+                      Owned(E));
+      if (!Res.isInvalid()) {
+        Expr *Init = Res.takeAs<Expr>();
+        if (isa<CXXConstructExpr>(Init))
+          BDRE->setCopyConstructorExpr(Init);
+      }
+    }
+    return Owned(BDRE);
   }
   // If this reference is not in a block or if the referenced variable is
   // within the block, create a normal DeclRefExpr.