Extend out the block descriptor structure for debug information with
the copy/dispose helpers as appropriate.


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@84817 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp
index 42a6704..442714a 100644
--- a/lib/AST/ASTContext.cpp
+++ b/lib/AST/ASTContext.cpp
@@ -40,7 +40,8 @@
                        bool FreeMem, unsigned size_reserve) :
   GlobalNestedNameSpecifier(0), CFConstantStringTypeDecl(0),
   ObjCFastEnumerationStateTypeDecl(0), FILEDecl(0), jmp_bufDecl(0),
-  sigjmp_bufDecl(0), BlockDescriptorType(0), SourceMgr(SM), LangOpts(LOpts),
+  sigjmp_bufDecl(0), BlockDescriptorType(0), BlockDescriptorExtendedType(0),
+  SourceMgr(SM), LangOpts(LOpts),
   LoadedExternalComments(false), FreeMemory(FreeMem), Target(t),
   Idents(idents), Selectors(sels),
   BuiltinInfo(builtins), ExternalSource(0), PrintingPolicy(LOpts) {
@@ -2714,7 +2715,7 @@
 
   const char *FieldNames[] = {
     "reserved",
-    "Size",
+    "Size"
   };
 
   for (size_t i = 0; i < 2; ++i) {
@@ -2741,6 +2742,53 @@
   BlockDescriptorType = Rec->getDecl();
 }
 
+QualType ASTContext::getBlockDescriptorExtendedType() {
+  if (BlockDescriptorExtendedType)
+    return getTagDeclType(BlockDescriptorExtendedType);
+
+  RecordDecl *T;
+  // FIXME: Needs the FlagAppleBlock bit.
+  T = RecordDecl::Create(*this, TagDecl::TK_struct, TUDecl, SourceLocation(),
+                         &Idents.get("__block_descriptor_withcopydispose"));
+  
+  QualType FieldTypes[] = {
+    UnsignedLongTy,
+    UnsignedLongTy,
+    getPointerType(VoidPtrTy),
+    getPointerType(VoidPtrTy)
+  };
+
+  const char *FieldNames[] = {
+    "reserved",
+    "Size",
+    "CopyFuncPtr",
+    "DestroyFuncPtr"
+  };
+
+  for (size_t i = 0; i < 4; ++i) {
+    FieldDecl *Field = FieldDecl::Create(*this,
+                                         T,
+                                         SourceLocation(),
+                                         &Idents.get(FieldNames[i]),
+                                         FieldTypes[i], /*DInfo=*/0,
+                                         /*BitWidth=*/0,
+                                         /*Mutable=*/false);
+    T->addDecl(Field);
+  }
+
+  T->completeDefinition(*this);
+
+  BlockDescriptorExtendedType = T;
+
+  return getTagDeclType(BlockDescriptorExtendedType);
+}
+
+void ASTContext::setBlockDescriptorExtendedType(QualType T) {
+  const RecordType *Rec = T->getAs<RecordType>();
+  assert(Rec && "Invalid BlockDescriptorType");
+  BlockDescriptorExtendedType = Rec->getDecl();
+}
+
 bool ASTContext::BlockRequiresCopying(QualType Ty) {
   if (Ty->isBlockPointerType())
     return true;
@@ -2811,6 +2859,7 @@
 
 
 QualType ASTContext::getBlockParmType(
+  bool BlockHasCopyDispose,
   llvm::SmallVector<const Expr *, 8> &BlockDeclRefDecls) {
   // FIXME: Move up
   static int UniqueBlockParmTypeID = 0;
@@ -2824,7 +2873,9 @@
     IntTy,
     IntTy,
     getPointerType(VoidPtrTy),
-    getPointerType(getBlockDescriptorType()),
+    (BlockHasCopyDispose ?
+     getPointerType(getBlockDescriptorExtendedType()) :
+     getPointerType(getBlockDescriptorType()))
   };
 
   const char *FieldNames[] = {
diff --git a/lib/CodeGen/CGBlocks.cpp b/lib/CodeGen/CGBlocks.cpp
index 26b19e5..dc4db4a 100644
--- a/lib/CodeGen/CGBlocks.cpp
+++ b/lib/CodeGen/CGBlocks.cpp
@@ -486,8 +486,10 @@
     return offset;
 
   // Don't run the expensive check, unless we have to.
-  if (!BlockHasCopyDispose && BlockRequiresCopying(E->getType()))
-    BlockHasCopyDispose = true;
+  if (!BlockHasCopyDispose)
+    if (E->isByRef()
+        || BlockRequiresCopying(E->getType()))
+      BlockHasCopyDispose = true;
 
   // if not, allocate one now.
   offset = getBlockOffset(E);
@@ -680,7 +682,8 @@
   // Allocate all BlockDeclRefDecls, so we can calculate the right ParmTy below.
   AllocateAllBlockDeclRefs(Info, this);
 
-  QualType ParmTy = getContext().getBlockParmType(BlockDeclRefDecls);
+  QualType ParmTy = getContext().getBlockParmType(BlockHasCopyDispose,
+                                                  BlockDeclRefDecls);
   // FIXME: This leaks
   ImplicitParamDecl *SelfDecl =
     ImplicitParamDecl::Create(getContext(), 0,
diff --git a/lib/Frontend/PCHReader.cpp b/lib/Frontend/PCHReader.cpp
index 82e5033..c02959f 100644
--- a/lib/Frontend/PCHReader.cpp
+++ b/lib/Frontend/PCHReader.cpp
@@ -1591,6 +1591,9 @@
     Context->ObjCClassRedefinitionType = GetType(ObjCClassRedef);
   if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_BLOCK_DESCRIPTOR])
     Context->setBlockDescriptorType(GetType(String));
+  if (unsigned String
+      = SpecialTypes[pch::SPECIAL_TYPE_BLOCK_EXTENDED_DESCRIPTOR])
+    Context->setBlockDescriptorExtendedType(GetType(String));
 }
 
 /// \brief Retrieve the name of the original source file name
diff --git a/lib/Frontend/PCHWriter.cpp b/lib/Frontend/PCHWriter.cpp
index c253047..c98080a 100644
--- a/lib/Frontend/PCHWriter.cpp
+++ b/lib/Frontend/PCHWriter.cpp
@@ -1988,6 +1988,7 @@
   AddTypeRef(Context.ObjCIdRedefinitionType, Record);
   AddTypeRef(Context.ObjCClassRedefinitionType, Record);
   AddTypeRef(Context.getRawBlockdescriptorType(), Record);
+  AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record);
   Stream.EmitRecord(pch::SPECIAL_TYPES, Record);
 
   // Keep writing types and declarations until all types and