SemaTemplateDeduction.cpp revision 8735b294a257a07ca158c28094d7324f0adf889a
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
80361d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor/// \brief Determine whether the parameter has qualifiers that are either
80461d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor/// inconsistent with or a superset of the argument's qualifiers.
80561d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregorstatic bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType,
80661d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor                                                  QualType ArgType) {
80761d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor  Qualifiers ParamQs = ParamType.getQualifiers();
80861d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor  Qualifiers ArgQs = ArgType.getQualifiers();
80961d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor
81061d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor  if (ParamQs == ArgQs)
81161d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor    return false;
81261d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor
81361d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor  // Mismatched (but not missing) Objective-C GC attributes.
81461d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor  if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() &&
81561d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor      ParamQs.hasObjCGCAttr())
81661d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor    return true;
81761d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor
81861d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor  // Mismatched (but not missing) address spaces.
81961d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor  if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() &&
82061d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor      ParamQs.hasAddressSpace())
82161d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor    return true;
82261d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor
82361d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor  // CVR qualifier superset.
82461d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor  return (ParamQs.getCVRQualifiers() != ArgQs.getCVRQualifiers()) &&
82561d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor      ((ParamQs.getCVRQualifiers() | ArgQs.getCVRQualifiers())
82661d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor                                                == ParamQs.getCVRQualifiers());
82761d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor}
82861d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor
829500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \brief Deduce the template arguments by comparing the parameter type and
830500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// the argument type (C++ [temp.deduct.type]).
831500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
832a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth/// \param S the semantic analysis object within which we are deducing
833500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
834500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param TemplateParams the template parameters that we are deducing
835500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
836500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param ParamIn the parameter type
837500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
838500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param ArgIn the argument type
839500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
840500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param Info information about the template argument deduction itself
841500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
842500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param Deduced the deduced template arguments
843500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
844508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
8451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// how template argument deduction is performed.
846500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
8475c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// \param PartialOrdering Whether we're performing template argument deduction
8485c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// in the context of partial ordering (C++0x [temp.deduct.partial]).
8495c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor///
850b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor/// \param RefParamComparisons If we're performing template argument deduction
8515c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// in the context of partial ordering, the set of qualifier comparisons.
8525c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor///
853500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \returns the result of template argument deduction so far. Note that a
854500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// "success" result means that template argument deduction has not yet failed,
855500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// but it may still fail, later, for other reasons.
856f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
857a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
858f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateParameterList *TemplateParams,
859f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        QualType ParamIn, QualType ArgIn,
8602a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                        TemplateDeductionInfo &Info,
86102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                     llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
8625c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                        unsigned TDF,
8635c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                        bool PartialOrdering,
864b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *RefParamComparisons) {
8650b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  // We only want to look at the canonical types, since typedefs and
8660b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  // sugar are not part of template argument deduction.
867a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  QualType Param = S.Context.getCanonicalType(ParamIn);
868a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  QualType Arg = S.Context.getCanonicalType(ArgIn);
8690b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
87077d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor  // If the argument type is a pack expansion, look at its pattern.
871dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // This isn't explicitly called out
87277d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor  if (const PackExpansionType *ArgExpansion
87377d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor                                            = dyn_cast<PackExpansionType>(Arg))
87477d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor    Arg = ArgExpansion->getPattern();
875dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
8765c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor  if (PartialOrdering) {
8775c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    // C++0x [temp.deduct.partial]p5:
878dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   Before the partial ordering is done, certain transformations are
879dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   performed on the types used for partial ordering:
880dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //     - If P is a reference type, P is replaced by the type referred to.
8815c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    const ReferenceType *ParamRef = Param->getAs<ReferenceType>();
8825c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    if (ParamRef)
8835c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      Param = ParamRef->getPointeeType();
884dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
8855c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //     - If A is a reference type, A is replaced by the type referred to.
8865c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    const ReferenceType *ArgRef = Arg->getAs<ReferenceType>();
8875c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    if (ArgRef)
8885c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      Arg = ArgRef->getPointeeType();
889dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
890b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    if (RefParamComparisons && ParamRef && ArgRef) {
8915c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      // C++0x [temp.deduct.partial]p6:
892dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      //   If both P and A were reference types (before being replaced with the
893dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      //   type referred to above), determine which of the two types (if any) is
8945c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      //   more cv-qualified than the other; otherwise the types are considered
895dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      //   to be equally cv-qualified for partial ordering purposes. The result
8965c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      //   of this determination will be used below.
8975c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      //
898dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      // We save this information for later, using it only when deduction
8995c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      // succeeds in both directions.
900b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      RefParamPartialOrderingComparison Comparison;
901b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      Comparison.ParamIsRvalueRef = ParamRef->getAs<RValueReferenceType>();
902b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      Comparison.ArgIsRvalueRef = ArgRef->getAs<RValueReferenceType>();
903b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      Comparison.Qualifiers = NeitherMoreQualified;
904769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor
905769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor      Qualifiers ParamQuals = Param.getQualifiers();
906769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor      Qualifiers ArgQuals = Arg.getQualifiers();
907769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor      if (ParamQuals.isStrictSupersetOf(ArgQuals))
908b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor        Comparison.Qualifiers = ParamMoreQualified;
909769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor      else if (ArgQuals.isStrictSupersetOf(ParamQuals))
910b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor        Comparison.Qualifiers = ArgMoreQualified;
911b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      RefParamComparisons->push_back(Comparison);
9125c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    }
913dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
9145c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    // C++0x [temp.deduct.partial]p7:
9155c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //   Remove any top-level cv-qualifiers:
916dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //     - If P is a cv-qualified type, P is replaced by the cv-unqualified
9175c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //       version of P.
9185c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    Param = Param.getUnqualifiedType();
919dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //     - If A is a cv-qualified type, A is replaced by the cv-unqualified
9205c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //       version of A.
9215c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    Arg = Arg.getUnqualifiedType();
9225c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor  } else {
9235c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    // C++0x [temp.deduct.call]p4 bullet 1:
9245c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //   - If the original P is a reference type, the deduced A (i.e., the type
9255c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //     referred to by the reference) can be more cv-qualified than the
9265c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //     transformed A.
9275c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    if (TDF & TDF_ParamWithReferenceType) {
9285c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      Qualifiers Quals;
9295c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals);
9305c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      Quals.setCVRQualifiers(Quals.getCVRQualifiers() &
93162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall                             Arg.getCVRQualifiers());
9325c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      Param = S.Context.getQualifiedType(UnqualParam, Quals);
9335c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    }
934dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
93573b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor    if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) {
93673b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      // C++0x [temp.deduct.type]p10:
93773b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      //   If P and A are function types that originated from deduction when
93873b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      //   taking the address of a function template (14.8.2.2) or when deducing
93973b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      //   template arguments from a function declaration (14.8.2.6) and Pi and
940dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      //   Ai are parameters of the top-level parameter-type-list of P and A,
941dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      //   respectively, Pi is adjusted if it is an rvalue reference to a
942dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      //   cv-unqualified template parameter and Ai is an lvalue reference, in
943dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      //   which case the type of Pi is changed to be the template parameter
94473b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      //   type (i.e., T&& is changed to simply T). [ Note: As a result, when
94573b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      //   Pi is T&& and Ai is X&, the adjusted Pi will be T, causing T to be
9460099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi      //   deduced as X&. - end note ]
94773b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      TDF &= ~TDF_TopLevelParameterTypeList;
948dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
94973b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      if (const RValueReferenceType *ParamRef
95073b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor                                        = Param->getAs<RValueReferenceType>()) {
95173b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor        if (isa<TemplateTypeParmType>(ParamRef->getPointeeType()) &&
95273b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor            !ParamRef->getPointeeType().getQualifiers())
95373b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor          if (Arg->isLValueReferenceType())
95473b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor            Param = ParamRef->getPointeeType();
95573b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      }
95673b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor    }
957500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor  }
958dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
959f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  // If the parameter type is not dependent, there is nothing to deduce.
9601282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor  if (!Param->isDependentType()) {
9613cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor    if (!(TDF & TDF_SkipNonDependent) && Param != Arg)
9621282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor      return Sema::TDK_NonDeducedMismatch;
963dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
964f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    return Sema::TDK_Success;
9651282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor  }
9660b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
967199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  // C++ [temp.deduct.type]p9:
9681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   A template type argument T, a template template argument TT or a
9691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   template non-type argument i can be deduced if P and A have one of
970199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  //   the following forms:
971199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  //
972199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  //     T
973199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  //     cv-list T
9741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const TemplateTypeParmType *TemplateTypeParm
975183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall        = Param->getAs<TemplateTypeParmType>()) {
976f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    unsigned Index = TemplateTypeParm->getIndex();
977f290e0db835a619014538c41ed552696efc0e977Douglas Gregor    bool RecanonicalizeArg = false;
9781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9799e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor    // If the argument type is an array type, move the qualifiers up to the
9809e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor    // top level, so they can be matched with the qualifiers on the parameter.
981f290e0db835a619014538c41ed552696efc0e977Douglas Gregor    if (isa<ArrayType>(Arg)) {
9820953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      Qualifiers Quals;
983a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      Arg = S.Context.getUnqualifiedArrayType(Arg, Quals);
9840953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (Quals) {
985a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        Arg = S.Context.getQualifiedType(Arg, Quals);
986f290e0db835a619014538c41ed552696efc0e977Douglas Gregor        RecanonicalizeArg = true;
987f290e0db835a619014538c41ed552696efc0e977Douglas Gregor      }
988f290e0db835a619014538c41ed552696efc0e977Douglas Gregor    }
9891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9900b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor    // The argument type can not be less qualified than the parameter
9910b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor    // type.
99261d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor    if (!(TDF & TDF_IgnoreQualifiers) &&
99361d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor        hasInconsistentOrSupersetQualifiersOf(Param, Arg)) {
994f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
99557e97786433e70197a089360228d8f0d82e3ad4cJohn McCall      Info.FirstArg = TemplateArgument(Param);
996833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      Info.SecondArg = TemplateArgument(Arg);
99757e97786433e70197a089360228d8f0d82e3ad4cJohn McCall      return Sema::TDK_Underqualified;
998f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    }
9990b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
10000b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor    assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
1001a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth    assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function");
10020953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    QualType DeducedType = Arg;
100349f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall
100461d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor    // Remove any qualifiers on the parameter from the deduced type.
100561d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor    // We checked the qualifiers for consistency above.
100661d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor    Qualifiers DeducedQs = DeducedType.getQualifiers();
100761d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor    Qualifiers ParamQs = Param.getQualifiers();
100861d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor    DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers());
100961d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor    if (ParamQs.hasObjCGCAttr())
101061d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor      DeducedQs.removeObjCGCAttr();
101161d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor    if (ParamQs.hasAddressSpace())
101261d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor      DeducedQs.removeAddressSpace();
101361d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor    DeducedType = S.Context.getQualifiedType(DeducedType.getUnqualifiedType(),
101461d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor                                             DeducedQs);
101561d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor
1016f290e0db835a619014538c41ed552696efc0e977Douglas Gregor    if (RecanonicalizeArg)
1017a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      DeducedType = S.Context.getCanonicalType(DeducedType);
10181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10190d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    DeducedTemplateArgument NewDeduced(DeducedType);
1020dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
10210d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                                                 Deduced[Index],
10220d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                                                   NewDeduced);
10230d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    if (Result.isNull()) {
10240d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
10250d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      Info.FirstArg = Deduced[Index];
10260d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      Info.SecondArg = NewDeduced;
1027dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      return Sema::TDK_Inconsistent;
10280b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor    }
1029dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
10300d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    Deduced[Index] = Result;
1031dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return Sema::TDK_Success;
10320b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
10330b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
1034f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  // Set up the template argument deduction information for a failure.
1035833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  Info.FirstArg = TemplateArgument(ParamIn);
1036833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  Info.SecondArg = TemplateArgument(ArgIn);
1037f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
10380bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  // If the parameter is an already-substituted template parameter
10390bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  // pack, do nothing: we don't know which of its arguments to look
10400bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  // at, so we have to wait until all of the parameter packs in this
10410bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  // expansion have arguments.
10420bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  if (isa<SubstTemplateTypeParmPackType>(Param))
10430bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    return Sema::TDK_Success;
10440bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor
1045508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  // Check the cv-qualifiers on the parameter and argument types.
1046508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  if (!(TDF & TDF_IgnoreQualifiers)) {
1047508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    if (TDF & TDF_ParamWithReferenceType) {
104861d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor      if (hasInconsistentOrSupersetQualifiersOf(Param, Arg))
1049508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
1050cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall    } else if (!IsPossiblyOpaquelyQualifiedType(Param)) {
1051508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor      if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
10521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        return Sema::TDK_NonDeducedMismatch;
1053508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    }
1054508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  }
10550b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
1056d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor  switch (Param->getTypeClass()) {
1057199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    // No deduction possible for these types
1058199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    case Type::Builtin:
1059f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
10601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1061199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T *
1062d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    case Type::Pointer: {
1063c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall      QualType PointeeType;
1064c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall      if (const PointerType *PointerArg = Arg->getAs<PointerType>()) {
1065c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall        PointeeType = PointerArg->getPointeeType();
1066c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall      } else if (const ObjCObjectPointerType *PointerArg
1067c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall                   = Arg->getAs<ObjCObjectPointerType>()) {
1068c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall        PointeeType = PointerArg->getPointeeType();
1069c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall      } else {
1070f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
1071c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall      }
10721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10734112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor      unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
1074a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
1075d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                                   cast<PointerType>(Param)->getPointeeType(),
1076c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall                                     PointeeType,
10774112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor                                     Info, Deduced, SubTDF);
1078d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    }
10791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1080199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T &
1081d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    case Type::LValueReference: {
10826217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
1083d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor      if (!ReferenceArg)
1084f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
10851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1086a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
1087d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                           cast<LValueReferenceType>(Param)->getPointeeType(),
1088d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                                     ReferenceArg->getPointeeType(),
1089508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
1090d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    }
10910b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
1092199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T && [C++0x]
1093d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    case Type::RValueReference: {
10946217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
1095d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor      if (!ReferenceArg)
1096f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
10971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1098a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
1099d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                           cast<RValueReferenceType>(Param)->getPointeeType(),
1100d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                                     ReferenceArg->getPointeeType(),
1101508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
1102d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    }
11031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1104199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T [] (implied, but not stated explicitly)
11054d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson    case Type::IncompleteArray: {
11061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const IncompleteArrayType *IncompleteArrayArg =
1107a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        S.Context.getAsIncompleteArrayType(Arg);
11084d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson      if (!IncompleteArrayArg)
1109f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
11101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1111e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall      unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
1112a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
1113a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth                     S.Context.getAsIncompleteArrayType(Param)->getElementType(),
11144d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson                                     IncompleteArrayArg->getElementType(),
1115e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall                                     Info, Deduced, SubTDF);
11164d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson    }
1117199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
1118199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T [integer-constant]
11194d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson    case Type::ConstantArray: {
11201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const ConstantArrayType *ConstantArrayArg =
1121a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        S.Context.getAsConstantArrayType(Arg);
11224d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson      if (!ConstantArrayArg)
1123f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
11241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const ConstantArrayType *ConstantArrayParm =
1126a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        S.Context.getAsConstantArrayType(Param);
11274d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson      if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
1128f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
11291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1130e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall      unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
1131a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
11324d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson                                     ConstantArrayParm->getElementType(),
11334d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson                                     ConstantArrayArg->getElementType(),
1134e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall                                     Info, Deduced, SubTDF);
11354d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson    }
11364d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson
1137199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     type [i]
1138199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    case Type::DependentSizedArray: {
1139a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg);
1140199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (!ArrayArg)
1141f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
11421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1143e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall      unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
1144e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall
1145199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // Check the element type of the arrays
1146199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      const DependentSizedArrayType *DependentArrayParm
1147a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = S.Context.getAsDependentSizedArrayType(Param);
1148f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (Sema::TemplateDeductionResult Result
1149a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            = DeduceTemplateArguments(S, TemplateParams,
1150f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      DependentArrayParm->getElementType(),
1151f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      ArrayArg->getElementType(),
1152e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall                                      Info, Deduced, SubTDF))
1153f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Result;
11541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1155199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // Determine the array bound is something we can deduce.
11561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NonTypeTemplateParmDecl *NTTP
1157199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor        = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
1158199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (!NTTP)
1159f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_Success;
11601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We can perform template argument deduction for the given non-type
1162199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // template parameter.
11631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(NTTP->getDepth() == 0 &&
1164199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor             "Cannot deduce non-type template argument at depth > 0");
11651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (const ConstantArrayType *ConstantArrayArg
1166335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson            = dyn_cast<ConstantArrayType>(ArrayArg)) {
1167335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson        llvm::APSInt Size(ConstantArrayArg->getSize());
1168dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        return DeduceNonTypeTemplateArgument(S, NTTP, Size,
11699d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor                                             S.Context.getSizeType(),
117002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                             /*ArrayBound=*/true,
1171f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                             Info, Deduced);
1172335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson      }
1173199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (const DependentSizedArrayType *DependentArrayArg
1174199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor            = dyn_cast<DependentSizedArrayType>(ArrayArg))
117534c2f8c8a16226f757947bf08c5f799d99c9ac1eDouglas Gregor        if (DependentArrayArg->getSizeExpr())
117634c2f8c8a16226f757947bf08c5f799d99c9ac1eDouglas Gregor          return DeduceNonTypeTemplateArgument(S, NTTP,
117734c2f8c8a16226f757947bf08c5f799d99c9ac1eDouglas Gregor                                               DependentArrayArg->getSizeExpr(),
117834c2f8c8a16226f757947bf08c5f799d99c9ac1eDouglas Gregor                                               Info, Deduced);
11791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1180199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // Incomplete type does not match a dependently-sized array type
1181f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
1182199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    }
11831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     type(*)(T)
11851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(*)()
11861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(*)(T)
1187a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson    case Type::FunctionProto: {
118873b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList;
11891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const FunctionProtoType *FunctionProtoArg =
1190a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson        dyn_cast<FunctionProtoType>(Arg);
1191a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson      if (!FunctionProtoArg)
1192f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
11931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const FunctionProtoType *FunctionProtoParam =
1195a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson        cast<FunctionProtoType>(Param);
1196994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson
1197dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      if (FunctionProtoParam->getTypeQuals()
1198e3c7a7ca66c02567543dbb5ec493818e00e8b177Douglas Gregor            != FunctionProtoArg->getTypeQuals() ||
1199dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi          FunctionProtoParam->getRefQualifier()
1200e3c7a7ca66c02567543dbb5ec493818e00e8b177Douglas Gregor            != FunctionProtoArg->getRefQualifier() ||
1201e3c7a7ca66c02567543dbb5ec493818e00e8b177Douglas Gregor          FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
1202f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
1203994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson
1204a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson      // Check return types.
1205f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (Sema::TemplateDeductionResult Result
1206a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            = DeduceTemplateArguments(S, TemplateParams,
1207f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      FunctionProtoParam->getResultType(),
1208f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      FunctionProtoArg->getResultType(),
1209508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                      Info, Deduced, 0))
1210f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Result;
12111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1212603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      return DeduceTemplateArguments(S, TemplateParams,
1213603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                                     FunctionProtoParam->arg_type_begin(),
1214603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                                     FunctionProtoParam->getNumArgs(),
1215603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                                     FunctionProtoArg->arg_type_begin(),
1216603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                                     FunctionProtoArg->getNumArgs(),
121773b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor                                     Info, Deduced, SubTDF);
1218a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson    }
12191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12203cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    case Type::InjectedClassName: {
12213cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // Treat a template's injected-class-name as if the template
12223cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // specialization type had been used.
122331f17ecbef57b5679c017c375db330546b7b5145John McCall      Param = cast<InjectedClassNameType>(Param)
122431f17ecbef57b5679c017c375db330546b7b5145John McCall        ->getInjectedSpecializationType();
12253cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      assert(isa<TemplateSpecializationType>(Param) &&
12263cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall             "injected class name is not a template specialization type");
12273cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // fall through
12283cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    }
12293cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
1230f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    //     template-name<T> (where template-name refers to a class template)
1231d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor    //     template-name<i>
1232db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    //     TT<T>
1233db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    //     TT<i>
1234db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    //     TT<>
1235d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor    case Type::TemplateSpecialization: {
1236d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor      const TemplateSpecializationType *SpecParam
1237d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor        = cast<TemplateSpecializationType>(Param);
12381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1239de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      // Try to deduce template arguments from the template-id.
1240de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      Sema::TemplateDeductionResult Result
1241a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg,
1242de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                  Info, Deduced);
12431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12444a5c15f75f76b95e1c2ceb6fa2737dcadd5f4be1Douglas Gregor      if (Result && (TDF & TDF_DerivedClass)) {
1245de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        // C++ [temp.deduct.call]p3b3:
1246de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   If P is a class, and P has the form template-id, then A can be a
1247de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   derived class of the deduced A. Likewise, if P is a pointer to a
12481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        //   class of the form template-id, A can be a pointer to a derived
1249de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   class pointed to by the deduced A.
1250de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //
1251de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        // More importantly:
12521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        //   These alternatives are considered only if type deduction would
1253de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   otherwise fail.
1254a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        if (const RecordType *RecordT = Arg->getAs<RecordType>()) {
1255a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          // We cannot inspect base classes as part of deduction when the type
1256a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          // is incomplete, so either instantiate any templates necessary to
1257a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          // complete the type, or skip over it if it cannot be completed.
12585769d6195087229770d7ac90449443e026c47103John McCall          if (S.RequireCompleteType(Info.getLocation(), Arg, 0))
1259a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            return Result;
1260a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth
1261de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          // Use data recursion to crawl through the list of base classes.
12621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          // Visited contains the set of nodes we have already visited, while
1263de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          // ToVisit is our stack of records that we still need to visit.
1264de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          llvm::SmallPtrSet<const RecordType *, 8> Visited;
1265de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          llvm::SmallVector<const RecordType *, 8> ToVisit;
1266de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          ToVisit.push_back(RecordT);
1267de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          bool Successful = false;
1268053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor          llvm::SmallVectorImpl<DeducedTemplateArgument> DeducedOrig(0);
1269053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor          DeducedOrig = Deduced;
1270de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          while (!ToVisit.empty()) {
1271de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // Retrieve the next class in the inheritance hierarchy.
1272de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            const RecordType *NextT = ToVisit.back();
1273de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            ToVisit.pop_back();
12741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1275de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // If we have already seen this type, skip it.
1276de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            if (!Visited.insert(NextT))
1277de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              continue;
12781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1279de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // If this is a base class, try to perform template argument
1280de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // deduction from it.
1281de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            if (NextT != RecordT) {
1282de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              Sema::TemplateDeductionResult BaseResult
1283a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth                = DeduceTemplateArguments(S, TemplateParams, SpecParam,
1284de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                          QualType(NextT, 0), Info, Deduced);
12851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1286de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              // If template argument deduction for this base was successful,
1287053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor              // note that we had some success. Otherwise, ignore any deductions
1288053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor              // from this base class.
1289053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor              if (BaseResult == Sema::TDK_Success) {
1290de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                Successful = true;
1291053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor                DeducedOrig = Deduced;
1292053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor              }
1293053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor              else
1294053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor                Deduced = DeducedOrig;
1295de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            }
12961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1297de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // Visit base classes
1298de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
1299de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
1300de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                                 BaseEnd = Next->bases_end();
13019994a34f6cf842721ba7723edc0b9036229fe387Sebastian Redl                 Base != BaseEnd; ++Base) {
13021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump              assert(Base->getType()->isRecordType() &&
1303de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                     "Base class that isn't a record?");
13046217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek              ToVisit.push_back(Base->getType()->getAs<RecordType>());
1305de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            }
1306de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          }
13071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1308de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          if (Successful)
1309de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            return Sema::TDK_Success;
1310de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        }
13111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1312de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      }
13131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1314de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      return Result;
1315d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor    }
1316d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor
1317637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T type::*
1318637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T T::*
1319637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (type::*)()
1320637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     type (T::*)()
1321637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     type (type::*)(T)
1322637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     type (T::*)(T)
1323637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (type::*)(T)
1324637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (T::*)()
1325637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (T::*)(T)
1326637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    case Type::MemberPointer: {
1327637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
1328637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
1329637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      if (!MemPtrArg)
1330f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
1331f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
1332f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (Sema::TemplateDeductionResult Result
1333a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            = DeduceTemplateArguments(S, TemplateParams,
1334f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      MemPtrParam->getPointeeType(),
1335f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      MemPtrArg->getPointeeType(),
1336508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                      Info, Deduced,
1337508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                      TDF & TDF_IgnoreQualifiers))
1338f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Result;
1339f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
1340a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
1341f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                     QualType(MemPtrParam->getClass(), 0),
1342f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                     QualType(MemPtrArg->getClass(), 0),
1343508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
1344637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    }
1345637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor
13469a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    //     (clang extension)
13479a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    //
13481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     type(^)(T)
13491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(^)()
13501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(^)(T)
1351859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson    case Type::BlockPointer: {
1352859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson      const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
1353859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson      const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
13541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1355859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson      if (!BlockPtrArg)
1356f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
13571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1358a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
1359859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson                                     BlockPtrParam->getPointeeType(),
1360f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                     BlockPtrArg->getPointeeType(), Info,
1361508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Deduced, 0);
1362859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson    }
1363859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson
1364637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    case Type::TypeOfExpr:
1365637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    case Type::TypeOf:
13664714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    case Type::DependentName:
1367637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      // No template argument deduction for these types
1368f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_Success;
1369637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor
1370d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    default:
1371d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor      break;
13720b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
13730b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
13740b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  // FIXME: Many more cases to go (to go).
1375f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  return Sema::TDK_Success;
13760b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
13770b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
1378f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
1379a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
1380f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateParameterList *TemplateParams,
1381f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        const TemplateArgument &Param,
138277d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor                        TemplateArgument Arg,
13832a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                        TemplateDeductionInfo &Info,
138402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
138577d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor  // If the template argument is a pack expansion, perform template argument
138677d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor  // deduction against the pattern of that expansion. This only occurs during
138777d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor  // partial ordering.
138877d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor  if (Arg.isPackExpansion())
138977d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor    Arg = Arg.getPackExpansionPattern();
1390dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
13910b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  switch (Param.getKind()) {
1392199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Null:
1393199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    assert(false && "Null template argument in parameter list");
1394199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    break;
13951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case TemplateArgument::Type:
1397788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    if (Arg.getKind() == TemplateArgument::Type)
1398a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams, Param.getAsType(),
1399788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                                     Arg.getAsType(), Info, Deduced, 0);
1400788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.FirstArg = Param;
1401788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.SecondArg = Arg;
1402788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
1403a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
1404788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  case TemplateArgument::Template:
1405db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    if (Arg.getKind() == TemplateArgument::Template)
1406dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      return DeduceTemplateArguments(S, TemplateParams,
1407788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                                     Param.getAsTemplate(),
1408db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor                                     Arg.getAsTemplate(), Info, Deduced);
1409788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.FirstArg = Param;
1410788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.SecondArg = Arg;
1411788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
1412a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
1413a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor  case TemplateArgument::TemplateExpansion:
1414a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    llvm_unreachable("caller should handle pack expansions");
1415a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    break;
1416dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1417199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Declaration:
1418788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    if (Arg.getKind() == TemplateArgument::Declaration &&
1419788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor        Param.getAsDecl()->getCanonicalDecl() ==
1420788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor          Arg.getAsDecl()->getCanonicalDecl())
1421788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return Sema::TDK_Success;
1422dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1423f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.FirstArg = Param;
1424f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.SecondArg = Arg;
1425f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_NonDeducedMismatch;
14261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1427199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Integral:
1428199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    if (Arg.getKind() == TemplateArgument::Integral) {
14299d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor      if (hasSameExtendedValue(*Param.getAsIntegral(), *Arg.getAsIntegral()))
1430f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_Success;
1431f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
1432f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.FirstArg = Param;
1433f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.SecondArg = Arg;
1434f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
1435f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    }
1436f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
1437f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    if (Arg.getKind() == TemplateArgument::Expression) {
1438f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.FirstArg = Param;
1439f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.SecondArg = Arg;
1440f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
1441199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    }
1442199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
1443f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.FirstArg = Param;
1444f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.SecondArg = Arg;
1445f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_NonDeducedMismatch;
14461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1447199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Expression: {
14481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (NonTypeTemplateParmDecl *NTTP
1449199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor          = getDeducedParameterFromExpr(Param.getAsExpr())) {
1450199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (Arg.getKind() == TemplateArgument::Integral)
1451a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP,
14521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             *Arg.getAsIntegral(),
14539d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor                                             Arg.getIntegralType(),
145402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                             /*ArrayBound=*/false,
1455f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                             Info, Deduced);
1456199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (Arg.getKind() == TemplateArgument::Expression)
1457a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(),
1458f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                             Info, Deduced);
145915755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor      if (Arg.getKind() == TemplateArgument::Declaration)
1460a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(),
146115755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor                                             Info, Deduced);
1462dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1463f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.FirstArg = Param;
1464f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.SecondArg = Arg;
1465f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
1466199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    }
14671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1468199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    // Can't deduce anything, but that's okay.
1469f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_Success;
1470199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  }
1471d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson  case TemplateArgument::Pack:
147220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor    llvm_unreachable("Argument packs should be expanded by the caller!");
14730b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
14741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1475f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  return Sema::TDK_Success;
14760b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
14770b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
147820a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// \brief Determine whether there is a template argument to be used for
147920a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// deduction.
148020a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor///
148120a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// This routine "expands" argument packs in-place, overriding its input
148220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// parameters so that \c Args[ArgIdx] will be the available template argument.
148320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor///
148420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// \returns true if there is another template argument (which will be at
148520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// \c Args[ArgIdx]), false otherwise.
1486dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic bool hasTemplateArgumentForDeduction(const TemplateArgument *&Args,
148720a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                                            unsigned &ArgIdx,
148820a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                                            unsigned &NumArgs) {
148920a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  if (ArgIdx == NumArgs)
149020a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor    return false;
1491dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
149220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  const TemplateArgument &Arg = Args[ArgIdx];
149320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  if (Arg.getKind() != TemplateArgument::Pack)
149420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor    return true;
149520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor
149620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  assert(ArgIdx == NumArgs - 1 && "Pack not at the end of argument list?");
149720a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  Args = Arg.pack_begin();
149820a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  NumArgs = Arg.pack_size();
149920a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  ArgIdx = 0;
150020a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  return ArgIdx < NumArgs;
150120a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor}
150220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor
15037b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor/// \brief Determine whether the given set of template arguments has a pack
15047b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor/// expansion that is not the last template argument.
15057b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregorstatic bool hasPackExpansionBeforeEnd(const TemplateArgument *Args,
15067b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor                                      unsigned NumArgs) {
15077b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor  unsigned ArgIdx = 0;
15087b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor  while (ArgIdx < NumArgs) {
15097b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    const TemplateArgument &Arg = Args[ArgIdx];
1510dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
15117b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    // Unwrap argument packs.
15127b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    if (Args[ArgIdx].getKind() == TemplateArgument::Pack) {
15137b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      Args = Arg.pack_begin();
15147b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      NumArgs = Arg.pack_size();
15157b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      ArgIdx = 0;
15167b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      continue;
15177b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    }
1518dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
15197b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    ++ArgIdx;
15207b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    if (ArgIdx == NumArgs)
15217b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      return false;
1522dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
15237b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    if (Arg.isPackExpansion())
15247b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      return true;
15257b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor  }
1526dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
15277b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor  return false;
15287b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor}
15297b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor
15301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic Sema::TemplateDeductionResult
1531a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
1532f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateParameterList *TemplateParams,
153320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        const TemplateArgument *Params, unsigned NumParams,
153420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        const TemplateArgument *Args, unsigned NumArgs,
15352a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                        TemplateDeductionInfo &Info,
15360972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
15370972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor                        bool NumberOfArgumentsMustMatch) {
1538e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor  // C++0x [temp.deduct.type]p9:
1539dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   If the template argument list of P contains a pack expansion that is not
1540dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   the last template argument, the entire template argument list is a
1541e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor  //   non-deduced context.
15427b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor  if (hasPackExpansionBeforeEnd(Params, NumParams))
15437b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    return Sema::TDK_Success;
1544dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1545e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor  // C++0x [temp.deduct.type]p9:
1546dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   If P has a form that contains <T> or <i>, then each argument Pi of the
1547dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   respective template argument list P is compared with the corresponding
1548e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor  //   argument Ai of the corresponding template argument list of A.
154920a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  unsigned ArgIdx = 0, ParamIdx = 0;
1550dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  for (; hasTemplateArgumentForDeduction(Params, ParamIdx, NumParams);
155120a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor       ++ParamIdx) {
155220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor    if (!Params[ParamIdx].isPackExpansion()) {
1553e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      // The simple case: deduce template arguments by matching Pi and Ai.
1554dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
155520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor      // Check whether we have enough arguments.
155620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor      if (!hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs))
15573cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor        return NumberOfArgumentsMustMatch? Sema::TDK_NonDeducedMismatch
15580972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor                                         : Sema::TDK_Success;
1559dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
156077d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor      if (Args[ArgIdx].isPackExpansion()) {
156177d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor        // FIXME: We follow the logic of C++0x [temp.deduct.type]p22 here,
156277d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor        // but applied to pack expansions that are template arguments.
156377d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
156477d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor      }
1565dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1566e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      // Perform deduction for this Pi/Ai pair.
156720a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor      if (Sema::TemplateDeductionResult Result
156877d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor            = DeduceTemplateArguments(S, TemplateParams,
156977d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor                                      Params[ParamIdx], Args[ArgIdx],
157077d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor                                      Info, Deduced))
1571dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        return Result;
1572dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
157320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor      // Move to the next argument.
157420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor      ++ArgIdx;
157520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor      continue;
157620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor    }
1577dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1578e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // The parameter is a pack expansion.
1579dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1580e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // C++0x [temp.deduct.type]p9:
1581dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   If Pi is a pack expansion, then the pattern of Pi is compared with
1582dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   each remaining argument in the template argument list of A. Each
1583dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   comparison deduces template arguments for subsequent positions in the
1584e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    //   template parameter packs expanded by Pi.
1585e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern();
1586dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1587e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // Compute the set of template parameter indices that correspond to
1588e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // parameter packs expanded by the pack expansion.
1589e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    llvm::SmallVector<unsigned, 2> PackIndices;
1590e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    {
1591e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      llvm::BitVector SawIndices(TemplateParams->size());
1592e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1593e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      S.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1594e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
1595e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor        unsigned Depth, Index;
1596e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor        llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]);
1597e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor        if (Depth == 0 && !SawIndices[Index]) {
1598e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor          SawIndices[Index] = true;
1599e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor          PackIndices.push_back(Index);
1600e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor        }
1601e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      }
1602e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    }
1603e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?");
1604dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1605e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // FIXME: If there are no remaining arguments, we can bail out early
1606e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // and set any deduced parameter packs to an empty argument pack.
1607e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // The latter part of this is a (minor) correctness issue.
1608dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1609e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // Save the deduced template arguments for each parameter pack expanded
1610e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // by this pack expansion, then clear out the deduction.
1611dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    llvm::SmallVector<DeducedTemplateArgument, 2>
1612e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      SavedPacks(PackIndices.size());
16135429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor    llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2>
16145429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor      NewlyDeducedPacks(PackIndices.size());
1615dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    PrepareArgumentPackDeduction(S, Deduced, PackIndices, SavedPacks,
16165429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor                                 NewlyDeducedPacks);
1617e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor
1618e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // Keep track of the deduced template arguments for each parameter pack
1619dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    // expanded by this pack expansion (the outer index) and for each
1620e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // template argument (the inner SmallVectors).
1621e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    bool HasAnyArguments = false;
1622e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    while (hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs)) {
1623e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      HasAnyArguments = true;
1624dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1625e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      // Deduce template arguments from the pattern.
1626dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      if (Sema::TemplateDeductionResult Result
1627e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor            = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx],
1628e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                                      Info, Deduced))
1629e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor        return Result;
1630dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1631e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      // Capture the deduced template arguments for each parameter pack expanded
1632e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      // by this pack expansion, add them to the list of arguments we've deduced
1633e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      // for that pack, then clear out the deduced argument.
1634e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
1635e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor        DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]];
1636e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor        if (!DeducedArg.isNull()) {
1637e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor          NewlyDeducedPacks[I].push_back(DeducedArg);
1638e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor          DeducedArg = DeducedTemplateArgument();
1639e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor        }
1640e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      }
1641dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1642e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      ++ArgIdx;
1643e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    }
1644dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1645e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // Build argument packs for each of the parameter packs expanded by this
1646e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // pack expansion.
16470216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    if (Sema::TemplateDeductionResult Result
1648dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi          = FinishArgumentPackDeduction(S, TemplateParams, HasAnyArguments,
16490216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                                        Deduced, PackIndices, SavedPacks,
16500216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                                        NewlyDeducedPacks, Info))
1651dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      return Result;
16520b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
1653dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
165420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  // If there is an argument remaining, then we had too many arguments.
16550972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor  if (NumberOfArgumentsMustMatch &&
16560972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor      hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs))
16573cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor    return Sema::TDK_NonDeducedMismatch;
1658dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1659f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  return Sema::TDK_Success;
16600b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
16610b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
166220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregorstatic Sema::TemplateDeductionResult
166320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas GregorDeduceTemplateArguments(Sema &S,
166420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        TemplateParameterList *TemplateParams,
166520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        const TemplateArgumentList &ParamList,
166620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        const TemplateArgumentList &ArgList,
166720a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        TemplateDeductionInfo &Info,
166820a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
1669dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  return DeduceTemplateArguments(S, TemplateParams,
167020a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                                 ParamList.data(), ParamList.size(),
167120a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                                 ArgList.data(), ArgList.size(),
167220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                                 Info, Deduced);
167320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor}
167420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor
1675f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// \brief Determine whether two template arguments are the same.
16761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool isSameTemplateArg(ASTContext &Context,
1677f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor                              const TemplateArgument &X,
1678f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor                              const TemplateArgument &Y) {
1679f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  if (X.getKind() != Y.getKind())
1680f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    return false;
16811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1682f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  switch (X.getKind()) {
1683f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Null:
1684f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      assert(false && "Comparing NULL template argument");
1685f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      break;
16861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1687f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Type:
1688f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      return Context.getCanonicalType(X.getAsType()) ==
1689f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor             Context.getCanonicalType(Y.getAsType());
16901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1691f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Declaration:
169297fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis      return X.getAsDecl()->getCanonicalDecl() ==
169397fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis             Y.getAsDecl()->getCanonicalDecl();
16941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1695788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Template:
1696a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    case TemplateArgument::TemplateExpansion:
1697a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      return Context.getCanonicalTemplateName(
1698a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                    X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() ==
1699a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor             Context.getCanonicalTemplateName(
1700a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                    Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer();
1701dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1702f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Integral:
1703f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      return *X.getAsIntegral() == *Y.getAsIntegral();
17041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1705788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Expression: {
1706788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      llvm::FoldingSetNodeID XID, YID;
1707788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      X.getAsExpr()->Profile(XID, Context, true);
1708dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      Y.getAsExpr()->Profile(YID, Context, true);
1709788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return XID == YID;
1710788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    }
17111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1712f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Pack:
1713f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      if (X.pack_size() != Y.pack_size())
1714f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor        return false;
17151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (TemplateArgument::pack_iterator XP = X.pack_begin(),
17171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        XPEnd = X.pack_end(),
1718f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor                                           YP = Y.pack_begin();
17191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump           XP != XPEnd; ++XP, ++YP)
1720f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor        if (!isSameTemplateArg(Context, *XP, *YP))
1721f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor          return false;
1722f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
1723f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      return true;
1724f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  }
1725f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
1726f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  return false;
1727f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor}
1728f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
172954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// \brief Allocate a TemplateArgumentLoc where all locations have
173054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// been initialized to the given location.
173154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor///
173254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// \param S The semantic analysis object.
173354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor///
173454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// \param The template argument we are producing template argument
173554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// location information for.
173654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor///
173754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// \param NTTPType For a declaration template argument, the type of
173854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// the non-type template parameter that corresponds to this template
173954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// argument.
174054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor///
174154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// \param Loc The source location to use for the resulting template
174254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// argument.
1743dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic TemplateArgumentLoc
174454c53cca105ed595e12fecf04e415c3712bda936Douglas GregorgetTrivialTemplateArgumentLoc(Sema &S,
1745dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                              const TemplateArgument &Arg,
174654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                              QualType NTTPType,
174754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                              SourceLocation Loc) {
174854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  switch (Arg.getKind()) {
174954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  case TemplateArgument::Null:
175054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    llvm_unreachable("Can't get a NULL template argument here");
175154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    break;
1752dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
175354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  case TemplateArgument::Type:
1754dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return TemplateArgumentLoc(Arg,
175554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                     S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
1756dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
175754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  case TemplateArgument::Declaration: {
175854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    Expr *E
1759ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor      = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc)
176054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    .takeAs<Expr>();
176154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    return TemplateArgumentLoc(TemplateArgument(E), E);
176254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  }
1763dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
176454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  case TemplateArgument::Integral: {
176554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    Expr *E
1766ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor      = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).takeAs<Expr>();
176754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    return TemplateArgumentLoc(TemplateArgument(E), E);
176854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  }
1769dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1770b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor    case TemplateArgument::Template:
1771b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor    case TemplateArgument::TemplateExpansion: {
1772b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor      NestedNameSpecifierLocBuilder Builder;
1773b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor      TemplateName Template = Arg.getAsTemplate();
1774b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor      if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
1775b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor        Builder.MakeTrivial(S.Context, DTN->getQualifier(), Loc);
1776b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor      else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
1777b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor        Builder.MakeTrivial(S.Context, QTN->getQualifier(), Loc);
1778b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor
1779b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor      if (Arg.getKind() == TemplateArgument::Template)
1780b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor        return TemplateArgumentLoc(Arg,
1781b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor                                   Builder.getWithLocInContext(S.Context),
1782b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor                                   Loc);
1783b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor
1784b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor
1785b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor      return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(S.Context),
1786b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor                                 Loc, Loc);
1787b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor    }
1788a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
178954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  case TemplateArgument::Expression:
179054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    return TemplateArgumentLoc(Arg, Arg.getAsExpr());
1791dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
179254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  case TemplateArgument::Pack:
179354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo());
179454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  }
1795dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
179654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  return TemplateArgumentLoc();
179754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor}
179854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor
179954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor
180054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// \brief Convert the given deduced template argument and add it to the set of
180154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// fully-converted template arguments.
1802dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic bool ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param,
180354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                           DeducedTemplateArgument Arg,
1804dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                           NamedDecl *Template,
1805dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                           QualType NTTPType,
18066952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                           unsigned ArgumentPackIndex,
180754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                           TemplateDeductionInfo &Info,
180854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                           bool InFunctionTemplate,
180954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                             llvm::SmallVectorImpl<TemplateArgument> &Output) {
181054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  if (Arg.getKind() == TemplateArgument::Pack) {
181154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    // This is a template argument pack, so check each of its arguments against
181254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    // the template parameter.
181354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    llvm::SmallVector<TemplateArgument, 2> PackedArgsBuilder;
1814dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    for (TemplateArgument::pack_iterator PA = Arg.pack_begin(),
1815135ffa7375fb5802b92f42774e02d0e6e4c78e5bDouglas Gregor                                      PAEnd = Arg.pack_end();
181654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor         PA != PAEnd; ++PA) {
1817d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor      // When converting the deduced template argument, append it to the
1818d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor      // general output list. We need to do this so that the template argument
1819d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor      // checking logic has all of the prior template arguments available.
182054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor      DeducedTemplateArgument InnerArg(*PA);
182154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor      InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound());
1822dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      if (ConvertDeducedTemplateArgument(S, Param, InnerArg, Template,
18236952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                         NTTPType, PackedArgsBuilder.size(),
18246952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                         Info, InFunctionTemplate, Output))
182554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor        return true;
1826dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1827d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor      // Move the converted template argument into our argument pack.
1828d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor      PackedArgsBuilder.push_back(Output.back());
1829d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor      Output.pop_back();
183054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    }
1831dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
183254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    // Create the resulting argument pack.
1833dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    Output.push_back(TemplateArgument::CreatePackCopy(S.Context,
1834203e6a322ae29d577acafcb1572a57ec16e1e730Douglas Gregor                                                      PackedArgsBuilder.data(),
1835203e6a322ae29d577acafcb1572a57ec16e1e730Douglas Gregor                                                     PackedArgsBuilder.size()));
183654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    return false;
183754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  }
1838dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
183954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  // Convert the deduced template argument into a template
184054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  // argument that we can check, almost as if the user had written
184154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  // the template argument explicitly.
184254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  TemplateArgumentLoc ArgLoc = getTrivialTemplateArgumentLoc(S, Arg, NTTPType,
184354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                                             Info.getLocation());
1844dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
184554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  // Check the template argument, converting it as necessary.
184654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  return S.CheckTemplateArgument(Param, ArgLoc,
184754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                 Template,
184854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                 Template->getLocation(),
184954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                 Template->getSourceRange().getEnd(),
18506952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                 ArgumentPackIndex,
185154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                 Output,
185254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                 InFunctionTemplate
185354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                  ? (Arg.wasDeducedFromArrayBound()
1854dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                       ? Sema::CTAK_DeducedFromArrayBound
185554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                       : Sema::CTAK_Deduced)
185654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                 : Sema::CTAK_Specified);
185754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor}
185854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor
185931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// Complete template argument deduction for a class template partial
186031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// specialization.
186131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregorstatic Sema::TemplateDeductionResult
1862dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiFinishTemplateArgumentDeduction(Sema &S,
186331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                ClassTemplatePartialSpecializationDecl *Partial,
186431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                const TemplateArgumentList &TemplateArgs,
186531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                      llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
18662a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                                TemplateDeductionInfo &Info) {
186731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // Trap errors.
186831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  Sema::SFINAETrap Trap(S);
1869dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
187031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  Sema::ContextRAII SavedContext(S, Partial);
1871f5813826802c2e76cdc13cae834ebfd4518d74a6John McCall
187202cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // C++ [temp.deduct.type]p2:
187302cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  //   [...] or if any template argument remains neither deduced nor
187402cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  //   explicitly specified, template argument deduction fails.
1875910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor  llvm::SmallVector<TemplateArgument, 4> Builder;
1876033a3cad525e5b221ba035ffc3e0abfa1241d90eDouglas Gregor  TemplateParameterList *PartialParams = Partial->getTemplateParameters();
1877033a3cad525e5b221ba035ffc3e0abfa1241d90eDouglas Gregor  for (unsigned I = 0, N = PartialParams->size(); I != N; ++I) {
187854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    NamedDecl *Param = PartialParams->getParam(I);
1879f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    if (Deduced[I].isNull()) {
188054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor      Info.Param = makeTemplateParameter(Param);
188131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor      return Sema::TDK_Incomplete;
1882f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    }
1883dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
188454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    // We have deduced this argument, so it still needs to be
188554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    // checked and converted.
1886dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
188754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    // First, for a non-type template parameter type that is
188854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    // initialized by a declaration, we need the type of the
188954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    // corresponding non-type template parameter.
189054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    QualType NTTPType;
1891dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    if (NonTypeTemplateParmDecl *NTTP
1892d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor                                  = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
189354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor      NTTPType = NTTP->getType();
1894d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor      if (NTTPType->isDependentType()) {
1895dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
1896d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor                                          Builder.data(), Builder.size());
1897d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor        NTTPType = S.SubstType(NTTPType,
1898d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor                               MultiLevelTemplateArgumentList(TemplateArgs),
1899d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor                               NTTP->getLocation(),
1900d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor                               NTTP->getDeclName());
1901d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor        if (NTTPType.isNull()) {
1902d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor          Info.Param = makeTemplateParameter(Param);
1903d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor          // FIXME: These template arguments are temporary. Free them!
1904dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi          Info.reset(TemplateArgumentList::CreateCopy(S.Context,
1905dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                      Builder.data(),
1906d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor                                                      Builder.size()));
1907d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor          return Sema::TDK_SubstitutionFailure;
1908d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor        }
1909d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor      }
1910d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor    }
1911d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor
191254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    if (ConvertDeducedTemplateArgument(S, Param, Deduced[I],
19136952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                       Partial, NTTPType, 0, Info, false,
191454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                       Builder)) {
191554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor      Info.Param = makeTemplateParameter(Param);
191654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor      // FIXME: These template arguments are temporary. Free them!
1917dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
1918dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                  Builder.size()));
191954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor      return Sema::TDK_SubstitutionFailure;
192054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    }
192102cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  }
1922dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
192302cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // Form the template argument list from the deduced template arguments.
19241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentList *DeducedArgumentList
1925dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    = TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
1926910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                       Builder.size());
1927910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor
1928f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  Info.reset(DeducedArgumentList);
192902cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor
193002cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // Substitute the deduced template arguments into the template
193102cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // arguments of the class template partial specialization, and
193202cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // verify that the instantiated template arguments are both valid
193302cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // and are equivalent to the template arguments originally provided
19341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to the class template.
19352a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope InstScope(S);
193602cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
1937833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  const TemplateArgumentLoc *PartialTemplateArgs
1938833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    = Partial->getTemplateArgsAsWritten();
1939d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
1940d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  // Note that we don't provide the langle and rangle locations.
1941d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstArgs;
1942d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
1943e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor  if (S.Subst(PartialTemplateArgs,
1944e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor              Partial->getNumTemplateArgsAsWritten(),
1945e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor              InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
1946e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx;
1947e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    if (ParamIdx >= Partial->getTemplateParameters()->size())
1948e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      ParamIdx = Partial->getTemplateParameters()->size() - 1;
1949e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor
1950e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    Decl *Param
1951e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      = const_cast<NamedDecl *>(
1952e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                          Partial->getTemplateParameters()->getParam(ParamIdx));
1953e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    Info.Param = makeTemplateParameter(Param);
1954e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument();
1955e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    return Sema::TDK_SubstitutionFailure;
1956833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  }
1957833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1958910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor  llvm::SmallVector<TemplateArgument, 4> ConvertedInstArgs;
195931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
196054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                  InstArgs, false, ConvertedInstArgs))
196131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return Sema::TDK_SubstitutionFailure;
1962dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
196354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  TemplateParameterList *TemplateParams
196454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    = ClassTemplate->getTemplateParameters();
196554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) {
1966910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor    TemplateArgument InstArg = ConvertedInstArgs.data()[I];
196731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) {
19682fdc5e8199e1e239620f2faae88997153703e16fDouglas Gregor      Info.Param = makeTemplateParameter(TemplateParams->getParam(I));
1969f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Info.FirstArg = TemplateArgs[I];
1970f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Info.SecondArg = InstArg;
197131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor      return Sema::TDK_NonDeducedMismatch;
1972f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    }
197302cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  }
197402cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor
1975bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor  if (Trap.hasErrorOccurred())
197631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return Sema::TDK_SubstitutionFailure;
1977bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor
197831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  return Sema::TDK_Success;
197931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor}
198031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor
198131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// \brief Perform template argument deduction to determine whether
198231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// the given template arguments match the given class template
198331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// partial specialization per C++ [temp.class.spec.match].
198431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas GregorSema::TemplateDeductionResult
198531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas GregorSema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
198631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                              const TemplateArgumentList &TemplateArgs,
198731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                              TemplateDeductionInfo &Info) {
198831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // C++ [temp.class.spec.match]p2:
198931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  //   A partial specialization matches a given actual template
199031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  //   argument list if the template arguments of the partial
199131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  //   specialization can be deduced from the actual template argument
199231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  //   list (14.8.2).
199331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  SFINAETrap Trap(*this);
199431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
199531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  Deduced.resize(Partial->getTemplateParameters()->size());
199631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  if (TemplateDeductionResult Result
199731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor        = ::DeduceTemplateArguments(*this,
199831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                    Partial->getTemplateParameters(),
199931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                    Partial->getTemplateArgs(),
200031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                    TemplateArgs, Info, Deduced))
200131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return Result;
200231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor
200331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
20049b623639378d53a675921ddfa7316034d571881eDouglas Gregor                             Deduced.data(), Deduced.size(), Info);
200531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  if (Inst)
200631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return TDK_InstantiationDepth;
200731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor
200831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  if (Trap.hasErrorOccurred())
200931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return Sema::TDK_SubstitutionFailure;
2010dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2011dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs,
201231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                           Deduced, Info);
20130b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
2014031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
20154112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor/// \brief Determine whether the given type T is a simple-template-id type.
20164112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregorstatic bool isSimpleTemplateIdType(QualType T) {
20171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const TemplateSpecializationType *Spec
2018183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall        = T->getAs<TemplateSpecializationType>())
20194112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    return Spec->getTemplateName().getAsTemplateDecl() != 0;
20201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20214112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor  return false;
20224112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor}
202383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
202483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \brief Substitute the explicitly-provided template arguments into the
202583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// given function template according to C++ [temp.arg.explicit].
202683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
202783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionTemplate the function template into which the explicit
202883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template arguments will be substituted.
202983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
20301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ExplicitTemplateArguments the explicitly-specified template
203183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// arguments.
203283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
20331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param Deduced the deduced template arguments, which will be populated
203483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// with the converted and checked explicit template arguments.
203583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
20361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ParamTypes will be populated with the instantiated function
203783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// parameters.
203883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
203983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionType if non-NULL, the result type of the function template
204083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// will also be instantiated and the pointed-to value will be updated with
204183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// the instantiated function type.
204283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
204383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Info if substitution fails for any reason, this object will be
204483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// populated with more information about the failure.
204583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
204683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \returns TDK_Success if substitution was successful, or some failure
204783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// condition.
204883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::TemplateDeductionResult
204983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::SubstituteExplicitTemplateArguments(
205083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      FunctionTemplateDecl *FunctionTemplate,
205167714230a191bc3c01f33378f34f34ef377991a6Douglas Gregor                               TemplateArgumentListInfo &ExplicitTemplateArgs,
205202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                       llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
205383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                 llvm::SmallVectorImpl<QualType> &ParamTypes,
205483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          QualType *FunctionType,
205583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          TemplateDeductionInfo &Info) {
205683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
205783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateParameterList *TemplateParams
205883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = FunctionTemplate->getTemplateParameters();
205983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
2060d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (ExplicitTemplateArgs.size() == 0) {
206183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    // No arguments to substitute; just copy over the parameter types and
206283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    // fill in the function type.
206383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    for (FunctionDecl::param_iterator P = Function->param_begin(),
206483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                   PEnd = Function->param_end();
206583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor         P != PEnd;
206683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor         ++P)
206783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      ParamTypes.push_back((*P)->getType());
20681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
206983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (FunctionType)
207083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      *FunctionType = Function->getType();
207183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_Success;
207283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
20731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
207483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Substitution of the explicit template arguments into a function template
207583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  /// is a SFINAE context. Trap any errors that might occur.
20761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SFINAETrap Trap(*this);
20771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
207883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // C++ [temp.arg.explicit]p3:
20791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   Template arguments that are present shall be specified in the
20801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   declaration order of their corresponding template-parameters. The
208183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   template argument list shall not specify more template-arguments than
20821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   there are corresponding template-parameters.
2083910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor  llvm::SmallVector<TemplateArgument, 4> Builder;
20841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
20851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Enter a new template instantiation context where we check the
208683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // explicitly-specified template arguments against this function template,
208783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // and then substitute them into the function parameter types.
20881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
208983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                             FunctionTemplate, Deduced.data(), Deduced.size(),
20909b623639378d53a675921ddfa7316034d571881eDouglas Gregor           ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution,
20919b623639378d53a675921ddfa7316034d571881eDouglas Gregor                             Info);
209283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (Inst)
209383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_InstantiationDepth;
20941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
209583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (CheckTemplateArgumentList(FunctionTemplate,
209683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                SourceLocation(),
2097d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                ExplicitTemplateArgs,
209883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                true,
2099f1a8445036a2d047c7165d4170e3058cdeaba6ebDouglas Gregor                                Builder) || Trap.hasErrorOccurred()) {
2100910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor    unsigned Index = Builder.size();
2101fe52c91dcd1ecda90b579f789baf70f7a992e3c9Douglas Gregor    if (Index >= TemplateParams->size())
2102fe52c91dcd1ecda90b579f789baf70f7a992e3c9Douglas Gregor      Index = TemplateParams->size() - 1;
2103fe52c91dcd1ecda90b579f789baf70f7a992e3c9Douglas Gregor    Info.Param = makeTemplateParameter(TemplateParams->getParam(Index));
210483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_InvalidExplicitArguments;
2105f1a8445036a2d047c7165d4170e3058cdeaba6ebDouglas Gregor  }
21061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
210783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Form the template argument list from the explicitly-specified
210883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // template arguments.
21091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentList *ExplicitArgumentList
2110910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor    = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size());
211183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Info.reset(ExplicitArgumentList);
2112dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2113df41f18936693f7c62e457eefb9fad5b2d2fe3cdJohn McCall  // Template argument deduction and the final substitution should be
2114df41f18936693f7c62e457eefb9fad5b2d2fe3cdJohn McCall  // done in the context of the templated declaration.  Explicit
2115df41f18936693f7c62e457eefb9fad5b2d2fe3cdJohn McCall  // argument substitution, on the other hand, needs to happen in the
2116df41f18936693f7c62e457eefb9fad5b2d2fe3cdJohn McCall  // calling context.
2117df41f18936693f7c62e457eefb9fad5b2d2fe3cdJohn McCall  ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
2118df41f18936693f7c62e457eefb9fad5b2d2fe3cdJohn McCall
2119dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // If we deduced template arguments for a template parameter pack,
2120d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  // note that the template argument pack is partially substituted and record
2121d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  // the explicit template arguments. They'll be used as part of deduction
2122d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  // for this template parameter pack.
2123d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  for (unsigned I = 0, N = Builder.size(); I != N; ++I) {
2124d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    const TemplateArgument &Arg = Builder[I];
2125d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    if (Arg.getKind() == TemplateArgument::Pack) {
2126d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      CurrentInstantiationScope->SetPartiallySubstitutedPack(
2127dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                 TemplateParams->getParam(I),
2128d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                                             Arg.pack_begin(),
2129d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                                             Arg.pack_size());
2130d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      break;
2131d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    }
2132d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  }
2133d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor
213483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Instantiate the types of each of the function parameters given the
213583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // explicitly-specified template arguments.
2136dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  if (SubstParmTypes(Function->getLocation(),
2137a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor                     Function->param_begin(), Function->getNumParams(),
2138a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor                     MultiLevelTemplateArgumentList(*ExplicitArgumentList),
2139a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor                     ParamTypes))
2140a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor    return TDK_SubstitutionFailure;
214183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
214283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // If the caller wants a full function type back, instantiate the return
214383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // type and form that function type.
214483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (FunctionType) {
214583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    // FIXME: exception-specifications?
21461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const FunctionProtoType *Proto
2147183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall      = Function->getType()->getAs<FunctionProtoType>();
214883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    assert(Proto && "Function template does not have a prototype?");
21491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    QualType ResultType
2151357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor      = SubstType(Proto->getResultType(),
2152357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  MultiLevelTemplateArgumentList(*ExplicitArgumentList),
2153357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  Function->getTypeSpecStartLoc(),
2154357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  Function->getDeclName());
215583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (ResultType.isNull() || Trap.hasErrorOccurred())
215683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_SubstitutionFailure;
21571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    *FunctionType = BuildFunctionType(ResultType,
215983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      ParamTypes.data(), ParamTypes.size(),
216083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      Proto->isVariadic(),
216183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      Proto->getTypeQuals(),
2162c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                                      Proto->getRefQualifier(),
216383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      Function->getLocation(),
2164fa869547eb1cab12d7e0c0dfa8ba594e336b9b32Eli Friedman                                      Function->getDeclName(),
2165fa869547eb1cab12d7e0c0dfa8ba594e336b9b32Eli Friedman                                      Proto->getExtInfo());
216683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (FunctionType->isNull() || Trap.hasErrorOccurred())
216783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_SubstitutionFailure;
216883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
21691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
217083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // C++ [temp.arg.explicit]p2:
21711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   Trailing template arguments that can be deduced (14.8.2) may be
21721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   omitted from the list of explicit template-arguments. If all of the
217383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   template arguments can be deduced, they may all be omitted; in this
217483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   case, the empty template argument list <> itself may also be omitted.
217583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //
2176d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  // Take all of the explicitly-specified arguments and put them into
2177d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  // the set of deduced template arguments. Explicitly-specified
2178d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  // parameter packs, however, will be set to NULL since the deduction
2179d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  // mechanisms handle explicitly-specified argument packs directly.
218083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Deduced.reserve(TemplateParams->size());
2181d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) {
2182d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    const TemplateArgument &Arg = ExplicitArgumentList->get(I);
2183d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    if (Arg.getKind() == TemplateArgument::Pack)
2184d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      Deduced.push_back(DeducedTemplateArgument());
2185d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    else
2186d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      Deduced.push_back(Arg);
2187d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  }
21881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
218983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  return TDK_Success;
219083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor}
219183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
21921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Finish template argument deduction for a function template,
219383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// checking the deduced template arguments for completeness and forming
219483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// the function template specialization.
21951eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSema::TemplateDeductionResult
219683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
219702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                       llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
219802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                      unsigned NumExplicitlySpecified,
219983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      FunctionDecl *&Specialization,
220083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      TemplateDeductionInfo &Info) {
220183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateParameterList *TemplateParams
220283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = FunctionTemplate->getTemplateParameters();
22031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
220451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // Template argument deduction for function templates in a SFINAE context.
220551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // Trap any errors that might occur.
220651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  SFINAETrap Trap(*this);
220751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
220851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // Enter a new template instantiation context while we instantiate the
220951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // actual function declaration.
221051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
221151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                             FunctionTemplate, Deduced.data(), Deduced.size(),
22129b623639378d53a675921ddfa7316034d571881eDouglas Gregor              ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution,
22139b623639378d53a675921ddfa7316034d571881eDouglas Gregor                             Info);
221451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  if (Inst)
221551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    return TDK_InstantiationDepth;
221651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
221796db310ab7ca59e1890ddef25a3701bc2909d20fJohn McCall  ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
2218f5813826802c2e76cdc13cae834ebfd4518d74a6John McCall
221983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // C++ [temp.deduct.type]p2:
222083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   [...] or if any template argument remains neither deduced nor
222183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   explicitly specified, template argument deduction fails.
2222910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor  llvm::SmallVector<TemplateArgument, 4> Builder;
2223b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor  for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
2224b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor    NamedDecl *Param = TemplateParams->getParam(I);
2225dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
222651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    if (!Deduced[I].isNull()) {
22273273b0cea879c7af345d6bf98502bbf73fc4fde1Douglas Gregor      if (I < NumExplicitlySpecified) {
222802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        // We have already fully type-checked and converted this
2229dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        // argument, because it was explicitly-specified. Just record the
22303273b0cea879c7af345d6bf98502bbf73fc4fde1Douglas Gregor        // presence of this argument.
2231910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor        Builder.push_back(Deduced[I]);
223202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        continue;
223302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      }
223402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
223502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // We have deduced this argument, so it still needs to be
223602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // checked and converted.
223702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
223802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // First, for a non-type template parameter type that is
223902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // initialized by a declaration, we need the type of the
224002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // corresponding non-type template parameter.
224102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      QualType NTTPType;
2242dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      if (NonTypeTemplateParmDecl *NTTP
2243dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2244b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor        NTTPType = NTTP->getType();
2245b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor        if (NTTPType->isDependentType()) {
2246dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi          TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
2247b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor                                            Builder.data(), Builder.size());
2248b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor          NTTPType = SubstType(NTTPType,
2249b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor                               MultiLevelTemplateArgumentList(TemplateArgs),
2250b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor                               NTTP->getLocation(),
2251b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor                               NTTP->getDeclName());
2252b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor          if (NTTPType.isNull()) {
2253b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor            Info.Param = makeTemplateParameter(Param);
2254b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor            // FIXME: These template arguments are temporary. Free them!
2255dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi            Info.reset(TemplateArgumentList::CreateCopy(Context,
2256dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                        Builder.data(),
2257b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor                                                        Builder.size()));
2258b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor            return TDK_SubstitutionFailure;
225902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor          }
226002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        }
226102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      }
226202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
2263b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor      if (ConvertDeducedTemplateArgument(*this, Param, Deduced[I],
22646952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                         FunctionTemplate, NTTPType, 0, Info,
226554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                         true, Builder)) {
2266b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor        Info.Param = makeTemplateParameter(Param);
2267910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor        // FIXME: These template arguments are temporary. Free them!
2268dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
2269dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                    Builder.size()));
227002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        return TDK_SubstitutionFailure;
227102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      }
227202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
227351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      continue;
227451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    }
2275dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2276ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor    // C++0x [temp.arg.explicit]p3:
2277dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //    A trailing template parameter pack (14.5.3) not otherwise deduced will
2278ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor    //    be deduced to an empty sequence of template arguments.
2279ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor    // FIXME: Where did the word "trailing" come from?
2280ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor    if (Param->isTemplateParameterPack()) {
2281d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      // We may have had explicitly-specified template arguments for this
2282d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      // template parameter pack. If so, our empty deduction extends the
2283d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      // explicitly-specified set (C++0x [temp.arg.explicit]p9).
2284d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      const TemplateArgument *ExplicitArgs;
2285d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      unsigned NumExplicitArgs;
2286d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      if (CurrentInstantiationScope->getPartiallySubstitutedPack(&ExplicitArgs,
2287d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                                             &NumExplicitArgs)
2288d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor          == Param)
2289d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor        Builder.push_back(TemplateArgument(ExplicitArgs, NumExplicitArgs));
2290dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      else
2291d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor        Builder.push_back(TemplateArgument(0, 0));
2292dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2293ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor      continue;
2294ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor    }
229551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
2296dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    // Substitute into the default template argument, if available.
229751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    TemplateArgumentLoc DefArg
229851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
229951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                              FunctionTemplate->getLocation(),
230051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                  FunctionTemplate->getSourceRange().getEnd(),
230151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                                Param,
230251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                                Builder);
230351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
230451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // If there was no default argument, deduction is incomplete.
230551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    if (DefArg.getArgument().isNull()) {
230683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      Info.Param = makeTemplateParameter(
230751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                         const_cast<NamedDecl *>(TemplateParams->getParam(I)));
230883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_Incomplete;
230983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    }
2310dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
231151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // Check whether we can actually use the default argument.
231251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    if (CheckTemplateArgument(Param, DefArg,
231351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                              FunctionTemplate,
231451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                              FunctionTemplate->getLocation(),
231551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                              FunctionTemplate->getSourceRange().getEnd(),
23166952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                              0, Builder,
23178735b294a257a07ca158c28094d7324f0adf889aDouglas Gregor                              CTAK_Specified)) {
231851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      Info.Param = makeTemplateParameter(
231951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                         const_cast<NamedDecl *>(TemplateParams->getParam(I)));
2320910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor      // FIXME: These template arguments are temporary. Free them!
2321dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
2322910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                  Builder.size()));
232351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      return TDK_SubstitutionFailure;
232451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    }
23251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
232651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // If we get here, we successfully used the default template argument.
232783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
23281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
232983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Form the template argument list from the deduced template arguments.
23301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentList *DeducedArgumentList
2331910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor    = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size());
233283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Info.reset(DeducedArgumentList);
23331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Substitute the deduced template arguments into the function template
233583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // declaration to produce the function template specialization.
2336d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor  DeclContext *Owner = FunctionTemplate->getDeclContext();
2337d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor  if (FunctionTemplate->getFriendObjectKind())
2338d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor    Owner = FunctionTemplate->getLexicalDeclContext();
233983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Specialization = cast_or_null<FunctionDecl>(
2340d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor                      SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner,
2341357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                         MultiLevelTemplateArgumentList(*DeducedArgumentList)));
234283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (!Specialization)
234383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_SubstitutionFailure;
23441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2345dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
2346f882574cf640d5c8355965e1c486f9d8d8ffcf47Douglas Gregor         FunctionTemplate->getCanonicalDecl());
2347dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
23481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If the template argument list is owned by the function template
234983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // specialization, release it.
2350ec20f46740a59758b12c22108002395bcf5b6f9bDouglas Gregor  if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList &&
2351ec20f46740a59758b12c22108002395bcf5b6f9bDouglas Gregor      !Trap.hasErrorOccurred())
235283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    Info.take();
23531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
235483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // There may have been an error that did not prevent us from constructing a
235583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // declaration. Mark the declaration invalid and return with a substitution
235683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // failure.
235783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (Trap.hasErrorOccurred()) {
235883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    Specialization->setInvalidDecl(true);
235983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_SubstitutionFailure;
236083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
23611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
23629b623639378d53a675921ddfa7316034d571881eDouglas Gregor  // If we suppressed any diagnostics while performing template argument
23639b623639378d53a675921ddfa7316034d571881eDouglas Gregor  // deduction, and if we haven't already instantiated this declaration,
23649b623639378d53a675921ddfa7316034d571881eDouglas Gregor  // keep track of these diagnostics. They'll be emitted if this specialization
23659b623639378d53a675921ddfa7316034d571881eDouglas Gregor  // is actually used.
23669b623639378d53a675921ddfa7316034d571881eDouglas Gregor  if (Info.diag_begin() != Info.diag_end()) {
23679b623639378d53a675921ddfa7316034d571881eDouglas Gregor    llvm::DenseMap<Decl *, llvm::SmallVector<PartialDiagnosticAt, 1> >::iterator
23689b623639378d53a675921ddfa7316034d571881eDouglas Gregor      Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl());
23699b623639378d53a675921ddfa7316034d571881eDouglas Gregor    if (Pos == SuppressedDiagnostics.end())
23709b623639378d53a675921ddfa7316034d571881eDouglas Gregor        SuppressedDiagnostics[Specialization->getCanonicalDecl()]
23719b623639378d53a675921ddfa7316034d571881eDouglas Gregor          .append(Info.diag_begin(), Info.diag_end());
2372dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  }
23739b623639378d53a675921ddfa7316034d571881eDouglas Gregor
23741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return TDK_Success;
237583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor}
237683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
23779c72c6088d591ace8503b842d39448c2040f3033John McCall/// Gets the type of a function for template-argument-deducton
23789c72c6088d591ace8503b842d39448c2040f3033John McCall/// purposes when it's considered as part of an overload set.
2379eff92135d32039c9874dc356f3e93143af6069c1John McCallstatic QualType GetTypeOfFunction(ASTContext &Context,
23809c72c6088d591ace8503b842d39448c2040f3033John McCall                                  const OverloadExpr::FindResult &R,
2381eff92135d32039c9874dc356f3e93143af6069c1John McCall                                  FunctionDecl *Fn) {
2382eff92135d32039c9874dc356f3e93143af6069c1John McCall  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
23839c72c6088d591ace8503b842d39448c2040f3033John McCall    if (Method->isInstance()) {
23849c72c6088d591ace8503b842d39448c2040f3033John McCall      // An instance method that's referenced in a form that doesn't
23859c72c6088d591ace8503b842d39448c2040f3033John McCall      // look like a member pointer is just invalid.
23869c72c6088d591ace8503b842d39448c2040f3033John McCall      if (!R.HasFormOfMemberPointer) return QualType();
23879c72c6088d591ace8503b842d39448c2040f3033John McCall
2388eff92135d32039c9874dc356f3e93143af6069c1John McCall      return Context.getMemberPointerType(Fn->getType(),
2389eff92135d32039c9874dc356f3e93143af6069c1John McCall               Context.getTypeDeclType(Method->getParent()).getTypePtr());
23909c72c6088d591ace8503b842d39448c2040f3033John McCall    }
23919c72c6088d591ace8503b842d39448c2040f3033John McCall
23929c72c6088d591ace8503b842d39448c2040f3033John McCall  if (!R.IsAddressOfOperand) return Fn->getType();
2393eff92135d32039c9874dc356f3e93143af6069c1John McCall  return Context.getPointerType(Fn->getType());
2394eff92135d32039c9874dc356f3e93143af6069c1John McCall}
2395eff92135d32039c9874dc356f3e93143af6069c1John McCall
2396eff92135d32039c9874dc356f3e93143af6069c1John McCall/// Apply the deduction rules for overload sets.
2397eff92135d32039c9874dc356f3e93143af6069c1John McCall///
2398eff92135d32039c9874dc356f3e93143af6069c1John McCall/// \return the null type if this argument should be treated as an
2399eff92135d32039c9874dc356f3e93143af6069c1John McCall/// undeduced context
2400eff92135d32039c9874dc356f3e93143af6069c1John McCallstatic QualType
2401eff92135d32039c9874dc356f3e93143af6069c1John McCallResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
240275f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor                            Expr *Arg, QualType ParamType,
240375f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor                            bool ParamWasReference) {
2404dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
24059c72c6088d591ace8503b842d39448c2040f3033John McCall  OverloadExpr::FindResult R = OverloadExpr::find(Arg);
2406eff92135d32039c9874dc356f3e93143af6069c1John McCall
24079c72c6088d591ace8503b842d39448c2040f3033John McCall  OverloadExpr *Ovl = R.Expression;
2408eff92135d32039c9874dc356f3e93143af6069c1John McCall
240975f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor  // C++0x [temp.deduct.call]p4
241075f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor  unsigned TDF = 0;
241175f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor  if (ParamWasReference)
241275f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor    TDF |= TDF_ParamWithReferenceType;
241375f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor  if (R.IsAddressOfOperand)
241475f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor    TDF |= TDF_IgnoreQualifiers;
241575f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor
2416eff92135d32039c9874dc356f3e93143af6069c1John McCall  // If there were explicit template arguments, we can only find
2417eff92135d32039c9874dc356f3e93143af6069c1John McCall  // something via C++ [temp.arg.explicit]p3, i.e. if the arguments
2418eff92135d32039c9874dc356f3e93143af6069c1John McCall  // unambiguously name a full specialization.
24197bb12da2b0749eeebb21854c77877736969e59f2John McCall  if (Ovl->hasExplicitTemplateArgs()) {
2420eff92135d32039c9874dc356f3e93143af6069c1John McCall    // But we can still look for an explicit specialization.
2421eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (FunctionDecl *ExplicitSpec
24227bb12da2b0749eeebb21854c77877736969e59f2John McCall          = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
24239c72c6088d591ace8503b842d39448c2040f3033John McCall      return GetTypeOfFunction(S.Context, R, ExplicitSpec);
2424eff92135d32039c9874dc356f3e93143af6069c1John McCall    return QualType();
2425eff92135d32039c9874dc356f3e93143af6069c1John McCall  }
2426eff92135d32039c9874dc356f3e93143af6069c1John McCall
2427eff92135d32039c9874dc356f3e93143af6069c1John McCall  // C++0x [temp.deduct.call]p6:
2428eff92135d32039c9874dc356f3e93143af6069c1John McCall  //   When P is a function type, pointer to function type, or pointer
2429eff92135d32039c9874dc356f3e93143af6069c1John McCall  //   to member function type:
2430eff92135d32039c9874dc356f3e93143af6069c1John McCall
2431eff92135d32039c9874dc356f3e93143af6069c1John McCall  if (!ParamType->isFunctionType() &&
2432eff92135d32039c9874dc356f3e93143af6069c1John McCall      !ParamType->isFunctionPointerType() &&
2433eff92135d32039c9874dc356f3e93143af6069c1John McCall      !ParamType->isMemberFunctionPointerType())
2434eff92135d32039c9874dc356f3e93143af6069c1John McCall    return QualType();
2435eff92135d32039c9874dc356f3e93143af6069c1John McCall
2436eff92135d32039c9874dc356f3e93143af6069c1John McCall  QualType Match;
24377bb12da2b0749eeebb21854c77877736969e59f2John McCall  for (UnresolvedSetIterator I = Ovl->decls_begin(),
24387bb12da2b0749eeebb21854c77877736969e59f2John McCall         E = Ovl->decls_end(); I != E; ++I) {
2439eff92135d32039c9874dc356f3e93143af6069c1John McCall    NamedDecl *D = (*I)->getUnderlyingDecl();
2440eff92135d32039c9874dc356f3e93143af6069c1John McCall
2441eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   - If the argument is an overload set containing one or more
2442eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     function templates, the parameter is treated as a
2443eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     non-deduced context.
2444eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (isa<FunctionTemplateDecl>(D))
2445eff92135d32039c9874dc356f3e93143af6069c1John McCall      return QualType();
2446eff92135d32039c9874dc356f3e93143af6069c1John McCall
2447eff92135d32039c9874dc356f3e93143af6069c1John McCall    FunctionDecl *Fn = cast<FunctionDecl>(D);
24489c72c6088d591ace8503b842d39448c2040f3033John McCall    QualType ArgType = GetTypeOfFunction(S.Context, R, Fn);
24499c72c6088d591ace8503b842d39448c2040f3033John McCall    if (ArgType.isNull()) continue;
2450eff92135d32039c9874dc356f3e93143af6069c1John McCall
245175f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor    // Function-to-pointer conversion.
2452dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    if (!ParamWasReference && ParamType->isPointerType() &&
245375f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor        ArgType->isFunctionType())
245475f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor      ArgType = S.Context.getPointerType(ArgType);
2455dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2456eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   - If the argument is an overload set (not containing function
2457eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     templates), trial argument deduction is attempted using each
2458eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     of the members of the set. If deduction succeeds for only one
2459eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     of the overload set members, that member is used as the
2460eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     argument value for the deduction. If deduction succeeds for
2461eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     more than one member of the overload set the parameter is
2462eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     treated as a non-deduced context.
2463eff92135d32039c9874dc356f3e93143af6069c1John McCall
2464eff92135d32039c9874dc356f3e93143af6069c1John McCall    // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
2465eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   Type deduction is done independently for each P/A pair, and
2466eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   the deduced template argument values are then combined.
2467eff92135d32039c9874dc356f3e93143af6069c1John McCall    // So we do not reject deductions which were made elsewhere.
2468dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    llvm::SmallVector<DeducedTemplateArgument, 8>
246902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      Deduced(TemplateParams->size());
24702a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    TemplateDeductionInfo Info(S.Context, Ovl->getNameLoc());
2471eff92135d32039c9874dc356f3e93143af6069c1John McCall    Sema::TemplateDeductionResult Result
2472a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      = DeduceTemplateArguments(S, TemplateParams,
2473eff92135d32039c9874dc356f3e93143af6069c1John McCall                                ParamType, ArgType,
2474eff92135d32039c9874dc356f3e93143af6069c1John McCall                                Info, Deduced, TDF);
2475eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (Result) continue;
2476eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (!Match.isNull()) return QualType();
2477eff92135d32039c9874dc356f3e93143af6069c1John McCall    Match = ArgType;
2478eff92135d32039c9874dc356f3e93143af6069c1John McCall  }
2479eff92135d32039c9874dc356f3e93143af6069c1John McCall
2480eff92135d32039c9874dc356f3e93143af6069c1John McCall  return Match;
2481eff92135d32039c9874dc356f3e93143af6069c1John McCall}
2482eff92135d32039c9874dc356f3e93143af6069c1John McCall
2483dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \brief Perform the adjustments to the parameter and argument types
2484f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor/// described in C++ [temp.deduct.call].
2485f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor///
2486f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor/// \returns true if the caller should not attempt to perform any template
2487f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor/// argument deduction based on this P/A pair.
2488f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregorstatic bool AdjustFunctionParmAndArgTypesForDeduction(Sema &S,
2489f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                          TemplateParameterList *TemplateParams,
2490f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                                      QualType &ParamType,
2491f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                                      QualType &ArgType,
2492f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                                      Expr *Arg,
2493f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                                      unsigned &TDF) {
2494f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // C++0x [temp.deduct.call]p3:
24950099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi  //   If P is a cv-qualified type, the top level cv-qualifiers of P's type
2496f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //   are ignored for type deduction.
2497a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor  if (ParamType.hasQualifiers())
2498a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor    ParamType = ParamType.getUnqualifiedType();
2499f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>();
2500f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (ParamRefType) {
250134b41d939a1328f484511c6002ba2456db879a29Richard Smith    QualType PointeeType = ParamRefType->getPointeeType();
250234b41d939a1328f484511c6002ba2456db879a29Richard Smith
25032ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor    //   [C++0x] If P is an rvalue reference to a cv-unqualified
25042ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor    //   template parameter and the argument is an lvalue, the type
25052ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor    //   "lvalue reference to A" is used in place of A for type
25062ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor    //   deduction.
250734b41d939a1328f484511c6002ba2456db879a29Richard Smith    if (isa<RValueReferenceType>(ParamType)) {
250834b41d939a1328f484511c6002ba2456db879a29Richard Smith      if (!PointeeType.getQualifiers() &&
250934b41d939a1328f484511c6002ba2456db879a29Richard Smith          isa<TemplateTypeParmType>(PointeeType) &&
25109625e44c0252485277a340746ed8ac950686156fDouglas Gregor          Arg->Classify(S.Context).isLValue() &&
25119625e44c0252485277a340746ed8ac950686156fDouglas Gregor          Arg->getType() != S.Context.OverloadTy &&
25129625e44c0252485277a340746ed8ac950686156fDouglas Gregor          Arg->getType() != S.Context.BoundMemberTy)
25132ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor        ArgType = S.Context.getLValueReferenceType(ArgType);
25142ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor    }
25152ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor
2516f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //   [...] If P is a reference type, the type referred to by P is used
2517f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //   for type deduction.
251834b41d939a1328f484511c6002ba2456db879a29Richard Smith    ParamType = PointeeType;
2519f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  }
2520dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2521f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // Overload sets usually make this parameter an undeduced
2522f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // context, but there are sometimes special circumstances.
2523f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (ArgType == S.Context.OverloadTy) {
2524f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    ArgType = ResolveOverloadForDeduction(S, TemplateParams,
2525f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                          Arg, ParamType,
2526f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                          ParamRefType != 0);
2527f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    if (ArgType.isNull())
2528f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      return true;
2529f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  }
2530dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2531f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (ParamRefType) {
2532f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    // C++0x [temp.deduct.call]p3:
2533f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //   [...] If P is of the form T&&, where T is a template parameter, and
2534f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //   the argument is an lvalue, the type A& is used in place of A for
2535f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //   type deduction.
2536f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    if (ParamRefType->isRValueReferenceType() &&
2537f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        ParamRefType->getAs<TemplateTypeParmType>() &&
2538f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        Arg->isLValue())
2539f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      ArgType = S.Context.getLValueReferenceType(ArgType);
2540f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  } else {
2541f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    // C++ [temp.deduct.call]p2:
2542f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //   If P is not a reference type:
2543f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //   - If A is an array type, the pointer type produced by the
2544f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //     array-to-pointer standard conversion (4.2) is used in place of
2545f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //     A for type deduction; otherwise,
2546f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    if (ArgType->isArrayType())
2547f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      ArgType = S.Context.getArrayDecayedType(ArgType);
2548f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //   - If A is a function type, the pointer type produced by the
2549f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //     function-to-pointer standard conversion (4.3) is used in place
2550f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //     of A for type deduction; otherwise,
2551f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    else if (ArgType->isFunctionType())
2552f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      ArgType = S.Context.getPointerType(ArgType);
2553f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    else {
25540099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi      // - If A is a cv-qualified type, the top level cv-qualifiers of A's
2555f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      //   type are ignored for type deduction.
2556a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor      ArgType = ArgType.getUnqualifiedType();
2557f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    }
2558f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  }
2559dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2560f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // C++0x [temp.deduct.call]p4:
2561f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //   In general, the deduction process attempts to find template argument
2562f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //   values that will make the deduced A identical to A (after the type A
2563f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //   is transformed as described above). [...]
2564f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  TDF = TDF_SkipNonDependent;
2565dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2566f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //     - If the original P is a reference type, the deduced A (i.e., the
2567f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //       type referred to by the reference) can be more cv-qualified than
2568f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //       the transformed A.
2569f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (ParamRefType)
2570f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    TDF |= TDF_ParamWithReferenceType;
2571f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //     - The transformed A can be another pointer or pointer to member
2572f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //       type that can be converted to the deduced A via a qualification
2573f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //       conversion (4.4).
2574f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (ArgType->isPointerType() || ArgType->isMemberPointerType() ||
2575f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      ArgType->isObjCObjectPointerType())
2576f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    TDF |= TDF_IgnoreQualifiers;
2577f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //     - If P is a class and P has the form simple-template-id, then the
2578f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //       transformed A can be a derived class of the deduced A. Likewise,
2579f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //       if P is a pointer to a class of the form simple-template-id, the
2580f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //       transformed A can be a pointer to a derived class pointed to by
2581f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //       the deduced A.
2582f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (isSimpleTemplateIdType(ParamType) ||
2583f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      (isa<PointerType>(ParamType) &&
2584f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor       isSimpleTemplateIdType(
2585f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                              ParamType->getAs<PointerType>()->getPointeeType())))
2586f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    TDF |= TDF_DerivedClass;
2587dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2588f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  return false;
2589f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor}
2590f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor
2591e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \brief Perform template argument deduction from a function call
2592e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// (C++ [temp.deduct.call]).
2593e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
2594e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param FunctionTemplate the function template for which we are performing
2595e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// template argument deduction.
2596e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
259748026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// \param ExplicitTemplateArguments the explicit template arguments provided
259848026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// for this call.
25996db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor///
2600e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Args the function call arguments
2601e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
2602e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param NumArgs the number of arguments in Args
2603e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
260448026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// \param Name the name of the function being called. This is only significant
260548026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// when the function template is a conversion function template, in which
260648026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// case this routine will also perform template argument deduction based on
2607dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// the function to which
260848026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor///
2609e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Specialization if template argument deduction was successful,
26101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// this will be set to the function template specialization produced by
2611e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// template argument deduction.
2612e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
2613e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Info the argument will be updated to provide additional information
2614e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// about template argument deduction.
2615e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
2616e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns the result of template argument deduction.
2617e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorSema::TemplateDeductionResult
2618e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
261967714230a191bc3c01f33378f34f34ef377991a6Douglas Gregor                              TemplateArgumentListInfo *ExplicitTemplateArgs,
2620e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                              Expr **Args, unsigned NumArgs,
2621e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                              FunctionDecl *&Specialization,
2622e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                              TemplateDeductionInfo &Info) {
2623e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
26246db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor
2625e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // C++ [temp.deduct.call]p1:
2626e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  //   Template argument deduction is done by comparing each function template
2627e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  //   parameter type (call it P) with the type of the corresponding argument
2628e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  //   of the call (call it A) as described below.
2629e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  unsigned CheckArgs = NumArgs;
26306db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  if (NumArgs < Function->getMinRequiredArguments())
2631e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return TDK_TooFewArguments;
2632e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  else if (NumArgs > Function->getNumParams()) {
26331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const FunctionProtoType *Proto
2634183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall      = Function->getType()->getAs<FunctionProtoType>();
2635f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    if (Proto->isTemplateVariadic())
2636f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      /* Do nothing */;
2637f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    else if (Proto->isVariadic())
2638f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      CheckArgs = Function->getNumParams();
2639dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    else
2640e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return TDK_TooManyArguments;
2641e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  }
26421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26436db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  // The types of the parameters from which we will perform template argument
26446db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  // deduction.
26452a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope InstScope(*this);
2646e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  TemplateParameterList *TemplateParams
2647e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    = FunctionTemplate->getTemplateParameters();
264802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
26496db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  llvm::SmallVector<QualType, 4> ParamTypes;
265002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  unsigned NumExplicitlySpecified = 0;
2651d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (ExplicitTemplateArgs) {
265283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    TemplateDeductionResult Result =
265383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      SubstituteExplicitTemplateArguments(FunctionTemplate,
2654d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                          *ExplicitTemplateArgs,
265583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          Deduced,
265683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          ParamTypes,
265783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          0,
265883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          Info);
265983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (Result)
266083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return Result;
266102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
266202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    NumExplicitlySpecified = Deduced.size();
26636db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  } else {
26646db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor    // Just fill in the parameter types from the function declaration.
2665f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
26666db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor      ParamTypes.push_back(Function->getParamDecl(I)->getType());
26676db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  }
26681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26696db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  // Deduce template arguments from the function parameters.
26701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Deduced.resize(TemplateParams->size());
2671f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  unsigned ArgIdx = 0;
2672dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  for (unsigned ParamIdx = 0, NumParams = ParamTypes.size();
2673f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor       ParamIdx != NumParams; ++ParamIdx) {
2674f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    QualType ParamType = ParamTypes[ParamIdx];
2675dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2676dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    const PackExpansionType *ParamExpansion
2677f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      = dyn_cast<PackExpansionType>(ParamType);
2678f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    if (!ParamExpansion) {
2679f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      // Simple case: matching a function parameter to a function argument.
2680f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      if (ArgIdx >= CheckArgs)
2681f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        break;
2682dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2683f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      Expr *Arg = Args[ArgIdx++];
2684f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      QualType ArgType = Arg->getType();
2685f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      unsigned TDF = 0;
2686f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams,
2687f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                                    ParamType, ArgType, Arg,
2688f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                                    TDF))
2689f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        continue;
2690dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2691f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      if (TemplateDeductionResult Result
2692f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor          = ::DeduceTemplateArguments(*this, TemplateParams,
2693f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                      ParamType, ArgType, Info, Deduced,
2694f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                      TDF))
2695f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        return Result;
26961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2697f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      // FIXME: we need to check that the deduced A is the same as A,
2698f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      // modulo the various allowed differences.
2699f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      continue;
270075f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor    }
2701dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2702f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    // C++0x [temp.deduct.call]p1:
2703dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   For a function parameter pack that occurs at the end of the
2704dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   parameter-declaration-list, the type A of each remaining argument of
2705dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   the call is compared with the type P of the declarator-id of the
2706dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   function parameter pack. Each comparison deduces template arguments
2707dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   for subsequent positions in the template parameter packs expanded by
27087d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    //   the function parameter pack. For a function parameter pack that does
2709dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   not occur at the end of the parameter-declaration-list, the type of
27107d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    //   the parameter pack is a non-deduced context.
27117d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    if (ParamIdx + 1 < NumParams)
27127d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor      break;
2713dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2714f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    QualType ParamPattern = ParamExpansion->getPattern();
2715f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    llvm::SmallVector<unsigned, 2> PackIndices;
2716f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    {
2717f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      llvm::BitVector SawIndices(TemplateParams->size());
2718f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2719f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      collectUnexpandedParameterPacks(ParamPattern, Unexpanded);
2720f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
2721f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        unsigned Depth, Index;
2722f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]);
2723f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        if (Depth == 0 && !SawIndices[Index]) {
2724f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor          SawIndices[Index] = true;
2725f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor          PackIndices.push_back(Index);
2726f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        }
2727f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      }
2728f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    }
2729f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?");
2730dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2731d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    // Keep track of the deduced template arguments for each parameter pack
2732dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    // expanded by this pack expansion (the outer index) and for each
2733d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    // template argument (the inner SmallVectors).
2734d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2>
2735d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      NewlyDeducedPacks(PackIndices.size());
2736dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    llvm::SmallVector<DeducedTemplateArgument, 2>
2737d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      SavedPacks(PackIndices.size());
27385429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor    PrepareArgumentPackDeduction(*this, Deduced, PackIndices, SavedPacks,
2739dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                 NewlyDeducedPacks);
2740f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    bool HasAnyArguments = false;
2741f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    for (; ArgIdx < NumArgs; ++ArgIdx) {
2742f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      HasAnyArguments = true;
2743dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2744f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      ParamType = ParamPattern;
2745f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      Expr *Arg = Args[ArgIdx];
2746f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      QualType ArgType = Arg->getType();
2747f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      unsigned TDF = 0;
2748f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams,
2749f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                                    ParamType, ArgType, Arg,
2750f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                                    TDF)) {
2751f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        // We can't actually perform any deduction for this argument, so stop
2752f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        // deduction at this point.
2753f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        ++ArgIdx;
2754f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        break;
2755f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      }
2756dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2757f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      if (TemplateDeductionResult Result
2758f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor          = ::DeduceTemplateArguments(*this, TemplateParams,
2759f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                      ParamType, ArgType, Info, Deduced,
2760f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                      TDF))
2761f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        return Result;
2762eff92135d32039c9874dc356f3e93143af6069c1John McCall
2763f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      // Capture the deduced template arguments for each parameter pack expanded
2764f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      // by this pack expansion, add them to the list of arguments we've deduced
2765f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      // for that pack, then clear out the deduced argument.
2766f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
2767f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]];
2768f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        if (!DeducedArg.isNull()) {
2769f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor          NewlyDeducedPacks[I].push_back(DeducedArg);
2770f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor          DeducedArg = DeducedTemplateArgument();
2771f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        }
2772e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      }
2773e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    }
2774dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2775f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    // Build argument packs for each of the parameter packs expanded by this
2776f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    // pack expansion.
27770216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    if (Sema::TemplateDeductionResult Result
2778dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi          = FinishArgumentPackDeduction(*this, TemplateParams, HasAnyArguments,
27790216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                                        Deduced, PackIndices, SavedPacks,
27800216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                                        NewlyDeducedPacks, Info))
2781dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      return Result;
27821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2783f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    // After we've matching against a parameter pack, we're done.
2784f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    break;
2785e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  }
278665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
27871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
278802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                         NumExplicitlySpecified,
278983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                         Specialization, Info);
279083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor}
2791127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor
279283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \brief Deduce template arguments when taking the address of a function
27934b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
27944b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// a template.
279583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
279683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionTemplate the function template for which we are performing
279783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template argument deduction.
279883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
2799dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \param ExplicitTemplateArguments the explicitly-specified template
28004b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// arguments.
280183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
280283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param ArgFunctionType the function type that will be used as the
280383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// "argument" type (A) when performing template argument deduction from the
28044b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// function template's function type. This type may be NULL, if there is no
28054b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
280683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
280783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Specialization if template argument deduction was successful,
28081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// this will be set to the function template specialization produced by
280983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template argument deduction.
281083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
281183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Info the argument will be updated to provide additional information
281283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// about template argument deduction.
281383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
281483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \returns the result of template argument deduction.
281583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::TemplateDeductionResult
281683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
281767714230a191bc3c01f33378f34f34ef377991a6Douglas Gregor                              TemplateArgumentListInfo *ExplicitTemplateArgs,
281883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                              QualType ArgFunctionType,
281983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                              FunctionDecl *&Specialization,
282083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                              TemplateDeductionInfo &Info) {
282183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
282283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateParameterList *TemplateParams
282383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = FunctionTemplate->getTemplateParameters();
282483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  QualType FunctionType = Function->getType();
28251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
282683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Substitute any explicit template arguments.
28272a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope InstScope(*this);
282802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
282902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  unsigned NumExplicitlySpecified = 0;
283083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  llvm::SmallVector<QualType, 4> ParamTypes;
2831d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (ExplicitTemplateArgs) {
28321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (TemplateDeductionResult Result
28331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          = SubstituteExplicitTemplateArguments(FunctionTemplate,
2834d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                *ExplicitTemplateArgs,
28351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                Deduced, ParamTypes,
283683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                                &FunctionType, Info))
283783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return Result;
283802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
283902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    NumExplicitlySpecified = Deduced.size();
2840127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
284183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
284283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Template argument deduction for function templates in a SFINAE context.
284383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Trap any errors that might occur.
28441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SFINAETrap Trap(*this);
28451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2846eff92135d32039c9874dc356f3e93143af6069c1John McCall  Deduced.resize(TemplateParams->size());
2847eff92135d32039c9874dc356f3e93143af6069c1John McCall
28484b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  if (!ArgFunctionType.isNull()) {
28494b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    // Deduce template arguments from the function type.
28504b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (TemplateDeductionResult Result
2851a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          = ::DeduceTemplateArguments(*this, TemplateParams,
28524b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                                      FunctionType, ArgFunctionType, Info,
285373b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor                                      Deduced, TDF_TopLevelParameterTypeList))
28544b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      return Result;
28554b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  }
2856fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor
2857dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  if (TemplateDeductionResult Result
2858fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor        = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
2859fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor                                          NumExplicitlySpecified,
2860fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor                                          Specialization, Info))
2861fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor    return Result;
2862fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor
2863fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor  // If the requested function type does not match the actual type of the
2864fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor  // specialization, template argument deduction fails.
2865fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor  if (!ArgFunctionType.isNull() &&
2866fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor      !Context.hasSameType(ArgFunctionType, Specialization->getType()))
2867fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor    return TDK_NonDeducedMismatch;
2868fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor
2869fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor  return TDK_Success;
2870e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
2871e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
287265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \brief Deduce template arguments for a templated conversion
287365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// function (C++ [temp.deduct.conv]) and, if successful, produce a
287465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// conversion function template specialization.
287565ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::TemplateDeductionResult
287665ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
287765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                              QualType ToType,
287865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                              CXXConversionDecl *&Specialization,
287965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                              TemplateDeductionInfo &Info) {
28801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  CXXConversionDecl *Conv
288165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
288265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  QualType FromType = Conv->getConversionType();
288365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
288465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Canonicalize the types for deduction.
288565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  QualType P = Context.getCanonicalType(FromType);
288665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  QualType A = Context.getCanonicalType(ToType);
288765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
28885453d93ab8668f2d9d0bc02182695b02d207e32dDouglas Gregor  // C++0x [temp.deduct.conv]p2:
288965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   If P is a reference type, the type referred to by P is used for
289065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   type deduction.
289165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (const ReferenceType *PRef = P->getAs<ReferenceType>())
289265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    P = PRef->getPointeeType();
289365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
28945453d93ab8668f2d9d0bc02182695b02d207e32dDouglas Gregor  // C++0x [temp.deduct.conv]p4:
28955453d93ab8668f2d9d0bc02182695b02d207e32dDouglas Gregor  //   [...] If A is a reference type, the type referred to by A is used
289665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   for type deduction.
289765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (const ReferenceType *ARef = A->getAs<ReferenceType>())
28985453d93ab8668f2d9d0bc02182695b02d207e32dDouglas Gregor    A = ARef->getPointeeType().getUnqualifiedType();
28995453d93ab8668f2d9d0bc02182695b02d207e32dDouglas Gregor  // C++ [temp.deduct.conv]p3:
290065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //
29011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   If A is not a reference type:
290265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  else {
290365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    assert(!A->isReferenceType() && "Reference types were handled above");
290465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
290565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   - If P is an array type, the pointer type produced by the
29061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     array-to-pointer standard conversion (4.2) is used in place
290765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     of P for type deduction; otherwise,
290865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    if (P->isArrayType())
290965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      P = Context.getArrayDecayedType(P);
291065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   - If P is a function type, the pointer type produced by the
291165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     function-to-pointer standard conversion (4.3) is used in
291265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     place of P for type deduction; otherwise,
291365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    else if (P->isFunctionType())
291465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      P = Context.getPointerType(P);
291565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   - If P is a cv-qualified type, the top level cv-qualifiers of
29160099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi    //     P's type are ignored for type deduction.
291765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    else
291865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      P = P.getUnqualifiedType();
291965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
29205453d93ab8668f2d9d0bc02182695b02d207e32dDouglas Gregor    // C++0x [temp.deduct.conv]p4:
29210099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi    //   If A is a cv-qualified type, the top level cv-qualifiers of A's
29225453d93ab8668f2d9d0bc02182695b02d207e32dDouglas Gregor    //   type are ignored for type deduction. If A is a reference type, the type
29235453d93ab8668f2d9d0bc02182695b02d207e32dDouglas Gregor    //   referred to by A is used for type deduction.
292465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    A = A.getUnqualifiedType();
292565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  }
292665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
292765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Template argument deduction for function templates in a SFINAE context.
292865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Trap any errors that might occur.
29291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SFINAETrap Trap(*this);
293065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
293165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++ [temp.deduct.conv]p1:
293265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   Template argument deduction is done by comparing the return
293365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   type of the template conversion function (call it P) with the
293465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   type that is required as the result of the conversion (call it
293565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   A) as described in 14.8.2.4.
293665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  TemplateParameterList *TemplateParams
293765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    = FunctionTemplate->getTemplateParameters();
293802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
29391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Deduced.resize(TemplateParams->size());
294065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
294165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++0x [temp.deduct.conv]p4:
294265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   In general, the deduction process attempts to find template
294365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   argument values that will make the deduced A identical to
294465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   A. However, there are two cases that allow a difference:
294565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  unsigned TDF = 0;
294665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //     - If the original A is a reference type, A can be more
294765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       cv-qualified than the deduced A (i.e., the type referred to
294865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       by the reference)
294965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (ToType->isReferenceType())
295065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    TDF |= TDF_ParamWithReferenceType;
295165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //     - The deduced A can be another pointer or pointer to member
29520099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi  //       type that can be converted to A via a qualification
295365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       conversion.
295465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //
295565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
295665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // both P and A are pointers or member pointers. In this case, we
295765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // just ignore cv-qualifiers completely).
295865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if ((P->isPointerType() && A->isPointerType()) ||
295965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      (P->isMemberPointerType() && P->isMemberPointerType()))
296065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    TDF |= TDF_IgnoreQualifiers;
296165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (TemplateDeductionResult Result
2962a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = ::DeduceTemplateArguments(*this, TemplateParams,
296365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                    P, A, Info, Deduced, TDF))
296465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    return Result;
296565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
296665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // FIXME: we need to check that the deduced A is the same as A,
296765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // modulo the various allowed differences.
29681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
296965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Finish template argument deduction.
29702a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope InstScope(*this);
297165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  FunctionDecl *Spec = 0;
297265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  TemplateDeductionResult Result
2973dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 0, Spec,
297402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                      Info);
297565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  Specialization = cast_or_null<CXXConversionDecl>(Spec);
297665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return Result;
297765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor}
297865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
29794b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \brief Deduce template arguments for a function template when there is
29804b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
29814b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
29824b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param FunctionTemplate the function template for which we are performing
29834b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template argument deduction.
29844b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
2985dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \param ExplicitTemplateArguments the explicitly-specified template
29864b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// arguments.
29874b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
29884b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param Specialization if template argument deduction was successful,
29894b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// this will be set to the function template specialization produced by
29904b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template argument deduction.
29914b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
29924b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param Info the argument will be updated to provide additional information
29934b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// about template argument deduction.
29944b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
29954b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \returns the result of template argument deduction.
29964b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas GregorSema::TemplateDeductionResult
29974b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
299867714230a191bc3c01f33378f34f34ef377991a6Douglas Gregor                              TemplateArgumentListInfo *ExplicitTemplateArgs,
29994b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                              FunctionDecl *&Specialization,
30004b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                              TemplateDeductionInfo &Info) {
30014b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
30024b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                                 QualType(), Specialization, Info);
30034b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor}
30044b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor
300534b41d939a1328f484511c6002ba2456db879a29Richard Smithnamespace {
300634b41d939a1328f484511c6002ba2456db879a29Richard Smith  /// Substitute the 'auto' type specifier within a type for a given replacement
300734b41d939a1328f484511c6002ba2456db879a29Richard Smith  /// type.
300834b41d939a1328f484511c6002ba2456db879a29Richard Smith  class SubstituteAutoTransform :
300934b41d939a1328f484511c6002ba2456db879a29Richard Smith    public TreeTransform<SubstituteAutoTransform> {
301034b41d939a1328f484511c6002ba2456db879a29Richard Smith    QualType Replacement;
301134b41d939a1328f484511c6002ba2456db879a29Richard Smith  public:
301234b41d939a1328f484511c6002ba2456db879a29Richard Smith    SubstituteAutoTransform(Sema &SemaRef, QualType Replacement) :
301334b41d939a1328f484511c6002ba2456db879a29Richard Smith      TreeTransform<SubstituteAutoTransform>(SemaRef), Replacement(Replacement) {
301434b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
301534b41d939a1328f484511c6002ba2456db879a29Richard Smith    QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) {
301634b41d939a1328f484511c6002ba2456db879a29Richard Smith      // If we're building the type pattern to deduce against, don't wrap the
301734b41d939a1328f484511c6002ba2456db879a29Richard Smith      // substituted type in an AutoType. Certain template deduction rules
301834b41d939a1328f484511c6002ba2456db879a29Richard Smith      // apply only when a template type parameter appears directly (and not if
301934b41d939a1328f484511c6002ba2456db879a29Richard Smith      // the parameter is found through desugaring). For instance:
302034b41d939a1328f484511c6002ba2456db879a29Richard Smith      //   auto &&lref = lvalue;
302134b41d939a1328f484511c6002ba2456db879a29Richard Smith      // must transform into "rvalue reference to T" not "rvalue reference to
302234b41d939a1328f484511c6002ba2456db879a29Richard Smith      // auto type deduced as T" in order for [temp.deduct.call]p3 to apply.
302334b41d939a1328f484511c6002ba2456db879a29Richard Smith      if (isa<TemplateTypeParmType>(Replacement)) {
302434b41d939a1328f484511c6002ba2456db879a29Richard Smith        QualType Result = Replacement;
302534b41d939a1328f484511c6002ba2456db879a29Richard Smith        TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
302634b41d939a1328f484511c6002ba2456db879a29Richard Smith        NewTL.setNameLoc(TL.getNameLoc());
302734b41d939a1328f484511c6002ba2456db879a29Richard Smith        return Result;
302834b41d939a1328f484511c6002ba2456db879a29Richard Smith      } else {
302934b41d939a1328f484511c6002ba2456db879a29Richard Smith        QualType Result = RebuildAutoType(Replacement);
303034b41d939a1328f484511c6002ba2456db879a29Richard Smith        AutoTypeLoc NewTL = TLB.push<AutoTypeLoc>(Result);
303134b41d939a1328f484511c6002ba2456db879a29Richard Smith        NewTL.setNameLoc(TL.getNameLoc());
303234b41d939a1328f484511c6002ba2456db879a29Richard Smith        return Result;
303334b41d939a1328f484511c6002ba2456db879a29Richard Smith      }
303434b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
303534b41d939a1328f484511c6002ba2456db879a29Richard Smith  };
303634b41d939a1328f484511c6002ba2456db879a29Richard Smith}
303734b41d939a1328f484511c6002ba2456db879a29Richard Smith
303834b41d939a1328f484511c6002ba2456db879a29Richard Smith/// \brief Deduce the type for an auto type-specifier (C++0x [dcl.spec.auto]p6)
303934b41d939a1328f484511c6002ba2456db879a29Richard Smith///
304034b41d939a1328f484511c6002ba2456db879a29Richard Smith/// \param Type the type pattern using the auto type-specifier.
304134b41d939a1328f484511c6002ba2456db879a29Richard Smith///
304234b41d939a1328f484511c6002ba2456db879a29Richard Smith/// \param Init the initializer for the variable whose type is to be deduced.
304334b41d939a1328f484511c6002ba2456db879a29Richard Smith///
304434b41d939a1328f484511c6002ba2456db879a29Richard Smith/// \param Result if type deduction was successful, this will be set to the
304534b41d939a1328f484511c6002ba2456db879a29Richard Smith/// deduced type. This may still contain undeduced autos if the type is
3046a085da86242c9b8e3466f8cf6f4397e9f248fd20Richard Smith/// dependent. This will be set to null if deduction succeeded, but auto
3047a085da86242c9b8e3466f8cf6f4397e9f248fd20Richard Smith/// substitution failed; the appropriate diagnostic will already have been
3048a085da86242c9b8e3466f8cf6f4397e9f248fd20Richard Smith/// produced in that case.
304934b41d939a1328f484511c6002ba2456db879a29Richard Smith///
305034b41d939a1328f484511c6002ba2456db879a29Richard Smith/// \returns true if deduction succeeded, false if it failed.
305134b41d939a1328f484511c6002ba2456db879a29Richard Smithbool
3052a085da86242c9b8e3466f8cf6f4397e9f248fd20Richard SmithSema::DeduceAutoType(TypeSourceInfo *Type, Expr *Init,
3053a085da86242c9b8e3466f8cf6f4397e9f248fd20Richard Smith                     TypeSourceInfo *&Result) {
305434b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (Init->isTypeDependent()) {
305534b41d939a1328f484511c6002ba2456db879a29Richard Smith    Result = Type;
305634b41d939a1328f484511c6002ba2456db879a29Richard Smith    return true;
305734b41d939a1328f484511c6002ba2456db879a29Richard Smith  }
305834b41d939a1328f484511c6002ba2456db879a29Richard Smith
305934b41d939a1328f484511c6002ba2456db879a29Richard Smith  SourceLocation Loc = Init->getExprLoc();
306034b41d939a1328f484511c6002ba2456db879a29Richard Smith
306134b41d939a1328f484511c6002ba2456db879a29Richard Smith  LocalInstantiationScope InstScope(*this);
306234b41d939a1328f484511c6002ba2456db879a29Richard Smith
306334b41d939a1328f484511c6002ba2456db879a29Richard Smith  // Build template<class TemplParam> void Func(FuncParam);
30644fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  TemplateTypeParmDecl *TemplParam =
30654fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    TemplateTypeParmDecl::Create(Context, 0, SourceLocation(), Loc, 0, 0, 0,
30664fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 false, false);
30674fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0);
30684fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  NamedDecl *TemplParamPtr = TemplParam;
3069483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  FixedSizeTemplateParameterList<1> TemplateParams(Loc, Loc, &TemplParamPtr,
3070483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith                                                   Loc);
3071483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith
3072a085da86242c9b8e3466f8cf6f4397e9f248fd20Richard Smith  TypeSourceInfo *FuncParamInfo =
307334b41d939a1328f484511c6002ba2456db879a29Richard Smith    SubstituteAutoTransform(*this, TemplArg).TransformType(Type);
3074a085da86242c9b8e3466f8cf6f4397e9f248fd20Richard Smith  assert(FuncParamInfo && "substituting template parameter for 'auto' failed");
3075a085da86242c9b8e3466f8cf6f4397e9f248fd20Richard Smith  QualType FuncParam = FuncParamInfo->getType();
307634b41d939a1328f484511c6002ba2456db879a29Richard Smith
307734b41d939a1328f484511c6002ba2456db879a29Richard Smith  // Deduce type of TemplParam in Func(Init)
307834b41d939a1328f484511c6002ba2456db879a29Richard Smith  llvm::SmallVector<DeducedTemplateArgument, 1> Deduced;
307934b41d939a1328f484511c6002ba2456db879a29Richard Smith  Deduced.resize(1);
308034b41d939a1328f484511c6002ba2456db879a29Richard Smith  QualType InitType = Init->getType();
308134b41d939a1328f484511c6002ba2456db879a29Richard Smith  unsigned TDF = 0;
3082483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  if (AdjustFunctionParmAndArgTypesForDeduction(*this, &TemplateParams,
308334b41d939a1328f484511c6002ba2456db879a29Richard Smith                                                FuncParam, InitType, Init,
308434b41d939a1328f484511c6002ba2456db879a29Richard Smith                                                TDF))
308534b41d939a1328f484511c6002ba2456db879a29Richard Smith    return false;
308634b41d939a1328f484511c6002ba2456db879a29Richard Smith
308734b41d939a1328f484511c6002ba2456db879a29Richard Smith  TemplateDeductionInfo Info(Context, Loc);
3088483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  if (::DeduceTemplateArguments(*this, &TemplateParams,
308934b41d939a1328f484511c6002ba2456db879a29Richard Smith                                FuncParam, InitType, Info, Deduced,
309034b41d939a1328f484511c6002ba2456db879a29Richard Smith                                TDF))
309134b41d939a1328f484511c6002ba2456db879a29Richard Smith    return false;
309234b41d939a1328f484511c6002ba2456db879a29Richard Smith
309334b41d939a1328f484511c6002ba2456db879a29Richard Smith  QualType DeducedType = Deduced[0].getAsType();
309434b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (DeducedType.isNull())
309534b41d939a1328f484511c6002ba2456db879a29Richard Smith    return false;
309634b41d939a1328f484511c6002ba2456db879a29Richard Smith
309734b41d939a1328f484511c6002ba2456db879a29Richard Smith  Result = SubstituteAutoTransform(*this, DeducedType).TransformType(Type);
309834b41d939a1328f484511c6002ba2456db879a29Richard Smith  return true;
309934b41d939a1328f484511c6002ba2456db879a29Richard Smith}
310034b41d939a1328f484511c6002ba2456db879a29Richard Smith
31018a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic void
3102e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, QualType T,
3103e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
3104ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Level,
3105e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Deduced);
3106dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3107dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \brief If this is a non-static member function,
310877bc572138543ef97ebec137522b763d6a6ee909Douglas Gregorstatic void MaybeAddImplicitObjectParameterType(ASTContext &Context,
310977bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor                                                CXXMethodDecl *Method,
311077bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor                                 llvm::SmallVectorImpl<QualType> &ArgTypes) {
311177bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  if (Method->isStatic())
311277bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor    return;
311377bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor
311477bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  // C++ [over.match.funcs]p4:
311577bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  //
311677bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  //   For non-static member functions, the type of the implicit
311777bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  //   object parameter is
31180099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi  //     - "lvalue reference to cv X" for functions declared without a
311977bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  //       ref-qualifier or with the & ref-qualifier
312077bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  //     - "rvalue reference to cv X" for functions declared with the
312177bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  //       && ref-qualifier
312277bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  //
312377bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  // FIXME: We don't have ref-qualifiers yet, so we don't do that part.
312477bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  QualType ArgTy = Context.getTypeDeclType(Method->getParent());
312577bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  ArgTy = Context.getQualifiedType(ArgTy,
312677bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor                        Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
312777bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  ArgTy = Context.getLValueReferenceType(ArgTy);
312877bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  ArgTypes.push_back(ArgTy);
312977bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor}
313077bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor
31318a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Determine whether the function template \p FT1 is at least as
31328a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// specialized as \p FT2.
31338a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic bool isAtLeastAsSpecializedAs(Sema &S,
31345769d6195087229770d7ac90449443e026c47103John McCall                                     SourceLocation Loc,
31358a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                     FunctionTemplateDecl *FT1,
31368a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                     FunctionTemplateDecl *FT2,
31378a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                     TemplatePartialOrderingContext TPOC,
3138dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                     unsigned NumCallArguments,
3139b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *RefParamComparisons) {
31408a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  FunctionDecl *FD1 = FT1->getTemplatedDecl();
3141dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  FunctionDecl *FD2 = FT2->getTemplatedDecl();
31428a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
31438a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
3144dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
31458a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  assert(Proto1 && Proto2 && "Function templates must have prototypes");
31468a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
314702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
31488a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Deduced.resize(TemplateParams->size());
31498a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
31508a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p3:
31518a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   The types used to determine the ordering depend on the context in which
31528a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   the partial ordering is done:
31532a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  TemplateDeductionInfo Info(S.Context, Loc);
31548d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor  CXXMethodDecl *Method1 = 0;
31558d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor  CXXMethodDecl *Method2 = 0;
31568d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor  bool IsNonStatic2 = false;
31578d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor  bool IsNonStatic1 = false;
31588d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor  unsigned Skip2 = 0;
31598a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  switch (TPOC) {
31608a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Call: {
31618a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   - In the context of a function call, the function parameter types are
31628a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //     used.
31638d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    Method1 = dyn_cast<CXXMethodDecl>(FD1);
31648d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    Method2 = dyn_cast<CXXMethodDecl>(FD2);
31658d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    IsNonStatic1 = Method1 && !Method1->isStatic();
31668d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    IsNonStatic2 = Method2 && !Method2->isStatic();
31678d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor
31688d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    // C++0x [temp.func.order]p3:
31698d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    //   [...] If only one of the function templates is a non-static
31708d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    //   member, that function template is considered to have a new
31718d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    //   first parameter inserted in its function parameter list. The
31728d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    //   new parameter is of type "reference to cv A," where cv are
31738d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    //   the cv-qualifiers of the function template (if any) and A is
31748d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    //   the class of which the function template is a member.
31758d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    //
31768d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    // C++98/03 doesn't have this provision, so instead we drop the
31778d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    // first argument of the free function or static member, which
31788d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    // seems to match existing practice.
317977bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor    llvm::SmallVector<QualType, 4> Args1;
3180dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    unsigned Skip1 = !S.getLangOptions().CPlusPlus0x &&
31818d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor      IsNonStatic2 && !IsNonStatic1;
31828d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    if (S.getLangOptions().CPlusPlus0x && IsNonStatic1 && !IsNonStatic2)
3183dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      MaybeAddImplicitObjectParameterType(S.Context, Method1, Args1);
3184dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    Args1.insert(Args1.end(),
31858d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor                 Proto1->arg_type_begin() + Skip1, Proto1->arg_type_end());
318677bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor
318777bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor    llvm::SmallVector<QualType, 4> Args2;
3188dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    Skip2 = !S.getLangOptions().CPlusPlus0x &&
31898d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor      IsNonStatic1 && !IsNonStatic2;
31908d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    if (S.getLangOptions().CPlusPlus0x && IsNonStatic2 && !IsNonStatic1)
319177bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor      MaybeAddImplicitObjectParameterType(S.Context, Method2, Args2);
3192dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    Args2.insert(Args2.end(),
31938d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor                 Proto2->arg_type_begin() + Skip2, Proto2->arg_type_end());
3194dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
31955c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    // C++ [temp.func.order]p5:
31965c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //   The presence of unused ellipsis and default arguments has no effect on
31975c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //   the partial ordering of function templates.
31985c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    if (Args1.size() > NumCallArguments)
31995c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      Args1.resize(NumCallArguments);
32005c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    if (Args2.size() > NumCallArguments)
32015c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      Args2.resize(NumCallArguments);
32025c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(),
32035c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                Args1.data(), Args1.size(), Info, Deduced,
32045c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                TDF_None, /*PartialOrdering=*/true,
3205b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                                RefParamComparisons))
32068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        return false;
3207dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
32088a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
32098a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
3210dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
32118a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Conversion:
32128a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   - In the context of a call to a conversion operator, the return types
32138a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //     of the conversion function templates are used.
32145c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    if (DeduceTemplateArguments(S, TemplateParams, Proto2->getResultType(),
32155c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                Proto1->getResultType(), Info, Deduced,
32165c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                TDF_None, /*PartialOrdering=*/true,
3217b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                                RefParamComparisons))
32188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      return false;
32198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
3220dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
32218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Other:
32220099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi    //   - In other contexts (14.6.6.2) the function template's function type
32238a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //     is used.
32245c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    // FIXME: Don't we actually want to perform the adjustments on the parameter
32255c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    // types?
3226dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    if (DeduceTemplateArguments(S, TemplateParams, FD2->getType(),
32275c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                FD1->getType(), Info, Deduced, TDF_None,
3228b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                                /*PartialOrdering=*/true, RefParamComparisons))
32298a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      return false;
32308a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
32318a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
3232dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
32338a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p11:
3234dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   In most cases, all template parameters must have values in order for
3235dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   deduction to succeed, but for partial ordering purposes a template
3236dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   parameter may remain without a value provided it is not used in the
32378a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   types being used for partial ordering. [ Note: a template parameter used
32388a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   in a non-deduced context is considered used. -end note]
32398a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  unsigned ArgIdx = 0, NumArgs = Deduced.size();
32408a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  for (; ArgIdx != NumArgs; ++ArgIdx)
32418a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    if (Deduced[ArgIdx].isNull())
32428a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      break;
32438a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
32448a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  if (ArgIdx == NumArgs) {
3245dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    // All template arguments were deduced. FT1 is at least as specialized
32468a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // as FT2.
32478a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return true;
32488a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
32498a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
3250e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // Figure out which template parameters were used.
32518a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  llvm::SmallVector<bool, 4> UsedParameters;
32528a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  UsedParameters.resize(TemplateParams->size());
32538a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  switch (TPOC) {
32548a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Call: {
3255dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    unsigned NumParams = std::min(NumCallArguments,
3256dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                  std::min(Proto1->getNumArgs(),
32575c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                           Proto2->getNumArgs()));
32588d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    if (S.getLangOptions().CPlusPlus0x && IsNonStatic2 && !IsNonStatic1)
3259dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      ::MarkUsedTemplateParameters(S, Method2->getThisType(S.Context), false,
32608d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor                                   TemplateParams->getDepth(), UsedParameters);
32618d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    for (unsigned I = Skip2; I < NumParams; ++I)
3262dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
3263ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                   TemplateParams->getDepth(),
3264e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                   UsedParameters);
32658a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
32668a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
3267dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
32688a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Conversion:
3269dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
3270ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 TemplateParams->getDepth(),
3271e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                 UsedParameters);
32728a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
3273dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
32748a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Other:
3275dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    ::MarkUsedTemplateParameters(S, FD2->getType(), false,
3276ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 TemplateParams->getDepth(),
3277ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 UsedParameters);
32788a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
32798a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
3280dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
32818a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  for (; ArgIdx != NumArgs; ++ArgIdx)
32828a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // If this argument had no value deduced but was used in one of the types
32838a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // used for partial ordering, then deduction fails.
32848a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
32858a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      return false;
3286dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
32878a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  return true;
32888a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor}
3289dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
32909da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor/// \brief Determine whether this a function template whose parameter-type-list
32919da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor/// ends with a function parameter pack.
32929da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregorstatic bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) {
32939da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  FunctionDecl *Function = FunTmpl->getTemplatedDecl();
32949da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  unsigned NumParams = Function->getNumParams();
32959da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  if (NumParams == 0)
32969da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor    return false;
3297dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
32989da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  ParmVarDecl *Last = Function->getParamDecl(NumParams - 1);
32999da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  if (!Last->isParameterPack())
33009da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor    return false;
3301dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
33029da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  // Make sure that no previous parameter is a parameter pack.
33039da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  while (--NumParams > 0) {
33049da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor    if (Function->getParamDecl(NumParams - 1)->isParameterPack())
33059da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor      return false;
33069da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  }
3307dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
33089da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  return true;
33099da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor}
3310dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3311bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \brief Returns the more specialized function template according
331265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// to the rules of function template partial ordering (C++ [temp.func.order]).
331365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor///
331465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \param FT1 the first function template
331565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor///
331665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \param FT2 the second function template
331765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor///
33188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param TPOC the context in which we are performing partial ordering of
33198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// function templates.
33201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
33215c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// \param NumCallArguments The number of arguments in a call, used only
33225c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// when \c TPOC is \c TPOC_Call.
33235c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor///
3324bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \returns the more specialized function template. If neither
332565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// template is more specialized, returns NULL.
332665ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorFunctionTemplateDecl *
332765ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
332865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                 FunctionTemplateDecl *FT2,
33295769d6195087229770d7ac90449443e026c47103John McCall                                 SourceLocation Loc,
33305c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                 TemplatePartialOrderingContext TPOC,
33315c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                 unsigned NumCallArguments) {
3332b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor  llvm::SmallVector<RefParamPartialOrderingComparison, 4> RefParamComparisons;
3333dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC,
33345c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                          NumCallArguments, 0);
3335dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC,
33365c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                          NumCallArguments,
3337b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                                          &RefParamComparisons);
3338dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
33398a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  if (Better1 != Better2) // We have a clear winner
33408a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return Better1? FT1 : FT2;
3341dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
33428a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  if (!Better1 && !Better2) // Neither is better than the other
334365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    return 0;
33448a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
33458a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p10:
3346dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   If for each type being considered a given template is at least as
33478a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   specialized for all types and more specialized for some set of types and
3348dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   the other template is not more specialized for any types or is not at
33498a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   least as specialized for any types, then the given template is more
33508a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   specialized than the other template. Otherwise, neither template is more
33518a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   specialized than the other.
33528a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Better1 = false;
33538a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Better2 = false;
3354b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor  for (unsigned I = 0, N = RefParamComparisons.size(); I != N; ++I) {
33558a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // C++0x [temp.deduct.partial]p9:
33568a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   If, for a given type, deduction succeeds in both directions (i.e., the
3357b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //   types are identical after the transformations above) and both P and A
3358b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //   were reference types (before being replaced with the type referred to
3359b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //   above):
3360b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor
3361dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //     -- if the type from the argument template was an lvalue reference
3362b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //        and the type from the parameter template was not, the argument
3363b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //        type is considered to be more specialized than the other;
3364b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //        otherwise,
3365b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    if (!RefParamComparisons[I].ArgIsRvalueRef &&
3366b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor        RefParamComparisons[I].ParamIsRvalueRef) {
3367b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      Better2 = true;
3368b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      if (Better1)
3369b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor        return 0;
3370b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      continue;
3371b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    } else if (!RefParamComparisons[I].ParamIsRvalueRef &&
3372b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor               RefParamComparisons[I].ArgIsRvalueRef) {
3373b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      Better1 = true;
3374b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      if (Better2)
3375b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor        return 0;
3376b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      continue;
3377b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    }
3378dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3379b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //     -- if the type from the argument template is more cv-qualified than
3380b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //        the type from the parameter template (as described above), the
3381b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //        argument type is considered to be more specialized than the
3382b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //        other; otherwise,
3383b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    switch (RefParamComparisons[I].Qualifiers) {
3384b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    case NeitherMoreQualified:
3385b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      break;
3386dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3387b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    case ParamMoreQualified:
3388b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      Better1 = true;
3389b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      if (Better2)
3390b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor        return 0;
3391b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      continue;
3392dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3393b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    case ArgMoreQualified:
3394b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      Better2 = true;
3395b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      if (Better1)
3396b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor        return 0;
3397b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      continue;
33988a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    }
3399dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3400b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //     -- neither type is more specialized than the other.
34018a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
3402dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
34038a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  assert(!(Better1 && Better2) && "Should have broken out in the loop above");
340465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (Better1)
340565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    return FT1;
34068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  else if (Better2)
34078a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return FT2;
3408dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
34099da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  // FIXME: This mimics what GCC implements, but doesn't match up with the
34109da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  // proposed resolution for core issue 692. This area needs to be sorted out,
34119da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  // but for now we attempt to maintain compatibility.
34129da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  bool Variadic1 = isVariadicFunctionTemplate(FT1);
34139da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  bool Variadic2 = isVariadicFunctionTemplate(FT2);
34149da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  if (Variadic1 != Variadic2)
34159da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor    return Variadic1? FT2 : FT1;
3416dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
34179da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  return 0;
341865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor}
341983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
3420d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \brief Determine if the two templates are equivalent.
3421d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregorstatic bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
3422d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  if (T1 == T2)
3423d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    return true;
3424dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3425d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  if (!T1 || !T2)
3426d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    return false;
3427dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3428d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  return T1->getCanonicalDecl() == T2->getCanonicalDecl();
3429d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor}
3430d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
3431d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \brief Retrieve the most specialized of the given function template
3432d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specializations.
3433d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3434c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// \param SpecBegin the start iterator of the function template
3435c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// specializations that we will be comparing.
3436d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3437c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// \param SpecEnd the end iterator of the function template
3438c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// specializations, paired with \p SpecBegin.
3439d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3440d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param TPOC the partial ordering context to use to compare the function
3441d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// template specializations.
3442d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
34435c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// \param NumCallArguments The number of arguments in a call, used only
34445c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// when \c TPOC is \c TPOC_Call.
34455c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor///
3446dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \param Loc the location where the ambiguity or no-specializations
3447d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// diagnostic should occur.
3448d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3449d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param NoneDiag partial diagnostic used to diagnose cases where there are
3450d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// no matching candidates.
3451d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3452d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
3453d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// occurs.
3454d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3455d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param CandidateDiag partial diagnostic used for each function template
3456d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specialization that is a candidate in the ambiguous ordering. One parameter
3457d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// in this diagnostic should be unbound, which will correspond to the string
3458d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// describing the template arguments for the function template specialization.
3459d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3460dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \param Index if non-NULL and the result of this function is non-nULL,
3461d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// receives the index corresponding to the resulting function template
3462d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specialization.
3463d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3464dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \returns the most specialized function template specialization, if
3465c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// found. Otherwise, returns SpecEnd.
3466d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3467dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
3468d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// template argument deduction.
3469c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallUnresolvedSetIterator
3470c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallSema::getMostSpecialized(UnresolvedSetIterator SpecBegin,
34715c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                        UnresolvedSetIterator SpecEnd,
3472c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         TemplatePartialOrderingContext TPOC,
34735c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                         unsigned NumCallArguments,
3474c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         SourceLocation Loc,
3475c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         const PartialDiagnostic &NoneDiag,
3476c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         const PartialDiagnostic &AmbigDiag,
34771be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor                         const PartialDiagnostic &CandidateDiag,
34781be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor                         bool Complain) {
3479c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  if (SpecBegin == SpecEnd) {
34801be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor    if (Complain)
34811be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor      Diag(Loc, NoneDiag);
3482c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    return SpecEnd;
3483d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
3484dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3485dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  if (SpecBegin + 1 == SpecEnd)
3486c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    return SpecBegin;
3487dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3488d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // Find the function template that is better than all of the templates it
3489d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // has been compared to.
3490c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  UnresolvedSetIterator Best = SpecBegin;
3491dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  FunctionTemplateDecl *BestTemplate
3492c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
3493d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  assert(BestTemplate && "Not a function template specialization?");
3494c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
3495c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    FunctionTemplateDecl *Challenger
3496c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall      = cast<FunctionDecl>(*I)->getPrimaryTemplate();
3497d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    assert(Challenger && "Not a function template specialization?");
3498c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
34995c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                                  Loc, TPOC, NumCallArguments),
3500d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor                       Challenger)) {
3501d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      Best = I;
3502d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      BestTemplate = Challenger;
3503d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    }
3504d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
3505dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3506d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // Make sure that the "best" function template is more specialized than all
3507d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // of the others.
3508d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  bool Ambiguous = false;
3509c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
3510c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    FunctionTemplateDecl *Challenger
3511c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall      = cast<FunctionDecl>(*I)->getPrimaryTemplate();
3512d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    if (I != Best &&
3513dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
35145c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                                   Loc, TPOC, NumCallArguments),
3515d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor                        BestTemplate)) {
3516d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      Ambiguous = true;
3517d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      break;
3518d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    }
3519d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
3520dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3521d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  if (!Ambiguous) {
3522d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    // We found an answer. Return it.
3523c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    return Best;
3524d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
3525dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3526d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // Diagnose the ambiguity.
35271be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor  if (Complain)
35281be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor    Diag(Loc, AmbigDiag);
3529dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
35301be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor  if (Complain)
3531d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // FIXME: Can we order the candidates in some sane way?
35321be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor    for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I)
35331be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor      Diag((*I)->getLocation(), CandidateDiag)
35341be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor        << getTemplateArgumentBindingsText(
35351be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor          cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
3536c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                    *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
3537dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3538c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  return SpecEnd;
3539d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor}
3540d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
3541bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \brief Returns the more specialized class template partial specialization
3542bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// according to the rules of partial ordering of class template partial
3543bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// specializations (C++ [temp.class.order]).
3544bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor///
3545bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \param PS1 the first class template partial specialization
3546bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor///
3547bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \param PS2 the second class template partial specialization
3548bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor///
3549bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \returns the more specialized class template partial specialization. If
3550bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// neither partial specialization is more specialized, returns NULL.
3551bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas GregorClassTemplatePartialSpecializationDecl *
3552bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas GregorSema::getMoreSpecializedPartialSpecialization(
3553bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                  ClassTemplatePartialSpecializationDecl *PS1,
35545769d6195087229770d7ac90449443e026c47103John McCall                                  ClassTemplatePartialSpecializationDecl *PS2,
35555769d6195087229770d7ac90449443e026c47103John McCall                                              SourceLocation Loc) {
3556bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // C++ [temp.class.order]p1:
3557bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   For two class template partial specializations, the first is at least as
3558dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   specialized as the second if, given the following rewrite to two
3559dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   function templates, the first function template is at least as
3560dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   specialized as the second according to the ordering rules for function
3561bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   templates (14.6.6.2):
3562bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //     - the first function template has the same template parameters as the
3563dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //       first partial specialization and has a single function parameter
3564dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //       whose type is a class template specialization with the template
3565bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       arguments of the first partial specialization, and
3566bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //     - the second function template has the same template parameters as the
3567dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //       second partial specialization and has a single function parameter
3568dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //       whose type is a class template specialization with the template
3569bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       arguments of the second partial specialization.
3570bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //
357131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // Rather than synthesize function templates, we merely perform the
357231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // equivalent partial ordering by performing deduction directly on
357331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // the template arguments of the class template partial
357431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // specializations. This computation is slightly simpler than the
357531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // general problem of function template partial ordering, because
357631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // class template partial specializations are more constrained. We
357731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // know that every template parameter is deducible from the class
357831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // template partial specialization's template arguments, for
357931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // example.
358002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
35812a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  TemplateDeductionInfo Info(Context, Loc);
358231f17ecbef57b5679c017c375db330546b7b5145John McCall
358331f17ecbef57b5679c017c375db330546b7b5145John McCall  QualType PT1 = PS1->getInjectedSpecializationType();
358431f17ecbef57b5679c017c375db330546b7b5145John McCall  QualType PT2 = PS2->getInjectedSpecializationType();
3585dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3586bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // Determine whether PS1 is at least as specialized as PS2
3587bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  Deduced.resize(PS2->getTemplateParameters()->size());
35885c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor  bool Better1 = !::DeduceTemplateArguments(*this, PS2->getTemplateParameters(),
35895c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                            PT2, PT1, Info, Deduced, TDF_None,
3590dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                            /*PartialOrdering=*/true,
3591b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                                            /*RefParamComparisons=*/0);
35922c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis  if (Better1) {
35932c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis    InstantiatingTemplate Inst(*this, PS2->getLocation(), PS2,
35942c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis                               Deduced.data(), Deduced.size(), Info);
3595dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    Better1 = !::FinishTemplateArgumentDeduction(*this, PS2,
3596dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                 PS1->getTemplateArgs(),
3597516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor                                                 Deduced, Info);
35982c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis  }
3599dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3600bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // Determine whether PS2 is at least as specialized as PS1
3601db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Deduced.clear();
3602bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  Deduced.resize(PS1->getTemplateParameters()->size());
36035c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor  bool Better2 = !::DeduceTemplateArguments(*this, PS1->getTemplateParameters(),
36045c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                            PT1, PT2, Info, Deduced, TDF_None,
36055c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                            /*PartialOrdering=*/true,
3606b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                                            /*RefParamComparisons=*/0);
36072c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis  if (Better2) {
36082c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis    InstantiatingTemplate Inst(*this, PS1->getLocation(), PS1,
36092c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis                               Deduced.data(), Deduced.size(), Info);
3610dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    Better2 = !::FinishTemplateArgumentDeduction(*this, PS1,
3611dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                 PS2->getTemplateArgs(),
3612516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor                                                 Deduced, Info);
36132c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis  }
3614dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3615bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  if (Better1 == Better2)
3616bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    return 0;
3617dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3618bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  return Better1? PS1 : PS2;
3619bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor}
3620bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor
36211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
3622e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
3623e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           const TemplateArgument &TemplateArg,
3624e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
3625ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
3626e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used);
3627031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3628e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
3629031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// expression.
36301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
3631e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
3632e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           const Expr *E,
3633e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
3634ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
3635e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
3636be230c36e32142cbdcdbe9c97511d097beeecbabDouglas Gregor  // We can deduce from a pack expansion.
3637be230c36e32142cbdcdbe9c97511d097beeecbabDouglas Gregor  if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E))
3638be230c36e32142cbdcdbe9c97511d097beeecbabDouglas Gregor    E = Expansion->getPattern();
3639dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
364054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  // Skip through any implicit casts we added while type-checking.
364154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
364254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    E = ICE->getSubExpr();
3643dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3644dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
3645e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // find other occurrences of template parameters.
3646f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor  const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3647c781f9cd854f3d5d1c826f4a13382c6abca4cff7Douglas Gregor  if (!DRE)
3648031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    return;
3649031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
36501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const NonTypeTemplateParmDecl *NTTP
3651031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
3652031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  if (!NTTP)
3653031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    return;
3654031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3655ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (NTTP->getDepth() == Depth)
3656ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Used[NTTP->getIndex()] = true;
3657031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
3658031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3659e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
3660e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// nested name specifier.
3661e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregorstatic void
3662e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
3663e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           NestedNameSpecifier *NNS,
3664e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
3665ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
3666e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
3667e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (!NNS)
3668e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    return;
3669dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3670ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
3671ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             Used);
3672dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
3673ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             OnlyDeduced, Depth, Used);
3674e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor}
3675dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3676e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
3677e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// template name.
3678e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregorstatic void
3679e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
3680e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           TemplateName Name,
3681e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
3682ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
3683e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
3684e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
3685e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    if (TemplateTemplateParmDecl *TTP
3686ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor          = dyn_cast<TemplateTemplateParmDecl>(Template)) {
3687ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      if (TTP->getDepth() == Depth)
3688ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        Used[TTP->getIndex()] = true;
3689ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    }
3690e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    return;
3691e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  }
3692dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3693788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
3694dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
3695788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                               Depth, Used);
3696e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
3697dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
3698ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
3699e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor}
3700e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
3701e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
3702031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// type.
37031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
3704e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, QualType T,
3705e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
3706ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
3707e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
3708e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (T.isNull())
3709e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    return;
3710dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3711031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  // Non-dependent types have nothing deducible
3712031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  if (!T->isDependentType())
3713031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    return;
3714031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3715031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  T = SemaRef.Context.getCanonicalType(T);
3716031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  switch (T->getTypeClass()) {
3717031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Pointer:
3718e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
3719e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<PointerType>(T)->getPointeeType(),
3720e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               OnlyDeduced,
3721ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth,
3722e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               Used);
3723031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3724031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3725031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::BlockPointer:
3726e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
3727e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<BlockPointerType>(T)->getPointeeType(),
3728e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               OnlyDeduced,
3729ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth,
3730e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               Used);
3731031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3732031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3733031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::LValueReference:
3734031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::RValueReference:
3735e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
3736e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<ReferenceType>(T)->getPointeeType(),
3737e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               OnlyDeduced,
3738ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth,
3739e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               Used);
3740031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3741031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3742031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::MemberPointer: {
3743031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
3744e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
3745ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
3746e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
3747ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
3748031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3749031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
3750031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3751031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::DependentSizedArray:
3752e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
3753e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<DependentSizedArrayType>(T)->getSizeExpr(),
3754ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
3755031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    // Fall through to check the element type
3756031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3757031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::ConstantArray:
3758031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::IncompleteArray:
3759e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
3760e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<ArrayType>(T)->getElementType(),
3761ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
3762031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3763031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3764031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Vector:
3765031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::ExtVector:
3766e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
3767e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<VectorType>(T)->getElementType(),
3768ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
3769031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3770031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
37719cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  case Type::DependentSizedExtVector: {
37729cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    const DependentSizedExtVectorType *VecType
3773f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor      = cast<DependentSizedExtVectorType>(T);
3774e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
3775ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
3776dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
3777ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
37789cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    break;
37799cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
37809cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
3781031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::FunctionProto: {
3782f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor    const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
3783e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
3784ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
3785031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
3786e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor      MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
3787ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 Depth, Used);
3788031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3789031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
3790031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3791ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  case Type::TemplateTypeParm: {
3792ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
3793ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (TTP->getDepth() == Depth)
3794ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      Used[TTP->getIndex()] = true;
3795031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3796ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
3797031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
37980bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  case Type::SubstTemplateTypeParmPack: {
37990bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    const SubstTemplateTypeParmPackType *Subst
38000bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor      = cast<SubstTemplateTypeParmPackType>(T);
3801dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    MarkUsedTemplateParameters(SemaRef,
38020bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor                               QualType(Subst->getReplacedParameter(), 0),
38030bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor                               OnlyDeduced, Depth, Used);
38040bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    MarkUsedTemplateParameters(SemaRef, Subst->getArgumentPack(),
38050bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor                               OnlyDeduced, Depth, Used);
38060bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    break;
38070bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  }
38080bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor
380931f17ecbef57b5679c017c375db330546b7b5145John McCall  case Type::InjectedClassName:
381031f17ecbef57b5679c017c375db330546b7b5145John McCall    T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
381131f17ecbef57b5679c017c375db330546b7b5145John McCall    // fall through
381231f17ecbef57b5679c017c375db330546b7b5145John McCall
3813031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::TemplateSpecialization: {
38141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const TemplateSpecializationType *Spec
3815f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor      = cast<TemplateSpecializationType>(T);
3816e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
3817ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
3818dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
38197b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    // C++0x [temp.deduct.type]p9:
3820dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   If the template argument list of P contains a pack expansion that is not
3821dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   the last template argument, the entire template argument list is a
38227b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    //   non-deduced context.
3823dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    if (OnlyDeduced &&
38247b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor        hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs()))
38257b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      break;
38267b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor
3827e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
3828ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
3829ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 Used);
3830e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    break;
3831e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  }
38321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3833e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  case Type::Complex:
3834e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    if (!OnlyDeduced)
3835dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      MarkUsedTemplateParameters(SemaRef,
3836e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                 cast<ComplexType>(T)->getElementType(),
3837ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 OnlyDeduced, Depth, Used);
3838e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    break;
3839031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
38404714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  case Type::DependentName:
3841e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    if (!OnlyDeduced)
3842e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor      MarkUsedTemplateParameters(SemaRef,
38434714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor                                 cast<DependentNameType>(T)->getQualifier(),
3844ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 OnlyDeduced, Depth, Used);
3845031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3846031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
384733500955d731c73717af52088b7fc0e7a85681e7John McCall  case Type::DependentTemplateSpecialization: {
384833500955d731c73717af52088b7fc0e7a85681e7John McCall    const DependentTemplateSpecializationType *Spec
384933500955d731c73717af52088b7fc0e7a85681e7John McCall      = cast<DependentTemplateSpecializationType>(T);
385033500955d731c73717af52088b7fc0e7a85681e7John McCall    if (!OnlyDeduced)
385133500955d731c73717af52088b7fc0e7a85681e7John McCall      MarkUsedTemplateParameters(SemaRef, Spec->getQualifier(),
385233500955d731c73717af52088b7fc0e7a85681e7John McCall                                 OnlyDeduced, Depth, Used);
3853dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
38547b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    // C++0x [temp.deduct.type]p9:
3855dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   If the template argument list of P contains a pack expansion that is not
3856dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   the last template argument, the entire template argument list is a
38577b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    //   non-deduced context.
3858dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    if (OnlyDeduced &&
38597b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor        hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs()))
38607b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      break;
38617b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor
386233500955d731c73717af52088b7fc0e7a85681e7John McCall    for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
386333500955d731c73717af52088b7fc0e7a85681e7John McCall      MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
386433500955d731c73717af52088b7fc0e7a85681e7John McCall                                 Used);
386533500955d731c73717af52088b7fc0e7a85681e7John McCall    break;
386633500955d731c73717af52088b7fc0e7a85681e7John McCall  }
386733500955d731c73717af52088b7fc0e7a85681e7John McCall
3868ad5e73887052193afda72db8efcb812bd083a4a8John McCall  case Type::TypeOf:
3869ad5e73887052193afda72db8efcb812bd083a4a8John McCall    if (!OnlyDeduced)
3870ad5e73887052193afda72db8efcb812bd083a4a8John McCall      MarkUsedTemplateParameters(SemaRef,
3871ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 cast<TypeOfType>(T)->getUnderlyingType(),
3872ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 OnlyDeduced, Depth, Used);
3873ad5e73887052193afda72db8efcb812bd083a4a8John McCall    break;
3874ad5e73887052193afda72db8efcb812bd083a4a8John McCall
3875ad5e73887052193afda72db8efcb812bd083a4a8John McCall  case Type::TypeOfExpr:
3876ad5e73887052193afda72db8efcb812bd083a4a8John McCall    if (!OnlyDeduced)
3877ad5e73887052193afda72db8efcb812bd083a4a8John McCall      MarkUsedTemplateParameters(SemaRef,
3878ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 cast<TypeOfExprType>(T)->getUnderlyingExpr(),
3879ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 OnlyDeduced, Depth, Used);
3880ad5e73887052193afda72db8efcb812bd083a4a8John McCall    break;
3881ad5e73887052193afda72db8efcb812bd083a4a8John McCall
3882ad5e73887052193afda72db8efcb812bd083a4a8John McCall  case Type::Decltype:
3883ad5e73887052193afda72db8efcb812bd083a4a8John McCall    if (!OnlyDeduced)
3884ad5e73887052193afda72db8efcb812bd083a4a8John McCall      MarkUsedTemplateParameters(SemaRef,
3885ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 cast<DecltypeType>(T)->getUnderlyingExpr(),
3886ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 OnlyDeduced, Depth, Used);
3887ad5e73887052193afda72db8efcb812bd083a4a8John McCall    break;
3888ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
3889ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case Type::UnaryTransform:
3890ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    if (!OnlyDeduced)
3891ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      MarkUsedTemplateParameters(SemaRef,
3892ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                               cast<UnaryTransformType>(T)->getUnderlyingType(),
3893ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                 OnlyDeduced, Depth, Used);
3894ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    break;
3895ad5e73887052193afda72db8efcb812bd083a4a8John McCall
38967536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  case Type::PackExpansion:
3897dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    MarkUsedTemplateParameters(SemaRef,
38987536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor                               cast<PackExpansionType>(T)->getPattern(),
38997536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor                               OnlyDeduced, Depth, Used);
39007536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    break;
39017536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
390234b41d939a1328f484511c6002ba2456db879a29Richard Smith  case Type::Auto:
390334b41d939a1328f484511c6002ba2456db879a29Richard Smith    MarkUsedTemplateParameters(SemaRef,
390434b41d939a1328f484511c6002ba2456db879a29Richard Smith                               cast<AutoType>(T)->getDeducedType(),
390534b41d939a1328f484511c6002ba2456db879a29Richard Smith                               OnlyDeduced, Depth, Used);
390634b41d939a1328f484511c6002ba2456db879a29Richard Smith
3907e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // None of these types have any template parameters in them.
3908031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Builtin:
3909031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::VariableArray:
3910031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::FunctionNoProto:
3911031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Record:
3912031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Enum:
3913031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::ObjCInterface:
3914c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject:
3915d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  case Type::ObjCObjectPointer:
3916ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  case Type::UnresolvedUsing:
3917031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define TYPE(Class, Base)
3918031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
3919031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define DEPENDENT_TYPE(Class, Base)
3920031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
3921031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#include "clang/AST/TypeNodes.def"
3922031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3923031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
3924031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
3925031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3926e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by this
3927031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// template argument.
39281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
3929e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
3930e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           const TemplateArgument &TemplateArg,
3931e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
3932ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
3933e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
3934031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  switch (TemplateArg.getKind()) {
3935031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Null:
3936031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Integral:
3937788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Declaration:
3938031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
39391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3940031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Type:
3941e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
3942ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
3943031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3944031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3945788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  case TemplateArgument::Template:
3946a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor  case TemplateArgument::TemplateExpansion:
3947dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    MarkUsedTemplateParameters(SemaRef,
3948dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                               TemplateArg.getAsTemplateOrTemplatePattern(),
3949788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                               OnlyDeduced, Depth, Used);
3950031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3951031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3952031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Expression:
3953dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
3954ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
3955031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3956dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3957d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson  case TemplateArgument::Pack:
3958e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
3959e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                      PEnd = TemplateArg.pack_end();
3960e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor         P != PEnd; ++P)
3961ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
3962d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson    break;
3963031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
3964031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
3965031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3966031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \brief Mark the template parameters can be deduced by the given
3967031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// template argument list.
3968031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor///
3969031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \param TemplateArgs the template argument list from which template
3970031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// parameters will be deduced.
3971031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor///
3972031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \param Deduced a bit vector whose elements will be set to \c true
3973031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// to indicate when the corresponding template parameter will be
3974031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// deduced.
39751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
3976e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorSema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
3977ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 bool OnlyDeduced, unsigned Depth,
3978e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                 llvm::SmallVectorImpl<bool> &Used) {
39797b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor  // C++0x [temp.deduct.type]p9:
3980dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   If the template argument list of P contains a pack expansion that is not
3981dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   the last template argument, the entire template argument list is a
39827b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor  //   non-deduced context.
3983dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  if (OnlyDeduced &&
39847b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      hasPackExpansionBeforeEnd(TemplateArgs.data(), TemplateArgs.size()))
39857b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    return;
39867b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor
3987031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
3988dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
3989ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 Depth, Used);
3990031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
399163f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor
399263f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor/// \brief Marks all of the template parameters that will be deduced by a
399363f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor/// call to the given function template.
3994dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumivoid
399502024a9f0d8e6c898de276193af604c42ee41269Douglas GregorSema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
399602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                    llvm::SmallVectorImpl<bool> &Deduced) {
3997dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  TemplateParameterList *TemplateParams
399863f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor    = FunctionTemplate->getTemplateParameters();
399963f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  Deduced.clear();
400063f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  Deduced.resize(TemplateParams->size());
4001dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
400263f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
400363f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
400463f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor    ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
4005ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 true, TemplateParams->getDepth(), Deduced);
400663f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor}
4007