[cfe-commits] r97210 - in /cfe/trunk: lib/Sema/SemaTemplate.cpp test/CXX/temp/temp.spec/temp.expl.spec/p6.cpp

Douglas Gregor dgregor at apple.com
Thu Feb 25 22:03:23 PST 2010


Author: dgregor
Date: Fri Feb 26 00:03:23 2010
New Revision: 97210

URL: http://llvm.org/viewvc/llvm-project?rev=97210&view=rev
Log:
An explicit specialization is allowed following an explicit
instantiation so long as that explicit specialization was declared
previously. Fixes PR6160.

Modified:
    cfe/trunk/lib/Sema/SemaTemplate.cpp
    cfe/trunk/test/CXX/temp/temp.spec/temp.expl.spec/p6.cpp

Modified: cfe/trunk/lib/Sema/SemaTemplate.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaTemplate.cpp?rev=97210&r1=97209&r2=97210&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaTemplate.cpp (original)
+++ cfe/trunk/lib/Sema/SemaTemplate.cpp Fri Feb 26 00:03:23 2010
@@ -3242,6 +3242,23 @@
   return false;
 }
 
+/// \brief Retrieve the previous declaration of the given declaration.
+static NamedDecl *getPreviousDecl(NamedDecl *ND) {
+  if (VarDecl *VD = dyn_cast<VarDecl>(ND))
+    return VD->getPreviousDeclaration();
+  if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND))
+    return FD->getPreviousDeclaration();
+  if (TagDecl *TD = dyn_cast<TagDecl>(ND))
+    return TD->getPreviousDeclaration();
+  if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND))
+    return TD->getPreviousDeclaration();
+  if (FunctionTemplateDecl *FTD = dyn_cast<FunctionTemplateDecl>(ND))
+    return FTD->getPreviousDeclaration();
+  if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(ND))
+    return CTD->getPreviousDeclaration();
+  return 0;
+}
+
 Sema::DeclResult
 Sema::ActOnClassTemplateSpecialization(Scope *S, unsigned TagSpec,
                                        TagUseKind TUK,
@@ -3547,15 +3564,26 @@
   //   instantiation to take place, in every translation unit in which such a 
   //   use occurs; no diagnostic is required.
   if (PrevDecl && PrevDecl->getPointOfInstantiation().isValid()) {
-    SourceRange Range(TemplateNameLoc, RAngleLoc);
-    Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
-      << Context.getTypeDeclType(Specialization) << Range;
-
-    Diag(PrevDecl->getPointOfInstantiation(), 
-         diag::note_instantiation_required_here)
-      << (PrevDecl->getTemplateSpecializationKind() 
+    bool Okay = false;
+    for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
+      // Is there any previous explicit specialization declaration?
+      if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization) {
+        Okay = true;
+        break;
+      }
+    }
+
+    if (!Okay) {
+      SourceRange Range(TemplateNameLoc, RAngleLoc);
+      Diag(TemplateNameLoc, diag::err_specialization_after_instantiation)
+        << Context.getTypeDeclType(Specialization) << Range;
+
+      Diag(PrevDecl->getPointOfInstantiation(), 
+           diag::note_instantiation_required_here)
+        << (PrevDecl->getTemplateSpecializationKind() 
                                                 != TSK_ImplicitInstantiation);
-    return true;
+      return true;
+    }
   }
   
   // If this is not a friend, note that this is an explicit specialization.
@@ -3728,6 +3756,12 @@
       //   before the first use of that specialization that would cause an 
       //   implicit instantiation to take place, in every translation unit in
       //   which such a use occurs; no diagnostic is required.
+      for (NamedDecl *Prev = PrevDecl; Prev; Prev = getPreviousDecl(Prev)) {
+        // Is there any previous explicit specialization declaration?
+        if (getTemplateSpecializationKind(Prev) == TSK_ExplicitSpecialization)
+          return false;
+      }
+
       Diag(NewLoc, diag::err_specialization_after_instantiation)
         << PrevDecl;
       Diag(PrevPointOfInstantiation, diag::note_instantiation_required_here)

Modified: cfe/trunk/test/CXX/temp/temp.spec/temp.expl.spec/p6.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CXX/temp/temp.spec/temp.expl.spec/p6.cpp?rev=97210&r1=97209&r2=97210&view=diff
==============================================================================
--- cfe/trunk/test/CXX/temp/temp.spec/temp.expl.spec/p6.cpp (original)
+++ cfe/trunk/test/CXX/temp/temp.spec/temp.expl.spec/p6.cpp Fri Feb 26 00:03:23 2010
@@ -54,3 +54,10 @@
 
 template<> void sort<String>(Array<String>& v); // // expected-error{{after instantiation}}
 template<> void sort<>(Array<char*>& v);	// OK: sort<char*> not yet used
+
+namespace PR6160 {
+  template<typename T> void f(T);
+  template<> void f(int);
+  extern template void f(int);
+  template<> void f(int) { }
+}





More information about the cfe-commits mailing list