From 64176c2c9751997bfbc0e3a7c070f5aaa767be25 Mon Sep 17 00:00:00 2001 From: Fariborz Jahanian Date: Fri, 4 Jun 2010 16:10:00 +0000 Subject: [PATCH] For C++ copied in objects, use copy constructors in setting up block's descriptor. This is on going work to support c++ specific issues in setting up blocks various APIs. llvm-svn: 105469 --- clang/lib/CodeGen/CGBlocks.cpp | 35 +++++++++++++++---- clang/lib/CodeGen/CGBlocks.h | 3 ++ .../test/CodeGenCXX/copy-in-cplus-object.cpp | 18 ++++++++++ 3 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 clang/test/CodeGenCXX/copy-in-cplus-object.cpp diff --git a/clang/lib/CodeGen/CGBlocks.cpp b/clang/lib/CodeGen/CGBlocks.cpp index 850c77c4b7d3..5a3e1002c62e 100644 --- a/clang/lib/CodeGen/CGBlocks.cpp +++ b/clang/lib/CodeGen/CGBlocks.cpp @@ -364,11 +364,28 @@ llvm::Value *CodeGenFunction::BuildBlockLiteralTmp(const BlockExpr *BE) { E = new (getContext()) DeclRefExpr(const_cast(VD), VD->getType().getNonReferenceType(), SourceLocation()); - if (VD->getType()->isReferenceType()) - E = new (getContext()) - UnaryOperator(const_cast(E), UnaryOperator::AddrOf, - getContext().getPointerType(E->getType()), - SourceLocation()); + if (getContext().getLangOptions().CPlusPlus) { + if (VD->getType()->isReferenceType()) { + E = new (getContext()) + UnaryOperator(const_cast(E), UnaryOperator::AddrOf, + getContext().getPointerType(E->getType()), + SourceLocation()); + } + else { + QualType T = E->getType(); + if (const RecordType *RT = T->getAs()) { + CXXRecordDecl *Record = cast(RT->getDecl()); + if (!Record->hasTrivialCopyConstructor()) { + CXXConstructorDecl *D = Record->getCopyConstructor(getContext(), + 0); + Expr *Arg = const_cast(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; } diff --git a/clang/lib/CodeGen/CGBlocks.h b/clang/lib/CodeGen/CGBlocks.h index e9b2bd549af5..4d92201d0d91 100644 --- a/clang/lib/CodeGen/CGBlocks.h +++ b/clang/lib/CodeGen/CGBlocks.h @@ -180,6 +180,9 @@ public: /// BlockDecls - Offsets for all Decls in BlockDeclRefExprs. llvm::DenseMap BlockDecls; + + /// BlockDeclsValue - llvm::Value for all Decls in BlockDeclRefExprs. + llvm::DenseMap BlockDeclsValue; /// BlockCXXThisOffset - The offset of the C++ 'this' value within /// the block structure. diff --git a/clang/test/CodeGenCXX/copy-in-cplus-object.cpp b/clang/test/CodeGenCXX/copy-in-cplus-object.cpp new file mode 100644 index 000000000000..6dcb1285faff --- /dev/null +++ b/clang/test/CodeGenCXX/copy-in-cplus-object.cpp @@ -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_ +