SemaTemplateDeduction.cpp revision 5453d93ab8668f2d9d0bc02182695b02d207e32d
10b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor//===------- SemaTemplateDeduction.cpp - Template Argument Deduction ------===/
20b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor//
30b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor//                     The LLVM Compiler Infrastructure
40b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor//
50b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor// This file is distributed under the University of Illinois Open Source
60b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor// License. See LICENSE.TXT for details.
70b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor//===----------------------------------------------------------------------===/
80b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor//
90b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor//  This file implements C++ template argument deduction.
100b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor//
110b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor//===----------------------------------------------------------------------===/
120b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
13e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Sema.h"
1419510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h"
1520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor#include "clang/Sema/SemaDiagnostic.h" // FIXME: temporary!
167cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall#include "clang/Sema/Template.h"
172a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall#include "clang/Sema/TemplateDeduction.h"
180b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/ASTContext.h"
197cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall#include "clang/AST/DeclObjC.h"
200b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/DeclTemplate.h"
210b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/StmtVisitor.h"
220b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/Expr.h"
230b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/ExprCXX.h"
24e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor#include "llvm/ADT/BitVector.h"
2534b41d939a1328f484511c6002ba2456db879a29Richard Smith#include "TreeTransform.h"
268a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor#include <algorithm>
27508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor
28508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregornamespace clang {
292a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  using namespace sema;
302a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall
31508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  /// \brief Various flags that control template argument deduction.
32508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  ///
33508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  /// These flags can be bitwise-OR'd together.
34508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  enum TemplateDeductionFlags {
35508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// \brief No template argument deduction flags, which indicates the
36508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// strictest results for template argument deduction (as used for, e.g.,
37508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// matching class template partial specializations).
38508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    TDF_None = 0,
39508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// \brief Within template argument deduction from a function call, we are
40508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// matching with a parameter type for which the original parameter was
41508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// a reference.
42508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    TDF_ParamWithReferenceType = 0x1,
43508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// \brief Within template argument deduction from a function call, we
44508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// are matching in a case where we ignore cv-qualifiers.
45508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    TDF_IgnoreQualifiers = 0x02,
46508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// \brief Within template argument deduction from a function call,
47508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// we are matching in a case where we can perform template argument
484112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    /// deduction from a template-id of a derived class of the argument type.
491282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor    TDF_DerivedClass = 0x04,
501282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor    /// \brief Allow non-dependent types to differ, e.g., when performing
511282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor    /// template argument deduction from a function call where conversions
521282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor    /// may apply.
5373b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor    TDF_SkipNonDependent = 0x08,
5473b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor    /// \brief Whether we are performing template argument deduction for
55dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    /// parameters and arguments in a top-level template argument
5673b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor    TDF_TopLevelParameterTypeList = 0x10
57508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  };
58508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor}
59508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor
600b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregorusing namespace clang;
610b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
629d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor/// \brief Compare two APSInts, extending and switching the sign as
639d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor/// necessary to compare their values regardless of underlying type.
649d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregorstatic bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) {
659d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor  if (Y.getBitWidth() > X.getBitWidth())
669f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    X = X.extend(Y.getBitWidth());
679d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor  else if (Y.getBitWidth() < X.getBitWidth())
689f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    Y = Y.extend(X.getBitWidth());
699d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor
709d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor  // If there is a signedness mismatch, correct it.
719d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor  if (X.isSigned() != Y.isSigned()) {
729d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor    // If the signed value is negative, then the values cannot be the same.
739d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor    if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative()))
749d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor      return false;
759d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor
769d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor    Y.setIsSigned(true);
779d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor    X.setIsSigned(true);
789d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor  }
799d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor
809d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor  return X == Y;
819d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor}
829d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor
83f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
84a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
85f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateParameterList *TemplateParams,
86f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        const TemplateArgument &Param,
8777d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor                        TemplateArgument Arg,
882a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                        TemplateDeductionInfo &Info,
890972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor                      llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced);
90d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor
91b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor/// \brief Whether template argument deduction for two reference parameters
92b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor/// resulted in the argument type, parameter type, or neither type being more
93b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor/// qualified than the other.
94dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumienum DeductionQualifierComparison {
95dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  NeitherMoreQualified = 0,
96dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  ParamMoreQualified,
97dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  ArgMoreQualified
985c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor};
995c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor
100b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor/// \brief Stores the result of comparing two reference parameters while
101b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor/// performing template argument deduction for partial ordering of function
102dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// templates.
103b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregorstruct RefParamPartialOrderingComparison {
104b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor  /// \brief Whether the parameter type is an rvalue reference type.
105b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor  bool ParamIsRvalueRef;
106b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor  /// \brief Whether the argument type is an rvalue reference type.
107b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor  bool ArgIsRvalueRef;
108dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
109b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor  /// \brief Whether the parameter or argument (or neither) is more qualified.
110b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor  DeductionQualifierComparison Qualifiers;
111b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor};
112b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor
113b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor
1145c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor
11520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregorstatic Sema::TemplateDeductionResult
11620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas GregorDeduceTemplateArguments(Sema &S,
11720a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        TemplateParameterList *TemplateParams,
118603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                        QualType Param,
119603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                        QualType Arg,
120603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                        TemplateDeductionInfo &Info,
121603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                        llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
1225c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                        unsigned TDF,
1235c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                        bool PartialOrdering = false,
124b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                      llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *
125b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                                                      RefParamComparisons = 0);
126603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor
127603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregorstatic Sema::TemplateDeductionResult
128603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas GregorDeduceTemplateArguments(Sema &S,
129603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                        TemplateParameterList *TemplateParams,
13020a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        const TemplateArgument *Params, unsigned NumParams,
13120a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        const TemplateArgument *Args, unsigned NumArgs,
13220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        TemplateDeductionInfo &Info,
1330972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor                        llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
1340972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor                        bool NumberOfArgumentsMustMatch = true);
13520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor
136199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// \brief If the given expression is of a form that permits the deduction
137199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// of a non-type template parameter, return the declaration of that
138199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// non-type template parameter.
139199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregorstatic NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
140199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
141199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    E = IC->getSubExpr();
1421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
143199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
144199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
1451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
146199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  return 0;
147199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor}
148199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
1490d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor/// \brief Determine whether two declaration pointers refer to the same
1500d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor/// declaration.
1510d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregorstatic bool isSameDeclaration(Decl *X, Decl *Y) {
1520d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  if (!X || !Y)
1530d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    return !X && !Y;
154dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1550d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  if (NamedDecl *NX = dyn_cast<NamedDecl>(X))
1560d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    X = NX->getUnderlyingDecl();
1570d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  if (NamedDecl *NY = dyn_cast<NamedDecl>(Y))
1580d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    Y = NY->getUnderlyingDecl();
159dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1600d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  return X->getCanonicalDecl() == Y->getCanonicalDecl();
1610d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor}
1620d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor
1630d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor/// \brief Verify that the given, deduced template arguments are compatible.
1640d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor///
1650d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor/// \returns The deduced template argument, or a NULL template argument if
1660d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor/// the deduced template arguments were incompatible.
167dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic DeducedTemplateArgument
1680d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas GregorcheckDeducedTemplateArguments(ASTContext &Context,
1690d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                              const DeducedTemplateArgument &X,
1700d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                              const DeducedTemplateArgument &Y) {
1710d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  // We have no deduction for one or both of the arguments; they're compatible.
1720d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  if (X.isNull())
1730d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    return Y;
1740d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  if (Y.isNull())
175dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return X;
1760d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor
1770d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  switch (X.getKind()) {
1780d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  case TemplateArgument::Null:
1790d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    llvm_unreachable("Non-deduced template arguments handled above");
1800d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor
1810d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  case TemplateArgument::Type:
1820d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // If two template type arguments have the same type, they're compatible.
1830d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    if (Y.getKind() == TemplateArgument::Type &&
1840d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor        Context.hasSameType(X.getAsType(), Y.getAsType()))
1850d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      return X;
186dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1870d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    return DeducedTemplateArgument();
188dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1890d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  case TemplateArgument::Integral:
1900d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // If we deduced a constant in one case and either a dependent expression or
1910d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // declaration in another case, keep the integral constant.
1920d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // If both are integral constants with the same value, keep that value.
1930d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    if (Y.getKind() == TemplateArgument::Expression ||
1940d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor        Y.getKind() == TemplateArgument::Declaration ||
1950d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor        (Y.getKind() == TemplateArgument::Integral &&
1960d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor         hasSameExtendedValue(*X.getAsIntegral(), *Y.getAsIntegral())))
197dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      return DeducedTemplateArgument(X,
1980d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                     X.wasDeducedFromArrayBound() &&
1990d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                     Y.wasDeducedFromArrayBound());
2000d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor
2010d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // All other combinations are incompatible.
2020d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    return DeducedTemplateArgument();
203dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2040d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  case TemplateArgument::Template:
2050d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    if (Y.getKind() == TemplateArgument::Template &&
2060d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor        Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate()))
2070d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      return X;
208dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2090d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // All other combinations are incompatible.
210dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return DeducedTemplateArgument();
211a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
212a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor  case TemplateArgument::TemplateExpansion:
213a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    if (Y.getKind() == TemplateArgument::TemplateExpansion &&
214dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(),
215a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                                    Y.getAsTemplateOrTemplatePattern()))
216a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      return X;
217dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
218a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    // All other combinations are incompatible.
219dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return DeducedTemplateArgument();
220a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
2210d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  case TemplateArgument::Expression:
222dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    // If we deduced a dependent expression in one case and either an integral
223dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    // constant or a declaration in another case, keep the integral constant
2240d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // or declaration.
2250d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    if (Y.getKind() == TemplateArgument::Integral ||
2260d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor        Y.getKind() == TemplateArgument::Declaration)
2270d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      return DeducedTemplateArgument(Y, X.wasDeducedFromArrayBound() &&
2280d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                     Y.wasDeducedFromArrayBound());
229dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2300d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    if (Y.getKind() == TemplateArgument::Expression) {
2310d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      // Compare the expressions for equality
2320d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      llvm::FoldingSetNodeID ID1, ID2;
2330d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      X.getAsExpr()->Profile(ID1, Context, true);
2340d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      Y.getAsExpr()->Profile(ID2, Context, true);
2350d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      if (ID1 == ID2)
2360d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor        return X;
2370d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    }
238dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2390d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // All other combinations are incompatible.
2400d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    return DeducedTemplateArgument();
241dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2420d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  case TemplateArgument::Declaration:
2430d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // If we deduced a declaration and a dependent expression, keep the
2440d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // declaration.
2450d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    if (Y.getKind() == TemplateArgument::Expression)
2460d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      return X;
247dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2480d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // If we deduced a declaration and an integral constant, keep the
2490d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // integral constant.
2500d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    if (Y.getKind() == TemplateArgument::Integral)
2510d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      return Y;
252dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2530d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // If we deduced two declarations, make sure they they refer to the
2540d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // same declaration.
2550d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    if (Y.getKind() == TemplateArgument::Declaration &&
2560d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor        isSameDeclaration(X.getAsDecl(), Y.getAsDecl()))
2570d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      return X;
258dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2590d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // All other combinations are incompatible.
2600d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    return DeducedTemplateArgument();
261dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2620d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  case TemplateArgument::Pack:
2630d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    if (Y.getKind() != TemplateArgument::Pack ||
2640d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor        X.pack_size() != Y.pack_size())
2650d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      return DeducedTemplateArgument();
266dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
267dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    for (TemplateArgument::pack_iterator XA = X.pack_begin(),
2680d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                      XAEnd = X.pack_end(),
2690d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                         YA = Y.pack_begin();
2700d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor         XA != XAEnd; ++XA, ++YA) {
271dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      if (checkDeducedTemplateArguments(Context,
272dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                    DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()),
273135ffa7375fb5802b92f42774e02d0e6e4c78e5bDouglas Gregor                    DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound()))
274135ffa7375fb5802b92f42774e02d0e6e4c78e5bDouglas Gregor            .isNull())
2750d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor        return DeducedTemplateArgument();
2760d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    }
277dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2780d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    return X;
2790d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  }
280dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2810d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  return DeducedTemplateArgument();
2820d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor}
2830d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor
2841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Deduce the value of the given non-type template parameter
285199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// from the given constant.
286f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
287a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceNonTypeTemplateArgument(Sema &S,
2881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                              NonTypeTemplateParmDecl *NTTP,
2899d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor                              llvm::APSInt Value, QualType ValueType,
29002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                              bool DeducedFromArrayBound,
2912a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                              TemplateDeductionInfo &Info,
29202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
2931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(NTTP->getDepth() == 0 &&
294199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor         "Cannot deduce non-type template argument with depth > 0");
2951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2960d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  DeducedTemplateArgument NewDeduced(Value, ValueType, DeducedFromArrayBound);
297dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
2980d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                                     Deduced[NTTP->getIndex()],
2990d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                                                 NewDeduced);
3000d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  if (Result.isNull()) {
301f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.Param = NTTP;
302f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.FirstArg = Deduced[NTTP->getIndex()];
3030d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    Info.SecondArg = NewDeduced;
304dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return Sema::TDK_Inconsistent;
305f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  }
306dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3070d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  Deduced[NTTP->getIndex()] = Result;
308f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  return Sema::TDK_Success;
309199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor}
310199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
3111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Deduce the value of the given non-type template parameter
312199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// from the given type- or value-dependent expression.
313199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor///
314199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// \returns true if deduction succeeded, false otherwise.
315f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
316a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceNonTypeTemplateArgument(Sema &S,
317f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                              NonTypeTemplateParmDecl *NTTP,
318f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                              Expr *Value,
3192a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                              TemplateDeductionInfo &Info,
32002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
3211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(NTTP->getDepth() == 0 &&
322199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor         "Cannot deduce non-type template argument with depth > 0");
323199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  assert((Value->isTypeDependent() || Value->isValueDependent()) &&
324199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor         "Expression template argument must be type- or value-dependent.");
3251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3260d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  DeducedTemplateArgument NewDeduced(Value);
327dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
328dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                     Deduced[NTTP->getIndex()],
3290d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                                                 NewDeduced);
330dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3310d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  if (Result.isNull()) {
3320d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    Info.Param = NTTP;
3330d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    Info.FirstArg = Deduced[NTTP->getIndex()];
3340d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    Info.SecondArg = NewDeduced;
335dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return Sema::TDK_Inconsistent;
3369eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor  }
337dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3380d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  Deduced[NTTP->getIndex()] = Result;
339f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  return Sema::TDK_Success;
340199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor}
341199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
34215755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor/// \brief Deduce the value of the given non-type template parameter
34315755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor/// from the given declaration.
34415755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor///
34515755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor/// \returns true if deduction succeeded, false otherwise.
34615755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregorstatic Sema::TemplateDeductionResult
347a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceNonTypeTemplateArgument(Sema &S,
34815755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor                              NonTypeTemplateParmDecl *NTTP,
34915755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor                              Decl *D,
3502a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                              TemplateDeductionInfo &Info,
35102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
35215755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor  assert(NTTP->getDepth() == 0 &&
35315755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor         "Cannot deduce non-type template argument with depth > 0");
354dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3550d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  DeducedTemplateArgument NewDeduced(D? D->getCanonicalDecl() : 0);
356dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
3570d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                                     Deduced[NTTP->getIndex()],
3580d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                                                 NewDeduced);
3590d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  if (Result.isNull()) {
3600d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    Info.Param = NTTP;
3610d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    Info.FirstArg = Deduced[NTTP->getIndex()];
3620d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    Info.SecondArg = NewDeduced;
363dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return Sema::TDK_Inconsistent;
36415755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor  }
365dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3660d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  Deduced[NTTP->getIndex()] = Result;
36715755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor  return Sema::TDK_Success;
36815755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor}
36915755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor
370f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
371a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
372db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor                        TemplateParameterList *TemplateParams,
373f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateName Param,
374f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateName Arg,
3752a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                        TemplateDeductionInfo &Info,
37602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
377d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor  TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
378db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  if (!ParamDecl) {
379db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    // The parameter type is dependent and is not a template template parameter,
380db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    // so there is nothing that we can deduce.
381db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    return Sema::TDK_Success;
382f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  }
383dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
384db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  if (TemplateTemplateParmDecl *TempParam
385db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor        = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) {
3860d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg));
387dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
3880d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                                 Deduced[TempParam->getIndex()],
3890d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                                                   NewDeduced);
3900d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    if (Result.isNull()) {
3910d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      Info.Param = TempParam;
3920d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      Info.FirstArg = Deduced[TempParam->getIndex()];
3930d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      Info.SecondArg = NewDeduced;
394dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      return Sema::TDK_Inconsistent;
395db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    }
396dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3970d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    Deduced[TempParam->getIndex()] = Result;
398dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return Sema::TDK_Success;
399f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  }
400dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
401db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  // Verify that the two template names are equivalent.
402a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  if (S.Context.hasSameTemplateName(Param, Arg))
403db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    return Sema::TDK_Success;
404dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
405db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  // Mismatch of non-dependent template parameter to argument.
406db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Info.FirstArg = TemplateArgument(Param);
407db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Info.SecondArg = TemplateArgument(Arg);
408db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  return Sema::TDK_NonDeducedMismatch;
409d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor}
410d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor
4111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Deduce the template arguments by comparing the template parameter
412de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// type (which is a template-id) with the template argument type.
413de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
414a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth/// \param S the Sema
415de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
416de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param TemplateParams the template parameters that we are deducing
417de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
418de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Param the parameter type
419de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
420de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Arg the argument type
421de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
422de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Info information about the template argument deduction itself
423de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
424de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Deduced the deduced template arguments
425de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
426de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \returns the result of template argument deduction so far. Note that a
427de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// "success" result means that template argument deduction has not yet failed,
428de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// but it may still fail, later, for other reasons.
429de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregorstatic Sema::TemplateDeductionResult
430a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
431de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                        TemplateParameterList *TemplateParams,
432de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                        const TemplateSpecializationType *Param,
433de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                        QualType Arg,
4342a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                        TemplateDeductionInfo &Info,
43502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
436467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(Arg.isCanonical() && "Argument type must be canonical");
4371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
438de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  // Check whether the template argument is a dependent template-id.
4391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const TemplateSpecializationType *SpecArg
440de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        = dyn_cast<TemplateSpecializationType>(Arg)) {
441de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    // Perform template argument deduction for the template name.
442de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    if (Sema::TemplateDeductionResult Result
443a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          = DeduceTemplateArguments(S, TemplateParams,
444de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                    Param->getTemplateName(),
445de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                    SpecArg->getTemplateName(),
446de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                    Info, Deduced))
447de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      return Result;
4481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
450de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    // Perform template argument deduction on each template
4510972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor    // argument. Ignore any missing/extra arguments, since they could be
4520972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor    // filled in by default arguments.
453dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return DeduceTemplateArguments(S, TemplateParams,
454dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                   Param->getArgs(), Param->getNumArgs(),
4550972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor                                   SpecArg->getArgs(), SpecArg->getNumArgs(),
4560972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor                                   Info, Deduced,
4570972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor                                   /*NumberOfArgumentsMustMatch=*/false);
458de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  }
4591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
460de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  // If the argument type is a class template specialization, we
461de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  // perform template argument deduction using its template
462de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  // arguments.
463de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
464de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  if (!RecordArg)
465de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
4661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ClassTemplateSpecializationDecl *SpecArg
468de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
469de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  if (!SpecArg)
470de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
4711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
472de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  // Perform template argument deduction for the template name.
473de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  if (Sema::TemplateDeductionResult Result
474a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = DeduceTemplateArguments(S,
475db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor                                  TemplateParams,
476de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                  Param->getTemplateName(),
477de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                               TemplateName(SpecArg->getSpecializedTemplate()),
478de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                  Info, Deduced))
479de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    return Result;
4801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
48120a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  // Perform template argument deduction for the template arguments.
482dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  return DeduceTemplateArguments(S, TemplateParams,
48320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                                 Param->getArgs(), Param->getNumArgs(),
48420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                                 SpecArg->getTemplateArgs().data(),
48520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                                 SpecArg->getTemplateArgs().size(),
48620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                                 Info, Deduced);
487de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor}
488de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor
489cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall/// \brief Determines whether the given type is an opaque type that
490cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall/// might be more qualified when instantiated.
491cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCallstatic bool IsPossiblyOpaquelyQualifiedType(QualType T) {
492cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall  switch (T->getTypeClass()) {
493cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall  case Type::TypeOfExpr:
494cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall  case Type::TypeOf:
495cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall  case Type::DependentName:
496cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall  case Type::Decltype:
497cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall  case Type::UnresolvedUsing:
49862c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  case Type::TemplateTypeParm:
499cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall    return true;
500cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall
501cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall  case Type::ConstantArray:
502cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall  case Type::IncompleteArray:
503cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall  case Type::VariableArray:
504cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall  case Type::DependentSizedArray:
505cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall    return IsPossiblyOpaquelyQualifiedType(
506cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall                                      cast<ArrayType>(T)->getElementType());
507cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall
508cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall  default:
509cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall    return false;
510cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall  }
511cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall}
512cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall
513d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor/// \brief Retrieve the depth and index of a template parameter.
514dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic std::pair<unsigned, unsigned>
515d3731198193eee92796ddeb493973b7a598b003eDouglas GregorgetDepthAndIndex(NamedDecl *ND) {
516603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
517603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    return std::make_pair(TTP->getDepth(), TTP->getIndex());
518dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
519603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
520603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
521dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
522603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
523603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  return std::make_pair(TTP->getDepth(), TTP->getIndex());
524603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor}
525603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor
526d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor/// \brief Retrieve the depth and index of an unexpanded parameter pack.
527dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic std::pair<unsigned, unsigned>
528d3731198193eee92796ddeb493973b7a598b003eDouglas GregorgetDepthAndIndex(UnexpandedParameterPack UPP) {
529d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  if (const TemplateTypeParmType *TTP
530d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                          = UPP.first.dyn_cast<const TemplateTypeParmType *>())
531d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    return std::make_pair(TTP->getDepth(), TTP->getIndex());
532dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
533d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  return getDepthAndIndex(UPP.first.get<NamedDecl *>());
534d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor}
535d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor
536603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \brief Helper function to build a TemplateParameter when we don't
537603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// know its type statically.
538603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregorstatic TemplateParameter makeTemplateParameter(Decl *D) {
539603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
540603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    return TemplateParameter(TTP);
541603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
542603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    return TemplateParameter(NTTP);
543dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
544603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
545603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor}
546603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor
5475429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor/// \brief Prepare to perform template argument deduction for all of the
5485429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor/// arguments in a set of argument packs.
549dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic void PrepareArgumentPackDeduction(Sema &S,
550dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                       llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
5515429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor                             const llvm::SmallVectorImpl<unsigned> &PackIndices,
552dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                     llvm::SmallVectorImpl<DeducedTemplateArgument> &SavedPacks,
5535429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor         llvm::SmallVectorImpl<
5545429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor           llvm::SmallVector<DeducedTemplateArgument, 4> > &NewlyDeducedPacks) {
5555429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor  // Save the deduced template arguments for each parameter pack expanded
5565429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor  // by this pack expansion, then clear out the deduction.
5575429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor  for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
5585429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor    // Save the previously-deduced argument pack, then clear it out so that we
5595429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor    // can deduce a new argument pack.
5605429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor    SavedPacks[I] = Deduced[PackIndices[I]];
561dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    Deduced[PackIndices[I]] = TemplateArgument();
562dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5635429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor    // If the template arugment pack was explicitly specified, add that to
5645429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor    // the set of deduced arguments.
5655429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor    const TemplateArgument *ExplicitArgs;
5665429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor    unsigned NumExplicitArgs;
567dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    if (NamedDecl *PartiallySubstitutedPack
5685429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor        = S.CurrentInstantiationScope->getPartiallySubstitutedPack(
5695429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor                                                           &ExplicitArgs,
5705429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor                                                           &NumExplicitArgs)) {
5715429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor      if (getDepthAndIndex(PartiallySubstitutedPack).second == PackIndices[I])
572dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        NewlyDeducedPacks[I].append(ExplicitArgs,
5735429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor                                    ExplicitArgs + NumExplicitArgs);
5745429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor    }
5755429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor  }
5765429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor}
5775429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor
5780216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor/// \brief Finish template argument deduction for a set of argument packs,
5790216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor/// producing the argument packs and checking for consistency with prior
5800216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor/// deductions.
5810216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregorstatic Sema::TemplateDeductionResult
5820216f8121df32b320cab659d5b703fee50cdfda5Douglas GregorFinishArgumentPackDeduction(Sema &S,
5830216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                            TemplateParameterList *TemplateParams,
5840216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                            bool HasAnyArguments,
585dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                        llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
5860216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                            const llvm::SmallVectorImpl<unsigned> &PackIndices,
587dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                    llvm::SmallVectorImpl<DeducedTemplateArgument> &SavedPacks,
5880216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor        llvm::SmallVectorImpl<
5890216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor          llvm::SmallVector<DeducedTemplateArgument, 4> > &NewlyDeducedPacks,
5900216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                            TemplateDeductionInfo &Info) {
5910216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor  // Build argument packs for each of the parameter packs expanded by this
5920216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor  // pack expansion.
5930216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor  for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
5940216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    if (HasAnyArguments && NewlyDeducedPacks[I].empty()) {
5950216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      // We were not able to deduce anything for this parameter pack,
5960216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      // so just restore the saved argument pack.
5970216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      Deduced[PackIndices[I]] = SavedPacks[I];
5980216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      continue;
5990216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    }
600dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
6010216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    DeducedTemplateArgument NewPack;
602dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
6030216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    if (NewlyDeducedPacks[I].empty()) {
6040216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      // If we deduced an empty argument pack, create it now.
6050216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      NewPack = DeducedTemplateArgument(TemplateArgument(0, 0));
6060216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    } else {
6070216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      TemplateArgument *ArgumentPack
608203e6a322ae29d577acafcb1572a57ec16e1e730Douglas Gregor        = new (S.Context) TemplateArgument [NewlyDeducedPacks[I].size()];
6090216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      std::copy(NewlyDeducedPacks[I].begin(), NewlyDeducedPacks[I].end(),
6100216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                ArgumentPack);
6110216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      NewPack
612203e6a322ae29d577acafcb1572a57ec16e1e730Douglas Gregor        = DeducedTemplateArgument(TemplateArgument(ArgumentPack,
613203e6a322ae29d577acafcb1572a57ec16e1e730Douglas Gregor                                                   NewlyDeducedPacks[I].size()),
614dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                            NewlyDeducedPacks[I][0].wasDeducedFromArrayBound());
6150216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    }
616dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
6170216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    DeducedTemplateArgument Result
6180216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      = checkDeducedTemplateArguments(S.Context, SavedPacks[I], NewPack);
6190216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    if (Result.isNull()) {
6200216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      Info.Param
6210216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor        = makeTemplateParameter(TemplateParams->getParam(PackIndices[I]));
6220216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      Info.FirstArg = SavedPacks[I];
6230216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      Info.SecondArg = NewPack;
6240216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      return Sema::TDK_Inconsistent;
6250216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    }
626dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
6270216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    Deduced[PackIndices[I]] = Result;
6280216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor  }
629dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
6300216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor  return Sema::TDK_Success;
6310216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor}
6320216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor
633603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \brief Deduce the template arguments by comparing the list of parameter
634dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// types to the list of argument types, as in the parameter-type-lists of
635dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// function types (C++ [temp.deduct.type]p10).
636603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor///
637603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param S The semantic analysis object within which we are deducing
638603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor///
639603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param TemplateParams The template parameters that we are deducing
640603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor///
641603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param Params The list of parameter types
642603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor///
643603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param NumParams The number of types in \c Params
644603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor///
645603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param Args The list of argument types
646603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor///
647603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param NumArgs The number of types in \c Args
648603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor///
649603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param Info information about the template argument deduction itself
650603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor///
651603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param Deduced the deduced template arguments
652603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor///
653603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
654603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// how template argument deduction is performed.
655603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor///
6565c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// \param PartialOrdering If true, we are performing template argument
657dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// deduction for during partial ordering for a call
6585c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// (C++0x [temp.deduct.partial]).
6595c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor///
660b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor/// \param RefParamComparisons If we're performing template argument deduction
6615c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// in the context of partial ordering, the set of qualifier comparisons.
6625c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor///
663603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \returns the result of template argument deduction so far. Note that a
664603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// "success" result means that template argument deduction has not yet failed,
665603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// but it may still fail, later, for other reasons.
666603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregorstatic Sema::TemplateDeductionResult
667603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas GregorDeduceTemplateArguments(Sema &S,
668603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                        TemplateParameterList *TemplateParams,
669603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                        const QualType *Params, unsigned NumParams,
670603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                        const QualType *Args, unsigned NumArgs,
671603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                        TemplateDeductionInfo &Info,
672603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                      llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
6735c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                        unsigned TDF,
6745c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                        bool PartialOrdering = false,
675b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                        llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *
676b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                                                     RefParamComparisons = 0) {
6770bbacf8d2b3c7a5f19f292f3201e371836445502Douglas Gregor  // Fast-path check to see if we have too many/too few arguments.
6780bbacf8d2b3c7a5f19f292f3201e371836445502Douglas Gregor  if (NumParams != NumArgs &&
6790bbacf8d2b3c7a5f19f292f3201e371836445502Douglas Gregor      !(NumParams && isa<PackExpansionType>(Params[NumParams - 1])) &&
6800bbacf8d2b3c7a5f19f292f3201e371836445502Douglas Gregor      !(NumArgs && isa<PackExpansionType>(Args[NumArgs - 1])))
6813cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor    return Sema::TDK_NonDeducedMismatch;
682dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
683603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  // C++0x [temp.deduct.type]p10:
684dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   Similarly, if P has a form that contains (T), then each parameter type
685dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   Pi of the respective parameter-type- list of P is compared with the
686dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   corresponding parameter type Ai of the corresponding parameter-type-list
687dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   of A. [...]
688603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  unsigned ArgIdx = 0, ParamIdx = 0;
689603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  for (; ParamIdx != NumParams; ++ParamIdx) {
690603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    // Check argument types.
691dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    const PackExpansionType *Expansion
692603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                                = dyn_cast<PackExpansionType>(Params[ParamIdx]);
693603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    if (!Expansion) {
694603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      // Simple case: compare the parameter and argument types at this point.
695dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
696603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      // Make sure we have an argument.
697603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      if (ArgIdx >= NumArgs)
6983cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
699dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
70077d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor      if (isa<PackExpansionType>(Args[ArgIdx])) {
70177d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor        // C++0x [temp.deduct.type]p22:
70277d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor        //   If the original function parameter associated with A is a function
70377d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor        //   parameter pack and the function parameter associated with P is not
70477d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor        //   a function parameter pack, then template argument deduction fails.
70577d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
70677d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor      }
707dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
708603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      if (Sema::TemplateDeductionResult Result
7095c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor            = DeduceTemplateArguments(S, TemplateParams,
7105c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                      Params[ParamIdx],
7115c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                      Args[ArgIdx],
7125c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                      Info, Deduced, TDF,
7135c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                      PartialOrdering,
714b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                                      RefParamComparisons))
715603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor        return Result;
716dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
717603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      ++ArgIdx;
718603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      continue;
719603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    }
720dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
7217d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    // C++0x [temp.deduct.type]p5:
7227d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    //   The non-deduced contexts are:
723dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //     - A function parameter pack that does not occur at the end of the
7247d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    //       parameter-declaration-clause.
7257d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    if (ParamIdx + 1 < NumParams)
7267d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor      return Sema::TDK_Success;
7277d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
728603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    // C++0x [temp.deduct.type]p10:
729dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   If the parameter-declaration corresponding to Pi is a function
730603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    //   parameter pack, then the type of its declarator- id is compared with
731dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   each remaining parameter type in the parameter-type-list of A. Each
732603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    //   comparison deduces template arguments for subsequent positions in the
733603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    //   template parameter packs expanded by the function parameter pack.
734dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
735603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    // Compute the set of template parameter indices that correspond to
736603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    // parameter packs expanded by the pack expansion.
737603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    llvm::SmallVector<unsigned, 2> PackIndices;
738603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    QualType Pattern = Expansion->getPattern();
739603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    {
740603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      llvm::BitVector SawIndices(TemplateParams->size());
741603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
742603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      S.collectUnexpandedParameterPacks(Pattern, Unexpanded);
743603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
744603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor        unsigned Depth, Index;
745603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor        llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]);
746603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor        if (Depth == 0 && !SawIndices[Index]) {
747603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor          SawIndices[Index] = true;
748603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor          PackIndices.push_back(Index);
749603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor        }
750603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      }
751603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    }
752603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?");
753603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor
754d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    // Keep track of the deduced template arguments for each parameter pack
755dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    // expanded by this pack expansion (the outer index) and for each
756d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    // template argument (the inner SmallVectors).
757d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2>
758d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      NewlyDeducedPacks(PackIndices.size());
759dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    llvm::SmallVector<DeducedTemplateArgument, 2>
7605429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor      SavedPacks(PackIndices.size());
761dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    PrepareArgumentPackDeduction(S, Deduced, PackIndices, SavedPacks,
7625429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor                                 NewlyDeducedPacks);
763dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
764603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    bool HasAnyArguments = false;
765603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    for (; ArgIdx < NumArgs; ++ArgIdx) {
766603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      HasAnyArguments = true;
767dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
768603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      // Deduce template arguments from the pattern.
769dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      if (Sema::TemplateDeductionResult Result
77073b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor            = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx],
77173b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor                                      Info, Deduced, TDF, PartialOrdering,
77273b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor                                      RefParamComparisons))
773603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor        return Result;
774dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
775603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      // Capture the deduced template arguments for each parameter pack expanded
776603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      // by this pack expansion, add them to the list of arguments we've deduced
777603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      // for that pack, then clear out the deduced argument.
778603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
779603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor        DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]];
780603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor        if (!DeducedArg.isNull()) {
781603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor          NewlyDeducedPacks[I].push_back(DeducedArg);
782603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor          DeducedArg = DeducedTemplateArgument();
783603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor        }
784603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      }
785603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    }
786dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
787603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    // Build argument packs for each of the parameter packs expanded by this
788603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    // pack expansion.
7890216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    if (Sema::TemplateDeductionResult Result
790dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi          = FinishArgumentPackDeduction(S, TemplateParams, HasAnyArguments,
7910216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                                        Deduced, PackIndices, SavedPacks,
7920216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                                        NewlyDeducedPacks, Info))
793dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      return Result;
794603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  }
795dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
796603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  // Make sure we don't have any extra arguments.
797603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  if (ArgIdx < NumArgs)
7983cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor    return Sema::TDK_NonDeducedMismatch;
799dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
800603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  return Sema::TDK_Success;
801603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor}
802603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor
803500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \brief Deduce the template arguments by comparing the parameter type and
804500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// the argument type (C++ [temp.deduct.type]).
805500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
806a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth/// \param S the semantic analysis object within which we are deducing
807500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
808500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param TemplateParams the template parameters that we are deducing
809500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
810500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param ParamIn the parameter type
811500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
812500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param ArgIn the argument type
813500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
814500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param Info information about the template argument deduction itself
815500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
816500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param Deduced the deduced template arguments
817500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
818508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
8191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// how template argument deduction is performed.
820500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
8215c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// \param PartialOrdering Whether we're performing template argument deduction
8225c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// in the context of partial ordering (C++0x [temp.deduct.partial]).
8235c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor///
824b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor/// \param RefParamComparisons If we're performing template argument deduction
8255c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// in the context of partial ordering, the set of qualifier comparisons.
8265c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor///
827500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \returns the result of template argument deduction so far. Note that a
828500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// "success" result means that template argument deduction has not yet failed,
829500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// but it may still fail, later, for other reasons.
830f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
831a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
832f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateParameterList *TemplateParams,
833f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        QualType ParamIn, QualType ArgIn,
8342a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                        TemplateDeductionInfo &Info,
83502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                     llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
8365c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                        unsigned TDF,
8375c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                        bool PartialOrdering,
838b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *RefParamComparisons) {
8390b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  // We only want to look at the canonical types, since typedefs and
8400b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  // sugar are not part of template argument deduction.
841a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  QualType Param = S.Context.getCanonicalType(ParamIn);
842a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  QualType Arg = S.Context.getCanonicalType(ArgIn);
8430b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
84477d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor  // If the argument type is a pack expansion, look at its pattern.
845dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // This isn't explicitly called out
84677d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor  if (const PackExpansionType *ArgExpansion
84777d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor                                            = dyn_cast<PackExpansionType>(Arg))
84877d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor    Arg = ArgExpansion->getPattern();
849dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
8505c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor  if (PartialOrdering) {
8515c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    // C++0x [temp.deduct.partial]p5:
852dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   Before the partial ordering is done, certain transformations are
853dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   performed on the types used for partial ordering:
854dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //     - If P is a reference type, P is replaced by the type referred to.
8555c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    const ReferenceType *ParamRef = Param->getAs<ReferenceType>();
8565c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    if (ParamRef)
8575c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      Param = ParamRef->getPointeeType();
858dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
8595c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //     - If A is a reference type, A is replaced by the type referred to.
8605c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    const ReferenceType *ArgRef = Arg->getAs<ReferenceType>();
8615c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    if (ArgRef)
8625c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      Arg = ArgRef->getPointeeType();
863dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
864b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    if (RefParamComparisons && ParamRef && ArgRef) {
8655c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      // C++0x [temp.deduct.partial]p6:
866dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      //   If both P and A were reference types (before being replaced with the
867dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      //   type referred to above), determine which of the two types (if any) is
8685c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      //   more cv-qualified than the other; otherwise the types are considered
869dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      //   to be equally cv-qualified for partial ordering purposes. The result
8705c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      //   of this determination will be used below.
8715c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      //
872dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      // We save this information for later, using it only when deduction
8735c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      // succeeds in both directions.
874b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      RefParamPartialOrderingComparison Comparison;
875b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      Comparison.ParamIsRvalueRef = ParamRef->getAs<RValueReferenceType>();
876b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      Comparison.ArgIsRvalueRef = ArgRef->getAs<RValueReferenceType>();
877b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      Comparison.Qualifiers = NeitherMoreQualified;
8785c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      if (Param.isMoreQualifiedThan(Arg))
879b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor        Comparison.Qualifiers = ParamMoreQualified;
8805c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      else if (Arg.isMoreQualifiedThan(Param))
881b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor        Comparison.Qualifiers = ArgMoreQualified;
882b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      RefParamComparisons->push_back(Comparison);
8835c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    }
884dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
8855c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    // C++0x [temp.deduct.partial]p7:
8865c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //   Remove any top-level cv-qualifiers:
887dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //     - If P is a cv-qualified type, P is replaced by the cv-unqualified
8885c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //       version of P.
8895c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    Param = Param.getUnqualifiedType();
890dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //     - If A is a cv-qualified type, A is replaced by the cv-unqualified
8915c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //       version of A.
8925c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    Arg = Arg.getUnqualifiedType();
8935c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor  } else {
8945c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    // C++0x [temp.deduct.call]p4 bullet 1:
8955c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //   - If the original P is a reference type, the deduced A (i.e., the type
8965c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //     referred to by the reference) can be more cv-qualified than the
8975c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //     transformed A.
8985c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    if (TDF & TDF_ParamWithReferenceType) {
8995c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      Qualifiers Quals;
9005c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals);
9015c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      Quals.setCVRQualifiers(Quals.getCVRQualifiers() &
90262c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall                             Arg.getCVRQualifiers());
9035c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      Param = S.Context.getQualifiedType(UnqualParam, Quals);
9045c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    }
905dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
90673b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor    if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) {
90773b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      // C++0x [temp.deduct.type]p10:
90873b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      //   If P and A are function types that originated from deduction when
90973b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      //   taking the address of a function template (14.8.2.2) or when deducing
91073b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      //   template arguments from a function declaration (14.8.2.6) and Pi and
911dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      //   Ai are parameters of the top-level parameter-type-list of P and A,
912dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      //   respectively, Pi is adjusted if it is an rvalue reference to a
913dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      //   cv-unqualified template parameter and Ai is an lvalue reference, in
914dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      //   which case the type of Pi is changed to be the template parameter
91573b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      //   type (i.e., T&& is changed to simply T). [ Note: As a result, when
91673b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      //   Pi is T&& and Ai is X&, the adjusted Pi will be T, causing T to be
9170099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi      //   deduced as X&. - end note ]
91873b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      TDF &= ~TDF_TopLevelParameterTypeList;
919dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
92073b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      if (const RValueReferenceType *ParamRef
92173b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor                                        = Param->getAs<RValueReferenceType>()) {
92273b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor        if (isa<TemplateTypeParmType>(ParamRef->getPointeeType()) &&
92373b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor            !ParamRef->getPointeeType().getQualifiers())
92473b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor          if (Arg->isLValueReferenceType())
92573b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor            Param = ParamRef->getPointeeType();
92673b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      }
92773b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor    }
928500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor  }
929dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
930f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  // If the parameter type is not dependent, there is nothing to deduce.
9311282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor  if (!Param->isDependentType()) {
9323cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor    if (!(TDF & TDF_SkipNonDependent) && Param != Arg)
9331282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor      return Sema::TDK_NonDeducedMismatch;
934dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
935f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    return Sema::TDK_Success;
9361282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor  }
9370b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
938199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  // C++ [temp.deduct.type]p9:
9391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   A template type argument T, a template template argument TT or a
9401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   template non-type argument i can be deduced if P and A have one of
941199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  //   the following forms:
942199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  //
943199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  //     T
944199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  //     cv-list T
9451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const TemplateTypeParmType *TemplateTypeParm
946183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall        = Param->getAs<TemplateTypeParmType>()) {
947f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    unsigned Index = TemplateTypeParm->getIndex();
948f290e0db835a619014538c41ed552696efc0e977Douglas Gregor    bool RecanonicalizeArg = false;
9491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9509e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor    // If the argument type is an array type, move the qualifiers up to the
9519e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor    // top level, so they can be matched with the qualifiers on the parameter.
9529e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor    // FIXME: address spaces, ObjC GC qualifiers
953f290e0db835a619014538c41ed552696efc0e977Douglas Gregor    if (isa<ArrayType>(Arg)) {
9540953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      Qualifiers Quals;
955a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      Arg = S.Context.getUnqualifiedArrayType(Arg, Quals);
9560953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (Quals) {
957a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        Arg = S.Context.getQualifiedType(Arg, Quals);
958f290e0db835a619014538c41ed552696efc0e977Douglas Gregor        RecanonicalizeArg = true;
959f290e0db835a619014538c41ed552696efc0e977Douglas Gregor      }
960f290e0db835a619014538c41ed552696efc0e977Douglas Gregor    }
9611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9620b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor    // The argument type can not be less qualified than the parameter
9630b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor    // type.
964508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) {
965f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
96657e97786433e70197a089360228d8f0d82e3ad4cJohn McCall      Info.FirstArg = TemplateArgument(Param);
967833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      Info.SecondArg = TemplateArgument(Arg);
96857e97786433e70197a089360228d8f0d82e3ad4cJohn McCall      return Sema::TDK_Underqualified;
969f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    }
9700b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
9710b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor    assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
972a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth    assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function");
9730953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    QualType DeducedType = Arg;
97449f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall
97549f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall    // local manipulation is okay because it's canonical
97649f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall    DeducedType.removeLocalCVRQualifiers(Param.getCVRQualifiers());
977f290e0db835a619014538c41ed552696efc0e977Douglas Gregor    if (RecanonicalizeArg)
978a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      DeducedType = S.Context.getCanonicalType(DeducedType);
9791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9800d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    DeducedTemplateArgument NewDeduced(DeducedType);
981dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
9820d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                                                 Deduced[Index],
9830d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                                                   NewDeduced);
9840d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    if (Result.isNull()) {
9850d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
9860d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      Info.FirstArg = Deduced[Index];
9870d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      Info.SecondArg = NewDeduced;
988dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      return Sema::TDK_Inconsistent;
9890b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor    }
990dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
9910d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    Deduced[Index] = Result;
992dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return Sema::TDK_Success;
9930b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
9940b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
995f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  // Set up the template argument deduction information for a failure.
996833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  Info.FirstArg = TemplateArgument(ParamIn);
997833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  Info.SecondArg = TemplateArgument(ArgIn);
998f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
9990bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  // If the parameter is an already-substituted template parameter
10000bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  // pack, do nothing: we don't know which of its arguments to look
10010bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  // at, so we have to wait until all of the parameter packs in this
10020bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  // expansion have arguments.
10030bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  if (isa<SubstTemplateTypeParmPackType>(Param))
10040bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    return Sema::TDK_Success;
10050bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor
1006508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  // Check the cv-qualifiers on the parameter and argument types.
1007508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  if (!(TDF & TDF_IgnoreQualifiers)) {
1008508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    if (TDF & TDF_ParamWithReferenceType) {
1009508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor      if (Param.isMoreQualifiedThan(Arg))
1010508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
1011cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall    } else if (!IsPossiblyOpaquelyQualifiedType(Param)) {
1012508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor      if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
10131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        return Sema::TDK_NonDeducedMismatch;
1014508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    }
1015508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  }
10160b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
1017d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor  switch (Param->getTypeClass()) {
1018199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    // No deduction possible for these types
1019199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    case Type::Builtin:
1020f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
10211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1022199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T *
1023d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    case Type::Pointer: {
1024c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall      QualType PointeeType;
1025c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall      if (const PointerType *PointerArg = Arg->getAs<PointerType>()) {
1026c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall        PointeeType = PointerArg->getPointeeType();
1027c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall      } else if (const ObjCObjectPointerType *PointerArg
1028c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall                   = Arg->getAs<ObjCObjectPointerType>()) {
1029c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall        PointeeType = PointerArg->getPointeeType();
1030c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall      } else {
1031f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
1032c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall      }
10331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10344112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor      unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
1035a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
1036d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                                   cast<PointerType>(Param)->getPointeeType(),
1037c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall                                     PointeeType,
10384112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor                                     Info, Deduced, SubTDF);
1039d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    }
10401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1041199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T &
1042d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    case Type::LValueReference: {
10436217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
1044d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor      if (!ReferenceArg)
1045f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
10461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1047a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
1048d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                           cast<LValueReferenceType>(Param)->getPointeeType(),
1049d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                                     ReferenceArg->getPointeeType(),
1050508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
1051d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    }
10520b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
1053199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T && [C++0x]
1054d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    case Type::RValueReference: {
10556217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
1056d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor      if (!ReferenceArg)
1057f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
10581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1059a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
1060d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                           cast<RValueReferenceType>(Param)->getPointeeType(),
1061d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                                     ReferenceArg->getPointeeType(),
1062508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
1063d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    }
10641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1065199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T [] (implied, but not stated explicitly)
10664d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson    case Type::IncompleteArray: {
10671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const IncompleteArrayType *IncompleteArrayArg =
1068a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        S.Context.getAsIncompleteArrayType(Arg);
10694d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson      if (!IncompleteArrayArg)
1070f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
10711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1072e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall      unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
1073a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
1074a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth                     S.Context.getAsIncompleteArrayType(Param)->getElementType(),
10754d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson                                     IncompleteArrayArg->getElementType(),
1076e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall                                     Info, Deduced, SubTDF);
10774d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson    }
1078199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
1079199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T [integer-constant]
10804d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson    case Type::ConstantArray: {
10811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const ConstantArrayType *ConstantArrayArg =
1082a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        S.Context.getAsConstantArrayType(Arg);
10834d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson      if (!ConstantArrayArg)
1084f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
10851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const ConstantArrayType *ConstantArrayParm =
1087a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        S.Context.getAsConstantArrayType(Param);
10884d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson      if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
1089f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
10901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1091e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall      unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
1092a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
10934d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson                                     ConstantArrayParm->getElementType(),
10944d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson                                     ConstantArrayArg->getElementType(),
1095e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall                                     Info, Deduced, SubTDF);
10964d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson    }
10974d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson
1098199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     type [i]
1099199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    case Type::DependentSizedArray: {
1100a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg);
1101199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (!ArrayArg)
1102f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
11031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1104e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall      unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
1105e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall
1106199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // Check the element type of the arrays
1107199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      const DependentSizedArrayType *DependentArrayParm
1108a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = S.Context.getAsDependentSizedArrayType(Param);
1109f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (Sema::TemplateDeductionResult Result
1110a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            = DeduceTemplateArguments(S, TemplateParams,
1111f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      DependentArrayParm->getElementType(),
1112f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      ArrayArg->getElementType(),
1113e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall                                      Info, Deduced, SubTDF))
1114f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Result;
11151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1116199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // Determine the array bound is something we can deduce.
11171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NonTypeTemplateParmDecl *NTTP
1118199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor        = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
1119199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (!NTTP)
1120f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_Success;
11211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We can perform template argument deduction for the given non-type
1123199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // template parameter.
11241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(NTTP->getDepth() == 0 &&
1125199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor             "Cannot deduce non-type template argument at depth > 0");
11261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (const ConstantArrayType *ConstantArrayArg
1127335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson            = dyn_cast<ConstantArrayType>(ArrayArg)) {
1128335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson        llvm::APSInt Size(ConstantArrayArg->getSize());
1129dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        return DeduceNonTypeTemplateArgument(S, NTTP, Size,
11309d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor                                             S.Context.getSizeType(),
113102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                             /*ArrayBound=*/true,
1132f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                             Info, Deduced);
1133335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson      }
1134199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (const DependentSizedArrayType *DependentArrayArg
1135199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor            = dyn_cast<DependentSizedArrayType>(ArrayArg))
113634c2f8c8a16226f757947bf08c5f799d99c9ac1eDouglas Gregor        if (DependentArrayArg->getSizeExpr())
113734c2f8c8a16226f757947bf08c5f799d99c9ac1eDouglas Gregor          return DeduceNonTypeTemplateArgument(S, NTTP,
113834c2f8c8a16226f757947bf08c5f799d99c9ac1eDouglas Gregor                                               DependentArrayArg->getSizeExpr(),
113934c2f8c8a16226f757947bf08c5f799d99c9ac1eDouglas Gregor                                               Info, Deduced);
11401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1141199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // Incomplete type does not match a dependently-sized array type
1142f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
1143199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    }
11441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     type(*)(T)
11461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(*)()
11471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(*)(T)
1148a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson    case Type::FunctionProto: {
114973b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList;
11501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const FunctionProtoType *FunctionProtoArg =
1151a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson        dyn_cast<FunctionProtoType>(Arg);
1152a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson      if (!FunctionProtoArg)
1153f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
11541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const FunctionProtoType *FunctionProtoParam =
1156a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson        cast<FunctionProtoType>(Param);
1157994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson
1158dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      if (FunctionProtoParam->getTypeQuals()
1159e3c7a7ca66c02567543dbb5ec493818e00e8b177Douglas Gregor            != FunctionProtoArg->getTypeQuals() ||
1160dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi          FunctionProtoParam->getRefQualifier()
1161e3c7a7ca66c02567543dbb5ec493818e00e8b177Douglas Gregor            != FunctionProtoArg->getRefQualifier() ||
1162e3c7a7ca66c02567543dbb5ec493818e00e8b177Douglas Gregor          FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
1163f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
1164994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson
1165a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson      // Check return types.
1166f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (Sema::TemplateDeductionResult Result
1167a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            = DeduceTemplateArguments(S, TemplateParams,
1168f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      FunctionProtoParam->getResultType(),
1169f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      FunctionProtoArg->getResultType(),
1170508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                      Info, Deduced, 0))
1171f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Result;
11721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1173603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      return DeduceTemplateArguments(S, TemplateParams,
1174603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                                     FunctionProtoParam->arg_type_begin(),
1175603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                                     FunctionProtoParam->getNumArgs(),
1176603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                                     FunctionProtoArg->arg_type_begin(),
1177603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                                     FunctionProtoArg->getNumArgs(),
117873b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor                                     Info, Deduced, SubTDF);
1179a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson    }
11801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11813cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    case Type::InjectedClassName: {
11823cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // Treat a template's injected-class-name as if the template
11833cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // specialization type had been used.
118431f17ecbef57b5679c017c375db330546b7b5145John McCall      Param = cast<InjectedClassNameType>(Param)
118531f17ecbef57b5679c017c375db330546b7b5145John McCall        ->getInjectedSpecializationType();
11863cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      assert(isa<TemplateSpecializationType>(Param) &&
11873cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall             "injected class name is not a template specialization type");
11883cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // fall through
11893cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    }
11903cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
1191f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    //     template-name<T> (where template-name refers to a class template)
1192d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor    //     template-name<i>
1193db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    //     TT<T>
1194db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    //     TT<i>
1195db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    //     TT<>
1196d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor    case Type::TemplateSpecialization: {
1197d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor      const TemplateSpecializationType *SpecParam
1198d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor        = cast<TemplateSpecializationType>(Param);
11991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1200de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      // Try to deduce template arguments from the template-id.
1201de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      Sema::TemplateDeductionResult Result
1202a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg,
1203de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                  Info, Deduced);
12041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12054a5c15f75f76b95e1c2ceb6fa2737dcadd5f4be1Douglas Gregor      if (Result && (TDF & TDF_DerivedClass)) {
1206de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        // C++ [temp.deduct.call]p3b3:
1207de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   If P is a class, and P has the form template-id, then A can be a
1208de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   derived class of the deduced A. Likewise, if P is a pointer to a
12091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        //   class of the form template-id, A can be a pointer to a derived
1210de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   class pointed to by the deduced A.
1211de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //
1212de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        // More importantly:
12131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        //   These alternatives are considered only if type deduction would
1214de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   otherwise fail.
1215a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        if (const RecordType *RecordT = Arg->getAs<RecordType>()) {
1216a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          // We cannot inspect base classes as part of deduction when the type
1217a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          // is incomplete, so either instantiate any templates necessary to
1218a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          // complete the type, or skip over it if it cannot be completed.
12195769d6195087229770d7ac90449443e026c47103John McCall          if (S.RequireCompleteType(Info.getLocation(), Arg, 0))
1220a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            return Result;
1221a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth
1222de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          // Use data recursion to crawl through the list of base classes.
12231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          // Visited contains the set of nodes we have already visited, while
1224de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          // ToVisit is our stack of records that we still need to visit.
1225de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          llvm::SmallPtrSet<const RecordType *, 8> Visited;
1226de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          llvm::SmallVector<const RecordType *, 8> ToVisit;
1227de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          ToVisit.push_back(RecordT);
1228de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          bool Successful = false;
1229053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor          llvm::SmallVectorImpl<DeducedTemplateArgument> DeducedOrig(0);
1230053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor          DeducedOrig = Deduced;
1231de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          while (!ToVisit.empty()) {
1232de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // Retrieve the next class in the inheritance hierarchy.
1233de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            const RecordType *NextT = ToVisit.back();
1234de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            ToVisit.pop_back();
12351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1236de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // If we have already seen this type, skip it.
1237de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            if (!Visited.insert(NextT))
1238de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              continue;
12391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1240de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // If this is a base class, try to perform template argument
1241de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // deduction from it.
1242de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            if (NextT != RecordT) {
1243de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              Sema::TemplateDeductionResult BaseResult
1244a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth                = DeduceTemplateArguments(S, TemplateParams, SpecParam,
1245de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                          QualType(NextT, 0), Info, Deduced);
12461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1247de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              // If template argument deduction for this base was successful,
1248053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor              // note that we had some success. Otherwise, ignore any deductions
1249053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor              // from this base class.
1250053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor              if (BaseResult == Sema::TDK_Success) {
1251de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                Successful = true;
1252053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor                DeducedOrig = Deduced;
1253053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor              }
1254053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor              else
1255053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor                Deduced = DeducedOrig;
1256de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            }
12571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1258de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // Visit base classes
1259de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
1260de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
1261de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                                 BaseEnd = Next->bases_end();
12629994a34f6cf842721ba7723edc0b9036229fe387Sebastian Redl                 Base != BaseEnd; ++Base) {
12631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump              assert(Base->getType()->isRecordType() &&
1264de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                     "Base class that isn't a record?");
12656217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek              ToVisit.push_back(Base->getType()->getAs<RecordType>());
1266de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            }
1267de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          }
12681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1269de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          if (Successful)
1270de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            return Sema::TDK_Success;
1271de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        }
12721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1273de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      }
12741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1275de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      return Result;
1276d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor    }
1277d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor
1278637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T type::*
1279637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T T::*
1280637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (type::*)()
1281637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     type (T::*)()
1282637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     type (type::*)(T)
1283637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     type (T::*)(T)
1284637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (type::*)(T)
1285637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (T::*)()
1286637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (T::*)(T)
1287637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    case Type::MemberPointer: {
1288637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
1289637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
1290637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      if (!MemPtrArg)
1291f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
1292f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
1293f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (Sema::TemplateDeductionResult Result
1294a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            = DeduceTemplateArguments(S, TemplateParams,
1295f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      MemPtrParam->getPointeeType(),
1296f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      MemPtrArg->getPointeeType(),
1297508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                      Info, Deduced,
1298508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                      TDF & TDF_IgnoreQualifiers))
1299f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Result;
1300f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
1301a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
1302f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                     QualType(MemPtrParam->getClass(), 0),
1303f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                     QualType(MemPtrArg->getClass(), 0),
1304508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
1305637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    }
1306637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor
13079a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    //     (clang extension)
13089a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    //
13091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     type(^)(T)
13101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(^)()
13111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(^)(T)
1312859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson    case Type::BlockPointer: {
1313859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson      const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
1314859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson      const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
13151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1316859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson      if (!BlockPtrArg)
1317f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
13181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1319a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
1320859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson                                     BlockPtrParam->getPointeeType(),
1321f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                     BlockPtrArg->getPointeeType(), Info,
1322508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Deduced, 0);
1323859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson    }
1324859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson
1325637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    case Type::TypeOfExpr:
1326637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    case Type::TypeOf:
13274714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    case Type::DependentName:
1328637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      // No template argument deduction for these types
1329f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_Success;
1330637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor
1331d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    default:
1332d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor      break;
13330b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
13340b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
13350b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  // FIXME: Many more cases to go (to go).
1336f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  return Sema::TDK_Success;
13370b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
13380b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
1339f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
1340a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
1341f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateParameterList *TemplateParams,
1342f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        const TemplateArgument &Param,
134377d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor                        TemplateArgument Arg,
13442a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                        TemplateDeductionInfo &Info,
134502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
134677d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor  // If the template argument is a pack expansion, perform template argument
134777d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor  // deduction against the pattern of that expansion. This only occurs during
134877d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor  // partial ordering.
134977d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor  if (Arg.isPackExpansion())
135077d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor    Arg = Arg.getPackExpansionPattern();
1351dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
13520b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  switch (Param.getKind()) {
1353199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Null:
1354199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    assert(false && "Null template argument in parameter list");
1355199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    break;
13561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case TemplateArgument::Type:
1358788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    if (Arg.getKind() == TemplateArgument::Type)
1359a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams, Param.getAsType(),
1360788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                                     Arg.getAsType(), Info, Deduced, 0);
1361788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.FirstArg = Param;
1362788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.SecondArg = Arg;
1363788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
1364a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
1365788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  case TemplateArgument::Template:
1366db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    if (Arg.getKind() == TemplateArgument::Template)
1367dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      return DeduceTemplateArguments(S, TemplateParams,
1368788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                                     Param.getAsTemplate(),
1369db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor                                     Arg.getAsTemplate(), Info, Deduced);
1370788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.FirstArg = Param;
1371788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.SecondArg = Arg;
1372788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
1373a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
1374a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor  case TemplateArgument::TemplateExpansion:
1375a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    llvm_unreachable("caller should handle pack expansions");
1376a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    break;
1377dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1378199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Declaration:
1379788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    if (Arg.getKind() == TemplateArgument::Declaration &&
1380788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor        Param.getAsDecl()->getCanonicalDecl() ==
1381788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor          Arg.getAsDecl()->getCanonicalDecl())
1382788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return Sema::TDK_Success;
1383dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1384f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.FirstArg = Param;
1385f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.SecondArg = Arg;
1386f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_NonDeducedMismatch;
13871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1388199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Integral:
1389199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    if (Arg.getKind() == TemplateArgument::Integral) {
13909d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor      if (hasSameExtendedValue(*Param.getAsIntegral(), *Arg.getAsIntegral()))
1391f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_Success;
1392f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
1393f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.FirstArg = Param;
1394f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.SecondArg = Arg;
1395f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
1396f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    }
1397f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
1398f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    if (Arg.getKind() == TemplateArgument::Expression) {
1399f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.FirstArg = Param;
1400f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.SecondArg = Arg;
1401f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
1402199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    }
1403199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
1404f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.FirstArg = Param;
1405f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.SecondArg = Arg;
1406f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_NonDeducedMismatch;
14071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1408199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Expression: {
14091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (NonTypeTemplateParmDecl *NTTP
1410199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor          = getDeducedParameterFromExpr(Param.getAsExpr())) {
1411199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (Arg.getKind() == TemplateArgument::Integral)
1412a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP,
14131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             *Arg.getAsIntegral(),
14149d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor                                             Arg.getIntegralType(),
141502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                             /*ArrayBound=*/false,
1416f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                             Info, Deduced);
1417199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (Arg.getKind() == TemplateArgument::Expression)
1418a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(),
1419f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                             Info, Deduced);
142015755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor      if (Arg.getKind() == TemplateArgument::Declaration)
1421a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(),
142215755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor                                             Info, Deduced);
1423dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1424f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.FirstArg = Param;
1425f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.SecondArg = Arg;
1426f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
1427199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    }
14281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1429199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    // Can't deduce anything, but that's okay.
1430f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_Success;
1431199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  }
1432d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson  case TemplateArgument::Pack:
143320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor    llvm_unreachable("Argument packs should be expanded by the caller!");
14340b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
14351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1436f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  return Sema::TDK_Success;
14370b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
14380b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
143920a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// \brief Determine whether there is a template argument to be used for
144020a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// deduction.
144120a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor///
144220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// This routine "expands" argument packs in-place, overriding its input
144320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// parameters so that \c Args[ArgIdx] will be the available template argument.
144420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor///
144520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// \returns true if there is another template argument (which will be at
144620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// \c Args[ArgIdx]), false otherwise.
1447dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic bool hasTemplateArgumentForDeduction(const TemplateArgument *&Args,
144820a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                                            unsigned &ArgIdx,
144920a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                                            unsigned &NumArgs) {
145020a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  if (ArgIdx == NumArgs)
145120a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor    return false;
1452dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
145320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  const TemplateArgument &Arg = Args[ArgIdx];
145420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  if (Arg.getKind() != TemplateArgument::Pack)
145520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor    return true;
145620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor
145720a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  assert(ArgIdx == NumArgs - 1 && "Pack not at the end of argument list?");
145820a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  Args = Arg.pack_begin();
145920a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  NumArgs = Arg.pack_size();
146020a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  ArgIdx = 0;
146120a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  return ArgIdx < NumArgs;
146220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor}
146320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor
14647b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor/// \brief Determine whether the given set of template arguments has a pack
14657b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor/// expansion that is not the last template argument.
14667b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregorstatic bool hasPackExpansionBeforeEnd(const TemplateArgument *Args,
14677b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor                                      unsigned NumArgs) {
14687b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor  unsigned ArgIdx = 0;
14697b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor  while (ArgIdx < NumArgs) {
14707b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    const TemplateArgument &Arg = Args[ArgIdx];
1471dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
14727b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    // Unwrap argument packs.
14737b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    if (Args[ArgIdx].getKind() == TemplateArgument::Pack) {
14747b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      Args = Arg.pack_begin();
14757b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      NumArgs = Arg.pack_size();
14767b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      ArgIdx = 0;
14777b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      continue;
14787b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    }
1479dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
14807b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    ++ArgIdx;
14817b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    if (ArgIdx == NumArgs)
14827b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      return false;
1483dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
14847b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    if (Arg.isPackExpansion())
14857b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      return true;
14867b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor  }
1487dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
14887b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor  return false;
14897b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor}
14907b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor
14911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic Sema::TemplateDeductionResult
1492a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
1493f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateParameterList *TemplateParams,
149420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        const TemplateArgument *Params, unsigned NumParams,
149520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        const TemplateArgument *Args, unsigned NumArgs,
14962a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                        TemplateDeductionInfo &Info,
14970972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
14980972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor                        bool NumberOfArgumentsMustMatch) {
1499e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor  // C++0x [temp.deduct.type]p9:
1500dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   If the template argument list of P contains a pack expansion that is not
1501dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   the last template argument, the entire template argument list is a
1502e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor  //   non-deduced context.
15037b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor  if (hasPackExpansionBeforeEnd(Params, NumParams))
15047b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    return Sema::TDK_Success;
1505dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1506e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor  // C++0x [temp.deduct.type]p9:
1507dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   If P has a form that contains <T> or <i>, then each argument Pi of the
1508dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   respective template argument list P is compared with the corresponding
1509e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor  //   argument Ai of the corresponding template argument list of A.
151020a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  unsigned ArgIdx = 0, ParamIdx = 0;
1511dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  for (; hasTemplateArgumentForDeduction(Params, ParamIdx, NumParams);
151220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor       ++ParamIdx) {
151320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor    if (!Params[ParamIdx].isPackExpansion()) {
1514e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      // The simple case: deduce template arguments by matching Pi and Ai.
1515dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
151620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor      // Check whether we have enough arguments.
151720a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor      if (!hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs))
15183cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor        return NumberOfArgumentsMustMatch? Sema::TDK_NonDeducedMismatch
15190972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor                                         : Sema::TDK_Success;
1520dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
152177d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor      if (Args[ArgIdx].isPackExpansion()) {
152277d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor        // FIXME: We follow the logic of C++0x [temp.deduct.type]p22 here,
152377d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor        // but applied to pack expansions that are template arguments.
152477d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
152577d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor      }
1526dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1527e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      // Perform deduction for this Pi/Ai pair.
152820a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor      if (Sema::TemplateDeductionResult Result
152977d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor            = DeduceTemplateArguments(S, TemplateParams,
153077d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor                                      Params[ParamIdx], Args[ArgIdx],
153177d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor                                      Info, Deduced))
1532dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        return Result;
1533dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
153420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor      // Move to the next argument.
153520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor      ++ArgIdx;
153620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor      continue;
153720a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor    }
1538dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1539e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // The parameter is a pack expansion.
1540dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1541e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // C++0x [temp.deduct.type]p9:
1542dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   If Pi is a pack expansion, then the pattern of Pi is compared with
1543dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   each remaining argument in the template argument list of A. Each
1544dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   comparison deduces template arguments for subsequent positions in the
1545e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    //   template parameter packs expanded by Pi.
1546e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern();
1547dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1548e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // Compute the set of template parameter indices that correspond to
1549e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // parameter packs expanded by the pack expansion.
1550e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    llvm::SmallVector<unsigned, 2> PackIndices;
1551e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    {
1552e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      llvm::BitVector SawIndices(TemplateParams->size());
1553e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1554e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      S.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1555e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
1556e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor        unsigned Depth, Index;
1557e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor        llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]);
1558e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor        if (Depth == 0 && !SawIndices[Index]) {
1559e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor          SawIndices[Index] = true;
1560e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor          PackIndices.push_back(Index);
1561e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor        }
1562e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      }
1563e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    }
1564e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?");
1565dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1566e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // FIXME: If there are no remaining arguments, we can bail out early
1567e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // and set any deduced parameter packs to an empty argument pack.
1568e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // The latter part of this is a (minor) correctness issue.
1569dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1570e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // Save the deduced template arguments for each parameter pack expanded
1571e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // by this pack expansion, then clear out the deduction.
1572dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    llvm::SmallVector<DeducedTemplateArgument, 2>
1573e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      SavedPacks(PackIndices.size());
15745429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor    llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2>
15755429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor      NewlyDeducedPacks(PackIndices.size());
1576dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    PrepareArgumentPackDeduction(S, Deduced, PackIndices, SavedPacks,
15775429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor                                 NewlyDeducedPacks);
1578e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor
1579e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // Keep track of the deduced template arguments for each parameter pack
1580dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    // expanded by this pack expansion (the outer index) and for each
1581e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // template argument (the inner SmallVectors).
1582e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    bool HasAnyArguments = false;
1583e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    while (hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs)) {
1584e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      HasAnyArguments = true;
1585dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1586e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      // Deduce template arguments from the pattern.
1587dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      if (Sema::TemplateDeductionResult Result
1588e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor            = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx],
1589e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                                      Info, Deduced))
1590e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor        return Result;
1591dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1592e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      // Capture the deduced template arguments for each parameter pack expanded
1593e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      // by this pack expansion, add them to the list of arguments we've deduced
1594e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      // for that pack, then clear out the deduced argument.
1595e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
1596e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor        DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]];
1597e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor        if (!DeducedArg.isNull()) {
1598e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor          NewlyDeducedPacks[I].push_back(DeducedArg);
1599e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor          DeducedArg = DeducedTemplateArgument();
1600e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor        }
1601e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      }
1602dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1603e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      ++ArgIdx;
1604e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    }
1605dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1606e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // Build argument packs for each of the parameter packs expanded by this
1607e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // pack expansion.
16080216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    if (Sema::TemplateDeductionResult Result
1609dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi          = FinishArgumentPackDeduction(S, TemplateParams, HasAnyArguments,
16100216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                                        Deduced, PackIndices, SavedPacks,
16110216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                                        NewlyDeducedPacks, Info))
1612dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      return Result;
16130b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
1614dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
161520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  // If there is an argument remaining, then we had too many arguments.
16160972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor  if (NumberOfArgumentsMustMatch &&
16170972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor      hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs))
16183cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor    return Sema::TDK_NonDeducedMismatch;
1619dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1620f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  return Sema::TDK_Success;
16210b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
16220b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
162320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregorstatic Sema::TemplateDeductionResult
162420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas GregorDeduceTemplateArguments(Sema &S,
162520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        TemplateParameterList *TemplateParams,
162620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        const TemplateArgumentList &ParamList,
162720a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        const TemplateArgumentList &ArgList,
162820a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        TemplateDeductionInfo &Info,
162920a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
1630dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  return DeduceTemplateArguments(S, TemplateParams,
163120a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                                 ParamList.data(), ParamList.size(),
163220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                                 ArgList.data(), ArgList.size(),
163320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                                 Info, Deduced);
163420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor}
163520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor
1636f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// \brief Determine whether two template arguments are the same.
16371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool isSameTemplateArg(ASTContext &Context,
1638f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor                              const TemplateArgument &X,
1639f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor                              const TemplateArgument &Y) {
1640f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  if (X.getKind() != Y.getKind())
1641f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    return false;
16421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1643f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  switch (X.getKind()) {
1644f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Null:
1645f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      assert(false && "Comparing NULL template argument");
1646f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      break;
16471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1648f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Type:
1649f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      return Context.getCanonicalType(X.getAsType()) ==
1650f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor             Context.getCanonicalType(Y.getAsType());
16511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1652f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Declaration:
165397fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis      return X.getAsDecl()->getCanonicalDecl() ==
165497fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis             Y.getAsDecl()->getCanonicalDecl();
16551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1656788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Template:
1657a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    case TemplateArgument::TemplateExpansion:
1658a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      return Context.getCanonicalTemplateName(
1659a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                    X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() ==
1660a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor             Context.getCanonicalTemplateName(
1661a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                    Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer();
1662dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1663f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Integral:
1664f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      return *X.getAsIntegral() == *Y.getAsIntegral();
16651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1666788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Expression: {
1667788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      llvm::FoldingSetNodeID XID, YID;
1668788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      X.getAsExpr()->Profile(XID, Context, true);
1669dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      Y.getAsExpr()->Profile(YID, Context, true);
1670788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return XID == YID;
1671788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    }
16721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1673f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Pack:
1674f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      if (X.pack_size() != Y.pack_size())
1675f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor        return false;
16761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (TemplateArgument::pack_iterator XP = X.pack_begin(),
16781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        XPEnd = X.pack_end(),
1679f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor                                           YP = Y.pack_begin();
16801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump           XP != XPEnd; ++XP, ++YP)
1681f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor        if (!isSameTemplateArg(Context, *XP, *YP))
1682f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor          return false;
1683f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
1684f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      return true;
1685f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  }
1686f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
1687f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  return false;
1688f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor}
1689f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
169054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// \brief Allocate a TemplateArgumentLoc where all locations have
169154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// been initialized to the given location.
169254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor///
169354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// \param S The semantic analysis object.
169454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor///
169554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// \param The template argument we are producing template argument
169654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// location information for.
169754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor///
169854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// \param NTTPType For a declaration template argument, the type of
169954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// the non-type template parameter that corresponds to this template
170054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// argument.
170154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor///
170254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// \param Loc The source location to use for the resulting template
170354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// argument.
1704dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic TemplateArgumentLoc
170554c53cca105ed595e12fecf04e415c3712bda936Douglas GregorgetTrivialTemplateArgumentLoc(Sema &S,
1706dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                              const TemplateArgument &Arg,
170754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                              QualType NTTPType,
170854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                              SourceLocation Loc) {
170954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  switch (Arg.getKind()) {
171054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  case TemplateArgument::Null:
171154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    llvm_unreachable("Can't get a NULL template argument here");
171254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    break;
1713dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
171454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  case TemplateArgument::Type:
1715dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return TemplateArgumentLoc(Arg,
171654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                     S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
1717dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
171854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  case TemplateArgument::Declaration: {
171954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    Expr *E
1720ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor      = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc)
172154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    .takeAs<Expr>();
172254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    return TemplateArgumentLoc(TemplateArgument(E), E);
172354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  }
1724dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
172554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  case TemplateArgument::Integral: {
172654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    Expr *E
1727ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor      = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).takeAs<Expr>();
172854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    return TemplateArgumentLoc(TemplateArgument(E), E);
172954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  }
1730dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1731b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor    case TemplateArgument::Template:
1732b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor    case TemplateArgument::TemplateExpansion: {
1733b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor      NestedNameSpecifierLocBuilder Builder;
1734b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor      TemplateName Template = Arg.getAsTemplate();
1735b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor      if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
1736b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor        Builder.MakeTrivial(S.Context, DTN->getQualifier(), Loc);
1737b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor      else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
1738b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor        Builder.MakeTrivial(S.Context, QTN->getQualifier(), Loc);
1739b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor
1740b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor      if (Arg.getKind() == TemplateArgument::Template)
1741b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor        return TemplateArgumentLoc(Arg,
1742b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor                                   Builder.getWithLocInContext(S.Context),
1743b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor                                   Loc);
1744b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor
1745b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor
1746b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor      return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(S.Context),
1747b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor                                 Loc, Loc);
1748b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor    }
1749a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
175054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  case TemplateArgument::Expression:
175154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    return TemplateArgumentLoc(Arg, Arg.getAsExpr());
1752dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
175354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  case TemplateArgument::Pack:
175454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo());
175554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  }
1756dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
175754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  return TemplateArgumentLoc();
175854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor}
175954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor
176054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor
176154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// \brief Convert the given deduced template argument and add it to the set of
176254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// fully-converted template arguments.
1763dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic bool ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param,
176454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                           DeducedTemplateArgument Arg,
1765dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                           NamedDecl *Template,
1766dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                           QualType NTTPType,
17676952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                           unsigned ArgumentPackIndex,
176854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                           TemplateDeductionInfo &Info,
176954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                           bool InFunctionTemplate,
177054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                             llvm::SmallVectorImpl<TemplateArgument> &Output) {
177154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  if (Arg.getKind() == TemplateArgument::Pack) {
177254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    // This is a template argument pack, so check each of its arguments against
177354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    // the template parameter.
177454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    llvm::SmallVector<TemplateArgument, 2> PackedArgsBuilder;
1775dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    for (TemplateArgument::pack_iterator PA = Arg.pack_begin(),
1776135ffa7375fb5802b92f42774e02d0e6e4c78e5bDouglas Gregor                                      PAEnd = Arg.pack_end();
177754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor         PA != PAEnd; ++PA) {
1778d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor      // When converting the deduced template argument, append it to the
1779d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor      // general output list. We need to do this so that the template argument
1780d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor      // checking logic has all of the prior template arguments available.
178154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor      DeducedTemplateArgument InnerArg(*PA);
178254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor      InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound());
1783dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      if (ConvertDeducedTemplateArgument(S, Param, InnerArg, Template,
17846952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                         NTTPType, PackedArgsBuilder.size(),
17856952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                         Info, InFunctionTemplate, Output))
178654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor        return true;
1787dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1788d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor      // Move the converted template argument into our argument pack.
1789d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor      PackedArgsBuilder.push_back(Output.back());
1790d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor      Output.pop_back();
179154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    }
1792dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
179354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    // Create the resulting argument pack.
1794dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    Output.push_back(TemplateArgument::CreatePackCopy(S.Context,
1795203e6a322ae29d577acafcb1572a57ec16e1e730Douglas Gregor                                                      PackedArgsBuilder.data(),
1796203e6a322ae29d577acafcb1572a57ec16e1e730Douglas Gregor                                                     PackedArgsBuilder.size()));
179754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    return false;
179854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  }
1799dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
180054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  // Convert the deduced template argument into a template
180154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  // argument that we can check, almost as if the user had written
180254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  // the template argument explicitly.
180354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  TemplateArgumentLoc ArgLoc = getTrivialTemplateArgumentLoc(S, Arg, NTTPType,
180454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                                             Info.getLocation());
1805dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
180654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  // Check the template argument, converting it as necessary.
180754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  return S.CheckTemplateArgument(Param, ArgLoc,
180854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                 Template,
180954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                 Template->getLocation(),
181054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                 Template->getSourceRange().getEnd(),
18116952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                 ArgumentPackIndex,
181254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                 Output,
181354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                 InFunctionTemplate
181454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                  ? (Arg.wasDeducedFromArrayBound()
1815dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                       ? Sema::CTAK_DeducedFromArrayBound
181654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                       : Sema::CTAK_Deduced)
181754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                 : Sema::CTAK_Specified);
181854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor}
181954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor
182031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// Complete template argument deduction for a class template partial
182131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// specialization.
182231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregorstatic Sema::TemplateDeductionResult
1823dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiFinishTemplateArgumentDeduction(Sema &S,
182431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                ClassTemplatePartialSpecializationDecl *Partial,
182531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                const TemplateArgumentList &TemplateArgs,
182631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                      llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
18272a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                                TemplateDeductionInfo &Info) {
182831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // Trap errors.
182931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  Sema::SFINAETrap Trap(S);
1830dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
183131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  Sema::ContextRAII SavedContext(S, Partial);
1832f5813826802c2e76cdc13cae834ebfd4518d74a6John McCall
183302cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // C++ [temp.deduct.type]p2:
183402cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  //   [...] or if any template argument remains neither deduced nor
183502cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  //   explicitly specified, template argument deduction fails.
1836910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor  llvm::SmallVector<TemplateArgument, 4> Builder;
1837033a3cad525e5b221ba035ffc3e0abfa1241d90eDouglas Gregor  TemplateParameterList *PartialParams = Partial->getTemplateParameters();
1838033a3cad525e5b221ba035ffc3e0abfa1241d90eDouglas Gregor  for (unsigned I = 0, N = PartialParams->size(); I != N; ++I) {
183954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    NamedDecl *Param = PartialParams->getParam(I);
1840f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    if (Deduced[I].isNull()) {
184154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor      Info.Param = makeTemplateParameter(Param);
184231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor      return Sema::TDK_Incomplete;
1843f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    }
1844dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
184554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    // We have deduced this argument, so it still needs to be
184654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    // checked and converted.
1847dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
184854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    // First, for a non-type template parameter type that is
184954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    // initialized by a declaration, we need the type of the
185054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    // corresponding non-type template parameter.
185154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    QualType NTTPType;
1852dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    if (NonTypeTemplateParmDecl *NTTP
1853d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor                                  = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
185454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor      NTTPType = NTTP->getType();
1855d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor      if (NTTPType->isDependentType()) {
1856dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1857d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor                                          Builder.data(), Builder.size());
1858d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor        NTTPType = S.SubstType(NTTPType,
1859d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor                               MultiLevelTemplateArgumentList(TemplateArgs),
1860d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor                               NTTP->getLocation(),
1861d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor                               NTTP->getDeclName());
1862d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor        if (NTTPType.isNull()) {
1863d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor          Info.Param = makeTemplateParameter(Param);
1864d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor          // FIXME: These template arguments are temporary. Free them!
1865dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi          Info.reset(TemplateArgumentList::CreateCopy(S.Context,
1866dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                      Builder.data(),
1867d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor                                                      Builder.size()));
1868d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor          return Sema::TDK_SubstitutionFailure;
1869d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor        }
1870d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor      }
1871d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor    }
1872d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor
187354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    if (ConvertDeducedTemplateArgument(S, Param, Deduced[I],
18746952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                       Partial, NTTPType, 0, Info, false,
187554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                       Builder)) {
187654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor      Info.Param = makeTemplateParameter(Param);
187754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor      // FIXME: These template arguments are temporary. Free them!
1878dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
1879dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                  Builder.size()));
188054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor      return Sema::TDK_SubstitutionFailure;
188154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    }
188202cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  }
1883dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
188402cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // Form the template argument list from the deduced template arguments.
18851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentList *DeducedArgumentList
1886dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    = TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
1887910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                       Builder.size());
1888910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor
1889f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  Info.reset(DeducedArgumentList);
189002cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor
189102cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // Substitute the deduced template arguments into the template
189202cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // arguments of the class template partial specialization, and
189302cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // verify that the instantiated template arguments are both valid
189402cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // and are equivalent to the template arguments originally provided
18951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to the class template.
18962a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope InstScope(S);
189702cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
1898833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  const TemplateArgumentLoc *PartialTemplateArgs
1899833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    = Partial->getTemplateArgsAsWritten();
1900d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
1901d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  // Note that we don't provide the langle and rangle locations.
1902d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstArgs;
1903d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
1904e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor  if (S.Subst(PartialTemplateArgs,
1905e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor              Partial->getNumTemplateArgsAsWritten(),
1906e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor              InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
1907e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx;
1908e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    if (ParamIdx >= Partial->getTemplateParameters()->size())
1909e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      ParamIdx = Partial->getTemplateParameters()->size() - 1;
1910e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor
1911e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    Decl *Param
1912e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      = const_cast<NamedDecl *>(
1913e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                          Partial->getTemplateParameters()->getParam(ParamIdx));
1914e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    Info.Param = makeTemplateParameter(Param);
1915e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument();
1916e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    return Sema::TDK_SubstitutionFailure;
1917833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  }
1918833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1919910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor  llvm::SmallVector<TemplateArgument, 4> ConvertedInstArgs;
192031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
192154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                  InstArgs, false, ConvertedInstArgs))
192231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return Sema::TDK_SubstitutionFailure;
1923dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
192454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  TemplateParameterList *TemplateParams
192554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    = ClassTemplate->getTemplateParameters();
192654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) {
1927910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor    TemplateArgument InstArg = ConvertedInstArgs.data()[I];
192831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) {
19292fdc5e8199e1e239620f2faae88997153703e16fDouglas Gregor      Info.Param = makeTemplateParameter(TemplateParams->getParam(I));
1930f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Info.FirstArg = TemplateArgs[I];
1931f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Info.SecondArg = InstArg;
193231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor      return Sema::TDK_NonDeducedMismatch;
1933f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    }
193402cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  }
193502cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor
1936bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor  if (Trap.hasErrorOccurred())
193731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return Sema::TDK_SubstitutionFailure;
1938bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor
193931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  return Sema::TDK_Success;
194031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor}
194131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor
194231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// \brief Perform template argument deduction to determine whether
194331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// the given template arguments match the given class template
194431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// partial specialization per C++ [temp.class.spec.match].
194531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas GregorSema::TemplateDeductionResult
194631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas GregorSema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
194731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                              const TemplateArgumentList &TemplateArgs,
194831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                              TemplateDeductionInfo &Info) {
194931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // C++ [temp.class.spec.match]p2:
195031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  //   A partial specialization matches a given actual template
195131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  //   argument list if the template arguments of the partial
195231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  //   specialization can be deduced from the actual template argument
195331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  //   list (14.8.2).
195431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  SFINAETrap Trap(*this);
195531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
195631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  Deduced.resize(Partial->getTemplateParameters()->size());
195731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  if (TemplateDeductionResult Result
195831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor        = ::DeduceTemplateArguments(*this,
195931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                    Partial->getTemplateParameters(),
196031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                    Partial->getTemplateArgs(),
196131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                    TemplateArgs, Info, Deduced))
196231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return Result;
196331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor
196431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
19659b623639378d53a675921ddfa7316034d571881eDouglas Gregor                             Deduced.data(), Deduced.size(), Info);
196631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  if (Inst)
196731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return TDK_InstantiationDepth;
196831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor
196931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  if (Trap.hasErrorOccurred())
197031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return Sema::TDK_SubstitutionFailure;
1971dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1972dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs,
197331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                           Deduced, Info);
19740b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
1975031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
19764112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor/// \brief Determine whether the given type T is a simple-template-id type.
19774112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregorstatic bool isSimpleTemplateIdType(QualType T) {
19781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const TemplateSpecializationType *Spec
1979183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall        = T->getAs<TemplateSpecializationType>())
19804112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    return Spec->getTemplateName().getAsTemplateDecl() != 0;
19811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
19824112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor  return false;
19834112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor}
198483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
198583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \brief Substitute the explicitly-provided template arguments into the
198683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// given function template according to C++ [temp.arg.explicit].
198783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
198883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionTemplate the function template into which the explicit
198983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template arguments will be substituted.
199083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
19911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ExplicitTemplateArguments the explicitly-specified template
199283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// arguments.
199383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
19941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param Deduced the deduced template arguments, which will be populated
199583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// with the converted and checked explicit template arguments.
199683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
19971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ParamTypes will be populated with the instantiated function
199883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// parameters.
199983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
200083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionType if non-NULL, the result type of the function template
200183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// will also be instantiated and the pointed-to value will be updated with
200283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// the instantiated function type.
200383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
200483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Info if substitution fails for any reason, this object will be
200583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// populated with more information about the failure.
200683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
200783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \returns TDK_Success if substitution was successful, or some failure
200883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// condition.
200983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::TemplateDeductionResult
201083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::SubstituteExplicitTemplateArguments(
201183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      FunctionTemplateDecl *FunctionTemplate,
201267714230a191bc3c01f33378f34f34ef377991a6Douglas Gregor                               TemplateArgumentListInfo &ExplicitTemplateArgs,
201302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                       llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
201483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                 llvm::SmallVectorImpl<QualType> &ParamTypes,
201583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          QualType *FunctionType,
201683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          TemplateDeductionInfo &Info) {
201783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
201883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateParameterList *TemplateParams
201983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = FunctionTemplate->getTemplateParameters();
202083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
2021d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (ExplicitTemplateArgs.size() == 0) {
202283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    // No arguments to substitute; just copy over the parameter types and
202383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    // fill in the function type.
202483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    for (FunctionDecl::param_iterator P = Function->param_begin(),
202583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                   PEnd = Function->param_end();
202683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor         P != PEnd;
202783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor         ++P)
202883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      ParamTypes.push_back((*P)->getType());
20291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
203083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (FunctionType)
203183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      *FunctionType = Function->getType();
203283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_Success;
203383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
20341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
203583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Substitution of the explicit template arguments into a function template
203683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  /// is a SFINAE context. Trap any errors that might occur.
20371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SFINAETrap Trap(*this);
20381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
203983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // C++ [temp.arg.explicit]p3:
20401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   Template arguments that are present shall be specified in the
20411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   declaration order of their corresponding template-parameters. The
204283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   template argument list shall not specify more template-arguments than
20431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   there are corresponding template-parameters.
2044910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor  llvm::SmallVector<TemplateArgument, 4> Builder;
20451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Enter a new template instantiation context where we check the
204783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // explicitly-specified template arguments against this function template,
204883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // and then substitute them into the function parameter types.
20491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
205083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                             FunctionTemplate, Deduced.data(), Deduced.size(),
20519b623639378d53a675921ddfa7316034d571881eDouglas Gregor           ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution,
20529b623639378d53a675921ddfa7316034d571881eDouglas Gregor                             Info);
205383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (Inst)
205483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_InstantiationDepth;
20551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
205683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (CheckTemplateArgumentList(FunctionTemplate,
205783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                SourceLocation(),
2058d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                ExplicitTemplateArgs,
205983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                true,
2060f1a8445036a2d047c7165d4170e3058cdeaba6ebDouglas Gregor                                Builder) || Trap.hasErrorOccurred()) {
2061910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor    unsigned Index = Builder.size();
2062fe52c91dcd1ecda90b579f789baf70f7a992e3c9Douglas Gregor    if (Index >= TemplateParams->size())
2063fe52c91dcd1ecda90b579f789baf70f7a992e3c9Douglas Gregor      Index = TemplateParams->size() - 1;
2064fe52c91dcd1ecda90b579f789baf70f7a992e3c9Douglas Gregor    Info.Param = makeTemplateParameter(TemplateParams->getParam(Index));
206583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_InvalidExplicitArguments;
2066f1a8445036a2d047c7165d4170e3058cdeaba6ebDouglas Gregor  }
20671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
206883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Form the template argument list from the explicitly-specified
206983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // template arguments.
20701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentList *ExplicitArgumentList
2071910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor    = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size());
207283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Info.reset(ExplicitArgumentList);
2073dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2074df41f18936693f7c62e457eefb9fad5b2d2fe3cdJohn McCall  // Template argument deduction and the final substitution should be
2075df41f18936693f7c62e457eefb9fad5b2d2fe3cdJohn McCall  // done in the context of the templated declaration.  Explicit
2076df41f18936693f7c62e457eefb9fad5b2d2fe3cdJohn McCall  // argument substitution, on the other hand, needs to happen in the
2077df41f18936693f7c62e457eefb9fad5b2d2fe3cdJohn McCall  // calling context.
2078df41f18936693f7c62e457eefb9fad5b2d2fe3cdJohn McCall  ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
2079df41f18936693f7c62e457eefb9fad5b2d2fe3cdJohn McCall
2080dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // If we deduced template arguments for a template parameter pack,
2081d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  // note that the template argument pack is partially substituted and record
2082d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  // the explicit template arguments. They'll be used as part of deduction
2083d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  // for this template parameter pack.
2084d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  for (unsigned I = 0, N = Builder.size(); I != N; ++I) {
2085d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    const TemplateArgument &Arg = Builder[I];
2086d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    if (Arg.getKind() == TemplateArgument::Pack) {
2087d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      CurrentInstantiationScope->SetPartiallySubstitutedPack(
2088dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                 TemplateParams->getParam(I),
2089d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                                             Arg.pack_begin(),
2090d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                                             Arg.pack_size());
2091d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      break;
2092d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    }
2093d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  }
2094d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor
209583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Instantiate the types of each of the function parameters given the
209683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // explicitly-specified template arguments.
2097dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  if (SubstParmTypes(Function->getLocation(),
2098a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor                     Function->param_begin(), Function->getNumParams(),
2099a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor                     MultiLevelTemplateArgumentList(*ExplicitArgumentList),
2100a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor                     ParamTypes))
2101a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor    return TDK_SubstitutionFailure;
210283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
210383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // If the caller wants a full function type back, instantiate the return
210483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // type and form that function type.
210583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (FunctionType) {
210683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    // FIXME: exception-specifications?
21071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const FunctionProtoType *Proto
2108183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall      = Function->getType()->getAs<FunctionProtoType>();
210983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    assert(Proto && "Function template does not have a prototype?");
21101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    QualType ResultType
2112357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor      = SubstType(Proto->getResultType(),
2113357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  MultiLevelTemplateArgumentList(*ExplicitArgumentList),
2114357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  Function->getTypeSpecStartLoc(),
2115357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  Function->getDeclName());
211683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (ResultType.isNull() || Trap.hasErrorOccurred())
211783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_SubstitutionFailure;
21181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    *FunctionType = BuildFunctionType(ResultType,
212083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      ParamTypes.data(), ParamTypes.size(),
212183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      Proto->isVariadic(),
212283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      Proto->getTypeQuals(),
2123c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                                      Proto->getRefQualifier(),
212483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      Function->getLocation(),
2125fa869547eb1cab12d7e0c0dfa8ba594e336b9b32Eli Friedman                                      Function->getDeclName(),
2126fa869547eb1cab12d7e0c0dfa8ba594e336b9b32Eli Friedman                                      Proto->getExtInfo());
212783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (FunctionType->isNull() || Trap.hasErrorOccurred())
212883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_SubstitutionFailure;
212983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
21301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
213183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // C++ [temp.arg.explicit]p2:
21321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   Trailing template arguments that can be deduced (14.8.2) may be
21331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   omitted from the list of explicit template-arguments. If all of the
213483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   template arguments can be deduced, they may all be omitted; in this
213583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   case, the empty template argument list <> itself may also be omitted.
213683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //
2137d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  // Take all of the explicitly-specified arguments and put them into
2138d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  // the set of deduced template arguments. Explicitly-specified
2139d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  // parameter packs, however, will be set to NULL since the deduction
2140d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  // mechanisms handle explicitly-specified argument packs directly.
214183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Deduced.reserve(TemplateParams->size());
2142d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) {
2143d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    const TemplateArgument &Arg = ExplicitArgumentList->get(I);
2144d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    if (Arg.getKind() == TemplateArgument::Pack)
2145d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      Deduced.push_back(DeducedTemplateArgument());
2146d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    else
2147d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      Deduced.push_back(Arg);
2148d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  }
21491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
215083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  return TDK_Success;
215183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor}
215283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
21531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Finish template argument deduction for a function template,
215483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// checking the deduced template arguments for completeness and forming
215583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// the function template specialization.
21561eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSema::TemplateDeductionResult
215783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
215802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                       llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
215902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                      unsigned NumExplicitlySpecified,
216083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      FunctionDecl *&Specialization,
216183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      TemplateDeductionInfo &Info) {
216283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateParameterList *TemplateParams
216383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = FunctionTemplate->getTemplateParameters();
21641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
216551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // Template argument deduction for function templates in a SFINAE context.
216651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // Trap any errors that might occur.
216751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  SFINAETrap Trap(*this);
216851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
216951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // Enter a new template instantiation context while we instantiate the
217051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // actual function declaration.
217151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
217251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                             FunctionTemplate, Deduced.data(), Deduced.size(),
21739b623639378d53a675921ddfa7316034d571881eDouglas Gregor              ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution,
21749b623639378d53a675921ddfa7316034d571881eDouglas Gregor                             Info);
217551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  if (Inst)
217651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    return TDK_InstantiationDepth;
217751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
217896db310ab7ca59e1890ddef25a3701bc2909d20fJohn McCall  ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
2179f5813826802c2e76cdc13cae834ebfd4518d74a6John McCall
218083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // C++ [temp.deduct.type]p2:
218183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   [...] or if any template argument remains neither deduced nor
218283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   explicitly specified, template argument deduction fails.
2183910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor  llvm::SmallVector<TemplateArgument, 4> Builder;
2184b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor  for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
2185b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor    NamedDecl *Param = TemplateParams->getParam(I);
2186dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
218751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    if (!Deduced[I].isNull()) {
21883273b0cea879c7af345d6bf98502bbf73fc4fde1Douglas Gregor      if (I < NumExplicitlySpecified) {
218902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        // We have already fully type-checked and converted this
2190dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        // argument, because it was explicitly-specified. Just record the
21913273b0cea879c7af345d6bf98502bbf73fc4fde1Douglas Gregor        // presence of this argument.
2192910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor        Builder.push_back(Deduced[I]);
219302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        continue;
219402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      }
219502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
219602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // We have deduced this argument, so it still needs to be
219702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // checked and converted.
219802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
219902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // First, for a non-type template parameter type that is
220002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // initialized by a declaration, we need the type of the
220102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // corresponding non-type template parameter.
220202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      QualType NTTPType;
2203dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      if (NonTypeTemplateParmDecl *NTTP
2204dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2205b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor        NTTPType = NTTP->getType();
2206b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor        if (NTTPType->isDependentType()) {
2207dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi          TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
2208b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor                                            Builder.data(), Builder.size());
2209b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor          NTTPType = SubstType(NTTPType,
2210b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor                               MultiLevelTemplateArgumentList(TemplateArgs),
2211b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor                               NTTP->getLocation(),
2212b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor                               NTTP->getDeclName());
2213b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor          if (NTTPType.isNull()) {
2214b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor            Info.Param = makeTemplateParameter(Param);
2215b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor            // FIXME: These template arguments are temporary. Free them!
2216dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi            Info.reset(TemplateArgumentList::CreateCopy(Context,
2217dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                        Builder.data(),
2218b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor                                                        Builder.size()));
2219b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor            return TDK_SubstitutionFailure;
222002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor          }
222102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        }
222202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      }
222302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
2224b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor      if (ConvertDeducedTemplateArgument(*this, Param, Deduced[I],
22256952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                         FunctionTemplate, NTTPType, 0, Info,
222654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                         true, Builder)) {
2227b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor        Info.Param = makeTemplateParameter(Param);
2228910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor        // FIXME: These template arguments are temporary. Free them!
2229dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
2230dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                    Builder.size()));
223102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        return TDK_SubstitutionFailure;
223202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      }
223302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
223451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      continue;
223551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    }
2236dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2237ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor    // C++0x [temp.arg.explicit]p3:
2238dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //    A trailing template parameter pack (14.5.3) not otherwise deduced will
2239ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor    //    be deduced to an empty sequence of template arguments.
2240ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor    // FIXME: Where did the word "trailing" come from?
2241ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor    if (Param->isTemplateParameterPack()) {
2242d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      // We may have had explicitly-specified template arguments for this
2243d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      // template parameter pack. If so, our empty deduction extends the
2244d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      // explicitly-specified set (C++0x [temp.arg.explicit]p9).
2245d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      const TemplateArgument *ExplicitArgs;
2246d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      unsigned NumExplicitArgs;
2247d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      if (CurrentInstantiationScope->getPartiallySubstitutedPack(&ExplicitArgs,
2248d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                                             &NumExplicitArgs)
2249d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor          == Param)
2250d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor        Builder.push_back(TemplateArgument(ExplicitArgs, NumExplicitArgs));
2251dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      else
2252d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor        Builder.push_back(TemplateArgument(0, 0));
2253dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2254ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor      continue;
2255ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor    }
225651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
2257dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    // Substitute into the default template argument, if available.
225851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    TemplateArgumentLoc DefArg
225951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
226051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                              FunctionTemplate->getLocation(),
226151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                  FunctionTemplate->getSourceRange().getEnd(),
226251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                                Param,
226351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                                Builder);
226451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
226551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // If there was no default argument, deduction is incomplete.
226651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    if (DefArg.getArgument().isNull()) {
226783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      Info.Param = makeTemplateParameter(
226851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                         const_cast<NamedDecl *>(TemplateParams->getParam(I)));
226983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_Incomplete;
227083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    }
2271dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
227251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // Check whether we can actually use the default argument.
227351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    if (CheckTemplateArgument(Param, DefArg,
227451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                              FunctionTemplate,
227551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                              FunctionTemplate->getLocation(),
227651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                              FunctionTemplate->getSourceRange().getEnd(),
22776952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                              0, Builder,
227802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                              CTAK_Deduced)) {
227951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      Info.Param = makeTemplateParameter(
228051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                         const_cast<NamedDecl *>(TemplateParams->getParam(I)));
2281910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor      // FIXME: These template arguments are temporary. Free them!
2282dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
2283910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                  Builder.size()));
228451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      return TDK_SubstitutionFailure;
228551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    }
22861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
228751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // If we get here, we successfully used the default template argument.
228883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
22891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
229083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Form the template argument list from the deduced template arguments.
22911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentList *DeducedArgumentList
2292910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor    = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size());
229383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Info.reset(DeducedArgumentList);
22941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Substitute the deduced template arguments into the function template
229683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // declaration to produce the function template specialization.
2297d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor  DeclContext *Owner = FunctionTemplate->getDeclContext();
2298d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor  if (FunctionTemplate->getFriendObjectKind())
2299d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor    Owner = FunctionTemplate->getLexicalDeclContext();
230083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Specialization = cast_or_null<FunctionDecl>(
2301d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor                      SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner,
2302357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                         MultiLevelTemplateArgumentList(*DeducedArgumentList)));
230383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (!Specialization)
230483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_SubstitutionFailure;
23051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2306dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
2307f882574cf640d5c8355965e1c486f9d8d8ffcf47Douglas Gregor         FunctionTemplate->getCanonicalDecl());
2308dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
23091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If the template argument list is owned by the function template
231083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // specialization, release it.
2311ec20f46740a59758b12c22108002395bcf5b6f9bDouglas Gregor  if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList &&
2312ec20f46740a59758b12c22108002395bcf5b6f9bDouglas Gregor      !Trap.hasErrorOccurred())
231383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    Info.take();
23141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
231583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // There may have been an error that did not prevent us from constructing a
231683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // declaration. Mark the declaration invalid and return with a substitution
231783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // failure.
231883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (Trap.hasErrorOccurred()) {
231983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    Specialization->setInvalidDecl(true);
232083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_SubstitutionFailure;
232183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
23221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23239b623639378d53a675921ddfa7316034d571881eDouglas Gregor  // If we suppressed any diagnostics while performing template argument
23249b623639378d53a675921ddfa7316034d571881eDouglas Gregor  // deduction, and if we haven't already instantiated this declaration,
23259b623639378d53a675921ddfa7316034d571881eDouglas Gregor  // keep track of these diagnostics. They'll be emitted if this specialization
23269b623639378d53a675921ddfa7316034d571881eDouglas Gregor  // is actually used.
23279b623639378d53a675921ddfa7316034d571881eDouglas Gregor  if (Info.diag_begin() != Info.diag_end()) {
23289b623639378d53a675921ddfa7316034d571881eDouglas Gregor    llvm::DenseMap<Decl *, llvm::SmallVector<PartialDiagnosticAt, 1> >::iterator
23299b623639378d53a675921ddfa7316034d571881eDouglas Gregor      Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl());
23309b623639378d53a675921ddfa7316034d571881eDouglas Gregor    if (Pos == SuppressedDiagnostics.end())
23319b623639378d53a675921ddfa7316034d571881eDouglas Gregor        SuppressedDiagnostics[Specialization->getCanonicalDecl()]
23329b623639378d53a675921ddfa7316034d571881eDouglas Gregor          .append(Info.diag_begin(), Info.diag_end());
2333dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  }
23349b623639378d53a675921ddfa7316034d571881eDouglas Gregor
23351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return TDK_Success;
233683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor}
233783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
23389c72c6088d591ace8503b842d39448c2040f3033John McCall/// Gets the type of a function for template-argument-deducton
23399c72c6088d591ace8503b842d39448c2040f3033John McCall/// purposes when it's considered as part of an overload set.
2340eff92135d32039c9874dc356f3e93143af6069c1John McCallstatic QualType GetTypeOfFunction(ASTContext &Context,
23419c72c6088d591ace8503b842d39448c2040f3033John McCall                                  const OverloadExpr::FindResult &R,
2342eff92135d32039c9874dc356f3e93143af6069c1John McCall                                  FunctionDecl *Fn) {
2343eff92135d32039c9874dc356f3e93143af6069c1John McCall  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
23449c72c6088d591ace8503b842d39448c2040f3033John McCall    if (Method->isInstance()) {
23459c72c6088d591ace8503b842d39448c2040f3033John McCall      // An instance method that's referenced in a form that doesn't
23469c72c6088d591ace8503b842d39448c2040f3033John McCall      // look like a member pointer is just invalid.
23479c72c6088d591ace8503b842d39448c2040f3033John McCall      if (!R.HasFormOfMemberPointer) return QualType();
23489c72c6088d591ace8503b842d39448c2040f3033John McCall
2349eff92135d32039c9874dc356f3e93143af6069c1John McCall      return Context.getMemberPointerType(Fn->getType(),
2350eff92135d32039c9874dc356f3e93143af6069c1John McCall               Context.getTypeDeclType(Method->getParent()).getTypePtr());
23519c72c6088d591ace8503b842d39448c2040f3033John McCall    }
23529c72c6088d591ace8503b842d39448c2040f3033John McCall
23539c72c6088d591ace8503b842d39448c2040f3033John McCall  if (!R.IsAddressOfOperand) return Fn->getType();
2354eff92135d32039c9874dc356f3e93143af6069c1John McCall  return Context.getPointerType(Fn->getType());
2355eff92135d32039c9874dc356f3e93143af6069c1John McCall}
2356eff92135d32039c9874dc356f3e93143af6069c1John McCall
2357eff92135d32039c9874dc356f3e93143af6069c1John McCall/// Apply the deduction rules for overload sets.
2358eff92135d32039c9874dc356f3e93143af6069c1John McCall///
2359eff92135d32039c9874dc356f3e93143af6069c1John McCall/// \return the null type if this argument should be treated as an
2360eff92135d32039c9874dc356f3e93143af6069c1John McCall/// undeduced context
2361eff92135d32039c9874dc356f3e93143af6069c1John McCallstatic QualType
2362eff92135d32039c9874dc356f3e93143af6069c1John McCallResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
236375f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor                            Expr *Arg, QualType ParamType,
236475f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor                            bool ParamWasReference) {
2365dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
23669c72c6088d591ace8503b842d39448c2040f3033John McCall  OverloadExpr::FindResult R = OverloadExpr::find(Arg);
2367eff92135d32039c9874dc356f3e93143af6069c1John McCall
23689c72c6088d591ace8503b842d39448c2040f3033John McCall  OverloadExpr *Ovl = R.Expression;
2369eff92135d32039c9874dc356f3e93143af6069c1John McCall
237075f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor  // C++0x [temp.deduct.call]p4
237175f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor  unsigned TDF = 0;
237275f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor  if (ParamWasReference)
237375f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor    TDF |= TDF_ParamWithReferenceType;
237475f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor  if (R.IsAddressOfOperand)
237575f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor    TDF |= TDF_IgnoreQualifiers;
237675f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor
2377eff92135d32039c9874dc356f3e93143af6069c1John McCall  // If there were explicit template arguments, we can only find
2378eff92135d32039c9874dc356f3e93143af6069c1John McCall  // something via C++ [temp.arg.explicit]p3, i.e. if the arguments
2379eff92135d32039c9874dc356f3e93143af6069c1John McCall  // unambiguously name a full specialization.
23807bb12da2b0749eeebb21854c77877736969e59f2John McCall  if (Ovl->hasExplicitTemplateArgs()) {
2381eff92135d32039c9874dc356f3e93143af6069c1John McCall    // But we can still look for an explicit specialization.
2382eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (FunctionDecl *ExplicitSpec
23837bb12da2b0749eeebb21854c77877736969e59f2John McCall          = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
23849c72c6088d591ace8503b842d39448c2040f3033John McCall      return GetTypeOfFunction(S.Context, R, ExplicitSpec);
2385eff92135d32039c9874dc356f3e93143af6069c1John McCall    return QualType();
2386eff92135d32039c9874dc356f3e93143af6069c1John McCall  }
2387eff92135d32039c9874dc356f3e93143af6069c1John McCall
2388eff92135d32039c9874dc356f3e93143af6069c1John McCall  // C++0x [temp.deduct.call]p6:
2389eff92135d32039c9874dc356f3e93143af6069c1John McCall  //   When P is a function type, pointer to function type, or pointer
2390eff92135d32039c9874dc356f3e93143af6069c1John McCall  //   to member function type:
2391eff92135d32039c9874dc356f3e93143af6069c1John McCall
2392eff92135d32039c9874dc356f3e93143af6069c1John McCall  if (!ParamType->isFunctionType() &&
2393eff92135d32039c9874dc356f3e93143af6069c1John McCall      !ParamType->isFunctionPointerType() &&
2394eff92135d32039c9874dc356f3e93143af6069c1John McCall      !ParamType->isMemberFunctionPointerType())
2395eff92135d32039c9874dc356f3e93143af6069c1John McCall    return QualType();
2396eff92135d32039c9874dc356f3e93143af6069c1John McCall
2397eff92135d32039c9874dc356f3e93143af6069c1John McCall  QualType Match;
23987bb12da2b0749eeebb21854c77877736969e59f2John McCall  for (UnresolvedSetIterator I = Ovl->decls_begin(),
23997bb12da2b0749eeebb21854c77877736969e59f2John McCall         E = Ovl->decls_end(); I != E; ++I) {
2400eff92135d32039c9874dc356f3e93143af6069c1John McCall    NamedDecl *D = (*I)->getUnderlyingDecl();
2401eff92135d32039c9874dc356f3e93143af6069c1John McCall
2402eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   - If the argument is an overload set containing one or more
2403eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     function templates, the parameter is treated as a
2404eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     non-deduced context.
2405eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (isa<FunctionTemplateDecl>(D))
2406eff92135d32039c9874dc356f3e93143af6069c1John McCall      return QualType();
2407eff92135d32039c9874dc356f3e93143af6069c1John McCall
2408eff92135d32039c9874dc356f3e93143af6069c1John McCall    FunctionDecl *Fn = cast<FunctionDecl>(D);
24099c72c6088d591ace8503b842d39448c2040f3033John McCall    QualType ArgType = GetTypeOfFunction(S.Context, R, Fn);
24109c72c6088d591ace8503b842d39448c2040f3033John McCall    if (ArgType.isNull()) continue;
2411eff92135d32039c9874dc356f3e93143af6069c1John McCall
241275f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor    // Function-to-pointer conversion.
2413dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    if (!ParamWasReference && ParamType->isPointerType() &&
241475f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor        ArgType->isFunctionType())
241575f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor      ArgType = S.Context.getPointerType(ArgType);
2416dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2417eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   - If the argument is an overload set (not containing function
2418eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     templates), trial argument deduction is attempted using each
2419eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     of the members of the set. If deduction succeeds for only one
2420eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     of the overload set members, that member is used as the
2421eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     argument value for the deduction. If deduction succeeds for
2422eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     more than one member of the overload set the parameter is
2423eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     treated as a non-deduced context.
2424eff92135d32039c9874dc356f3e93143af6069c1John McCall
2425eff92135d32039c9874dc356f3e93143af6069c1John McCall    // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
2426eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   Type deduction is done independently for each P/A pair, and
2427eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   the deduced template argument values are then combined.
2428eff92135d32039c9874dc356f3e93143af6069c1John McCall    // So we do not reject deductions which were made elsewhere.
2429dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    llvm::SmallVector<DeducedTemplateArgument, 8>
243002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      Deduced(TemplateParams->size());
24312a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    TemplateDeductionInfo Info(S.Context, Ovl->getNameLoc());
2432eff92135d32039c9874dc356f3e93143af6069c1John McCall    Sema::TemplateDeductionResult Result
2433a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      = DeduceTemplateArguments(S, TemplateParams,
2434eff92135d32039c9874dc356f3e93143af6069c1John McCall                                ParamType, ArgType,
2435eff92135d32039c9874dc356f3e93143af6069c1John McCall                                Info, Deduced, TDF);
2436eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (Result) continue;
2437eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (!Match.isNull()) return QualType();
2438eff92135d32039c9874dc356f3e93143af6069c1John McCall    Match = ArgType;
2439eff92135d32039c9874dc356f3e93143af6069c1John McCall  }
2440eff92135d32039c9874dc356f3e93143af6069c1John McCall
2441eff92135d32039c9874dc356f3e93143af6069c1John McCall  return Match;
2442eff92135d32039c9874dc356f3e93143af6069c1John McCall}
2443eff92135d32039c9874dc356f3e93143af6069c1John McCall
2444dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \brief Perform the adjustments to the parameter and argument types
2445f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor/// described in C++ [temp.deduct.call].
2446f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor///
2447f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor/// \returns true if the caller should not attempt to perform any template
2448f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor/// argument deduction based on this P/A pair.
2449f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregorstatic bool AdjustFunctionParmAndArgTypesForDeduction(Sema &S,
2450f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                          TemplateParameterList *TemplateParams,
2451f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                                      QualType &ParamType,
2452f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                                      QualType &ArgType,
2453f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                                      Expr *Arg,
2454f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                                      unsigned &TDF) {
2455f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // C++0x [temp.deduct.call]p3:
24560099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi  //   If P is a cv-qualified type, the top level cv-qualifiers of P's type
2457f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //   are ignored for type deduction.
2458f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (ParamType.getCVRQualifiers())
2459f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    ParamType = ParamType.getLocalUnqualifiedType();
2460f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>();
2461f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (ParamRefType) {
246234b41d939a1328f484511c6002ba2456db879a29Richard Smith    QualType PointeeType = ParamRefType->getPointeeType();
246334b41d939a1328f484511c6002ba2456db879a29Richard Smith
24642ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor    //   [C++0x] If P is an rvalue reference to a cv-unqualified
24652ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor    //   template parameter and the argument is an lvalue, the type
24662ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor    //   "lvalue reference to A" is used in place of A for type
24672ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor    //   deduction.
246834b41d939a1328f484511c6002ba2456db879a29Richard Smith    if (isa<RValueReferenceType>(ParamType)) {
246934b41d939a1328f484511c6002ba2456db879a29Richard Smith      if (!PointeeType.getQualifiers() &&
247034b41d939a1328f484511c6002ba2456db879a29Richard Smith          isa<TemplateTypeParmType>(PointeeType) &&
24712ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor          Arg->Classify(S.Context).isLValue())
24722ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor        ArgType = S.Context.getLValueReferenceType(ArgType);
24732ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor    }
24742ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor
2475f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //   [...] If P is a reference type, the type referred to by P is used
2476f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //   for type deduction.
247734b41d939a1328f484511c6002ba2456db879a29Richard Smith    ParamType = PointeeType;
2478f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  }
2479dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2480f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // Overload sets usually make this parameter an undeduced
2481f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // context, but there are sometimes special circumstances.
2482f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (ArgType == S.Context.OverloadTy) {
2483f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    ArgType = ResolveOverloadForDeduction(S, TemplateParams,
2484f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                          Arg, ParamType,
2485f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                          ParamRefType != 0);
2486f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    if (ArgType.isNull())
2487f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      return true;
2488f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  }
2489dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2490f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (ParamRefType) {
2491f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    // C++0x [temp.deduct.call]p3:
2492f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //   [...] If P is of the form T&&, where T is a template parameter, and
2493f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //   the argument is an lvalue, the type A& is used in place of A for
2494f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //   type deduction.
2495f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    if (ParamRefType->isRValueReferenceType() &&
2496f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        ParamRefType->getAs<TemplateTypeParmType>() &&
2497f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        Arg->isLValue())
2498f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      ArgType = S.Context.getLValueReferenceType(ArgType);
2499f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  } else {
2500f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    // C++ [temp.deduct.call]p2:
2501f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //   If P is not a reference type:
2502f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //   - If A is an array type, the pointer type produced by the
2503f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //     array-to-pointer standard conversion (4.2) is used in place of
2504f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //     A for type deduction; otherwise,
2505f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    if (ArgType->isArrayType())
2506f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      ArgType = S.Context.getArrayDecayedType(ArgType);
2507f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //   - If A is a function type, the pointer type produced by the
2508f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //     function-to-pointer standard conversion (4.3) is used in place
2509f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //     of A for type deduction; otherwise,
2510f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    else if (ArgType->isFunctionType())
2511f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      ArgType = S.Context.getPointerType(ArgType);
2512f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    else {
25130099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi      // - If A is a cv-qualified type, the top level cv-qualifiers of A's
2514f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      //   type are ignored for type deduction.
2515f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      if (ArgType.getCVRQualifiers())
2516f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        ArgType = ArgType.getUnqualifiedType();
2517f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    }
2518f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  }
2519dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2520f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // C++0x [temp.deduct.call]p4:
2521f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //   In general, the deduction process attempts to find template argument
2522f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //   values that will make the deduced A identical to A (after the type A
2523f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //   is transformed as described above). [...]
2524f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  TDF = TDF_SkipNonDependent;
2525dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2526f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //     - If the original P is a reference type, the deduced A (i.e., the
2527f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //       type referred to by the reference) can be more cv-qualified than
2528f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //       the transformed A.
2529f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (ParamRefType)
2530f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    TDF |= TDF_ParamWithReferenceType;
2531f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //     - The transformed A can be another pointer or pointer to member
2532f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //       type that can be converted to the deduced A via a qualification
2533f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //       conversion (4.4).
2534f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (ArgType->isPointerType() || ArgType->isMemberPointerType() ||
2535f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      ArgType->isObjCObjectPointerType())
2536f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    TDF |= TDF_IgnoreQualifiers;
2537f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //     - If P is a class and P has the form simple-template-id, then the
2538f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //       transformed A can be a derived class of the deduced A. Likewise,
2539f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //       if P is a pointer to a class of the form simple-template-id, the
2540f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //       transformed A can be a pointer to a derived class pointed to by
2541f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //       the deduced A.
2542f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (isSimpleTemplateIdType(ParamType) ||
2543f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      (isa<PointerType>(ParamType) &&
2544f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor       isSimpleTemplateIdType(
2545f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                              ParamType->getAs<PointerType>()->getPointeeType())))
2546f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    TDF |= TDF_DerivedClass;
2547dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2548f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  return false;
2549f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor}
2550f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor
2551e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \brief Perform template argument deduction from a function call
2552e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// (C++ [temp.deduct.call]).
2553e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
2554e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param FunctionTemplate the function template for which we are performing
2555e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// template argument deduction.
2556e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
255748026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// \param ExplicitTemplateArguments the explicit template arguments provided
255848026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// for this call.
25596db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor///
2560e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Args the function call arguments
2561e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
2562e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param NumArgs the number of arguments in Args
2563e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
256448026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// \param Name the name of the function being called. This is only significant
256548026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// when the function template is a conversion function template, in which
256648026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// case this routine will also perform template argument deduction based on
2567dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// the function to which
256848026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor///
2569e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Specialization if template argument deduction was successful,
25701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// this will be set to the function template specialization produced by
2571e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// template argument deduction.
2572e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
2573e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Info the argument will be updated to provide additional information
2574e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// about template argument deduction.
2575e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
2576e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns the result of template argument deduction.
2577e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorSema::TemplateDeductionResult
2578e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
257967714230a191bc3c01f33378f34f34ef377991a6Douglas Gregor                              TemplateArgumentListInfo *ExplicitTemplateArgs,
2580e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                              Expr **Args, unsigned NumArgs,
2581e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                              FunctionDecl *&Specialization,
2582e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                              TemplateDeductionInfo &Info) {
2583e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
25846db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor
2585e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // C++ [temp.deduct.call]p1:
2586e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  //   Template argument deduction is done by comparing each function template
2587e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  //   parameter type (call it P) with the type of the corresponding argument
2588e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  //   of the call (call it A) as described below.
2589e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  unsigned CheckArgs = NumArgs;
25906db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  if (NumArgs < Function->getMinRequiredArguments())
2591e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return TDK_TooFewArguments;
2592e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  else if (NumArgs > Function->getNumParams()) {
25931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const FunctionProtoType *Proto
2594183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall      = Function->getType()->getAs<FunctionProtoType>();
2595f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    if (Proto->isTemplateVariadic())
2596f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      /* Do nothing */;
2597f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    else if (Proto->isVariadic())
2598f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      CheckArgs = Function->getNumParams();
2599dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    else
2600e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return TDK_TooManyArguments;
2601e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  }
26021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26036db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  // The types of the parameters from which we will perform template argument
26046db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  // deduction.
26052a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope InstScope(*this);
2606e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  TemplateParameterList *TemplateParams
2607e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    = FunctionTemplate->getTemplateParameters();
260802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
26096db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  llvm::SmallVector<QualType, 4> ParamTypes;
261002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  unsigned NumExplicitlySpecified = 0;
2611d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (ExplicitTemplateArgs) {
261283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    TemplateDeductionResult Result =
261383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      SubstituteExplicitTemplateArguments(FunctionTemplate,
2614d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                          *ExplicitTemplateArgs,
261583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          Deduced,
261683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          ParamTypes,
261783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          0,
261883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          Info);
261983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (Result)
262083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return Result;
262102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
262202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    NumExplicitlySpecified = Deduced.size();
26236db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  } else {
26246db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor    // Just fill in the parameter types from the function declaration.
2625f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
26266db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor      ParamTypes.push_back(Function->getParamDecl(I)->getType());
26276db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  }
26281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26296db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  // Deduce template arguments from the function parameters.
26301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Deduced.resize(TemplateParams->size());
2631f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  unsigned ArgIdx = 0;
2632dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  for (unsigned ParamIdx = 0, NumParams = ParamTypes.size();
2633f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor       ParamIdx != NumParams; ++ParamIdx) {
2634f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    QualType ParamType = ParamTypes[ParamIdx];
2635dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2636dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    const PackExpansionType *ParamExpansion
2637f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      = dyn_cast<PackExpansionType>(ParamType);
2638f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    if (!ParamExpansion) {
2639f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      // Simple case: matching a function parameter to a function argument.
2640f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      if (ArgIdx >= CheckArgs)
2641f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        break;
2642dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2643f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      Expr *Arg = Args[ArgIdx++];
2644f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      QualType ArgType = Arg->getType();
2645f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      unsigned TDF = 0;
2646f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams,
2647f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                                    ParamType, ArgType, Arg,
2648f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                                    TDF))
2649f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        continue;
2650dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2651f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      if (TemplateDeductionResult Result
2652f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor          = ::DeduceTemplateArguments(*this, TemplateParams,
2653f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                      ParamType, ArgType, Info, Deduced,
2654f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                      TDF))
2655f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        return Result;
26561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2657f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      // FIXME: we need to check that the deduced A is the same as A,
2658f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      // modulo the various allowed differences.
2659f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      continue;
266075f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor    }
2661dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2662f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    // C++0x [temp.deduct.call]p1:
2663dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   For a function parameter pack that occurs at the end of the
2664dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   parameter-declaration-list, the type A of each remaining argument of
2665dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   the call is compared with the type P of the declarator-id of the
2666dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   function parameter pack. Each comparison deduces template arguments
2667dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   for subsequent positions in the template parameter packs expanded by
26687d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    //   the function parameter pack. For a function parameter pack that does
2669dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   not occur at the end of the parameter-declaration-list, the type of
26707d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    //   the parameter pack is a non-deduced context.
26717d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    if (ParamIdx + 1 < NumParams)
26727d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor      break;
2673dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2674f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    QualType ParamPattern = ParamExpansion->getPattern();
2675f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    llvm::SmallVector<unsigned, 2> PackIndices;
2676f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    {
2677f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      llvm::BitVector SawIndices(TemplateParams->size());
2678f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2679f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      collectUnexpandedParameterPacks(ParamPattern, Unexpanded);
2680f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
2681f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        unsigned Depth, Index;
2682f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]);
2683f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        if (Depth == 0 && !SawIndices[Index]) {
2684f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor          SawIndices[Index] = true;
2685f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor          PackIndices.push_back(Index);
2686f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        }
2687f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      }
2688f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    }
2689f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?");
2690dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2691d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    // Keep track of the deduced template arguments for each parameter pack
2692dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    // expanded by this pack expansion (the outer index) and for each
2693d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    // template argument (the inner SmallVectors).
2694d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2>
2695d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      NewlyDeducedPacks(PackIndices.size());
2696dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    llvm::SmallVector<DeducedTemplateArgument, 2>
2697d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      SavedPacks(PackIndices.size());
26985429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor    PrepareArgumentPackDeduction(*this, Deduced, PackIndices, SavedPacks,
2699dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                 NewlyDeducedPacks);
2700f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    bool HasAnyArguments = false;
2701f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    for (; ArgIdx < NumArgs; ++ArgIdx) {
2702f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      HasAnyArguments = true;
2703dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2704f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      ParamType = ParamPattern;
2705f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      Expr *Arg = Args[ArgIdx];
2706f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      QualType ArgType = Arg->getType();
2707f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      unsigned TDF = 0;
2708f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams,
2709f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                                    ParamType, ArgType, Arg,
2710f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                                    TDF)) {
2711f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        // We can't actually perform any deduction for this argument, so stop
2712f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        // deduction at this point.
2713f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        ++ArgIdx;
2714f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        break;
2715f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      }
2716dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2717f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      if (TemplateDeductionResult Result
2718f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor          = ::DeduceTemplateArguments(*this, TemplateParams,
2719f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                      ParamType, ArgType, Info, Deduced,
2720f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                      TDF))
2721f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        return Result;
2722eff92135d32039c9874dc356f3e93143af6069c1John McCall
2723f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      // Capture the deduced template arguments for each parameter pack expanded
2724f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      // by this pack expansion, add them to the list of arguments we've deduced
2725f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      // for that pack, then clear out the deduced argument.
2726f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
2727f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]];
2728f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        if (!DeducedArg.isNull()) {
2729f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor          NewlyDeducedPacks[I].push_back(DeducedArg);
2730f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor          DeducedArg = DeducedTemplateArgument();
2731f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        }
2732e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      }
2733e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    }
2734dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2735f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    // Build argument packs for each of the parameter packs expanded by this
2736f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    // pack expansion.
27370216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    if (Sema::TemplateDeductionResult Result
2738dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi          = FinishArgumentPackDeduction(*this, TemplateParams, HasAnyArguments,
27390216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                                        Deduced, PackIndices, SavedPacks,
27400216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                                        NewlyDeducedPacks, Info))
2741dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      return Result;
27421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2743f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    // After we've matching against a parameter pack, we're done.
2744f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    break;
2745e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  }
274665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
27471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
274802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                         NumExplicitlySpecified,
274983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                         Specialization, Info);
275083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor}
2751127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor
275283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \brief Deduce template arguments when taking the address of a function
27534b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
27544b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// a template.
275583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
275683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionTemplate the function template for which we are performing
275783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template argument deduction.
275883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
2759dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \param ExplicitTemplateArguments the explicitly-specified template
27604b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// arguments.
276183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
276283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param ArgFunctionType the function type that will be used as the
276383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// "argument" type (A) when performing template argument deduction from the
27644b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// function template's function type. This type may be NULL, if there is no
27654b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
276683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
276783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Specialization if template argument deduction was successful,
27681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// this will be set to the function template specialization produced by
276983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template argument deduction.
277083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
277183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Info the argument will be updated to provide additional information
277283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// about template argument deduction.
277383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
277483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \returns the result of template argument deduction.
277583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::TemplateDeductionResult
277683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
277767714230a191bc3c01f33378f34f34ef377991a6Douglas Gregor                              TemplateArgumentListInfo *ExplicitTemplateArgs,
277883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                              QualType ArgFunctionType,
277983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                              FunctionDecl *&Specialization,
278083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                              TemplateDeductionInfo &Info) {
278183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
278283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateParameterList *TemplateParams
278383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = FunctionTemplate->getTemplateParameters();
278483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  QualType FunctionType = Function->getType();
27851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
278683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Substitute any explicit template arguments.
27872a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope InstScope(*this);
278802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
278902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  unsigned NumExplicitlySpecified = 0;
279083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  llvm::SmallVector<QualType, 4> ParamTypes;
2791d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (ExplicitTemplateArgs) {
27921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (TemplateDeductionResult Result
27931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          = SubstituteExplicitTemplateArguments(FunctionTemplate,
2794d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                *ExplicitTemplateArgs,
27951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                Deduced, ParamTypes,
279683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                                &FunctionType, Info))
279783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return Result;
279802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
279902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    NumExplicitlySpecified = Deduced.size();
2800127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
280183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
280283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Template argument deduction for function templates in a SFINAE context.
280383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Trap any errors that might occur.
28041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SFINAETrap Trap(*this);
28051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2806eff92135d32039c9874dc356f3e93143af6069c1John McCall  Deduced.resize(TemplateParams->size());
2807eff92135d32039c9874dc356f3e93143af6069c1John McCall
28084b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  if (!ArgFunctionType.isNull()) {
28094b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    // Deduce template arguments from the function type.
28104b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (TemplateDeductionResult Result
2811a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          = ::DeduceTemplateArguments(*this, TemplateParams,
28124b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                                      FunctionType, ArgFunctionType, Info,
281373b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor                                      Deduced, TDF_TopLevelParameterTypeList))
28144b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      return Result;
28154b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  }
2816fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor
2817dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  if (TemplateDeductionResult Result
2818fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor        = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
2819fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor                                          NumExplicitlySpecified,
2820fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor                                          Specialization, Info))
2821fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor    return Result;
2822fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor
2823fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor  // If the requested function type does not match the actual type of the
2824fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor  // specialization, template argument deduction fails.
2825fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor  if (!ArgFunctionType.isNull() &&
2826fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor      !Context.hasSameType(ArgFunctionType, Specialization->getType()))
2827fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor    return TDK_NonDeducedMismatch;
2828fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor
2829fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor  return TDK_Success;
2830e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
2831e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
283265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \brief Deduce template arguments for a templated conversion
283365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// function (C++ [temp.deduct.conv]) and, if successful, produce a
283465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// conversion function template specialization.
283565ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::TemplateDeductionResult
283665ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
283765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                              QualType ToType,
283865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                              CXXConversionDecl *&Specialization,
283965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                              TemplateDeductionInfo &Info) {
28401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  CXXConversionDecl *Conv
284165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
284265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  QualType FromType = Conv->getConversionType();
284365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
284465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Canonicalize the types for deduction.
284565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  QualType P = Context.getCanonicalType(FromType);
284665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  QualType A = Context.getCanonicalType(ToType);
284765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
28485453d93ab8668f2d9d0bc02182695b02d207e32dDouglas Gregor  // C++0x [temp.deduct.conv]p2:
284965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   If P is a reference type, the type referred to by P is used for
285065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   type deduction.
285165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (const ReferenceType *PRef = P->getAs<ReferenceType>())
285265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    P = PRef->getPointeeType();
285365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
28545453d93ab8668f2d9d0bc02182695b02d207e32dDouglas Gregor  // C++0x [temp.deduct.conv]p4:
28555453d93ab8668f2d9d0bc02182695b02d207e32dDouglas Gregor  //   [...] If A is a reference type, the type referred to by A is used
285665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   for type deduction.
285765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (const ReferenceType *ARef = A->getAs<ReferenceType>())
28585453d93ab8668f2d9d0bc02182695b02d207e32dDouglas Gregor    A = ARef->getPointeeType().getUnqualifiedType();
28595453d93ab8668f2d9d0bc02182695b02d207e32dDouglas Gregor  // C++ [temp.deduct.conv]p3:
286065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //
28611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   If A is not a reference type:
286265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  else {
286365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    assert(!A->isReferenceType() && "Reference types were handled above");
286465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
286565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   - If P is an array type, the pointer type produced by the
28661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     array-to-pointer standard conversion (4.2) is used in place
286765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     of P for type deduction; otherwise,
286865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    if (P->isArrayType())
286965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      P = Context.getArrayDecayedType(P);
287065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   - If P is a function type, the pointer type produced by the
287165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     function-to-pointer standard conversion (4.3) is used in
287265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     place of P for type deduction; otherwise,
287365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    else if (P->isFunctionType())
287465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      P = Context.getPointerType(P);
287565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   - If P is a cv-qualified type, the top level cv-qualifiers of
28760099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi    //     P's type are ignored for type deduction.
287765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    else
287865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      P = P.getUnqualifiedType();
287965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
28805453d93ab8668f2d9d0bc02182695b02d207e32dDouglas Gregor    // C++0x [temp.deduct.conv]p4:
28810099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi    //   If A is a cv-qualified type, the top level cv-qualifiers of A's
28825453d93ab8668f2d9d0bc02182695b02d207e32dDouglas Gregor    //   type are ignored for type deduction. If A is a reference type, the type
28835453d93ab8668f2d9d0bc02182695b02d207e32dDouglas Gregor    //   referred to by A is used for type deduction.
288465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    A = A.getUnqualifiedType();
288565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  }
288665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
288765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Template argument deduction for function templates in a SFINAE context.
288865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Trap any errors that might occur.
28891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SFINAETrap Trap(*this);
289065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
289165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++ [temp.deduct.conv]p1:
289265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   Template argument deduction is done by comparing the return
289365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   type of the template conversion function (call it P) with the
289465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   type that is required as the result of the conversion (call it
289565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   A) as described in 14.8.2.4.
289665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  TemplateParameterList *TemplateParams
289765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    = FunctionTemplate->getTemplateParameters();
289802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
28991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Deduced.resize(TemplateParams->size());
290065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
290165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++0x [temp.deduct.conv]p4:
290265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   In general, the deduction process attempts to find template
290365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   argument values that will make the deduced A identical to
290465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   A. However, there are two cases that allow a difference:
290565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  unsigned TDF = 0;
290665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //     - If the original A is a reference type, A can be more
290765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       cv-qualified than the deduced A (i.e., the type referred to
290865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       by the reference)
290965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (ToType->isReferenceType())
291065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    TDF |= TDF_ParamWithReferenceType;
291165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //     - The deduced A can be another pointer or pointer to member
29120099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi  //       type that can be converted to A via a qualification
291365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       conversion.
291465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //
291565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
291665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // both P and A are pointers or member pointers. In this case, we
291765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // just ignore cv-qualifiers completely).
291865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if ((P->isPointerType() && A->isPointerType()) ||
291965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      (P->isMemberPointerType() && P->isMemberPointerType()))
292065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    TDF |= TDF_IgnoreQualifiers;
292165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (TemplateDeductionResult Result
2922a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = ::DeduceTemplateArguments(*this, TemplateParams,
292365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                    P, A, Info, Deduced, TDF))
292465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    return Result;
292565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
292665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // FIXME: we need to check that the deduced A is the same as A,
292765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // modulo the various allowed differences.
29281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
292965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Finish template argument deduction.
29302a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope InstScope(*this);
293165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  FunctionDecl *Spec = 0;
293265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  TemplateDeductionResult Result
2933dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 0, Spec,
293402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                      Info);
293565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  Specialization = cast_or_null<CXXConversionDecl>(Spec);
293665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return Result;
293765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor}
293865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
29394b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \brief Deduce template arguments for a function template when there is
29404b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
29414b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
29424b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param FunctionTemplate the function template for which we are performing
29434b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template argument deduction.
29444b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
2945dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \param ExplicitTemplateArguments the explicitly-specified template
29464b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// arguments.
29474b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
29484b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param Specialization if template argument deduction was successful,
29494b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// this will be set to the function template specialization produced by
29504b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template argument deduction.
29514b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
29524b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param Info the argument will be updated to provide additional information
29534b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// about template argument deduction.
29544b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
29554b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \returns the result of template argument deduction.
29564b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas GregorSema::TemplateDeductionResult
29574b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
295867714230a191bc3c01f33378f34f34ef377991a6Douglas Gregor                              TemplateArgumentListInfo *ExplicitTemplateArgs,
29594b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                              FunctionDecl *&Specialization,
29604b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                              TemplateDeductionInfo &Info) {
29614b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
29624b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                                 QualType(), Specialization, Info);
29634b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor}
29644b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor
296534b41d939a1328f484511c6002ba2456db879a29Richard Smithnamespace {
296634b41d939a1328f484511c6002ba2456db879a29Richard Smith  /// Substitute the 'auto' type specifier within a type for a given replacement
296734b41d939a1328f484511c6002ba2456db879a29Richard Smith  /// type.
296834b41d939a1328f484511c6002ba2456db879a29Richard Smith  class SubstituteAutoTransform :
296934b41d939a1328f484511c6002ba2456db879a29Richard Smith    public TreeTransform<SubstituteAutoTransform> {
297034b41d939a1328f484511c6002ba2456db879a29Richard Smith    QualType Replacement;
297134b41d939a1328f484511c6002ba2456db879a29Richard Smith  public:
297234b41d939a1328f484511c6002ba2456db879a29Richard Smith    SubstituteAutoTransform(Sema &SemaRef, QualType Replacement) :
297334b41d939a1328f484511c6002ba2456db879a29Richard Smith      TreeTransform<SubstituteAutoTransform>(SemaRef), Replacement(Replacement) {
297434b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
297534b41d939a1328f484511c6002ba2456db879a29Richard Smith    QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) {
297634b41d939a1328f484511c6002ba2456db879a29Richard Smith      // If we're building the type pattern to deduce against, don't wrap the
297734b41d939a1328f484511c6002ba2456db879a29Richard Smith      // substituted type in an AutoType. Certain template deduction rules
297834b41d939a1328f484511c6002ba2456db879a29Richard Smith      // apply only when a template type parameter appears directly (and not if
297934b41d939a1328f484511c6002ba2456db879a29Richard Smith      // the parameter is found through desugaring). For instance:
298034b41d939a1328f484511c6002ba2456db879a29Richard Smith      //   auto &&lref = lvalue;
298134b41d939a1328f484511c6002ba2456db879a29Richard Smith      // must transform into "rvalue reference to T" not "rvalue reference to
298234b41d939a1328f484511c6002ba2456db879a29Richard Smith      // auto type deduced as T" in order for [temp.deduct.call]p3 to apply.
298334b41d939a1328f484511c6002ba2456db879a29Richard Smith      if (isa<TemplateTypeParmType>(Replacement)) {
298434b41d939a1328f484511c6002ba2456db879a29Richard Smith        QualType Result = Replacement;
298534b41d939a1328f484511c6002ba2456db879a29Richard Smith        TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
298634b41d939a1328f484511c6002ba2456db879a29Richard Smith        NewTL.setNameLoc(TL.getNameLoc());
298734b41d939a1328f484511c6002ba2456db879a29Richard Smith        return Result;
298834b41d939a1328f484511c6002ba2456db879a29Richard Smith      } else {
298934b41d939a1328f484511c6002ba2456db879a29Richard Smith        QualType Result = RebuildAutoType(Replacement);
299034b41d939a1328f484511c6002ba2456db879a29Richard Smith        AutoTypeLoc NewTL = TLB.push<AutoTypeLoc>(Result);
299134b41d939a1328f484511c6002ba2456db879a29Richard Smith        NewTL.setNameLoc(TL.getNameLoc());
299234b41d939a1328f484511c6002ba2456db879a29Richard Smith        return Result;
299334b41d939a1328f484511c6002ba2456db879a29Richard Smith      }
299434b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
299534b41d939a1328f484511c6002ba2456db879a29Richard Smith  };
299634b41d939a1328f484511c6002ba2456db879a29Richard Smith}
299734b41d939a1328f484511c6002ba2456db879a29Richard Smith
299834b41d939a1328f484511c6002ba2456db879a29Richard Smith/// \brief Deduce the type for an auto type-specifier (C++0x [dcl.spec.auto]p6)
299934b41d939a1328f484511c6002ba2456db879a29Richard Smith///
300034b41d939a1328f484511c6002ba2456db879a29Richard Smith/// \param Type the type pattern using the auto type-specifier.
300134b41d939a1328f484511c6002ba2456db879a29Richard Smith///
300234b41d939a1328f484511c6002ba2456db879a29Richard Smith/// \param Init the initializer for the variable whose type is to be deduced.
300334b41d939a1328f484511c6002ba2456db879a29Richard Smith///
300434b41d939a1328f484511c6002ba2456db879a29Richard Smith/// \param Result if type deduction was successful, this will be set to the
300534b41d939a1328f484511c6002ba2456db879a29Richard Smith/// deduced type. This may still contain undeduced autos if the type is
300634b41d939a1328f484511c6002ba2456db879a29Richard Smith/// dependent.
300734b41d939a1328f484511c6002ba2456db879a29Richard Smith///
300834b41d939a1328f484511c6002ba2456db879a29Richard Smith/// \returns true if deduction succeeded, false if it failed.
300934b41d939a1328f484511c6002ba2456db879a29Richard Smithbool
301034b41d939a1328f484511c6002ba2456db879a29Richard SmithSema::DeduceAutoType(QualType Type, Expr *Init, QualType &Result) {
301134b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (Init->isTypeDependent()) {
301234b41d939a1328f484511c6002ba2456db879a29Richard Smith    Result = Type;
301334b41d939a1328f484511c6002ba2456db879a29Richard Smith    return true;
301434b41d939a1328f484511c6002ba2456db879a29Richard Smith  }
301534b41d939a1328f484511c6002ba2456db879a29Richard Smith
301634b41d939a1328f484511c6002ba2456db879a29Richard Smith  SourceLocation Loc = Init->getExprLoc();
301734b41d939a1328f484511c6002ba2456db879a29Richard Smith
301834b41d939a1328f484511c6002ba2456db879a29Richard Smith  LocalInstantiationScope InstScope(*this);
301934b41d939a1328f484511c6002ba2456db879a29Richard Smith
302034b41d939a1328f484511c6002ba2456db879a29Richard Smith  // Build template<class TemplParam> void Func(FuncParam);
302134b41d939a1328f484511c6002ba2456db879a29Richard Smith  QualType TemplArg = Context.getTemplateTypeParmType(0, 0, false);
3022483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  TemplateTypeParmDecl TemplParam(0, Loc, 0, false, TemplArg, false);
3023483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  NamedDecl *TemplParamPtr = &TemplParam;
3024483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  FixedSizeTemplateParameterList<1> TemplateParams(Loc, Loc, &TemplParamPtr,
3025483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith                                                   Loc);
3026483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith
302734b41d939a1328f484511c6002ba2456db879a29Richard Smith  QualType FuncParam =
302834b41d939a1328f484511c6002ba2456db879a29Richard Smith    SubstituteAutoTransform(*this, TemplArg).TransformType(Type);
302934b41d939a1328f484511c6002ba2456db879a29Richard Smith
303034b41d939a1328f484511c6002ba2456db879a29Richard Smith  // Deduce type of TemplParam in Func(Init)
303134b41d939a1328f484511c6002ba2456db879a29Richard Smith  llvm::SmallVector<DeducedTemplateArgument, 1> Deduced;
303234b41d939a1328f484511c6002ba2456db879a29Richard Smith  Deduced.resize(1);
303334b41d939a1328f484511c6002ba2456db879a29Richard Smith  QualType InitType = Init->getType();
303434b41d939a1328f484511c6002ba2456db879a29Richard Smith  unsigned TDF = 0;
3035483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  if (AdjustFunctionParmAndArgTypesForDeduction(*this, &TemplateParams,
303634b41d939a1328f484511c6002ba2456db879a29Richard Smith                                                FuncParam, InitType, Init,
303734b41d939a1328f484511c6002ba2456db879a29Richard Smith                                                TDF))
303834b41d939a1328f484511c6002ba2456db879a29Richard Smith    return false;
303934b41d939a1328f484511c6002ba2456db879a29Richard Smith
304034b41d939a1328f484511c6002ba2456db879a29Richard Smith  TemplateDeductionInfo Info(Context, Loc);
3041483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  if (::DeduceTemplateArguments(*this, &TemplateParams,
304234b41d939a1328f484511c6002ba2456db879a29Richard Smith                                FuncParam, InitType, Info, Deduced,
304334b41d939a1328f484511c6002ba2456db879a29Richard Smith                                TDF))
304434b41d939a1328f484511c6002ba2456db879a29Richard Smith    return false;
304534b41d939a1328f484511c6002ba2456db879a29Richard Smith
304634b41d939a1328f484511c6002ba2456db879a29Richard Smith  QualType DeducedType = Deduced[0].getAsType();
304734b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (DeducedType.isNull())
304834b41d939a1328f484511c6002ba2456db879a29Richard Smith    return false;
304934b41d939a1328f484511c6002ba2456db879a29Richard Smith
305034b41d939a1328f484511c6002ba2456db879a29Richard Smith  Result = SubstituteAutoTransform(*this, DeducedType).TransformType(Type);
305134b41d939a1328f484511c6002ba2456db879a29Richard Smith  return true;
305234b41d939a1328f484511c6002ba2456db879a29Richard Smith}
305334b41d939a1328f484511c6002ba2456db879a29Richard Smith
30548a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic void
3055e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, QualType T,
3056e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
3057ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Level,
3058e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Deduced);
3059dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3060dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \brief If this is a non-static member function,
306177bc572138543ef97ebec137522b763d6a6ee909Douglas Gregorstatic void MaybeAddImplicitObjectParameterType(ASTContext &Context,
306277bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor                                                CXXMethodDecl *Method,
306377bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor                                 llvm::SmallVectorImpl<QualType> &ArgTypes) {
306477bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  if (Method->isStatic())
306577bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor    return;
306677bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor
306777bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  // C++ [over.match.funcs]p4:
306877bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  //
306977bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  //   For non-static member functions, the type of the implicit
307077bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  //   object parameter is
30710099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi  //     - "lvalue reference to cv X" for functions declared without a
307277bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  //       ref-qualifier or with the & ref-qualifier
307377bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  //     - "rvalue reference to cv X" for functions declared with the
307477bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  //       && ref-qualifier
307577bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  //
307677bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  // FIXME: We don't have ref-qualifiers yet, so we don't do that part.
307777bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  QualType ArgTy = Context.getTypeDeclType(Method->getParent());
307877bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  ArgTy = Context.getQualifiedType(ArgTy,
307977bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor                        Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
308077bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  ArgTy = Context.getLValueReferenceType(ArgTy);
308177bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  ArgTypes.push_back(ArgTy);
308277bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor}
308377bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor
30848a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Determine whether the function template \p FT1 is at least as
30858a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// specialized as \p FT2.
30868a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic bool isAtLeastAsSpecializedAs(Sema &S,
30875769d6195087229770d7ac90449443e026c47103John McCall                                     SourceLocation Loc,
30888a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                     FunctionTemplateDecl *FT1,
30898a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                     FunctionTemplateDecl *FT2,
30908a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                     TemplatePartialOrderingContext TPOC,
3091dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                     unsigned NumCallArguments,
3092b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *RefParamComparisons) {
30938a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  FunctionDecl *FD1 = FT1->getTemplatedDecl();
3094dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  FunctionDecl *FD2 = FT2->getTemplatedDecl();
30958a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
30968a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
3097dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
30988a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  assert(Proto1 && Proto2 && "Function templates must have prototypes");
30998a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
310002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
31018a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Deduced.resize(TemplateParams->size());
31028a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
31038a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p3:
31048a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   The types used to determine the ordering depend on the context in which
31058a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   the partial ordering is done:
31062a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  TemplateDeductionInfo Info(S.Context, Loc);
31078d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor  CXXMethodDecl *Method1 = 0;
31088d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor  CXXMethodDecl *Method2 = 0;
31098d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor  bool IsNonStatic2 = false;
31108d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor  bool IsNonStatic1 = false;
31118d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor  unsigned Skip2 = 0;
31128a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  switch (TPOC) {
31138a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Call: {
31148a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   - In the context of a function call, the function parameter types are
31158a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //     used.
31168d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    Method1 = dyn_cast<CXXMethodDecl>(FD1);
31178d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    Method2 = dyn_cast<CXXMethodDecl>(FD2);
31188d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    IsNonStatic1 = Method1 && !Method1->isStatic();
31198d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    IsNonStatic2 = Method2 && !Method2->isStatic();
31208d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor
31218d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    // C++0x [temp.func.order]p3:
31228d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    //   [...] If only one of the function templates is a non-static
31238d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    //   member, that function template is considered to have a new
31248d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    //   first parameter inserted in its function parameter list. The
31258d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    //   new parameter is of type "reference to cv A," where cv are
31268d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    //   the cv-qualifiers of the function template (if any) and A is
31278d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    //   the class of which the function template is a member.
31288d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    //
31298d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    // C++98/03 doesn't have this provision, so instead we drop the
31308d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    // first argument of the free function or static member, which
31318d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    // seems to match existing practice.
313277bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor    llvm::SmallVector<QualType, 4> Args1;
3133dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    unsigned Skip1 = !S.getLangOptions().CPlusPlus0x &&
31348d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor      IsNonStatic2 && !IsNonStatic1;
31358d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    if (S.getLangOptions().CPlusPlus0x && IsNonStatic1 && !IsNonStatic2)
3136dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      MaybeAddImplicitObjectParameterType(S.Context, Method1, Args1);
3137dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    Args1.insert(Args1.end(),
31388d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor                 Proto1->arg_type_begin() + Skip1, Proto1->arg_type_end());
313977bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor
314077bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor    llvm::SmallVector<QualType, 4> Args2;
3141dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    Skip2 = !S.getLangOptions().CPlusPlus0x &&
31428d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor      IsNonStatic1 && !IsNonStatic2;
31438d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    if (S.getLangOptions().CPlusPlus0x && IsNonStatic2 && !IsNonStatic1)
314477bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor      MaybeAddImplicitObjectParameterType(S.Context, Method2, Args2);
3145dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    Args2.insert(Args2.end(),
31468d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor                 Proto2->arg_type_begin() + Skip2, Proto2->arg_type_end());
3147dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
31485c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    // C++ [temp.func.order]p5:
31495c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //   The presence of unused ellipsis and default arguments has no effect on
31505c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //   the partial ordering of function templates.
31515c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    if (Args1.size() > NumCallArguments)
31525c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      Args1.resize(NumCallArguments);
31535c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    if (Args2.size() > NumCallArguments)
31545c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      Args2.resize(NumCallArguments);
31555c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(),
31565c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                Args1.data(), Args1.size(), Info, Deduced,
31575c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                TDF_None, /*PartialOrdering=*/true,
3158b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                                RefParamComparisons))
31598a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        return false;
3160dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
31618a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
31628a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
3163dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
31648a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Conversion:
31658a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   - In the context of a call to a conversion operator, the return types
31668a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //     of the conversion function templates are used.
31675c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    if (DeduceTemplateArguments(S, TemplateParams, Proto2->getResultType(),
31685c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                Proto1->getResultType(), Info, Deduced,
31695c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                TDF_None, /*PartialOrdering=*/true,
3170b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                                RefParamComparisons))
31718a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      return false;
31728a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
3173dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
31748a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Other:
31750099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi    //   - In other contexts (14.6.6.2) the function template's function type
31768a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //     is used.
31775c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    // FIXME: Don't we actually want to perform the adjustments on the parameter
31785c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    // types?
3179dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    if (DeduceTemplateArguments(S, TemplateParams, FD2->getType(),
31805c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                FD1->getType(), Info, Deduced, TDF_None,
3181b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                                /*PartialOrdering=*/true, RefParamComparisons))
31828a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      return false;
31838a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
31848a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
3185dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
31868a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p11:
3187dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   In most cases, all template parameters must have values in order for
3188dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   deduction to succeed, but for partial ordering purposes a template
3189dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   parameter may remain without a value provided it is not used in the
31908a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   types being used for partial ordering. [ Note: a template parameter used
31918a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   in a non-deduced context is considered used. -end note]
31928a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  unsigned ArgIdx = 0, NumArgs = Deduced.size();
31938a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  for (; ArgIdx != NumArgs; ++ArgIdx)
31948a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    if (Deduced[ArgIdx].isNull())
31958a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      break;
31968a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
31978a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  if (ArgIdx == NumArgs) {
3198dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    // All template arguments were deduced. FT1 is at least as specialized
31998a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // as FT2.
32008a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return true;
32018a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
32028a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
3203e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // Figure out which template parameters were used.
32048a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  llvm::SmallVector<bool, 4> UsedParameters;
32058a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  UsedParameters.resize(TemplateParams->size());
32068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  switch (TPOC) {
32078a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Call: {
3208dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    unsigned NumParams = std::min(NumCallArguments,
3209dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                  std::min(Proto1->getNumArgs(),
32105c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                           Proto2->getNumArgs()));
32118d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    if (S.getLangOptions().CPlusPlus0x && IsNonStatic2 && !IsNonStatic1)
3212dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      ::MarkUsedTemplateParameters(S, Method2->getThisType(S.Context), false,
32138d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor                                   TemplateParams->getDepth(), UsedParameters);
32148d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    for (unsigned I = Skip2; I < NumParams; ++I)
3215dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
3216ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                   TemplateParams->getDepth(),
3217e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                   UsedParameters);
32188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
32198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
3220dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
32218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Conversion:
3222dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
3223ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 TemplateParams->getDepth(),
3224e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                 UsedParameters);
32258a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
3226dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
32278a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Other:
3228dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    ::MarkUsedTemplateParameters(S, FD2->getType(), false,
3229ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 TemplateParams->getDepth(),
3230ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 UsedParameters);
32318a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
32328a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
3233dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
32348a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  for (; ArgIdx != NumArgs; ++ArgIdx)
32358a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // If this argument had no value deduced but was used in one of the types
32368a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // used for partial ordering, then deduction fails.
32378a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
32388a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      return false;
3239dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
32408a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  return true;
32418a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor}
3242dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
32439da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor/// \brief Determine whether this a function template whose parameter-type-list
32449da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor/// ends with a function parameter pack.
32459da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregorstatic bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) {
32469da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  FunctionDecl *Function = FunTmpl->getTemplatedDecl();
32479da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  unsigned NumParams = Function->getNumParams();
32489da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  if (NumParams == 0)
32499da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor    return false;
3250dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
32519da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  ParmVarDecl *Last = Function->getParamDecl(NumParams - 1);
32529da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  if (!Last->isParameterPack())
32539da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor    return false;
3254dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
32559da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  // Make sure that no previous parameter is a parameter pack.
32569da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  while (--NumParams > 0) {
32579da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor    if (Function->getParamDecl(NumParams - 1)->isParameterPack())
32589da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor      return false;
32599da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  }
3260dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
32619da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  return true;
32629da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor}
3263dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3264bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \brief Returns the more specialized function template according
326565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// to the rules of function template partial ordering (C++ [temp.func.order]).
326665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor///
326765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \param FT1 the first function template
326865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor///
326965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \param FT2 the second function template
327065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor///
32718a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param TPOC the context in which we are performing partial ordering of
32728a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// function templates.
32731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
32745c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// \param NumCallArguments The number of arguments in a call, used only
32755c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// when \c TPOC is \c TPOC_Call.
32765c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor///
3277bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \returns the more specialized function template. If neither
327865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// template is more specialized, returns NULL.
327965ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorFunctionTemplateDecl *
328065ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
328165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                 FunctionTemplateDecl *FT2,
32825769d6195087229770d7ac90449443e026c47103John McCall                                 SourceLocation Loc,
32835c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                 TemplatePartialOrderingContext TPOC,
32845c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                 unsigned NumCallArguments) {
3285b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor  llvm::SmallVector<RefParamPartialOrderingComparison, 4> RefParamComparisons;
3286dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC,
32875c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                          NumCallArguments, 0);
3288dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC,
32895c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                          NumCallArguments,
3290b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                                          &RefParamComparisons);
3291dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
32928a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  if (Better1 != Better2) // We have a clear winner
32938a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return Better1? FT1 : FT2;
3294dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
32958a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  if (!Better1 && !Better2) // Neither is better than the other
329665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    return 0;
32978a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
32988a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p10:
3299dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   If for each type being considered a given template is at least as
33008a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   specialized for all types and more specialized for some set of types and
3301dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   the other template is not more specialized for any types or is not at
33028a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   least as specialized for any types, then the given template is more
33038a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   specialized than the other template. Otherwise, neither template is more
33048a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   specialized than the other.
33058a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Better1 = false;
33068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Better2 = false;
3307b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor  for (unsigned I = 0, N = RefParamComparisons.size(); I != N; ++I) {
33088a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // C++0x [temp.deduct.partial]p9:
33098a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   If, for a given type, deduction succeeds in both directions (i.e., the
3310b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //   types are identical after the transformations above) and both P and A
3311b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //   were reference types (before being replaced with the type referred to
3312b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //   above):
3313b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor
3314dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //     -- if the type from the argument template was an lvalue reference
3315b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //        and the type from the parameter template was not, the argument
3316b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //        type is considered to be more specialized than the other;
3317b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //        otherwise,
3318b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    if (!RefParamComparisons[I].ArgIsRvalueRef &&
3319b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor        RefParamComparisons[I].ParamIsRvalueRef) {
3320b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      Better2 = true;
3321b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      if (Better1)
3322b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor        return 0;
3323b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      continue;
3324b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    } else if (!RefParamComparisons[I].ParamIsRvalueRef &&
3325b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor               RefParamComparisons[I].ArgIsRvalueRef) {
3326b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      Better1 = true;
3327b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      if (Better2)
3328b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor        return 0;
3329b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      continue;
3330b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    }
3331dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3332b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //     -- if the type from the argument template is more cv-qualified than
3333b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //        the type from the parameter template (as described above), the
3334b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //        argument type is considered to be more specialized than the
3335b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //        other; otherwise,
3336b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    switch (RefParamComparisons[I].Qualifiers) {
3337b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    case NeitherMoreQualified:
3338b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      break;
3339dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3340b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    case ParamMoreQualified:
3341b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      Better1 = true;
3342b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      if (Better2)
3343b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor        return 0;
3344b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      continue;
3345dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3346b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    case ArgMoreQualified:
3347b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      Better2 = true;
3348b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      if (Better1)
3349b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor        return 0;
3350b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      continue;
33518a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    }
3352dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3353b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //     -- neither type is more specialized than the other.
33548a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
3355dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
33568a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  assert(!(Better1 && Better2) && "Should have broken out in the loop above");
335765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (Better1)
335865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    return FT1;
33598a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  else if (Better2)
33608a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return FT2;
3361dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
33629da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  // FIXME: This mimics what GCC implements, but doesn't match up with the
33639da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  // proposed resolution for core issue 692. This area needs to be sorted out,
33649da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  // but for now we attempt to maintain compatibility.
33659da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  bool Variadic1 = isVariadicFunctionTemplate(FT1);
33669da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  bool Variadic2 = isVariadicFunctionTemplate(FT2);
33679da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  if (Variadic1 != Variadic2)
33689da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor    return Variadic1? FT2 : FT1;
3369dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
33709da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  return 0;
337165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor}
337283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
3373d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \brief Determine if the two templates are equivalent.
3374d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregorstatic bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
3375d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  if (T1 == T2)
3376d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    return true;
3377dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3378d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  if (!T1 || !T2)
3379d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    return false;
3380dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3381d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  return T1->getCanonicalDecl() == T2->getCanonicalDecl();
3382d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor}
3383d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
3384d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \brief Retrieve the most specialized of the given function template
3385d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specializations.
3386d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3387c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// \param SpecBegin the start iterator of the function template
3388c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// specializations that we will be comparing.
3389d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3390c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// \param SpecEnd the end iterator of the function template
3391c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// specializations, paired with \p SpecBegin.
3392d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3393d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param TPOC the partial ordering context to use to compare the function
3394d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// template specializations.
3395d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
33965c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// \param NumCallArguments The number of arguments in a call, used only
33975c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// when \c TPOC is \c TPOC_Call.
33985c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor///
3399dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \param Loc the location where the ambiguity or no-specializations
3400d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// diagnostic should occur.
3401d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3402d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param NoneDiag partial diagnostic used to diagnose cases where there are
3403d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// no matching candidates.
3404d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3405d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
3406d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// occurs.
3407d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3408d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param CandidateDiag partial diagnostic used for each function template
3409d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specialization that is a candidate in the ambiguous ordering. One parameter
3410d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// in this diagnostic should be unbound, which will correspond to the string
3411d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// describing the template arguments for the function template specialization.
3412d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3413dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \param Index if non-NULL and the result of this function is non-nULL,
3414d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// receives the index corresponding to the resulting function template
3415d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specialization.
3416d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3417dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \returns the most specialized function template specialization, if
3418c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// found. Otherwise, returns SpecEnd.
3419d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3420dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
3421d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// template argument deduction.
3422c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallUnresolvedSetIterator
3423c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallSema::getMostSpecialized(UnresolvedSetIterator SpecBegin,
34245c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                        UnresolvedSetIterator SpecEnd,
3425c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         TemplatePartialOrderingContext TPOC,
34265c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                         unsigned NumCallArguments,
3427c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         SourceLocation Loc,
3428c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         const PartialDiagnostic &NoneDiag,
3429c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         const PartialDiagnostic &AmbigDiag,
34301be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor                         const PartialDiagnostic &CandidateDiag,
34311be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor                         bool Complain) {
3432c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  if (SpecBegin == SpecEnd) {
34331be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor    if (Complain)
34341be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor      Diag(Loc, NoneDiag);
3435c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    return SpecEnd;
3436d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
3437dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3438dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  if (SpecBegin + 1 == SpecEnd)
3439c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    return SpecBegin;
3440dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3441d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // Find the function template that is better than all of the templates it
3442d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // has been compared to.
3443c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  UnresolvedSetIterator Best = SpecBegin;
3444dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  FunctionTemplateDecl *BestTemplate
3445c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
3446d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  assert(BestTemplate && "Not a function template specialization?");
3447c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
3448c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    FunctionTemplateDecl *Challenger
3449c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall      = cast<FunctionDecl>(*I)->getPrimaryTemplate();
3450d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    assert(Challenger && "Not a function template specialization?");
3451c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
34525c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                                  Loc, TPOC, NumCallArguments),
3453d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor                       Challenger)) {
3454d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      Best = I;
3455d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      BestTemplate = Challenger;
3456d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    }
3457d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
3458dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3459d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // Make sure that the "best" function template is more specialized than all
3460d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // of the others.
3461d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  bool Ambiguous = false;
3462c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
3463c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    FunctionTemplateDecl *Challenger
3464c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall      = cast<FunctionDecl>(*I)->getPrimaryTemplate();
3465d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    if (I != Best &&
3466dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
34675c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                                   Loc, TPOC, NumCallArguments),
3468d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor                        BestTemplate)) {
3469d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      Ambiguous = true;
3470d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      break;
3471d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    }
3472d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
3473dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3474d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  if (!Ambiguous) {
3475d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    // We found an answer. Return it.
3476c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    return Best;
3477d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
3478dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3479d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // Diagnose the ambiguity.
34801be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor  if (Complain)
34811be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor    Diag(Loc, AmbigDiag);
3482dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
34831be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor  if (Complain)
3484d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // FIXME: Can we order the candidates in some sane way?
34851be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor    for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I)
34861be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor      Diag((*I)->getLocation(), CandidateDiag)
34871be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor        << getTemplateArgumentBindingsText(
34881be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor          cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
3489c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                    *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
3490dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3491c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  return SpecEnd;
3492d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor}
3493d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
3494bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \brief Returns the more specialized class template partial specialization
3495bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// according to the rules of partial ordering of class template partial
3496bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// specializations (C++ [temp.class.order]).
3497bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor///
3498bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \param PS1 the first class template partial specialization
3499bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor///
3500bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \param PS2 the second class template partial specialization
3501bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor///
3502bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \returns the more specialized class template partial specialization. If
3503bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// neither partial specialization is more specialized, returns NULL.
3504bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas GregorClassTemplatePartialSpecializationDecl *
3505bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas GregorSema::getMoreSpecializedPartialSpecialization(
3506bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                  ClassTemplatePartialSpecializationDecl *PS1,
35075769d6195087229770d7ac90449443e026c47103John McCall                                  ClassTemplatePartialSpecializationDecl *PS2,
35085769d6195087229770d7ac90449443e026c47103John McCall                                              SourceLocation Loc) {
3509bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // C++ [temp.class.order]p1:
3510bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   For two class template partial specializations, the first is at least as
3511dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   specialized as the second if, given the following rewrite to two
3512dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   function templates, the first function template is at least as
3513dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   specialized as the second according to the ordering rules for function
3514bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   templates (14.6.6.2):
3515bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //     - the first function template has the same template parameters as the
3516dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //       first partial specialization and has a single function parameter
3517dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //       whose type is a class template specialization with the template
3518bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       arguments of the first partial specialization, and
3519bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //     - the second function template has the same template parameters as the
3520dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //       second partial specialization and has a single function parameter
3521dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //       whose type is a class template specialization with the template
3522bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       arguments of the second partial specialization.
3523bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //
352431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // Rather than synthesize function templates, we merely perform the
352531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // equivalent partial ordering by performing deduction directly on
352631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // the template arguments of the class template partial
352731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // specializations. This computation is slightly simpler than the
352831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // general problem of function template partial ordering, because
352931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // class template partial specializations are more constrained. We
353031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // know that every template parameter is deducible from the class
353131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // template partial specialization's template arguments, for
353231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // example.
353302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
35342a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  TemplateDeductionInfo Info(Context, Loc);
353531f17ecbef57b5679c017c375db330546b7b5145John McCall
353631f17ecbef57b5679c017c375db330546b7b5145John McCall  QualType PT1 = PS1->getInjectedSpecializationType();
353731f17ecbef57b5679c017c375db330546b7b5145John McCall  QualType PT2 = PS2->getInjectedSpecializationType();
3538dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3539bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // Determine whether PS1 is at least as specialized as PS2
3540bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  Deduced.resize(PS2->getTemplateParameters()->size());
35415c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor  bool Better1 = !::DeduceTemplateArguments(*this, PS2->getTemplateParameters(),
35425c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                            PT2, PT1, Info, Deduced, TDF_None,
3543dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                            /*PartialOrdering=*/true,
3544b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                                            /*RefParamComparisons=*/0);
35452c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis  if (Better1) {
35462c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis    InstantiatingTemplate Inst(*this, PS2->getLocation(), PS2,
35472c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis                               Deduced.data(), Deduced.size(), Info);
3548dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    Better1 = !::FinishTemplateArgumentDeduction(*this, PS2,
3549dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                 PS1->getTemplateArgs(),
3550516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor                                                 Deduced, Info);
35512c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis  }
3552dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3553bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // Determine whether PS2 is at least as specialized as PS1
3554db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Deduced.clear();
3555bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  Deduced.resize(PS1->getTemplateParameters()->size());
35565c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor  bool Better2 = !::DeduceTemplateArguments(*this, PS1->getTemplateParameters(),
35575c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                            PT1, PT2, Info, Deduced, TDF_None,
35585c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                            /*PartialOrdering=*/true,
3559b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                                            /*RefParamComparisons=*/0);
35602c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis  if (Better2) {
35612c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis    InstantiatingTemplate Inst(*this, PS1->getLocation(), PS1,
35622c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis                               Deduced.data(), Deduced.size(), Info);
3563dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    Better2 = !::FinishTemplateArgumentDeduction(*this, PS1,
3564dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                 PS2->getTemplateArgs(),
3565516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor                                                 Deduced, Info);
35662c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis  }
3567dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3568bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  if (Better1 == Better2)
3569bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    return 0;
3570dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3571bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  return Better1? PS1 : PS2;
3572bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor}
3573bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor
35741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
3575e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
3576e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           const TemplateArgument &TemplateArg,
3577e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
3578ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
3579e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used);
3580031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3581e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
3582031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// expression.
35831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
3584e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
3585e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           const Expr *E,
3586e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
3587ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
3588e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
3589be230c36e32142cbdcdbe9c97511d097beeecbabDouglas Gregor  // We can deduce from a pack expansion.
3590be230c36e32142cbdcdbe9c97511d097beeecbabDouglas Gregor  if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E))
3591be230c36e32142cbdcdbe9c97511d097beeecbabDouglas Gregor    E = Expansion->getPattern();
3592dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
359354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  // Skip through any implicit casts we added while type-checking.
359454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
359554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    E = ICE->getSubExpr();
3596dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3597dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
3598e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // find other occurrences of template parameters.
3599f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor  const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3600c781f9cd854f3d5d1c826f4a13382c6abca4cff7Douglas Gregor  if (!DRE)
3601031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    return;
3602031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
36031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const NonTypeTemplateParmDecl *NTTP
3604031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
3605031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  if (!NTTP)
3606031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    return;
3607031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3608ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (NTTP->getDepth() == Depth)
3609ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Used[NTTP->getIndex()] = true;
3610031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
3611031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3612e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
3613e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// nested name specifier.
3614e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregorstatic void
3615e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
3616e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           NestedNameSpecifier *NNS,
3617e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
3618ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
3619e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
3620e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (!NNS)
3621e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    return;
3622dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3623ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
3624ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             Used);
3625dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
3626ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             OnlyDeduced, Depth, Used);
3627e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor}
3628dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3629e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
3630e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// template name.
3631e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregorstatic void
3632e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
3633e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           TemplateName Name,
3634e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
3635ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
3636e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
3637e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
3638e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    if (TemplateTemplateParmDecl *TTP
3639ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor          = dyn_cast<TemplateTemplateParmDecl>(Template)) {
3640ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      if (TTP->getDepth() == Depth)
3641ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        Used[TTP->getIndex()] = true;
3642ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    }
3643e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    return;
3644e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  }
3645dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3646788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
3647dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
3648788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                               Depth, Used);
3649e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
3650dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
3651ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
3652e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor}
3653e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
3654e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
3655031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// type.
36561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
3657e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, QualType T,
3658e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
3659ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
3660e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
3661e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (T.isNull())
3662e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    return;
3663dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3664031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  // Non-dependent types have nothing deducible
3665031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  if (!T->isDependentType())
3666031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    return;
3667031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3668031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  T = SemaRef.Context.getCanonicalType(T);
3669031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  switch (T->getTypeClass()) {
3670031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Pointer:
3671e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
3672e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<PointerType>(T)->getPointeeType(),
3673e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               OnlyDeduced,
3674ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth,
3675e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               Used);
3676031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3677031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3678031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::BlockPointer:
3679e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
3680e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<BlockPointerType>(T)->getPointeeType(),
3681e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               OnlyDeduced,
3682ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth,
3683e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               Used);
3684031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3685031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3686031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::LValueReference:
3687031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::RValueReference:
3688e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
3689e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<ReferenceType>(T)->getPointeeType(),
3690e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               OnlyDeduced,
3691ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth,
3692e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               Used);
3693031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3694031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3695031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::MemberPointer: {
3696031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
3697e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
3698ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
3699e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
3700ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
3701031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3702031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
3703031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3704031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::DependentSizedArray:
3705e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
3706e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<DependentSizedArrayType>(T)->getSizeExpr(),
3707ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
3708031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    // Fall through to check the element type
3709031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3710031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::ConstantArray:
3711031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::IncompleteArray:
3712e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
3713e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<ArrayType>(T)->getElementType(),
3714ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
3715031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3716031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3717031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Vector:
3718031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::ExtVector:
3719e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
3720e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<VectorType>(T)->getElementType(),
3721ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
3722031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3723031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
37249cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  case Type::DependentSizedExtVector: {
37259cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    const DependentSizedExtVectorType *VecType
3726f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor      = cast<DependentSizedExtVectorType>(T);
3727e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
3728ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
3729dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
3730ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
37319cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    break;
37329cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
37339cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
3734031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::FunctionProto: {
3735f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor    const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
3736e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
3737ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
3738031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
3739e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor      MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
3740ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 Depth, Used);
3741031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3742031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
3743031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3744ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  case Type::TemplateTypeParm: {
3745ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
3746ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (TTP->getDepth() == Depth)
3747ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      Used[TTP->getIndex()] = true;
3748031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3749ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
3750031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
37510bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  case Type::SubstTemplateTypeParmPack: {
37520bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    const SubstTemplateTypeParmPackType *Subst
37530bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor      = cast<SubstTemplateTypeParmPackType>(T);
3754dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    MarkUsedTemplateParameters(SemaRef,
37550bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor                               QualType(Subst->getReplacedParameter(), 0),
37560bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor                               OnlyDeduced, Depth, Used);
37570bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    MarkUsedTemplateParameters(SemaRef, Subst->getArgumentPack(),
37580bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor                               OnlyDeduced, Depth, Used);
37590bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    break;
37600bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  }
37610bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor
376231f17ecbef57b5679c017c375db330546b7b5145John McCall  case Type::InjectedClassName:
376331f17ecbef57b5679c017c375db330546b7b5145John McCall    T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
376431f17ecbef57b5679c017c375db330546b7b5145John McCall    // fall through
376531f17ecbef57b5679c017c375db330546b7b5145John McCall
3766031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::TemplateSpecialization: {
37671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const TemplateSpecializationType *Spec
3768f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor      = cast<TemplateSpecializationType>(T);
3769e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
3770ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
3771dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
37727b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    // C++0x [temp.deduct.type]p9:
3773dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   If the template argument list of P contains a pack expansion that is not
3774dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   the last template argument, the entire template argument list is a
37757b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    //   non-deduced context.
3776dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    if (OnlyDeduced &&
37777b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor        hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs()))
37787b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      break;
37797b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor
3780e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
3781ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
3782ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 Used);
3783e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    break;
3784e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  }
37851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3786e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  case Type::Complex:
3787e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    if (!OnlyDeduced)
3788dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      MarkUsedTemplateParameters(SemaRef,
3789e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                 cast<ComplexType>(T)->getElementType(),
3790ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 OnlyDeduced, Depth, Used);
3791e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    break;
3792031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
37934714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  case Type::DependentName:
3794e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    if (!OnlyDeduced)
3795e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor      MarkUsedTemplateParameters(SemaRef,
37964714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor                                 cast<DependentNameType>(T)->getQualifier(),
3797ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 OnlyDeduced, Depth, Used);
3798031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3799031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
380033500955d731c73717af52088b7fc0e7a85681e7John McCall  case Type::DependentTemplateSpecialization: {
380133500955d731c73717af52088b7fc0e7a85681e7John McCall    const DependentTemplateSpecializationType *Spec
380233500955d731c73717af52088b7fc0e7a85681e7John McCall      = cast<DependentTemplateSpecializationType>(T);
380333500955d731c73717af52088b7fc0e7a85681e7John McCall    if (!OnlyDeduced)
380433500955d731c73717af52088b7fc0e7a85681e7John McCall      MarkUsedTemplateParameters(SemaRef, Spec->getQualifier(),
380533500955d731c73717af52088b7fc0e7a85681e7John McCall                                 OnlyDeduced, Depth, Used);
3806dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
38077b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    // C++0x [temp.deduct.type]p9:
3808dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   If the template argument list of P contains a pack expansion that is not
3809dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   the last template argument, the entire template argument list is a
38107b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    //   non-deduced context.
3811dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    if (OnlyDeduced &&
38127b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor        hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs()))
38137b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      break;
38147b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor
381533500955d731c73717af52088b7fc0e7a85681e7John McCall    for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
381633500955d731c73717af52088b7fc0e7a85681e7John McCall      MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
381733500955d731c73717af52088b7fc0e7a85681e7John McCall                                 Used);
381833500955d731c73717af52088b7fc0e7a85681e7John McCall    break;
381933500955d731c73717af52088b7fc0e7a85681e7John McCall  }
382033500955d731c73717af52088b7fc0e7a85681e7John McCall
3821ad5e73887052193afda72db8efcb812bd083a4a8John McCall  case Type::TypeOf:
3822ad5e73887052193afda72db8efcb812bd083a4a8John McCall    if (!OnlyDeduced)
3823ad5e73887052193afda72db8efcb812bd083a4a8John McCall      MarkUsedTemplateParameters(SemaRef,
3824ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 cast<TypeOfType>(T)->getUnderlyingType(),
3825ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 OnlyDeduced, Depth, Used);
3826ad5e73887052193afda72db8efcb812bd083a4a8John McCall    break;
3827ad5e73887052193afda72db8efcb812bd083a4a8John McCall
3828ad5e73887052193afda72db8efcb812bd083a4a8John McCall  case Type::TypeOfExpr:
3829ad5e73887052193afda72db8efcb812bd083a4a8John McCall    if (!OnlyDeduced)
3830ad5e73887052193afda72db8efcb812bd083a4a8John McCall      MarkUsedTemplateParameters(SemaRef,
3831ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 cast<TypeOfExprType>(T)->getUnderlyingExpr(),
3832ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 OnlyDeduced, Depth, Used);
3833ad5e73887052193afda72db8efcb812bd083a4a8John McCall    break;
3834ad5e73887052193afda72db8efcb812bd083a4a8John McCall
3835ad5e73887052193afda72db8efcb812bd083a4a8John McCall  case Type::Decltype:
3836ad5e73887052193afda72db8efcb812bd083a4a8John McCall    if (!OnlyDeduced)
3837ad5e73887052193afda72db8efcb812bd083a4a8John McCall      MarkUsedTemplateParameters(SemaRef,
3838ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 cast<DecltypeType>(T)->getUnderlyingExpr(),
3839ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 OnlyDeduced, Depth, Used);
3840ad5e73887052193afda72db8efcb812bd083a4a8John McCall    break;
3841ad5e73887052193afda72db8efcb812bd083a4a8John McCall
38427536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  case Type::PackExpansion:
3843dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    MarkUsedTemplateParameters(SemaRef,
38447536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor                               cast<PackExpansionType>(T)->getPattern(),
38457536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor                               OnlyDeduced, Depth, Used);
38467536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    break;
38477536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
384834b41d939a1328f484511c6002ba2456db879a29Richard Smith  case Type::Auto:
384934b41d939a1328f484511c6002ba2456db879a29Richard Smith    MarkUsedTemplateParameters(SemaRef,
385034b41d939a1328f484511c6002ba2456db879a29Richard Smith                               cast<AutoType>(T)->getDeducedType(),
385134b41d939a1328f484511c6002ba2456db879a29Richard Smith                               OnlyDeduced, Depth, Used);
385234b41d939a1328f484511c6002ba2456db879a29Richard Smith
3853e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // None of these types have any template parameters in them.
3854031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Builtin:
3855031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::VariableArray:
3856031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::FunctionNoProto:
3857031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Record:
3858031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Enum:
3859031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::ObjCInterface:
3860c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject:
3861d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  case Type::ObjCObjectPointer:
3862ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  case Type::UnresolvedUsing:
3863031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define TYPE(Class, Base)
3864031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
3865031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define DEPENDENT_TYPE(Class, Base)
3866031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3867031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#include "clang/AST/TypeNodes.def"
3868031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3869031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
3870031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
3871031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3872e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by this
3873031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// template argument.
38741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
3875e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
3876e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           const TemplateArgument &TemplateArg,
3877e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
3878ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
3879e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
3880031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  switch (TemplateArg.getKind()) {
3881031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Null:
3882031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Integral:
3883788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Declaration:
3884031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
38851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3886031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Type:
3887e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
3888ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
3889031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3890031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3891788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  case TemplateArgument::Template:
3892a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor  case TemplateArgument::TemplateExpansion:
3893dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    MarkUsedTemplateParameters(SemaRef,
3894dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                               TemplateArg.getAsTemplateOrTemplatePattern(),
3895788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                               OnlyDeduced, Depth, Used);
3896031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3897031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3898031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Expression:
3899dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
3900ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
3901031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3902dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3903d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson  case TemplateArgument::Pack:
3904e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
3905e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                      PEnd = TemplateArg.pack_end();
3906e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor         P != PEnd; ++P)
3907ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
3908d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson    break;
3909031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
3910031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
3911031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3912031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \brief Mark the template parameters can be deduced by the given
3913031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// template argument list.
3914031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor///
3915031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \param TemplateArgs the template argument list from which template
3916031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// parameters will be deduced.
3917031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor///
3918031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \param Deduced a bit vector whose elements will be set to \c true
3919031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// to indicate when the corresponding template parameter will be
3920031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// deduced.
39211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
3922e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorSema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
3923ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 bool OnlyDeduced, unsigned Depth,
3924e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                 llvm::SmallVectorImpl<bool> &Used) {
39257b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor  // C++0x [temp.deduct.type]p9:
3926dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   If the template argument list of P contains a pack expansion that is not
3927dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   the last template argument, the entire template argument list is a
39287b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor  //   non-deduced context.
3929dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  if (OnlyDeduced &&
39307b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      hasPackExpansionBeforeEnd(TemplateArgs.data(), TemplateArgs.size()))
39317b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    return;
39327b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor
3933031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
3934dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
3935ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 Depth, Used);
3936031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
393763f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor
393863f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor/// \brief Marks all of the template parameters that will be deduced by a
393963f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor/// call to the given function template.
3940dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumivoid
394102024a9f0d8e6c898de276193af604c42ee41269Douglas GregorSema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
394202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                    llvm::SmallVectorImpl<bool> &Deduced) {
3943dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  TemplateParameterList *TemplateParams
394463f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor    = FunctionTemplate->getTemplateParameters();
394563f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  Deduced.clear();
394663f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  Deduced.resize(TemplateParams->size());
3947dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
394863f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
394963f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
395063f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor    ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
3951ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 true, TemplateParams->getDepth(), Deduced);
395263f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor}
3953