Template argument deduction for member pointers.

Also, introduced some of the framework for performing instantiation as
part of template argument deduction.



git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@73175 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Sema/Sema.h b/lib/Sema/Sema.h
index 12ebc4a..ac8997d 100644
--- a/lib/Sema/Sema.h
+++ b/lib/Sema/Sema.h
@@ -2056,7 +2056,16 @@
       /// parameter. The Entity is the template, and
       /// TemplateArgs/NumTemplateArguments provides the template
       /// arguments as specified.
-      DefaultTemplateArgumentInstantiation
+      /// FIXME: Use a TemplateArgumentList
+      DefaultTemplateArgumentInstantiation,
+
+      /// We are performing template argument deduction for a class
+      /// template partial specialization. The Entity is the class
+      /// template partial specialization, and
+      /// TemplateArgs/NumTemplateArgs provides the deduced template
+      /// arguments.
+      /// FIXME: Use a TemplateArgumentList
+      PartialSpecDeductionInstantiation
     } Kind;
 
     /// \brief The point of instantiation within the source code.
@@ -2090,6 +2099,7 @@
         return true;
 
       case DefaultTemplateArgumentInstantiation:
+      case PartialSpecDeductionInstantiation:
         return X.TemplateArgs == Y.TemplateArgs;
       }
 
@@ -2146,6 +2156,15 @@
                           unsigned NumTemplateArgs,
                           SourceRange InstantiationRange = SourceRange());
 
+    /// \brief Note that we are instantiating as part of template
+    /// argument deduction for a class template partial
+    /// specialization.
+    InstantiatingTemplate(Sema &SemaRef, SourceLocation PointOfInstantiation,
+                          ClassTemplatePartialSpecializationDecl *PartialSpec,
+                          const TemplateArgument *TemplateArgs,
+                          unsigned NumTemplateArgs,
+                          SourceRange InstantiationRange = SourceRange());
+
     /// \brief Note that we have finished instantiating this template.
     void Clear();
 
diff --git a/lib/Sema/SemaTemplateDeduction.cpp b/lib/Sema/SemaTemplateDeduction.cpp
index 0ad7935..6e7a47a 100644
--- a/lib/Sema/SemaTemplateDeduction.cpp
+++ b/lib/Sema/SemaTemplateDeduction.cpp
@@ -408,6 +408,37 @@
       return true;
     }
 
+    //     T type::*
+    //     T T::*
+    //     T (type::*)()
+    //     type (T::*)()
+    //     type (type::*)(T)
+    //     type (T::*)(T)
+    //     T (type::*)(T)
+    //     T (T::*)()
+    //     T (T::*)(T)
+    case Type::MemberPointer: {
+      const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
+      const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
+      if (!MemPtrArg)
+        return false;
+
+      return DeduceTemplateArguments(Context,
+                                     MemPtrParam->getPointeeType(),
+                                     MemPtrArg->getPointeeType(),
+                                     Deduced) &&
+        DeduceTemplateArguments(Context,
+                                QualType(MemPtrParam->getClass(), 0),
+                                QualType(MemPtrArg->getClass(), 0),
+                                Deduced);
+    }
+
+    case Type::TypeOfExpr:
+    case Type::TypeOf:
+    case Type::Typename:
+      // No template argument deduction for these types
+      return true;
+
     default:
       break;
   }
@@ -492,6 +523,14 @@
   if (! ::DeduceTemplateArguments(Context, Partial->getTemplateArgs(), 
                                   TemplateArgs, Deduced))
     return 0;
+
+  // FIXME: It isn't clear whether we want the diagnostic to point at
+  // the partial specialization itself or at the actual point of
+  // instantiation.
+  InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
+                             Deduced.data(), Deduced.size());
+  if (Inst)
+    return 0;
   
   // FIXME: Substitute the deduced template arguments into the template
   // arguments of the class template partial specialization; the resulting
diff --git a/lib/Sema/SemaTemplateInstantiate.cpp b/lib/Sema/SemaTemplateInstantiate.cpp
index c1f67c7..9a3d9e0 100644
--- a/lib/Sema/SemaTemplateInstantiate.cpp
+++ b/lib/Sema/SemaTemplateInstantiate.cpp
@@ -90,6 +90,30 @@
   }
 }
 
+Sema::InstantiatingTemplate::InstantiatingTemplate(Sema &SemaRef, 
+                                         SourceLocation PointOfInstantiation,
+                          ClassTemplatePartialSpecializationDecl *PartialSpec,
+                                         const TemplateArgument *TemplateArgs,
+                                         unsigned NumTemplateArgs,
+                                         SourceRange InstantiationRange)
+  : SemaRef(SemaRef) {
+
+  Invalid = CheckInstantiationDepth(PointOfInstantiation,
+                                    InstantiationRange);
+  if (!Invalid) {
+    ActiveTemplateInstantiation Inst;
+    Inst.Kind 
+      = ActiveTemplateInstantiation::PartialSpecDeductionInstantiation;
+    Inst.PointOfInstantiation = PointOfInstantiation;
+    Inst.Entity = reinterpret_cast<uintptr_t>(PartialSpec);
+    Inst.TemplateArgs = TemplateArgs;
+    Inst.NumTemplateArgs = NumTemplateArgs;
+    Inst.InstantiationRange = InstantiationRange;
+    SemaRef.ActiveTemplateInstantiations.push_back(Inst);
+    Invalid = false;
+  }
+}
+
 void Sema::InstantiatingTemplate::Clear() {
   if (!Invalid) {
     SemaRef.ActiveTemplateInstantiations.pop_back();
@@ -157,6 +181,26 @@
         << Active->InstantiationRange;
       break;
     }
+
+    case ActiveTemplateInstantiation::PartialSpecDeductionInstantiation: {
+      ClassTemplatePartialSpecializationDecl *PartialSpec
+        = cast<ClassTemplatePartialSpecializationDecl>((Decl *)Active->Entity);
+      std::string TemplateArgsStr
+        = TemplateSpecializationType::PrintTemplateArgumentList(
+                        PartialSpec->getTemplateArgs().getFlatArgumentList(),
+                                  PartialSpec->getTemplateArgs().flat_size(),
+                                                      Context.PrintingPolicy);
+      // FIXME: The active template instantiation's template arguments
+      // are interesting, too. We should add something like [with T =
+      // foo, U = bar, etc.] to the string.
+      Diags.Report(FullSourceLoc(Active->PointOfInstantiation, SourceMgr),
+                   diag::note_partial_spec_deduct_instantiation_here)
+        << (PartialSpec->getSpecializedTemplate()->getNameAsString() + 
+            TemplateArgsStr)
+        << Active->InstantiationRange;
+      break;
+    }
+
     }
   }
 }