[cfe-commits] r78938 - in /cfe/trunk: lib/CodeGen/CGCXX.cpp lib/CodeGen/CodeGenModule.cpp lib/CodeGen/CodeGenModule.h lib/Sema/SemaDeclCXX.cpp test/CodeGenCXX/copy-assign-synthesis.cpp

Fariborz Jahanian fjahanian at apple.com
Thu Aug 13 14:09:41 PDT 2009


Author: fjahanian
Date: Thu Aug 13 16:09:41 2009
New Revision: 78938

URL: http://llvm.org/viewvc/llvm-project?rev=78938&view=rev
Log:
Patch to force synthesis of copy assignment operator
function in the order according to c++03. ir-gen 
for copy assignment in the trivial case and the first
test case.

Added:
    cfe/trunk/test/CodeGenCXX/copy-assign-synthesis.cpp
Modified:
    cfe/trunk/lib/CodeGen/CGCXX.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp
    cfe/trunk/lib/CodeGen/CodeGenModule.h
    cfe/trunk/lib/Sema/SemaDeclCXX.cpp

Modified: cfe/trunk/lib/CodeGen/CGCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CGCXX.cpp?rev=78938&r1=78937&r2=78938&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CGCXX.cpp (original)
+++ cfe/trunk/lib/CodeGen/CGCXX.cpp Thu Aug 13 16:09:41 2009
@@ -228,6 +228,18 @@
   assert(MD->isInstance() && 
          "Trying to emit a member call expr on a static method!");
   
+  if (MD->isCopyAssignment()) {
+    const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(MD->getDeclContext());
+    if (ClassDecl->hasTrivialCopyAssignment()) {
+      assert(!ClassDecl->hasUserDeclaredCopyAssignment() &&
+             "EmitCXXOperatorMemberCallExpr - user declared copy assignment");
+      llvm::Value *This = EmitLValue(E->getArg(0)).getAddress();
+      llvm::Value *Src = EmitLValue(E->getArg(1)).getAddress();
+      QualType Ty = E->getType();
+      EmitAggregateCopy(This, Src, Ty);
+      return RValue::get(This);
+    }
+  }
   
   const FunctionProtoType *FPT = MD->getType()->getAsFunctionProtoType();
   const llvm::Type *Ty = 

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=78938&r1=78937&r2=78938&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Thu Aug 13 16:09:41 2009
@@ -653,11 +653,9 @@
       else if (!ClassDecl->hasUserDeclaredConstructor())
         DeferredDeclsToEmit.push_back(D);
     }
-    else 
-      if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
-        if (MD->isCopyAssignment()) {
-          DeferredDeclsToEmit.push_back(D);
-        }
+    else if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
+           if (MD->isCopyAssignment())
+             DeferredCopyAssignmentToEmit(D);
   }
   
   // This function doesn't have a complete type (for example, the return
@@ -718,7 +716,46 @@
     }
   }
   DeferredDeclsToEmit.push_back(CopyCtorDecl);
+}
+
+/// Defer definition of copy assignments which need be implicitly defined.
+void CodeGenModule::DeferredCopyAssignmentToEmit(GlobalDecl CopyAssignDecl) {
+  const CXXMethodDecl *CD = cast<CXXMethodDecl>(CopyAssignDecl.getDecl());
+  const CXXRecordDecl *ClassDecl = cast<CXXRecordDecl>(CD->getDeclContext());
   
+  if (ClassDecl->hasTrivialCopyAssignment() ||
+      ClassDecl->hasUserDeclaredCopyAssignment())
+    return;
+  
+  // First make sure all direct base classes and virtual bases and non-static
+  // data mebers which need to have their copy assignments implicitly defined
+  // are defined. 12.8.p12
+  for (CXXRecordDecl::base_class_const_iterator Base = ClassDecl->bases_begin();
+       Base != ClassDecl->bases_end(); ++Base) {
+    CXXRecordDecl *BaseClassDecl
+      = cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
+    const CXXMethodDecl *MD = 0;
+    if (BaseClassDecl->hasConstCopyAssignment(getContext(), MD))
+      GetAddrOfFunction(GlobalDecl(MD), 0);
+  }
+  
+  for (CXXRecordDecl::field_iterator Field = ClassDecl->field_begin(),
+       FieldEnd = ClassDecl->field_end();
+       Field != FieldEnd; ++Field) {
+    QualType FieldType = Context.getCanonicalType((*Field)->getType());
+    if (const ArrayType *Array = Context.getAsArrayType(FieldType))
+      FieldType = Array->getElementType();
+    if (const RecordType *FieldClassType = FieldType->getAs<RecordType>()) {
+      if ((*Field)->isAnonymousStructOrUnion())
+        continue;
+      CXXRecordDecl *FieldClassDecl
+        = cast<CXXRecordDecl>(FieldClassType->getDecl());
+      const CXXMethodDecl *MD = 0;
+      if (FieldClassDecl->hasConstCopyAssignment(getContext(), MD))
+          GetAddrOfFunction(GlobalDecl(MD), 0);
+    }
+  }
+  DeferredDeclsToEmit.push_back(CopyAssignDecl);  
 }
 
 /// GetAddrOfFunction - Return the address of the given function.  If Ty is

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.h?rev=78938&r1=78937&r2=78938&view=diff

==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.h (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.h Thu Aug 13 16:09:41 2009
@@ -398,6 +398,7 @@
                                         const llvm::PointerType *PTy,
                                         const VarDecl *D);
   void DeferredCopyConstructorToEmit(GlobalDecl D);
+  void DeferredCopyAssignmentToEmit(GlobalDecl D);
   
   /// SetCommonAttributes - Set attributes which are common to any
   /// form of a global definition (alias, Objective-C method,

Modified: cfe/trunk/lib/Sema/SemaDeclCXX.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclCXX.cpp?rev=78938&r1=78937&r2=78938&view=diff

==============================================================================
--- cfe/trunk/lib/Sema/SemaDeclCXX.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDeclCXX.cpp Thu Aug 13 16:09:41 2009
@@ -3128,6 +3128,7 @@
     assert(isa<CXXMethodDecl>(FnDecl) &&
       "Overloaded = not member, but not filtered.");
     CXXMethodDecl *Method = cast<CXXMethodDecl>(FnDecl);
+    Method->setCopyAssignment(true);
     Method->getParent()->addedAssignmentOperator(Context, Method);
   }
 

Added: cfe/trunk/test/CodeGenCXX/copy-assign-synthesis.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/copy-assign-synthesis.cpp?rev=78938&view=auto

==============================================================================
--- cfe/trunk/test/CodeGenCXX/copy-assign-synthesis.cpp (added)
+++ cfe/trunk/test/CodeGenCXX/copy-assign-synthesis.cpp Thu Aug 13 16:09:41 2009
@@ -0,0 +1,28 @@
+// RUN: clang-cc -emit-llvm -o %t %s &&
+// RUN: grep "_ZN1XaSERK1X" %t | count 0
+
+extern "C" int printf(...);
+
+struct X { 
+  X() : d(0.0), d1(1.1), d2(1.2), d3(1.3) {}
+  double d;
+  double d1;
+  double d2;
+  double d3;
+  void pr() {
+    printf("d = %f d1 = %f d2 = %f d3 = %f\n", d, d1,d2,d3);
+  }
+}; 
+
+
+X srcX; 
+X dstX; 
+X dstY; 
+
+int main() {
+  dstY = dstX = srcX;
+  srcX.pr();
+  dstX.pr();
+  dstY.pr();
+}
+





More information about the cfe-commits mailing list