Support friend function specializations.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@99389 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/SemaAccess.cpp b/lib/Sema/SemaAccess.cpp
index e74c8f6..5b1a9d8 100644
--- a/lib/Sema/SemaAccess.cpp
+++ b/lib/Sema/SemaAccess.cpp
@@ -53,10 +53,11 @@
 
 namespace {
 struct EffectiveContext {
-  EffectiveContext() : Function(0), Dependent(false) {}
+  EffectiveContext() : Inner(0), Function(0), Dependent(false) {}
 
-  explicit EffectiveContext(DeclContext *DC) {
-    Dependent = DC->isDependentContext();
+  explicit EffectiveContext(DeclContext *DC)
+    : Inner(DC),
+      Dependent(DC->isDependentContext()) {
 
     if (isa<FunctionDecl>(DC)) {
       Function = cast<FunctionDecl>(DC)->getCanonicalDecl();
@@ -86,14 +87,15 @@
              != Records.end();
   }
 
-  DeclContext *getPrimaryContext() const {
-    assert((Function || !Records.empty()) && "context has no primary context");
-    if (Function) return Function;
-    return Records[0];
+  /// Retrieves the innermost "useful" context.  Can be null if we're
+  /// doing access-control without privileges.
+  DeclContext *getInnerContext() const {
+    return Inner;
   }
 
   typedef llvm::SmallVectorImpl<CXXRecordDecl*>::const_iterator record_iterator;
 
+  DeclContext *Inner;
   llvm::SmallVector<CXXRecordDecl*, 4> Records;
   FunctionDecl *Function;
   bool Dependent;
@@ -636,7 +638,7 @@
                         SourceLocation Loc,
                         const Sema::AccessedEntity &Entity) {
   assert(EC.isDependent() && "delaying non-dependent access");
-  DeclContext *DC = EC.getPrimaryContext();
+  DeclContext *DC = EC.getInnerContext();
   assert(DC->isDependentContext() && "delaying non-dependent access");
   DependentDiagnostic::Create(S.Context, DC, DependentDiagnostic::Access,
                               Loc,
diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp
index bf92ef8..1435a8f 100644
--- a/lib/Sema/SemaDecl.cpp
+++ b/lib/Sema/SemaDecl.cpp
@@ -2918,6 +2918,13 @@
     } else {
       // This is a function template specialization.
       isFunctionTemplateSpecialization = true;
+
+      // C++0x [temp.expl.spec]p20 forbids "template<> void foo(int);".
+      if (isFriend && isFunctionTemplateSpecialization) {
+        SourceRange Range = TemplateParams->getSourceRange();
+        Diag(D.getIdentifierLoc(), diag::err_template_spec_decl_friend)
+          << Name << Range << CodeModificationHint::CreateRemoval(Range);
+      }
     }
 
     // FIXME: Free this memory properly.
@@ -3101,6 +3108,9 @@
                                    D.getDeclSpec().getSourceRange().getBegin(),
                                                  "template<> ");
       isFunctionTemplateSpecialization = true;
+    } else {
+      // "friend void foo<>(int);" is an implicit specialization decl.
+      isFunctionTemplateSpecialization = true;
     }
   }
 
diff --git a/lib/Sema/SemaTemplate.cpp b/lib/Sema/SemaTemplate.cpp
index abe9363..504b513 100644
--- a/lib/Sema/SemaTemplate.cpp
+++ b/lib/Sema/SemaTemplate.cpp
@@ -3961,9 +3961,14 @@
   
   // FIXME: Check if the prior specialization has a point of instantiation.
   // If so, we have run afoul of .
+
+  // If this is a friend declaration, then we're not really declaring
+  // an explicit specialization.
+  bool isFriend = (FD->getFriendObjectKind() != Decl::FOK_None);
   
   // Check the scope of this explicit specialization.
-  if (CheckTemplateSpecializationScope(*this, 
+  if (!isFriend &&
+      CheckTemplateSpecializationScope(*this, 
                                        Specialization->getPrimaryTemplate(),
                                        Specialization, FD->getLocation(), 
                                        false))
@@ -3980,7 +3985,8 @@
   assert(SpecInfo && "Function template specialization info missing?");
 
   bool SuppressNew = false;
-  if (CheckSpecializationInstantiationRedecl(FD->getLocation(),
+  if (!isFriend &&
+      CheckSpecializationInstantiationRedecl(FD->getLocation(),
                                              TSK_ExplicitSpecialization,
                                              Specialization,
                                    SpecInfo->getTemplateSpecializationKind(),
@@ -3990,7 +3996,8 @@
   
   // Mark the prior declaration as an explicit specialization, so that later
   // clients know that this is an explicit specialization.
-  SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
+  if (!isFriend)
+    SpecInfo->setTemplateSpecializationKind(TSK_ExplicitSpecialization);
   
   // Turn the given function declaration into a function template
   // specialization, with the template arguments from the previous
@@ -3999,7 +4006,7 @@
                          new (Context) TemplateArgumentList(
                              *Specialization->getTemplateSpecializationArgs()), 
                                         /*InsertPos=*/0, 
-                                        TSK_ExplicitSpecialization);
+                                    SpecInfo->getTemplateSpecializationKind());
   
   // The "previous declaration" for this function template specialization is
   // the prior function template specialization.