Sema/AST work for capturing copy init expression
to be used in copy helper synthesis of __block
variables. wip.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@120617 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp
index e5081e3..e179766 100644
--- a/lib/AST/ASTContext.cpp
+++ b/lib/AST/ASTContext.cpp
@@ -1008,6 +1008,20 @@
   ObjCImpls[CatD] = ImplD;
 }
 
+/// \brief Get the copy initialization expression of VarDecl,or NULL if 
+/// none exists.
+Expr *ASTContext::getBlockVarCopyInits(VarDecl*VD) {
+  llvm::DenseMap<VarDecl*, Expr*>::iterator
+  I = BlockVarCopyInits.find(VD);
+  return (I != BlockVarCopyInits.end()) ? cast<Expr>(I->second) : 0;
+}
+
+/// \brief Set the copy inialization expression of a block var decl.
+void ASTContext::setBlockVarCopyInits(VarDecl*VD, Expr* Init) {
+  assert(VD && Init && "Passed null params");
+  BlockVarCopyInits[VD] = Init;
+}
+
 /// \brief Allocate an uninitialized TypeSourceInfo.
 ///
 /// The caller should initialize the memory held by TypeSourceInfo using
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index 786a13f..7b0afe1 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -3022,6 +3022,27 @@
   if (NewVD->getLinkage() == ExternalLinkage && !DC->isRecord())
     AddPushedVisibilityAttribute(NewVD);
 
+
+  // For variables declared as __block which require copy construction,
+  // must capture copy initialization expression here.
+  if (getLangOptions().CPlusPlus && NewVD->hasAttr<BlocksAttr>()) {
+    QualType T = NewVD->getType();
+    if (!T->isDependentType() && !T->isReferenceType() &&
+        T->getAs<RecordType>() && !T->isUnionType()) {
+      Expr *E = new (Context) DeclRefExpr(NewVD, T,
+                                          VK_LValue, SourceLocation());
+      ExprResult Res = PerformCopyInitialization(
+                          InitializedEntity::InitializeBlock(NewVD->getLocation(), 
+                                                      T, false),
+                                                      SourceLocation(),
+                                                      Owned(E));
+      if (!Res.isInvalid()) {
+        Res = MaybeCreateCXXExprWithTemporaries(Res.get());
+        Expr *Init = Res.takeAs<Expr>();
+        Context.setBlockVarCopyInits(NewVD, Init);
+      }
+    }
+  }
   MarkUnusedFileScopedDecl(NewVD);
 
   return NewVD;