]> granicus.if.org Git - clang/commitdiff
Factor emitting a call to a copy constructor out into a separate function.
authorAnders Carlsson <andersca@mac.com>
Tue, 30 Mar 2010 03:27:09 +0000 (03:27 +0000)
committerAnders Carlsson <andersca@mac.com>
Tue, 30 Mar 2010 03:27:09 +0000 (03:27 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@99866 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGClass.cpp
lib/CodeGen/CodeGenFunction.h

index 0cf1ac45b8e1c3e9a72b629e772a4d0005673e20..93f5a7bc438dfabd4d2a0a5b0312da6cc087b557 100644 (file)
@@ -308,6 +308,53 @@ CodeGenFunction::GetAddressOfDerivedClass(llvm::Value *Value,
   return Value;
 }
 
+/// EmitCopyCtorCall - Emit a call to a copy constructor.
+static void
+EmitCopyCtorCall(CodeGenFunction &CGF,
+                 const CXXConstructorDecl *CopyCtor, CXXCtorType CopyCtorType,
+                 llvm::Value *ThisPtr, llvm::Value *VTT, llvm::Value *Src) {
+  llvm::Value *Callee = CGF.CGM.GetAddrOfCXXConstructor(CopyCtor, CopyCtorType);
+
+  CallArgList CallArgs;
+
+  // Push the this ptr.
+  CallArgs.push_back(std::make_pair(RValue::get(ThisPtr),
+                                    CopyCtor->getThisType(CGF.getContext())));
+  
+  // Push the VTT parameter if necessary.
+  if (VTT) {
+    QualType T = CGF.getContext().getPointerType(CGF.getContext().VoidPtrTy);
+    CallArgs.push_back(std::make_pair(RValue::get(VTT), T));
+  }
+  // Push the Src ptr.
+  CallArgs.push_back(std::make_pair(RValue::get(Src),
+                                    CopyCtor->getParamDecl(0)->getType()));
+
+
+  {
+    CodeGenFunction::CXXTemporariesCleanupScope Scope(CGF);
+
+    // If the copy constructor has default arguments, emit them.
+    for (unsigned I = 1, E = CopyCtor->getNumParams(); I < E; ++I) {
+      const ParmVarDecl *Param = CopyCtor->getParamDecl(I);
+      const Expr *DefaultArgExpr = Param->getDefaultArg();
+
+      assert(DefaultArgExpr && "Ctor parameter must have default arg!");
+
+      QualType ArgType = Param->getType();
+      CallArgs.push_back(std::make_pair(CGF.EmitCallArg(DefaultArgExpr, 
+                                                        ArgType),
+                                        ArgType));
+    }
+
+    const FunctionProtoType *FPT =
+      CopyCtor->getType()->getAs<FunctionProtoType>();
+    CGF.EmitCall(CGF.CGM.getTypes().getFunctionInfo(CallArgs, FPT),
+                 Callee, ReturnValueSlot(), CallArgs, CopyCtor);
+  }
+}
+                             
 /// EmitClassAggrMemberwiseCopy - This routine generates code to copy a class
 /// array of objects from SrcValue to DestValue. Copying can be either a bitwise
 /// copy or via a copy constructor call.
@@ -531,47 +578,13 @@ void CodeGenFunction::EmitClassMemberwiseCopy(
     return;
   }
 
-  if (CXXConstructorDecl *BaseCopyCtor =
-      BaseClassDecl->getCopyConstructor(getContext(), 0)) {
-    llvm::Value *Callee = CGM.GetAddrOfCXXConstructor(BaseCopyCtor, CtorType);
-    CallArgList CallArgs;
-    // Push the this (Dest) ptr.
-    CallArgs.push_back(std::make_pair(RValue::get(Dest),
-                                      BaseCopyCtor->getThisType(getContext())));
-
-    // Push the VTT parameter, if necessary.
-    if (llvm::Value *VTT = 
-          GetVTTParameter(*this, GlobalDecl(BaseCopyCtor, CtorType))) {
-      QualType T = getContext().getPointerType(getContext().VoidPtrTy);
-      CallArgs.push_back(std::make_pair(RValue::get(VTT), T));
-    }
-
-    // Push the Src ptr.
-    CallArgs.push_back(std::make_pair(RValue::get(Src),
-                       BaseCopyCtor->getParamDecl(0)->getType()));
-
-    {
-      CXXTemporariesCleanupScope Scope(*this);
-
-      // If the copy constructor has default arguments, emit them.
-      for (unsigned I = 1, E = BaseCopyCtor->getNumParams(); I < E; ++I) {
-        const ParmVarDecl *Param = BaseCopyCtor->getParamDecl(I);
-        const Expr *DefaultArgExpr = Param->getDefaultArg();
-
-        assert(DefaultArgExpr && "Ctor parameter must have default arg!");
-
-        QualType ArgType = Param->getType();
-        CallArgs.push_back(std::make_pair(EmitCallArg(DefaultArgExpr, ArgType),
-                                          ArgType));
-
-      }
+  CXXConstructorDecl *BaseCopyCtor =
+    BaseClassDecl->getCopyConstructor(getContext(), 0);
+  if (!BaseCopyCtor)
+    return;
 
-      const FunctionProtoType *FPT =
-        BaseCopyCtor->getType()->getAs<FunctionProtoType>();
-      EmitCall(CGM.getTypes().getFunctionInfo(CallArgs, FPT),
-               Callee, ReturnValueSlot(), CallArgs, BaseCopyCtor);
-    }
-  }
+  llvm::Value *VTT = GetVTTParameter(*this, GlobalDecl(BaseCopyCtor, CtorType));
+  EmitCopyCtorCall(*this, BaseCopyCtor, CtorType, Dest, VTT, Src);
 }
 
 /// EmitClassCopyAssignment - This routine generates code to copy assign a class
index 373c46be96e22011a7a95810cc0ce1fa3c5580d4..ccbbf849838bc607489ff9f8a3ebc388c232ecb5 100644 (file)
@@ -1292,6 +1292,10 @@ public:
   /// getTrapBB - Create a basic block that will call the trap intrinsic.  We'll
   /// generate a branch around the created basic block as necessary.
   llvm::BasicBlock* getTrapBB();
+  
+  /// EmitCallArg - Emit a single call argument.
+  RValue EmitCallArg(const Expr *E, QualType ArgType);
+
 private:
 
   void EmitReturnOfRValue(RValue RV, QualType Ty);
@@ -1323,9 +1327,6 @@ private:
   /// current cleanup scope.
   void AddBranchFixup(llvm::BranchInst *BI);
 
-  /// EmitCallArg - Emit a single call argument.
-  RValue EmitCallArg(const Expr *E, QualType ArgType);
-
   /// EmitCallArgs - Emit call arguments for a function.
   /// The CallArgTypeInfo parameter is used for iterating over the known
   /// argument types of the function being called.