Hide FunctionTemplateDecl's specializations folding set as implementation detail and introduce
FunctionTemplateDecl::findSpecialization.

Redeclarations of specializations will not cause the previous decl to be removed from the set,
the set will keep the canonical decl. findSpecialization will return the most recent redeclaration.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@108834 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Frontend/PCHReaderDecl.cpp b/lib/Frontend/PCHReaderDecl.cpp
index 5b41280..e021d3c 100644
--- a/lib/Frontend/PCHReaderDecl.cpp
+++ b/lib/Frontend/PCHReaderDecl.cpp
@@ -254,11 +254,12 @@
     
     SourceLocation POI = Reader.ReadSourceLocation(Record, Idx);
 
-    FD->setFunctionTemplateSpecialization(Template, TemplArgs.size(),
-                                          TemplArgs.data(), TSK,
-                                          TemplArgLocs.size(),
-                                          TemplArgLocs.data(),
-                                          LAngleLoc, RAngleLoc, POI);
+    if (FD->isCanonicalDecl()) // if canonical add to template's set.
+      FD->setFunctionTemplateSpecialization(Template, TemplArgs.size(),
+                                            TemplArgs.data(), TSK,
+                                            TemplArgLocs.size(),
+                                            TemplArgLocs.data(),
+                                            LAngleLoc, RAngleLoc, POI);
     break;
   }
   case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
diff --git a/lib/Frontend/PCHWriterDecl.cpp b/lib/Frontend/PCHWriterDecl.cpp
index 5d6a6d2..eb7c52a 100644
--- a/lib/Frontend/PCHWriterDecl.cpp
+++ b/lib/Frontend/PCHWriterDecl.cpp
@@ -929,8 +929,11 @@
     Record.push_back(D->getSpecializations().size());
     for (llvm::FoldingSet<FunctionTemplateSpecializationInfo>::iterator
            I = D->getSpecializations().begin(),
-           E = D->getSpecializations().end()   ; I != E; ++I)
+           E = D->getSpecializations().end()   ; I != E; ++I) {
+      assert(I->Function->isCanonicalDecl() &&
+             "Expected only canonical decls in set");
       Writer.AddDeclRef(I->Function, Record);
+    }
 
     Writer.AddDeclRef(D->getInstantiatedFromMemberTemplate(), Record);
     if (D->getInstantiatedFromMemberTemplate())