]> granicus.if.org Git - clang/commitdiff
Patch for synthesizing copy assignment operator.
authorFariborz Jahanian <fjahanian@apple.com>
Wed, 12 Aug 2009 21:14:35 +0000 (21:14 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Wed, 12 Aug 2009 21:14:35 +0000 (21:14 +0000)
WIP.

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

include/clang/AST/Decl.h
lib/CodeGen/CGCXX.cpp
lib/CodeGen/CodeGenFunction.cpp
lib/CodeGen/CodeGenFunction.h
lib/CodeGen/CodeGenModule.cpp
lib/Sema/SemaDeclCXX.cpp

index 9ce589ba80582311ef490b42411de519d4ceabd8..697c561cb791fecadf527b488292bb9ddb4bed0f 100644 (file)
@@ -682,6 +682,7 @@ private:
   bool HasWrittenPrototype : 1;
   bool IsDeleted : 1;
   bool IsTrivial : 1; // sunk from CXXMethodDecl
+  bool IsCopyAssignment : 1;  // sunk from CXXMethodDecl
   bool HasImplicitReturnZero : 1;
 
   // Move to DeclGroup when it is implemented.
@@ -723,6 +724,7 @@ protected:
       SClass(S), IsInline(isInline), C99InlineDefinition(false), 
       IsVirtualAsWritten(false), IsPure(false), HasInheritedPrototype(false), 
       HasWrittenPrototype(true), IsDeleted(false), IsTrivial(false),
+      IsCopyAssignment(false),
       HasImplicitReturnZero(false),
       TypeSpecStartLoc(TSSL), EndRangeLoc(L), TemplateOrSpecialization() {}
 
@@ -799,6 +801,9 @@ public:
   /// the class has been fully built by Sema.
   bool isTrivial() const { return IsTrivial; }
   void setTrivial(bool IT) { IsTrivial = IT; }
+                       
+  bool isCopyAssignment() const { return IsCopyAssignment; }
+  void setCopyAssignment(bool CA) { IsCopyAssignment = CA; }
 
   /// Whether falling off this function implicitly returns null/zero.
   /// If a more specific implicit return value is required, front-ends
index f161f9f68eb4e2b9349feb303d01edb5525045d9..b65c6b32db75c9146374fda56042bdf9a0432992 100644 (file)
@@ -902,6 +902,32 @@ void CodeGenFunction::SynthesizeCXXCopyConstructor(const CXXConstructorDecl *CD,
   FinishFunction();
 }  
 
+/// SynthesizeCXXCopyAssignment - Implicitly define copy assignment operator.
+/// Before the implicitly-declared copy assignment operator for a class is 
+/// implicitly defined, all implicitly- declared copy assignment operators for 
+/// its direct base classes and its nonstatic data members shall have been 
+/// implicitly defined. [12.8-p12]
+/// The implicitly-defined copy assignment operator for class X performs 
+/// memberwise assignment of its subob- jects. The direct base classes of X are 
+/// assigned first, in the order of their declaration in 
+/// the base-specifier-list, and then the immediate nonstatic data members of X 
+/// are assigned, in the order in which they were declared in the class 
+/// definition.Each subobject is assigned in the manner appropriate to its type:
+/// — if the subobject is of class type, the copy assignment operator for the 
+///   class is used (as if by explicit qual- ification; that is, ignoring any 
+///   possible virtual overriding functions in more derived classes);
+/// — if the subobject is an array, each element is assigned, in the manner 
+///   appropriate to the element type;
+/// — if the subobject is of scalar type, the built-in assignment operator is 
+///   used.
+void CodeGenFunction::SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
+                                                  const FunctionDecl *FD,
+                                                  llvm::Function *Fn,
+                                                  const FunctionArgList &Args) {
+  StartFunction(FD, FD->getResultType(), Fn, Args, SourceLocation());
+  
+  FinishFunction();
+}  
 
 /// EmitCtorPrologue - This routine generates necessary code to initialize
 /// base classes and non-static data members belonging to this constructor.
index 309c76bea5ce0399fd9bb5f55d0fcb17639c4cc9..b7ae7c1f0d29f1471f5fad40b6d4c3d10e767a76 100644 (file)
@@ -254,6 +254,9 @@ void CodeGenFunction::GenerateCode(const FunctionDecl *FD,
         SynthesizeDefaultConstructor(CD, FD, Fn, Args);
       }
     }
+  else if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
+         if (MD->isCopyAssignment())
+           SynthesizeCXXCopyAssignment(MD, FD, Fn, Args);
     
   // Destroy the 'this' declaration.
   if (CXXThisDecl)
index ece4f105dc20f59441912ad7857f69f9bc4b7b7b..226c272c318bbc6ea7b192ef53c8ed9074e1fbe3 100644 (file)
@@ -374,6 +374,12 @@ public:
                                     const FunctionDecl *FD,
                                     llvm::Function *Fn,
                                     const FunctionArgList &Args);
+  
+  void SynthesizeCXXCopyAssignment(const CXXMethodDecl *CD,
+                                   const FunctionDecl *FD,
+                                   llvm::Function *Fn,
+                                   const FunctionArgList &Args);
+  
   void SynthesizeDefaultConstructor(const CXXConstructorDecl *CD,
                                     const FunctionDecl *FD,
                                     llvm::Function *Fn,
index a642de9a464ea8af02a8941719854d045ecbdc85..22737a4d933309754ec19182a059bf01cd8b175f 100644 (file)
@@ -653,6 +653,11 @@ llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(const char *MangledName,
       else if (!ClassDecl->hasUserDeclaredConstructor())
         DeferredDeclsToEmit.push_back(D);
     }
+    else 
+      if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD))
+        if (MD->isCopyAssignment()) {
+          DeferredDeclsToEmit.push_back(D);
+        }
   }
   
   // This function doesn't have a complete type (for example, the return
index 0affe512b3e365db6ad3ff0d747d22d97f0c5686..f00b3157f9a385b73a430558c8a60f8bba9df17e 100644 (file)
@@ -1441,6 +1441,7 @@ void Sema::AddImplicitlyDeclaredMembersToClass(CXXRecordDecl *ClassDecl) {
     CopyAssignment->setAccess(AS_public);
     CopyAssignment->setImplicit();
     CopyAssignment->setTrivial(ClassDecl->hasTrivialCopyAssignment());
+    CopyAssignment->setCopyAssignment(true);
 
     // Add the parameter to the operator.
     ParmVarDecl *FromParam = ParmVarDecl::Create(Context, CopyAssignment,