]> granicus.if.org Git - clang/commitdiff
For C++ copied in objects, use copy constructors in
authorFariborz Jahanian <fjahanian@apple.com>
Fri, 4 Jun 2010 16:10:00 +0000 (16:10 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Fri, 4 Jun 2010 16:10:00 +0000 (16:10 +0000)
setting up block's descriptor. This is on going work to
support c++ specific issues in setting up blocks
various APIs.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@105469 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGBlocks.cpp
lib/CodeGen/CGBlocks.h
test/CodeGenCXX/copy-in-cplus-object.cpp [new file with mode: 0644]

index 850c77c4b7d365b1eef5af59311545e076ccd88b..5a3e1002c62e065a2d91b667699939cf6341dc31 100644 (file)
@@ -364,11 +364,28 @@ llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) {
           E = new (getContext()) DeclRefExpr(const_cast<ValueDecl*>(VD),
                                             VD->getType().getNonReferenceType(),
                                             SourceLocation());
-          if (VD->getType()->isReferenceType())
-            E = new (getContext())
-              UnaryOperator(const_cast<Expr*>(E), UnaryOperator::AddrOf,
-                          getContext().getPointerType(E->getType()),
-                          SourceLocation());
+          if (getContext().getLangOptions().CPlusPlus) {
+            if (VD->getType()->isReferenceType()) {
+              E = new (getContext())
+                UnaryOperator(const_cast<Expr*>(E), UnaryOperator::AddrOf,
+                              getContext().getPointerType(E->getType()),
+                              SourceLocation());
+            } 
+            else {
+              QualType T = E->getType();
+              if (const RecordType *RT = T->getAs<RecordType>()) {
+                CXXRecordDecl *Record = cast<CXXRecordDecl>(RT->getDecl());
+                if (!Record->hasTrivialCopyConstructor()) {
+                  CXXConstructorDecl *D = Record->getCopyConstructor(getContext(),
+                                                                     0);
+                  Expr *Arg = const_cast<Expr*>(E);
+                  E = CXXConstructExpr::Create(getContext(), T, D->getLocation(), 
+                                               D, false, &Arg, 1, false,
+                                               CXXConstructExpr::CK_Complete);
+                }
+              }
+            }
+          }
         }
       }
 
@@ -607,6 +624,10 @@ void CodeGenFunction::AllocateBlockDecl(const BlockDeclRefExpr *E) {
 
 llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const ValueDecl *VD,
                                                  bool IsByRef) {
+  llvm::Value *&VE = BlockDeclsValue[VD];
+  if (VE)
+    return VE;
+  
   CharUnits offset = BlockDecls[VD];
   assert(!offset.isZero() && "getting address of unallocated decl");
 
@@ -634,12 +655,12 @@ llvm::Value *CodeGenFunction::GetAddrOfBlockDecl(const ValueDecl *VD,
       V = Builder.CreateLoad(V);
   } else {
     const llvm::Type *Ty = CGM.getTypes().ConvertType(VD->getType());
-
     Ty = llvm::PointerType::get(Ty, 0);
     V = Builder.CreateBitCast(V, Ty);
     if (VD->getType()->isReferenceType())
-      V = Builder.CreateLoad(V, "tmp");
+      V = Builder.CreateLoad(V, "ref.tmp");
   }
+  VE = V;
   return V;
 }
 
index e9b2bd549af542dcb3628a2f98b5b83389ccae6e..4d92201d0d91e1efbf104df84d16534008d4d809 100644 (file)
@@ -180,6 +180,9 @@ public:
 
   /// BlockDecls - Offsets for all Decls in BlockDeclRefExprs.
   llvm::DenseMap<const Decl*, CharUnits> BlockDecls;
+  
+  /// BlockDeclsValue - llvm::Value for all Decls in BlockDeclRefExprs.
+  llvm::DenseMap<const Decl*, llvm::Value *> BlockDeclsValue;
 
   /// BlockCXXThisOffset - The offset of the C++ 'this' value within
   /// the block structure.
diff --git a/test/CodeGenCXX/copy-in-cplus-object.cpp b/test/CodeGenCXX/copy-in-cplus-object.cpp
new file mode 100644 (file)
index 0000000..6dcb128
--- /dev/null
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 %s -fblocks -triple x86_64-apple-darwin -emit-llvm -o - | FileCheck %s
+
+struct TestObject
+{
+       TestObject(const TestObject& inObj);
+       TestObject();
+       TestObject& operator=(const TestObject& inObj);
+       int version() const;
+
+};
+
+void testRoutine() {
+    TestObject one;
+    int (^V)() = ^{ return one.version(); };
+}
+
+// CHECK: call void @_ZN10TestObjectC1ERKS_
+