Clean up the handling of the DeclaredDefaultConstructor and
DeclaredCopyConstructor bits in CXXRecordDecl's DefinitionData
structure. Rather than having Sema call addedConstructor or set the
bits directly at semi-random places, move all of the logic for
managing these bits into CXXRecordDecl itself and tie the
addedConstructor call into DeclContext::addDecl().

This makes it easier for AST-building clients to get the right bits
set in DefinitionData, and is one small part of <rdar://problem/8459981>.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@114889 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/AST/DeclBase.cpp b/lib/AST/DeclBase.cpp
index f629722..207edde 100644
--- a/lib/AST/DeclBase.cpp
+++ b/lib/AST/DeclBase.cpp
@@ -778,6 +778,14 @@
   } else {
     FirstDecl = LastDecl = D;
   }
+  
+  if (CXXRecordDecl *Record = dyn_cast<CXXRecordDecl>(this)) {
+    Decl *InnerD = D;
+    if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D))
+      InnerD = FunTmpl->getTemplatedDecl();
+    if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(InnerD))
+      Record->addedConstructor(Constructor);
+  }
 }
 
 void DeclContext::addDecl(Decl *D) {
diff --git a/lib/AST/DeclCXX.cpp b/lib/AST/DeclCXX.cpp
index 88301c2..ffdb98d 100644
--- a/lib/AST/DeclCXX.cpp
+++ b/lib/AST/DeclCXX.cpp
@@ -259,9 +259,25 @@
 }
 
 void
-CXXRecordDecl::addedConstructor(ASTContext &Context,
-                                CXXConstructorDecl *ConDecl) {
-  assert(!ConDecl->isImplicit() && "addedConstructor - not for implicit decl");
+CXXRecordDecl::addedConstructor(CXXConstructorDecl *Constructor) {
+  // Ignore friends.
+  if (Constructor->getFriendObjectKind())
+    return;
+  
+  if (Constructor->isImplicit()) {
+    // If this is the implicit default constructor, note that we have now
+    // declared it.
+    if (Constructor->isDefaultConstructor())
+      data().DeclaredDefaultConstructor = true;
+    // If this is the implicit copy constructor, note that we have now
+    // declared it.
+    else if (Constructor->isCopyConstructor())
+      data().DeclaredCopyConstructor = true;
+    
+    // Nothing else to do for implicitly-declared constructors.
+    return;
+  }
+  
   // Note that we have a user-declared constructor.
   data().UserDeclaredConstructor = true;
 
@@ -285,7 +301,8 @@
 
   // Note when we have a user-declared copy constructor, which will
   // suppress the implicit declaration of a copy constructor.
-  if (ConDecl->isCopyConstructor()) {
+  if (!Constructor->getDescribedFunctionTemplate() &&
+      Constructor->isCopyConstructor()) {
     data().UserDeclaredCopyConstructor = true;
     data().DeclaredCopyConstructor = true;
     
@@ -293,7 +310,6 @@
     //   A copy constructor is trivial if it is implicitly declared.
     // FIXME: C++0x: don't do this for "= default" copy constructors.
     data().HasTrivialCopyConstructor = false;
-    
   }
 }
 
diff --git a/lib/Sema/SemaDeclCXX.cpp b/lib/Sema/SemaDeclCXX.cpp
index b171d85..025ac3b 100644
--- a/lib/Sema/SemaDeclCXX.cpp
+++ b/lib/Sema/SemaDeclCXX.cpp
@@ -2985,13 +2985,6 @@
       Constructor->setInvalidDecl();
     }
   }
-
-  // Notify the class that we've added a constructor.  In principle we
-  // don't need to do this for out-of-line declarations; in practice
-  // we only instantiate the most recent declaration of a method, so
-  // we have to call this for everything but friends.
-  if (!Constructor->getFriendObjectKind())
-    ClassDecl->addedConstructor(Context, Constructor);
 }
 
 /// CheckDestructor - Checks a fully-formed destructor definition for
@@ -4441,7 +4434,6 @@
   DefaultCon->setTrivial(ClassDecl->hasTrivialConstructor());
   
   // Note that we have declared this constructor.
-  ClassDecl->setDeclaredDefaultConstructor(true);
   ++ASTContext::NumImplicitDefaultConstructorsDeclared;
   
   if (Scope *S = getScopeForContext(ClassDecl))
@@ -5412,7 +5404,6 @@
   CopyConstructor->setTrivial(ClassDecl->hasTrivialCopyConstructor());
   
   // Note that we have declared this constructor.
-  ClassDecl->setDeclaredCopyConstructor(true);
   ++ASTContext::NumImplicitCopyConstructorsDeclared;
   
   // Add the parameter to the constructor.