]> granicus.if.org Git - clang/commitdiff
Hide FunctionTemplateDecl's specializations folding set as implementation detail...
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Tue, 20 Jul 2010 13:59:58 +0000 (13:59 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Tue, 20 Jul 2010 13:59:58 +0000 (13:59 +0000)
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

include/clang/AST/DeclTemplate.h
lib/AST/Decl.cpp
lib/AST/DeclTemplate.cpp
lib/Frontend/PCHReaderDecl.cpp
lib/Frontend/PCHWriterDecl.cpp
lib/Sema/SemaTemplateInstantiateDecl.cpp

index d2506e97a84d24ab470a93c841edc12501012ce5..f6421736cd92c4f5f616188f1cd8d1c8d9de7675 100644 (file)
@@ -522,6 +522,20 @@ protected:
   /// may implicitly allocate memory for the common pointer.
   Common *getCommonPtr();
 
+  /// \brief Retrieve the set of function template specializations of this
+  /// function template.
+  llvm::FoldingSet<FunctionTemplateSpecializationInfo> &getSpecializations() {
+    return getCommonPtr()->Specializations;
+  }
+  
+  friend void FunctionDecl::setFunctionTemplateSpecialization(
+                                       FunctionTemplateDecl *Template,
+                                       const TemplateArgumentList *TemplateArgs,
+                                       void *InsertPos,
+                                       TemplateSpecializationKind TSK,
+                          const TemplateArgumentListInfo *TemplateArgsAsWritten,
+                                       SourceLocation PointOfInstantiation);
+
   FunctionTemplateDecl(DeclContext *DC, SourceLocation L, DeclarationName Name,
                        TemplateParameterList *Params, NamedDecl *Decl)
     : TemplateDecl(FunctionTemplate, DC, L, Name, Params, Decl),
@@ -535,11 +549,10 @@ public:
     return static_cast<FunctionDecl*>(TemplatedDecl);
   }
 
-  /// \brief Retrieve the set of function template specializations of this
-  /// function template.
-  llvm::FoldingSet<FunctionTemplateSpecializationInfo> &getSpecializations() {
-    return getCommonPtr()->Specializations;
-  }
+  /// \brief Return the specialization with the provided arguments if it exists,
+  /// otherwise return the insertion point.
+  FunctionDecl *findSpecialization(const TemplateArgument *Args,
+                                   unsigned NumArgs, void *&InsertPos);
 
   /// \brief Retrieve the previous declaration of this function template, or
   /// NULL if no such declaration exists.
index 149938fc5c861bab30e80b9146f6d7c974d22218..e695310c3734ae025ab147c3ba8024ae5018d863 100644 (file)
@@ -1400,14 +1400,15 @@ FunctionDecl::setFunctionTemplateSpecialization(FunctionTemplateDecl *Template,
   if (InsertPos)
     Template->getSpecializations().InsertNode(Info, InsertPos);
   else {
-    // Try to insert the new node. If there is an existing node, remove it 
-    // first.
+    // Try to insert the new node. If there is an existing node, leave it, the
+    // set will contain the canonical decls while
+    // FunctionTemplateDecl::findSpecialization will return
+    // the most recent redeclarations.
     FunctionTemplateSpecializationInfo *Existing
       = Template->getSpecializations().GetOrInsertNode(Info);
-    if (Existing) {
-      Template->getSpecializations().RemoveNode(Existing);
-      Template->getSpecializations().GetOrInsertNode(Info);
-    }
+    (void)Existing;
+    assert((!Existing || Existing->Function->isCanonicalDecl()) &&
+           "Set is supposed to only contain canonical decls");
   }
 }
 
index a75c1c0d582689a67d4bdffc61294db74711d15f..f308d71d5f695760e5db93a62fefe9d3d91a4238 100644 (file)
@@ -119,6 +119,16 @@ void FunctionTemplateDecl::Destroy(ASTContext &C) {
   Decl::Destroy(C);
 }
 
+FunctionDecl *
+FunctionTemplateDecl::findSpecialization(const TemplateArgument *Args,
+                                         unsigned NumArgs, void *&InsertPos) {
+  llvm::FoldingSetNodeID ID;
+  FunctionTemplateSpecializationInfo::Profile(ID,Args,NumArgs, getASTContext());
+  FunctionTemplateSpecializationInfo *Info
+      = getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
+  return Info ? Info->Function->getMostRecentDeclaration() : 0;
+}
+
 FunctionTemplateDecl *FunctionTemplateDecl::getCanonicalDecl() {
   FunctionTemplateDecl *FunTmpl = this;
   while (FunTmpl->getPreviousDeclaration())
index 5b41280138fb638bcee7e96bbf1f518b95a09032..e021d3cabab62b44d63247614093b5b659f0838c 100644 (file)
@@ -254,11 +254,12 @@ void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
     
     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: {
index 5d6a6d267e95172fabd65a19d7d0c46878107cab..eb7c52ae95b3aa77a82ef0cb0e62f4c8ae54a8c6 100644 (file)
@@ -929,8 +929,11 @@ void PCHDeclWriter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
     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())
index 7e0617568bf7b1e4889eca8971b1aeffb0c17bac..4209fd7a14b2220e63b68738a30747c84a5adfb1 100644 (file)
@@ -968,20 +968,16 @@ Decl *TemplateDeclInstantiator::VisitFunctionDecl(FunctionDecl *D,
   FunctionTemplateDecl *FunctionTemplate = D->getDescribedFunctionTemplate();
   void *InsertPos = 0;
   if (FunctionTemplate && !TemplateParams) {
-    llvm::FoldingSetNodeID ID;
     std::pair<const TemplateArgument *, unsigned> Innermost 
       = TemplateArgs.getInnermost();
-    FunctionTemplateSpecializationInfo::Profile(ID, Innermost.first,
-                                                Innermost.second,
-                                                SemaRef.Context);
 
-    FunctionTemplateSpecializationInfo *Info
-      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
-                                                                   InsertPos);
+    FunctionDecl *SpecFunc
+      = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
+                                             InsertPos);
 
     // If we already have a function template specialization, return it.
-    if (Info)
-      return Info->Function;
+    if (SpecFunc)
+      return SpecFunc;
   }
 
   bool isFriend;
@@ -1222,20 +1218,16 @@ TemplateDeclInstantiator::VisitCXXMethodDecl(CXXMethodDecl *D,
     // We are creating a function template specialization from a function
     // template. Check whether there is already a function template
     // specialization for this particular set of template arguments.
-    llvm::FoldingSetNodeID ID;
     std::pair<const TemplateArgument *, unsigned> Innermost 
       = TemplateArgs.getInnermost();
-    FunctionTemplateSpecializationInfo::Profile(ID, Innermost.first,
-                                                Innermost.second,
-                                                SemaRef.Context);
 
-    FunctionTemplateSpecializationInfo *Info
-      = FunctionTemplate->getSpecializations().FindNodeOrInsertPos(ID,
-                                                                   InsertPos);
+    FunctionDecl *SpecFunc
+      = FunctionTemplate->findSpecialization(Innermost.first, Innermost.second,
+                                             InsertPos);
 
     // If we already have a function template specialization, return it.
-    if (Info)
-      return Info->Function;
+    if (SpecFunc)
+      return SpecFunc;
   }
 
   bool isFriend;