SemaTemplateDeduction.cpp revision e559ca1672ecef59345a928af0a6809b09282d2c
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"
157cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall#include "clang/Sema/Template.h"
162a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall#include "clang/Sema/TemplateDeduction.h"
170b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/ASTContext.h"
187cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall#include "clang/AST/DeclObjC.h"
190b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/DeclTemplate.h"
200b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/StmtVisitor.h"
210b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/Expr.h"
220b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/ExprCXX.h"
23e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor#include "llvm/ADT/BitVector.h"
2434b41d939a1328f484511c6002ba2456db879a29Richard Smith#include "TreeTransform.h"
258a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor#include <algorithm>
26508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor
27508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregornamespace clang {
282a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  using namespace sema;
292a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall
30508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  /// \brief Various flags that control template argument deduction.
31508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  ///
32508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  /// These flags can be bitwise-OR'd together.
33508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  enum TemplateDeductionFlags {
34508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// \brief No template argument deduction flags, which indicates the
35508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// strictest results for template argument deduction (as used for, e.g.,
36508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// matching class template partial specializations).
37508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    TDF_None = 0,
38508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// \brief Within template argument deduction from a function call, we are
39508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// matching with a parameter type for which the original parameter was
40508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// a reference.
41508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    TDF_ParamWithReferenceType = 0x1,
42508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// \brief Within template argument deduction from a function call, we
43508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// are matching in a case where we ignore cv-qualifiers.
44508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    TDF_IgnoreQualifiers = 0x02,
45508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// \brief Within template argument deduction from a function call,
46508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// we are matching in a case where we can perform template argument
474112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    /// deduction from a template-id of a derived class of the argument type.
481282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor    TDF_DerivedClass = 0x04,
491282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor    /// \brief Allow non-dependent types to differ, e.g., when performing
501282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor    /// template argument deduction from a function call where conversions
511282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor    /// may apply.
5273b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor    TDF_SkipNonDependent = 0x08,
5373b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor    /// \brief Whether we are performing template argument deduction for
54dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    /// parameters and arguments in a top-level template argument
5573b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor    TDF_TopLevelParameterTypeList = 0x10
56508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  };
57508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor}
58508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor
590b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregorusing namespace clang;
600b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
619d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor/// \brief Compare two APSInts, extending and switching the sign as
629d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor/// necessary to compare their values regardless of underlying type.
639d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregorstatic bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) {
649d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor  if (Y.getBitWidth() > X.getBitWidth())
659f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    X = X.extend(Y.getBitWidth());
669d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor  else if (Y.getBitWidth() < X.getBitWidth())
679f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad    Y = Y.extend(X.getBitWidth());
689d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor
699d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor  // If there is a signedness mismatch, correct it.
709d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor  if (X.isSigned() != Y.isSigned()) {
719d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor    // If the signed value is negative, then the values cannot be the same.
729d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor    if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative()))
739d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor      return false;
749d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor
759d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor    Y.setIsSigned(true);
769d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor    X.setIsSigned(true);
779d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor  }
789d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor
799d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor  return X == Y;
809d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor}
819d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor
82f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
83a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
84f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateParameterList *TemplateParams,
85f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        const TemplateArgument &Param,
8677d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor                        TemplateArgument Arg,
872a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                        TemplateDeductionInfo &Info,
880972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor                      llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced);
89d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor
90b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor/// \brief Whether template argument deduction for two reference parameters
91b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor/// resulted in the argument type, parameter type, or neither type being more
92b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor/// qualified than the other.
93dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumienum DeductionQualifierComparison {
94dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  NeitherMoreQualified = 0,
95dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  ParamMoreQualified,
96dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  ArgMoreQualified
975c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor};
985c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor
99b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor/// \brief Stores the result of comparing two reference parameters while
100b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor/// performing template argument deduction for partial ordering of function
101dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// templates.
102b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregorstruct RefParamPartialOrderingComparison {
103b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor  /// \brief Whether the parameter type is an rvalue reference type.
104b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor  bool ParamIsRvalueRef;
105b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor  /// \brief Whether the argument type is an rvalue reference type.
106b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor  bool ArgIsRvalueRef;
107dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
108b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor  /// \brief Whether the parameter or argument (or neither) is more qualified.
109b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor  DeductionQualifierComparison Qualifiers;
110b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor};
111b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor
112b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor
1135c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor
11420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregorstatic Sema::TemplateDeductionResult
11520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas GregorDeduceTemplateArguments(Sema &S,
11620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        TemplateParameterList *TemplateParams,
117603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                        QualType Param,
118603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                        QualType Arg,
119603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                        TemplateDeductionInfo &Info,
120603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                        llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
1215c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                        unsigned TDF,
1225c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                        bool PartialOrdering = false,
123b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                      llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *
124b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                                                      RefParamComparisons = 0);
125603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor
126603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregorstatic Sema::TemplateDeductionResult
127603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas GregorDeduceTemplateArguments(Sema &S,
128603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                        TemplateParameterList *TemplateParams,
12920a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        const TemplateArgument *Params, unsigned NumParams,
13020a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        const TemplateArgument *Args, unsigned NumArgs,
13120a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        TemplateDeductionInfo &Info,
1320972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor                        llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
1330972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor                        bool NumberOfArgumentsMustMatch = true);
13420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor
135199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// \brief If the given expression is of a form that permits the deduction
136199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// of a non-type template parameter, return the declaration of that
137199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// non-type template parameter.
138199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregorstatic NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
139199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
140199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    E = IC->getSubExpr();
1411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
142199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
143199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
1441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
145199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  return 0;
146199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor}
147199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
1480d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor/// \brief Determine whether two declaration pointers refer to the same
1490d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor/// declaration.
1500d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregorstatic bool isSameDeclaration(Decl *X, Decl *Y) {
1510d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  if (!X || !Y)
1520d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    return !X && !Y;
153dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1540d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  if (NamedDecl *NX = dyn_cast<NamedDecl>(X))
1550d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    X = NX->getUnderlyingDecl();
1560d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  if (NamedDecl *NY = dyn_cast<NamedDecl>(Y))
1570d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    Y = NY->getUnderlyingDecl();
158dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1590d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  return X->getCanonicalDecl() == Y->getCanonicalDecl();
1600d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor}
1610d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor
1620d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor/// \brief Verify that the given, deduced template arguments are compatible.
1630d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor///
1640d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor/// \returns The deduced template argument, or a NULL template argument if
1650d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor/// the deduced template arguments were incompatible.
166dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic DeducedTemplateArgument
1670d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas GregorcheckDeducedTemplateArguments(ASTContext &Context,
1680d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                              const DeducedTemplateArgument &X,
1690d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                              const DeducedTemplateArgument &Y) {
1700d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  // We have no deduction for one or both of the arguments; they're compatible.
1710d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  if (X.isNull())
1720d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    return Y;
1730d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  if (Y.isNull())
174dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return X;
1750d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor
1760d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  switch (X.getKind()) {
1770d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  case TemplateArgument::Null:
1780d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    llvm_unreachable("Non-deduced template arguments handled above");
1790d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor
1800d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  case TemplateArgument::Type:
1810d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // If two template type arguments have the same type, they're compatible.
1820d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    if (Y.getKind() == TemplateArgument::Type &&
1830d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor        Context.hasSameType(X.getAsType(), Y.getAsType()))
1840d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      return X;
185dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1860d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    return DeducedTemplateArgument();
187dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1880d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  case TemplateArgument::Integral:
1890d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // If we deduced a constant in one case and either a dependent expression or
1900d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // declaration in another case, keep the integral constant.
1910d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // If both are integral constants with the same value, keep that value.
1920d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    if (Y.getKind() == TemplateArgument::Expression ||
1930d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor        Y.getKind() == TemplateArgument::Declaration ||
1940d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor        (Y.getKind() == TemplateArgument::Integral &&
1950d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor         hasSameExtendedValue(*X.getAsIntegral(), *Y.getAsIntegral())))
196dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      return DeducedTemplateArgument(X,
1970d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                     X.wasDeducedFromArrayBound() &&
1980d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                     Y.wasDeducedFromArrayBound());
1990d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor
2000d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // All other combinations are incompatible.
2010d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    return DeducedTemplateArgument();
202dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2030d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  case TemplateArgument::Template:
2040d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    if (Y.getKind() == TemplateArgument::Template &&
2050d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor        Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate()))
2060d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      return X;
207dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2080d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // All other combinations are incompatible.
209dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return DeducedTemplateArgument();
210a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
211a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor  case TemplateArgument::TemplateExpansion:
212a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    if (Y.getKind() == TemplateArgument::TemplateExpansion &&
213dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(),
214a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                                    Y.getAsTemplateOrTemplatePattern()))
215a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      return X;
216dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
217a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    // All other combinations are incompatible.
218dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return DeducedTemplateArgument();
219a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
2200d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  case TemplateArgument::Expression:
221dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    // If we deduced a dependent expression in one case and either an integral
222dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    // constant or a declaration in another case, keep the integral constant
2230d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // or declaration.
2240d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    if (Y.getKind() == TemplateArgument::Integral ||
2250d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor        Y.getKind() == TemplateArgument::Declaration)
2260d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      return DeducedTemplateArgument(Y, X.wasDeducedFromArrayBound() &&
2270d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                     Y.wasDeducedFromArrayBound());
228dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2290d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    if (Y.getKind() == TemplateArgument::Expression) {
2300d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      // Compare the expressions for equality
2310d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      llvm::FoldingSetNodeID ID1, ID2;
2320d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      X.getAsExpr()->Profile(ID1, Context, true);
2330d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      Y.getAsExpr()->Profile(ID2, Context, true);
2340d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      if (ID1 == ID2)
2350d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor        return X;
2360d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    }
237dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2380d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // All other combinations are incompatible.
2390d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    return DeducedTemplateArgument();
240dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2410d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  case TemplateArgument::Declaration:
2420d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // If we deduced a declaration and a dependent expression, keep the
2430d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // declaration.
2440d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    if (Y.getKind() == TemplateArgument::Expression)
2450d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      return X;
246dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2470d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // If we deduced a declaration and an integral constant, keep the
2480d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // integral constant.
2490d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    if (Y.getKind() == TemplateArgument::Integral)
2500d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      return Y;
251dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2520d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // If we deduced two declarations, make sure they they refer to the
2530d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // same declaration.
2540d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    if (Y.getKind() == TemplateArgument::Declaration &&
2550d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor        isSameDeclaration(X.getAsDecl(), Y.getAsDecl()))
2560d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      return X;
257dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2580d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    // All other combinations are incompatible.
2590d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    return DeducedTemplateArgument();
260dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2610d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  case TemplateArgument::Pack:
2620d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    if (Y.getKind() != TemplateArgument::Pack ||
2630d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor        X.pack_size() != Y.pack_size())
2640d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      return DeducedTemplateArgument();
265dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
266dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    for (TemplateArgument::pack_iterator XA = X.pack_begin(),
2670d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                      XAEnd = X.pack_end(),
2680d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                         YA = Y.pack_begin();
2690d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor         XA != XAEnd; ++XA, ++YA) {
270dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      if (checkDeducedTemplateArguments(Context,
271dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                    DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()),
272135ffa7375fb5802b92f42774e02d0e6e4c78e5bDouglas Gregor                    DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound()))
273135ffa7375fb5802b92f42774e02d0e6e4c78e5bDouglas Gregor            .isNull())
2740d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor        return DeducedTemplateArgument();
2750d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    }
276dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2770d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    return X;
2780d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  }
279dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2800d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  return DeducedTemplateArgument();
2810d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor}
2820d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor
2831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Deduce the value of the given non-type template parameter
284199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// from the given constant.
285f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
286a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceNonTypeTemplateArgument(Sema &S,
2871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                              NonTypeTemplateParmDecl *NTTP,
2889d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor                              llvm::APSInt Value, QualType ValueType,
28902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                              bool DeducedFromArrayBound,
2902a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                              TemplateDeductionInfo &Info,
29102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
2921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(NTTP->getDepth() == 0 &&
293199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor         "Cannot deduce non-type template argument with depth > 0");
2941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2950d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  DeducedTemplateArgument NewDeduced(Value, ValueType, DeducedFromArrayBound);
296dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
2970d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                                     Deduced[NTTP->getIndex()],
2980d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                                                 NewDeduced);
2990d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  if (Result.isNull()) {
300f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.Param = NTTP;
301f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.FirstArg = Deduced[NTTP->getIndex()];
3020d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    Info.SecondArg = NewDeduced;
303dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return Sema::TDK_Inconsistent;
304f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  }
305dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3060d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  Deduced[NTTP->getIndex()] = Result;
307f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  return Sema::TDK_Success;
308199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor}
309199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
3101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Deduce the value of the given non-type template parameter
311199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// from the given type- or value-dependent expression.
312199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor///
313199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// \returns true if deduction succeeded, false otherwise.
314f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
315a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceNonTypeTemplateArgument(Sema &S,
316f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                              NonTypeTemplateParmDecl *NTTP,
317f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                              Expr *Value,
3182a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                              TemplateDeductionInfo &Info,
31902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
3201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(NTTP->getDepth() == 0 &&
321199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor         "Cannot deduce non-type template argument with depth > 0");
322199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  assert((Value->isTypeDependent() || Value->isValueDependent()) &&
323199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor         "Expression template argument must be type- or value-dependent.");
3241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3250d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  DeducedTemplateArgument NewDeduced(Value);
326dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
327dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                     Deduced[NTTP->getIndex()],
3280d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                                                 NewDeduced);
329dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3300d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  if (Result.isNull()) {
3310d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    Info.Param = NTTP;
3320d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    Info.FirstArg = Deduced[NTTP->getIndex()];
3330d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    Info.SecondArg = NewDeduced;
334dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return Sema::TDK_Inconsistent;
3359eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor  }
336dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3370d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  Deduced[NTTP->getIndex()] = Result;
338f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  return Sema::TDK_Success;
339199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor}
340199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
34115755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor/// \brief Deduce the value of the given non-type template parameter
34215755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor/// from the given declaration.
34315755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor///
34415755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor/// \returns true if deduction succeeded, false otherwise.
34515755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregorstatic Sema::TemplateDeductionResult
346a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceNonTypeTemplateArgument(Sema &S,
34715755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor                              NonTypeTemplateParmDecl *NTTP,
34815755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor                              Decl *D,
3492a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                              TemplateDeductionInfo &Info,
35002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
35115755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor  assert(NTTP->getDepth() == 0 &&
35215755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor         "Cannot deduce non-type template argument with depth > 0");
353dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3540d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  DeducedTemplateArgument NewDeduced(D? D->getCanonicalDecl() : 0);
355dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
3560d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                                     Deduced[NTTP->getIndex()],
3570d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                                                 NewDeduced);
3580d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  if (Result.isNull()) {
3590d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    Info.Param = NTTP;
3600d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    Info.FirstArg = Deduced[NTTP->getIndex()];
3610d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    Info.SecondArg = NewDeduced;
362dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return Sema::TDK_Inconsistent;
36315755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor  }
364dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3650d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor  Deduced[NTTP->getIndex()] = Result;
36615755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor  return Sema::TDK_Success;
36715755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor}
36815755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor
369f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
370a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
371db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor                        TemplateParameterList *TemplateParams,
372f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateName Param,
373f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateName Arg,
3742a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                        TemplateDeductionInfo &Info,
37502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
376d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor  TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
377db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  if (!ParamDecl) {
378db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    // The parameter type is dependent and is not a template template parameter,
379db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    // so there is nothing that we can deduce.
380db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    return Sema::TDK_Success;
381f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  }
382dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
383db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  if (TemplateTemplateParmDecl *TempParam
384db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor        = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) {
3850d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg));
386dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
3870d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                                 Deduced[TempParam->getIndex()],
3880d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                                                   NewDeduced);
3890d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    if (Result.isNull()) {
3900d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      Info.Param = TempParam;
3910d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      Info.FirstArg = Deduced[TempParam->getIndex()];
3920d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      Info.SecondArg = NewDeduced;
393dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      return Sema::TDK_Inconsistent;
394db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    }
395dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3960d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    Deduced[TempParam->getIndex()] = Result;
397dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return Sema::TDK_Success;
398f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  }
399dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
400db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  // Verify that the two template names are equivalent.
401a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  if (S.Context.hasSameTemplateName(Param, Arg))
402db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    return Sema::TDK_Success;
403dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
404db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  // Mismatch of non-dependent template parameter to argument.
405db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Info.FirstArg = TemplateArgument(Param);
406db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Info.SecondArg = TemplateArgument(Arg);
407db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  return Sema::TDK_NonDeducedMismatch;
408d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor}
409d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor
4101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Deduce the template arguments by comparing the template parameter
411de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// type (which is a template-id) with the template argument type.
412de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
413a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth/// \param S the Sema
414de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
415de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param TemplateParams the template parameters that we are deducing
416de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
417de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Param the parameter type
418de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
419de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Arg the argument type
420de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
421de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Info information about the template argument deduction itself
422de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
423de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Deduced the deduced template arguments
424de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
425de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \returns the result of template argument deduction so far. Note that a
426de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// "success" result means that template argument deduction has not yet failed,
427de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// but it may still fail, later, for other reasons.
428de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregorstatic Sema::TemplateDeductionResult
429a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
430de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                        TemplateParameterList *TemplateParams,
431de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                        const TemplateSpecializationType *Param,
432de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                        QualType Arg,
4332a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                        TemplateDeductionInfo &Info,
43402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
435467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(Arg.isCanonical() && "Argument type must be canonical");
4361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
437de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  // Check whether the template argument is a dependent template-id.
4381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const TemplateSpecializationType *SpecArg
439de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        = dyn_cast<TemplateSpecializationType>(Arg)) {
440de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    // Perform template argument deduction for the template name.
441de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    if (Sema::TemplateDeductionResult Result
442a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          = DeduceTemplateArguments(S, TemplateParams,
443de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                    Param->getTemplateName(),
444de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                    SpecArg->getTemplateName(),
445de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                    Info, Deduced))
446de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      return Result;
4471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
449de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    // Perform template argument deduction on each template
4500972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor    // argument. Ignore any missing/extra arguments, since they could be
4510972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor    // filled in by default arguments.
452dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return DeduceTemplateArguments(S, TemplateParams,
453dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                   Param->getArgs(), Param->getNumArgs(),
4540972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor                                   SpecArg->getArgs(), SpecArg->getNumArgs(),
4550972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor                                   Info, Deduced,
4560972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor                                   /*NumberOfArgumentsMustMatch=*/false);
457de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  }
4581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
459de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  // If the argument type is a class template specialization, we
460de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  // perform template argument deduction using its template
461de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  // arguments.
462de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
463de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  if (!RecordArg)
464de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
4651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ClassTemplateSpecializationDecl *SpecArg
467de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
468de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  if (!SpecArg)
469de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
4701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
471de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  // Perform template argument deduction for the template name.
472de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  if (Sema::TemplateDeductionResult Result
473a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = DeduceTemplateArguments(S,
474db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor                                  TemplateParams,
475de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                  Param->getTemplateName(),
476de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                               TemplateName(SpecArg->getSpecializedTemplate()),
477de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                  Info, Deduced))
478de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    return Result;
4791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
48020a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  // Perform template argument deduction for the template arguments.
481dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  return DeduceTemplateArguments(S, TemplateParams,
48220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                                 Param->getArgs(), Param->getNumArgs(),
48320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                                 SpecArg->getTemplateArgs().data(),
48420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                                 SpecArg->getTemplateArgs().size(),
48520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                                 Info, Deduced);
486de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor}
487de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor
488cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall/// \brief Determines whether the given type is an opaque type that
489cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall/// might be more qualified when instantiated.
490cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCallstatic bool IsPossiblyOpaquelyQualifiedType(QualType T) {
491cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall  switch (T->getTypeClass()) {
492cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall  case Type::TypeOfExpr:
493cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall  case Type::TypeOf:
494cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall  case Type::DependentName:
495cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall  case Type::Decltype:
496cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall  case Type::UnresolvedUsing:
49762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall  case Type::TemplateTypeParm:
498cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall    return true;
499cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall
500cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall  case Type::ConstantArray:
501cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall  case Type::IncompleteArray:
502cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall  case Type::VariableArray:
503cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall  case Type::DependentSizedArray:
504cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall    return IsPossiblyOpaquelyQualifiedType(
505cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall                                      cast<ArrayType>(T)->getElementType());
506cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall
507cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall  default:
508cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall    return false;
509cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall  }
510cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall}
511cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall
512d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor/// \brief Retrieve the depth and index of a template parameter.
513dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic std::pair<unsigned, unsigned>
514d3731198193eee92796ddeb493973b7a598b003eDouglas GregorgetDepthAndIndex(NamedDecl *ND) {
515603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND))
516603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    return std::make_pair(TTP->getDepth(), TTP->getIndex());
517dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
518603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND))
519603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    return std::make_pair(NTTP->getDepth(), NTTP->getIndex());
520dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
521603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND);
522603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  return std::make_pair(TTP->getDepth(), TTP->getIndex());
523603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor}
524603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor
525d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor/// \brief Retrieve the depth and index of an unexpanded parameter pack.
526dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic std::pair<unsigned, unsigned>
527d3731198193eee92796ddeb493973b7a598b003eDouglas GregorgetDepthAndIndex(UnexpandedParameterPack UPP) {
528d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  if (const TemplateTypeParmType *TTP
529d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                          = UPP.first.dyn_cast<const TemplateTypeParmType *>())
530d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    return std::make_pair(TTP->getDepth(), TTP->getIndex());
531dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
532d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  return getDepthAndIndex(UPP.first.get<NamedDecl *>());
533d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor}
534d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor
535603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \brief Helper function to build a TemplateParameter when we don't
536603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// know its type statically.
537603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregorstatic TemplateParameter makeTemplateParameter(Decl *D) {
538603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
539603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    return TemplateParameter(TTP);
540603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
541603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    return TemplateParameter(NTTP);
542dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
543603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
544603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor}
545603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor
5465429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor/// \brief Prepare to perform template argument deduction for all of the
5475429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor/// arguments in a set of argument packs.
548dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic void PrepareArgumentPackDeduction(Sema &S,
549dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                       llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
5505429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor                             const llvm::SmallVectorImpl<unsigned> &PackIndices,
551dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                     llvm::SmallVectorImpl<DeducedTemplateArgument> &SavedPacks,
5525429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor         llvm::SmallVectorImpl<
5535429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor           llvm::SmallVector<DeducedTemplateArgument, 4> > &NewlyDeducedPacks) {
5545429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor  // Save the deduced template arguments for each parameter pack expanded
5555429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor  // by this pack expansion, then clear out the deduction.
5565429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor  for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
5575429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor    // Save the previously-deduced argument pack, then clear it out so that we
5585429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor    // can deduce a new argument pack.
5595429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor    SavedPacks[I] = Deduced[PackIndices[I]];
560dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    Deduced[PackIndices[I]] = TemplateArgument();
561dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
5625429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor    // If the template arugment pack was explicitly specified, add that to
5635429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor    // the set of deduced arguments.
5645429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor    const TemplateArgument *ExplicitArgs;
5655429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor    unsigned NumExplicitArgs;
566dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    if (NamedDecl *PartiallySubstitutedPack
5675429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor        = S.CurrentInstantiationScope->getPartiallySubstitutedPack(
5685429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor                                                           &ExplicitArgs,
5695429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor                                                           &NumExplicitArgs)) {
5705429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor      if (getDepthAndIndex(PartiallySubstitutedPack).second == PackIndices[I])
571dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        NewlyDeducedPacks[I].append(ExplicitArgs,
5725429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor                                    ExplicitArgs + NumExplicitArgs);
5735429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor    }
5745429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor  }
5755429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor}
5765429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor
5770216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor/// \brief Finish template argument deduction for a set of argument packs,
5780216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor/// producing the argument packs and checking for consistency with prior
5790216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor/// deductions.
5800216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregorstatic Sema::TemplateDeductionResult
5810216f8121df32b320cab659d5b703fee50cdfda5Douglas GregorFinishArgumentPackDeduction(Sema &S,
5820216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                            TemplateParameterList *TemplateParams,
5830216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                            bool HasAnyArguments,
584dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                        llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
5850216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                            const llvm::SmallVectorImpl<unsigned> &PackIndices,
586dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                    llvm::SmallVectorImpl<DeducedTemplateArgument> &SavedPacks,
5870216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor        llvm::SmallVectorImpl<
5880216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor          llvm::SmallVector<DeducedTemplateArgument, 4> > &NewlyDeducedPacks,
5890216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                            TemplateDeductionInfo &Info) {
5900216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor  // Build argument packs for each of the parameter packs expanded by this
5910216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor  // pack expansion.
5920216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor  for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
5930216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    if (HasAnyArguments && NewlyDeducedPacks[I].empty()) {
5940216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      // We were not able to deduce anything for this parameter pack,
5950216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      // so just restore the saved argument pack.
5960216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      Deduced[PackIndices[I]] = SavedPacks[I];
5970216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      continue;
5980216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    }
599dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
6000216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    DeducedTemplateArgument NewPack;
601dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
6020216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    if (NewlyDeducedPacks[I].empty()) {
6030216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      // If we deduced an empty argument pack, create it now.
6040216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      NewPack = DeducedTemplateArgument(TemplateArgument(0, 0));
6050216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    } else {
6060216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      TemplateArgument *ArgumentPack
607203e6a322ae29d577acafcb1572a57ec16e1e730Douglas Gregor        = new (S.Context) TemplateArgument [NewlyDeducedPacks[I].size()];
6080216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      std::copy(NewlyDeducedPacks[I].begin(), NewlyDeducedPacks[I].end(),
6090216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                ArgumentPack);
6100216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      NewPack
611203e6a322ae29d577acafcb1572a57ec16e1e730Douglas Gregor        = DeducedTemplateArgument(TemplateArgument(ArgumentPack,
612203e6a322ae29d577acafcb1572a57ec16e1e730Douglas Gregor                                                   NewlyDeducedPacks[I].size()),
613dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                            NewlyDeducedPacks[I][0].wasDeducedFromArrayBound());
6140216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    }
615dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
6160216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    DeducedTemplateArgument Result
6170216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      = checkDeducedTemplateArguments(S.Context, SavedPacks[I], NewPack);
6180216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    if (Result.isNull()) {
6190216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      Info.Param
6200216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor        = makeTemplateParameter(TemplateParams->getParam(PackIndices[I]));
6210216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      Info.FirstArg = SavedPacks[I];
6220216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      Info.SecondArg = NewPack;
6230216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor      return Sema::TDK_Inconsistent;
6240216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    }
625dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
6260216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    Deduced[PackIndices[I]] = Result;
6270216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor  }
628dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
6290216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor  return Sema::TDK_Success;
6300216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor}
6310216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor
632603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \brief Deduce the template arguments by comparing the list of parameter
633dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// types to the list of argument types, as in the parameter-type-lists of
634dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// function types (C++ [temp.deduct.type]p10).
635603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor///
636603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param S The semantic analysis object within which we are deducing
637603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor///
638603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param TemplateParams The template parameters that we are deducing
639603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor///
640603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param Params The list of parameter types
641603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor///
642603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param NumParams The number of types in \c Params
643603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor///
644603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param Args The list of argument types
645603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor///
646603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param NumArgs The number of types in \c Args
647603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor///
648603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param Info information about the template argument deduction itself
649603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor///
650603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param Deduced the deduced template arguments
651603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor///
652603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
653603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// how template argument deduction is performed.
654603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor///
6555c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// \param PartialOrdering If true, we are performing template argument
656dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// deduction for during partial ordering for a call
6575c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// (C++0x [temp.deduct.partial]).
6585c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor///
659b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor/// \param RefParamComparisons If we're performing template argument deduction
6605c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// in the context of partial ordering, the set of qualifier comparisons.
6615c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor///
662603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \returns the result of template argument deduction so far. Note that a
663603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// "success" result means that template argument deduction has not yet failed,
664603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// but it may still fail, later, for other reasons.
665603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregorstatic Sema::TemplateDeductionResult
666603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas GregorDeduceTemplateArguments(Sema &S,
667603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                        TemplateParameterList *TemplateParams,
668603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                        const QualType *Params, unsigned NumParams,
669603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                        const QualType *Args, unsigned NumArgs,
670603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                        TemplateDeductionInfo &Info,
671603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                      llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
6725c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                        unsigned TDF,
6735c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                        bool PartialOrdering = false,
674b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                        llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *
675b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                                                     RefParamComparisons = 0) {
6760bbacf8d2b3c7a5f19f292f3201e371836445502Douglas Gregor  // Fast-path check to see if we have too many/too few arguments.
6770bbacf8d2b3c7a5f19f292f3201e371836445502Douglas Gregor  if (NumParams != NumArgs &&
6780bbacf8d2b3c7a5f19f292f3201e371836445502Douglas Gregor      !(NumParams && isa<PackExpansionType>(Params[NumParams - 1])) &&
6790bbacf8d2b3c7a5f19f292f3201e371836445502Douglas Gregor      !(NumArgs && isa<PackExpansionType>(Args[NumArgs - 1])))
6803cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor    return Sema::TDK_NonDeducedMismatch;
681dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
682603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  // C++0x [temp.deduct.type]p10:
683dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   Similarly, if P has a form that contains (T), then each parameter type
684dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   Pi of the respective parameter-type- list of P is compared with the
685dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   corresponding parameter type Ai of the corresponding parameter-type-list
686dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   of A. [...]
687603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  unsigned ArgIdx = 0, ParamIdx = 0;
688603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  for (; ParamIdx != NumParams; ++ParamIdx) {
689603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    // Check argument types.
690dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    const PackExpansionType *Expansion
691603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                                = dyn_cast<PackExpansionType>(Params[ParamIdx]);
692603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    if (!Expansion) {
693603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      // Simple case: compare the parameter and argument types at this point.
694dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
695603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      // Make sure we have an argument.
696603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      if (ArgIdx >= NumArgs)
6973cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
698dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
69977d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor      if (isa<PackExpansionType>(Args[ArgIdx])) {
70077d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor        // C++0x [temp.deduct.type]p22:
70177d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor        //   If the original function parameter associated with A is a function
70277d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor        //   parameter pack and the function parameter associated with P is not
70377d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor        //   a function parameter pack, then template argument deduction fails.
70477d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
70577d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor      }
706dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
707603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      if (Sema::TemplateDeductionResult Result
7085c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor            = DeduceTemplateArguments(S, TemplateParams,
7095c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                      Params[ParamIdx],
7105c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                      Args[ArgIdx],
7115c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                      Info, Deduced, TDF,
7125c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                      PartialOrdering,
713b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                                      RefParamComparisons))
714603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor        return Result;
715dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
716603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      ++ArgIdx;
717603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      continue;
718603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    }
719dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
7207d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    // C++0x [temp.deduct.type]p5:
7217d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    //   The non-deduced contexts are:
722dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //     - A function parameter pack that does not occur at the end of the
7237d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    //       parameter-declaration-clause.
7247d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    if (ParamIdx + 1 < NumParams)
7257d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor      return Sema::TDK_Success;
7267d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor
727603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    // C++0x [temp.deduct.type]p10:
728dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   If the parameter-declaration corresponding to Pi is a function
729603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    //   parameter pack, then the type of its declarator- id is compared with
730dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   each remaining parameter type in the parameter-type-list of A. Each
731603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    //   comparison deduces template arguments for subsequent positions in the
732603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    //   template parameter packs expanded by the function parameter pack.
733dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
734603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    // Compute the set of template parameter indices that correspond to
735603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    // parameter packs expanded by the pack expansion.
736603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    llvm::SmallVector<unsigned, 2> PackIndices;
737603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    QualType Pattern = Expansion->getPattern();
738603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    {
739603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      llvm::BitVector SawIndices(TemplateParams->size());
740603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
741603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      S.collectUnexpandedParameterPacks(Pattern, Unexpanded);
742603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
743603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor        unsigned Depth, Index;
744603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor        llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]);
745603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor        if (Depth == 0 && !SawIndices[Index]) {
746603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor          SawIndices[Index] = true;
747603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor          PackIndices.push_back(Index);
748603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor        }
749603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      }
750603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    }
751603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?");
752603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor
753d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    // Keep track of the deduced template arguments for each parameter pack
754dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    // expanded by this pack expansion (the outer index) and for each
755d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    // template argument (the inner SmallVectors).
756d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2>
757d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      NewlyDeducedPacks(PackIndices.size());
758dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    llvm::SmallVector<DeducedTemplateArgument, 2>
7595429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor      SavedPacks(PackIndices.size());
760dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    PrepareArgumentPackDeduction(S, Deduced, PackIndices, SavedPacks,
7615429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor                                 NewlyDeducedPacks);
762dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
763603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    bool HasAnyArguments = false;
764603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    for (; ArgIdx < NumArgs; ++ArgIdx) {
765603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      HasAnyArguments = true;
766dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
767603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      // Deduce template arguments from the pattern.
768dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      if (Sema::TemplateDeductionResult Result
76973b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor            = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx],
77073b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor                                      Info, Deduced, TDF, PartialOrdering,
77173b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor                                      RefParamComparisons))
772603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor        return Result;
773dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
774603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      // Capture the deduced template arguments for each parameter pack expanded
775603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      // by this pack expansion, add them to the list of arguments we've deduced
776603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      // for that pack, then clear out the deduced argument.
777603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
778603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor        DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]];
779603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor        if (!DeducedArg.isNull()) {
780603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor          NewlyDeducedPacks[I].push_back(DeducedArg);
781603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor          DeducedArg = DeducedTemplateArgument();
782603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor        }
783603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      }
784603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    }
785dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
786603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    // Build argument packs for each of the parameter packs expanded by this
787603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor    // pack expansion.
7880216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    if (Sema::TemplateDeductionResult Result
789dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi          = FinishArgumentPackDeduction(S, TemplateParams, HasAnyArguments,
7900216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                                        Deduced, PackIndices, SavedPacks,
7910216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                                        NewlyDeducedPacks, Info))
792dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      return Result;
793603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  }
794dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
795603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  // Make sure we don't have any extra arguments.
796603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  if (ArgIdx < NumArgs)
7973cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor    return Sema::TDK_NonDeducedMismatch;
798dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
799603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor  return Sema::TDK_Success;
800603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor}
801603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor
80261d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor/// \brief Determine whether the parameter has qualifiers that are either
80361d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor/// inconsistent with or a superset of the argument's qualifiers.
80461d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregorstatic bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType,
80561d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor                                                  QualType ArgType) {
80661d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor  Qualifiers ParamQs = ParamType.getQualifiers();
80761d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor  Qualifiers ArgQs = ArgType.getQualifiers();
80861d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor
80961d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor  if (ParamQs == ArgQs)
81061d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor    return false;
81161d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor
81261d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor  // Mismatched (but not missing) Objective-C GC attributes.
81361d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor  if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() &&
81461d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor      ParamQs.hasObjCGCAttr())
81561d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor    return true;
81661d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor
81761d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor  // Mismatched (but not missing) address spaces.
81861d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor  if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() &&
81961d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor      ParamQs.hasAddressSpace())
82061d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor    return true;
82161d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor
822f85e193739c953358c865005855253af4f68a497John McCall  // Mismatched (but not missing) Objective-C lifetime qualifiers.
823f85e193739c953358c865005855253af4f68a497John McCall  if (ParamQs.getObjCLifetime() != ArgQs.getObjCLifetime() &&
824f85e193739c953358c865005855253af4f68a497John McCall      ParamQs.hasObjCLifetime())
825f85e193739c953358c865005855253af4f68a497John McCall    return true;
826f85e193739c953358c865005855253af4f68a497John McCall
82761d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor  // CVR qualifier superset.
82861d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor  return (ParamQs.getCVRQualifiers() != ArgQs.getCVRQualifiers()) &&
82961d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor      ((ParamQs.getCVRQualifiers() | ArgQs.getCVRQualifiers())
83061d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor                                                == ParamQs.getCVRQualifiers());
83161d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor}
83261d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor
833500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \brief Deduce the template arguments by comparing the parameter type and
834500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// the argument type (C++ [temp.deduct.type]).
835500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
836a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth/// \param S the semantic analysis object within which we are deducing
837500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
838500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param TemplateParams the template parameters that we are deducing
839500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
840500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param ParamIn the parameter type
841500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
842500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param ArgIn the argument type
843500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
844500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param Info information about the template argument deduction itself
845500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
846500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param Deduced the deduced template arguments
847500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
848508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
8491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// how template argument deduction is performed.
850500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
8515c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// \param PartialOrdering Whether we're performing template argument deduction
8525c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// in the context of partial ordering (C++0x [temp.deduct.partial]).
8535c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor///
854b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor/// \param RefParamComparisons If we're performing template argument deduction
8555c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// in the context of partial ordering, the set of qualifier comparisons.
8565c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor///
857500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \returns the result of template argument deduction so far. Note that a
858500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// "success" result means that template argument deduction has not yet failed,
859500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// but it may still fail, later, for other reasons.
860f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
861a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
862f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateParameterList *TemplateParams,
863f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        QualType ParamIn, QualType ArgIn,
8642a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                        TemplateDeductionInfo &Info,
86502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                     llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
8665c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                        unsigned TDF,
8675c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                        bool PartialOrdering,
868b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *RefParamComparisons) {
8690b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  // We only want to look at the canonical types, since typedefs and
8700b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  // sugar are not part of template argument deduction.
871a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  QualType Param = S.Context.getCanonicalType(ParamIn);
872a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  QualType Arg = S.Context.getCanonicalType(ArgIn);
8730b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
87477d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor  // If the argument type is a pack expansion, look at its pattern.
875dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // This isn't explicitly called out
87677d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor  if (const PackExpansionType *ArgExpansion
87777d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor                                            = dyn_cast<PackExpansionType>(Arg))
87877d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor    Arg = ArgExpansion->getPattern();
879dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
8805c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor  if (PartialOrdering) {
8815c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    // C++0x [temp.deduct.partial]p5:
882dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   Before the partial ordering is done, certain transformations are
883dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   performed on the types used for partial ordering:
884dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //     - If P is a reference type, P is replaced by the type referred to.
8855c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    const ReferenceType *ParamRef = Param->getAs<ReferenceType>();
8865c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    if (ParamRef)
8875c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      Param = ParamRef->getPointeeType();
888dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
8895c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //     - If A is a reference type, A is replaced by the type referred to.
8905c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    const ReferenceType *ArgRef = Arg->getAs<ReferenceType>();
8915c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    if (ArgRef)
8925c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      Arg = ArgRef->getPointeeType();
893dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
894b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    if (RefParamComparisons && ParamRef && ArgRef) {
8955c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      // C++0x [temp.deduct.partial]p6:
896dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      //   If both P and A were reference types (before being replaced with the
897dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      //   type referred to above), determine which of the two types (if any) is
8985c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      //   more cv-qualified than the other; otherwise the types are considered
899dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      //   to be equally cv-qualified for partial ordering purposes. The result
9005c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      //   of this determination will be used below.
9015c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      //
902dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      // We save this information for later, using it only when deduction
9035c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      // succeeds in both directions.
904b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      RefParamPartialOrderingComparison Comparison;
905b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      Comparison.ParamIsRvalueRef = ParamRef->getAs<RValueReferenceType>();
906b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      Comparison.ArgIsRvalueRef = ArgRef->getAs<RValueReferenceType>();
907b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      Comparison.Qualifiers = NeitherMoreQualified;
908769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor
909769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor      Qualifiers ParamQuals = Param.getQualifiers();
910769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor      Qualifiers ArgQuals = Arg.getQualifiers();
911769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor      if (ParamQuals.isStrictSupersetOf(ArgQuals))
912b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor        Comparison.Qualifiers = ParamMoreQualified;
913769d0cc72b1831785596d0e76f327bdb887823beDouglas Gregor      else if (ArgQuals.isStrictSupersetOf(ParamQuals))
914b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor        Comparison.Qualifiers = ArgMoreQualified;
915b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      RefParamComparisons->push_back(Comparison);
9165c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    }
917dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
9185c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    // C++0x [temp.deduct.partial]p7:
9195c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //   Remove any top-level cv-qualifiers:
920dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //     - If P is a cv-qualified type, P is replaced by the cv-unqualified
9215c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //       version of P.
9225c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    Param = Param.getUnqualifiedType();
923dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //     - If A is a cv-qualified type, A is replaced by the cv-unqualified
9245c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //       version of A.
9255c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    Arg = Arg.getUnqualifiedType();
9265c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor  } else {
9275c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    // C++0x [temp.deduct.call]p4 bullet 1:
9285c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //   - If the original P is a reference type, the deduced A (i.e., the type
9295c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //     referred to by the reference) can be more cv-qualified than the
9305c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //     transformed A.
9315c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    if (TDF & TDF_ParamWithReferenceType) {
9325c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      Qualifiers Quals;
9335c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals);
9345c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      Quals.setCVRQualifiers(Quals.getCVRQualifiers() &
93562c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall                             Arg.getCVRQualifiers());
9365c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      Param = S.Context.getQualifiedType(UnqualParam, Quals);
9375c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    }
938dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
93973b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor    if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) {
94073b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      // C++0x [temp.deduct.type]p10:
94173b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      //   If P and A are function types that originated from deduction when
94273b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      //   taking the address of a function template (14.8.2.2) or when deducing
94373b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      //   template arguments from a function declaration (14.8.2.6) and Pi and
944dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      //   Ai are parameters of the top-level parameter-type-list of P and A,
945dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      //   respectively, Pi is adjusted if it is an rvalue reference to a
946dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      //   cv-unqualified template parameter and Ai is an lvalue reference, in
947dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      //   which case the type of Pi is changed to be the template parameter
94873b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      //   type (i.e., T&& is changed to simply T). [ Note: As a result, when
94973b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      //   Pi is T&& and Ai is X&, the adjusted Pi will be T, causing T to be
9500099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi      //   deduced as X&. - end note ]
95173b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      TDF &= ~TDF_TopLevelParameterTypeList;
952dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
95373b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      if (const RValueReferenceType *ParamRef
95473b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor                                        = Param->getAs<RValueReferenceType>()) {
95573b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor        if (isa<TemplateTypeParmType>(ParamRef->getPointeeType()) &&
95673b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor            !ParamRef->getPointeeType().getQualifiers())
95773b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor          if (Arg->isLValueReferenceType())
95873b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor            Param = ParamRef->getPointeeType();
95973b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      }
96073b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor    }
961500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor  }
962dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
963f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  // If the parameter type is not dependent, there is nothing to deduce.
9641282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor  if (!Param->isDependentType()) {
9653cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor    if (!(TDF & TDF_SkipNonDependent) && Param != Arg)
9661282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor      return Sema::TDK_NonDeducedMismatch;
967dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
968f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    return Sema::TDK_Success;
9691282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor  }
9700b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
971199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  // C++ [temp.deduct.type]p9:
9721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   A template type argument T, a template template argument TT or a
9731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   template non-type argument i can be deduced if P and A have one of
974199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  //   the following forms:
975199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  //
976199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  //     T
977199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  //     cv-list T
9781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const TemplateTypeParmType *TemplateTypeParm
979183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall        = Param->getAs<TemplateTypeParmType>()) {
980f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    unsigned Index = TemplateTypeParm->getIndex();
981f290e0db835a619014538c41ed552696efc0e977Douglas Gregor    bool RecanonicalizeArg = false;
9821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9839e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor    // If the argument type is an array type, move the qualifiers up to the
9849e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor    // top level, so they can be matched with the qualifiers on the parameter.
985f290e0db835a619014538c41ed552696efc0e977Douglas Gregor    if (isa<ArrayType>(Arg)) {
9860953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      Qualifiers Quals;
987a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      Arg = S.Context.getUnqualifiedArrayType(Arg, Quals);
9880953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (Quals) {
989a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        Arg = S.Context.getQualifiedType(Arg, Quals);
990f290e0db835a619014538c41ed552696efc0e977Douglas Gregor        RecanonicalizeArg = true;
991f290e0db835a619014538c41ed552696efc0e977Douglas Gregor      }
992f290e0db835a619014538c41ed552696efc0e977Douglas Gregor    }
9931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9940b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor    // The argument type can not be less qualified than the parameter
9950b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor    // type.
99661d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor    if (!(TDF & TDF_IgnoreQualifiers) &&
99761d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor        hasInconsistentOrSupersetQualifiersOf(Param, Arg)) {
998f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
99957e97786433e70197a089360228d8f0d82e3ad4cJohn McCall      Info.FirstArg = TemplateArgument(Param);
1000833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      Info.SecondArg = TemplateArgument(Arg);
100157e97786433e70197a089360228d8f0d82e3ad4cJohn McCall      return Sema::TDK_Underqualified;
1002f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    }
10030b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
10040b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor    assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
1005a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth    assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function");
10060953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    QualType DeducedType = Arg;
100749f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall
100861d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor    // Remove any qualifiers on the parameter from the deduced type.
100961d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor    // We checked the qualifiers for consistency above.
101061d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor    Qualifiers DeducedQs = DeducedType.getQualifiers();
101161d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor    Qualifiers ParamQs = Param.getQualifiers();
101261d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor    DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers());
101361d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor    if (ParamQs.hasObjCGCAttr())
101461d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor      DeducedQs.removeObjCGCAttr();
101561d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor    if (ParamQs.hasAddressSpace())
101661d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor      DeducedQs.removeAddressSpace();
1017f85e193739c953358c865005855253af4f68a497John McCall    if (ParamQs.hasObjCLifetime())
1018f85e193739c953358c865005855253af4f68a497John McCall      DeducedQs.removeObjCLifetime();
1019e559ca1672ecef59345a928af0a6809b09282d2cDouglas Gregor
1020e559ca1672ecef59345a928af0a6809b09282d2cDouglas Gregor    // Objective-C ARC:
1021e559ca1672ecef59345a928af0a6809b09282d2cDouglas Gregor    //   If template deduction would produce an argument type with lifetime type
1022e559ca1672ecef59345a928af0a6809b09282d2cDouglas Gregor    //   but no lifetime qualifier, the __strong lifetime qualifier is inferred.
1023e559ca1672ecef59345a928af0a6809b09282d2cDouglas Gregor    if (S.getLangOptions().ObjCAutoRefCount &&
1024e559ca1672ecef59345a928af0a6809b09282d2cDouglas Gregor        DeducedType->isObjCLifetimeType() &&
1025e559ca1672ecef59345a928af0a6809b09282d2cDouglas Gregor        !DeducedQs.hasObjCLifetime())
1026e559ca1672ecef59345a928af0a6809b09282d2cDouglas Gregor      DeducedQs.setObjCLifetime(Qualifiers::OCL_Strong);
1027e559ca1672ecef59345a928af0a6809b09282d2cDouglas Gregor
102861d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor    DeducedType = S.Context.getQualifiedType(DeducedType.getUnqualifiedType(),
102961d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor                                             DeducedQs);
103061d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor
1031f290e0db835a619014538c41ed552696efc0e977Douglas Gregor    if (RecanonicalizeArg)
1032a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      DeducedType = S.Context.getCanonicalType(DeducedType);
10331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10340d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    DeducedTemplateArgument NewDeduced(DeducedType);
1035dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context,
10360d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                                                 Deduced[Index],
10370d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor                                                                   NewDeduced);
10380d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    if (Result.isNull()) {
10390d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
10400d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      Info.FirstArg = Deduced[Index];
10410d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor      Info.SecondArg = NewDeduced;
1042dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      return Sema::TDK_Inconsistent;
10430b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor    }
1044dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
10450d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor    Deduced[Index] = Result;
1046dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return Sema::TDK_Success;
10470b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
10480b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
1049f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  // Set up the template argument deduction information for a failure.
1050833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  Info.FirstArg = TemplateArgument(ParamIn);
1051833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  Info.SecondArg = TemplateArgument(ArgIn);
1052f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
10530bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  // If the parameter is an already-substituted template parameter
10540bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  // pack, do nothing: we don't know which of its arguments to look
10550bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  // at, so we have to wait until all of the parameter packs in this
10560bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  // expansion have arguments.
10570bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  if (isa<SubstTemplateTypeParmPackType>(Param))
10580bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    return Sema::TDK_Success;
10590bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor
1060508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  // Check the cv-qualifiers on the parameter and argument types.
1061508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  if (!(TDF & TDF_IgnoreQualifiers)) {
1062508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    if (TDF & TDF_ParamWithReferenceType) {
106361d0b6baf47cf411f6c0f6ddb4acffcfeec724f1Douglas Gregor      if (hasInconsistentOrSupersetQualifiersOf(Param, Arg))
1064508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
1065cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall    } else if (!IsPossiblyOpaquelyQualifiedType(Param)) {
1066508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor      if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
10671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        return Sema::TDK_NonDeducedMismatch;
1068508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    }
1069508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  }
10700b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
1071d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor  switch (Param->getTypeClass()) {
10724ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    // Non-canonical types cannot appear here.
10734ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) \
10744ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor  case Type::Class: llvm_unreachable("deducing non-canonical type: " #Class);
10754ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor#define TYPE(Class, Base)
10764ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor#include "clang/AST/TypeNodes.def"
10774ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
10784ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    case Type::TemplateTypeParm:
10794ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    case Type::SubstTemplateTypeParmPack:
10804ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      llvm_unreachable("Type nodes handled above");
10814ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
10824ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    // These types cannot be used in templates or cannot be dependent, so
10834ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    // deduction always fails.
1084199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    case Type::Builtin:
10854ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    case Type::VariableArray:
10864ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    case Type::Vector:
10874ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    case Type::FunctionNoProto:
10884ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    case Type::Record:
10894ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    case Type::Enum:
10904ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    case Type::ObjCObject:
10914ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    case Type::ObjCInterface:
10924ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    case Type::ObjCObjectPointer:
1093f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
10941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10954ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    //     _Complex T   [placeholder extension]
10964ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    case Type::Complex:
10974ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      if (const ComplexType *ComplexArg = Arg->getAs<ComplexType>())
10984ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        return DeduceTemplateArguments(S, TemplateParams,
10994ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                    cast<ComplexType>(Param)->getElementType(),
11004ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                       ComplexArg->getElementType(),
11014ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                       Info, Deduced, TDF);
11024ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
11034ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      return Sema::TDK_NonDeducedMismatch;
11044ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
1105199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T *
1106d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    case Type::Pointer: {
1107c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall      QualType PointeeType;
1108c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall      if (const PointerType *PointerArg = Arg->getAs<PointerType>()) {
1109c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall        PointeeType = PointerArg->getPointeeType();
1110c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall      } else if (const ObjCObjectPointerType *PointerArg
1111c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall                   = Arg->getAs<ObjCObjectPointerType>()) {
1112c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall        PointeeType = PointerArg->getPointeeType();
1113c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall      } else {
1114f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
1115c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall      }
11161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11174112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor      unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
1118a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
1119d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                                   cast<PointerType>(Param)->getPointeeType(),
1120c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall                                     PointeeType,
11214112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor                                     Info, Deduced, SubTDF);
1122d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    }
11231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1124199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T &
1125d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    case Type::LValueReference: {
11266217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
1127d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor      if (!ReferenceArg)
1128f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
11291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1130a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
1131d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                           cast<LValueReferenceType>(Param)->getPointeeType(),
1132d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                                     ReferenceArg->getPointeeType(),
1133508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
1134d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    }
11350b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
1136199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T && [C++0x]
1137d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    case Type::RValueReference: {
11386217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
1139d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor      if (!ReferenceArg)
1140f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
11411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1142a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
1143d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                           cast<RValueReferenceType>(Param)->getPointeeType(),
1144d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                                     ReferenceArg->getPointeeType(),
1145508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
1146d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    }
11471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1148199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T [] (implied, but not stated explicitly)
11494d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson    case Type::IncompleteArray: {
11501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const IncompleteArrayType *IncompleteArrayArg =
1151a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        S.Context.getAsIncompleteArrayType(Arg);
11524d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson      if (!IncompleteArrayArg)
1153f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
11541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1155e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall      unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
1156a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
1157a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth                     S.Context.getAsIncompleteArrayType(Param)->getElementType(),
11584d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson                                     IncompleteArrayArg->getElementType(),
1159e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall                                     Info, Deduced, SubTDF);
11604d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson    }
1161199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
1162199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T [integer-constant]
11634d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson    case Type::ConstantArray: {
11641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const ConstantArrayType *ConstantArrayArg =
1165a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        S.Context.getAsConstantArrayType(Arg);
11664d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson      if (!ConstantArrayArg)
1167f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
11681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const ConstantArrayType *ConstantArrayParm =
1170a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        S.Context.getAsConstantArrayType(Param);
11714d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson      if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
1172f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
11731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1174e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall      unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
1175a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
11764d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson                                     ConstantArrayParm->getElementType(),
11774d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson                                     ConstantArrayArg->getElementType(),
1178e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall                                     Info, Deduced, SubTDF);
11794d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson    }
11804d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson
1181199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     type [i]
1182199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    case Type::DependentSizedArray: {
1183a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg);
1184199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (!ArrayArg)
1185f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
11861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1187e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall      unsigned SubTDF = TDF & TDF_IgnoreQualifiers;
1188e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall
1189199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // Check the element type of the arrays
1190199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      const DependentSizedArrayType *DependentArrayParm
1191a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = S.Context.getAsDependentSizedArrayType(Param);
1192f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (Sema::TemplateDeductionResult Result
1193a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            = DeduceTemplateArguments(S, TemplateParams,
1194f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      DependentArrayParm->getElementType(),
1195f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      ArrayArg->getElementType(),
1196e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall                                      Info, Deduced, SubTDF))
1197f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Result;
11981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1199199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // Determine the array bound is something we can deduce.
12001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NonTypeTemplateParmDecl *NTTP
1201199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor        = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
1202199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (!NTTP)
1203f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_Success;
12041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We can perform template argument deduction for the given non-type
1206199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // template parameter.
12071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(NTTP->getDepth() == 0 &&
1208199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor             "Cannot deduce non-type template argument at depth > 0");
12091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (const ConstantArrayType *ConstantArrayArg
1210335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson            = dyn_cast<ConstantArrayType>(ArrayArg)) {
1211335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson        llvm::APSInt Size(ConstantArrayArg->getSize());
1212dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        return DeduceNonTypeTemplateArgument(S, NTTP, Size,
12139d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor                                             S.Context.getSizeType(),
121402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                             /*ArrayBound=*/true,
1215f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                             Info, Deduced);
1216335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson      }
1217199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (const DependentSizedArrayType *DependentArrayArg
1218199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor            = dyn_cast<DependentSizedArrayType>(ArrayArg))
121934c2f8c8a16226f757947bf08c5f799d99c9ac1eDouglas Gregor        if (DependentArrayArg->getSizeExpr())
122034c2f8c8a16226f757947bf08c5f799d99c9ac1eDouglas Gregor          return DeduceNonTypeTemplateArgument(S, NTTP,
122134c2f8c8a16226f757947bf08c5f799d99c9ac1eDouglas Gregor                                               DependentArrayArg->getSizeExpr(),
122234c2f8c8a16226f757947bf08c5f799d99c9ac1eDouglas Gregor                                               Info, Deduced);
12231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1224199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // Incomplete type does not match a dependently-sized array type
1225f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
1226199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    }
12271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     type(*)(T)
12291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(*)()
12301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(*)(T)
1231a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson    case Type::FunctionProto: {
123273b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor      unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList;
12331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const FunctionProtoType *FunctionProtoArg =
1234a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson        dyn_cast<FunctionProtoType>(Arg);
1235a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson      if (!FunctionProtoArg)
1236f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
12371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const FunctionProtoType *FunctionProtoParam =
1239a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson        cast<FunctionProtoType>(Param);
1240994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson
1241dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      if (FunctionProtoParam->getTypeQuals()
1242e3c7a7ca66c02567543dbb5ec493818e00e8b177Douglas Gregor            != FunctionProtoArg->getTypeQuals() ||
1243dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi          FunctionProtoParam->getRefQualifier()
1244e3c7a7ca66c02567543dbb5ec493818e00e8b177Douglas Gregor            != FunctionProtoArg->getRefQualifier() ||
1245e3c7a7ca66c02567543dbb5ec493818e00e8b177Douglas Gregor          FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
1246f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
1247994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson
1248a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson      // Check return types.
1249f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (Sema::TemplateDeductionResult Result
1250a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            = DeduceTemplateArguments(S, TemplateParams,
1251f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      FunctionProtoParam->getResultType(),
1252f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      FunctionProtoArg->getResultType(),
1253508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                      Info, Deduced, 0))
1254f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Result;
12551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1256603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor      return DeduceTemplateArguments(S, TemplateParams,
1257603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                                     FunctionProtoParam->arg_type_begin(),
1258603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                                     FunctionProtoParam->getNumArgs(),
1259603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                                     FunctionProtoArg->arg_type_begin(),
1260603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor                                     FunctionProtoArg->getNumArgs(),
126173b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor                                     Info, Deduced, SubTDF);
1262a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson    }
12631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12643cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    case Type::InjectedClassName: {
12653cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // Treat a template's injected-class-name as if the template
12663cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // specialization type had been used.
126731f17ecbef57b5679c017c375db330546b7b5145John McCall      Param = cast<InjectedClassNameType>(Param)
126831f17ecbef57b5679c017c375db330546b7b5145John McCall        ->getInjectedSpecializationType();
12693cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      assert(isa<TemplateSpecializationType>(Param) &&
12703cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall             "injected class name is not a template specialization type");
12713cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // fall through
12723cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    }
12733cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
1274f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    //     template-name<T> (where template-name refers to a class template)
1275d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor    //     template-name<i>
1276db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    //     TT<T>
1277db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    //     TT<i>
1278db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    //     TT<>
1279d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor    case Type::TemplateSpecialization: {
1280d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor      const TemplateSpecializationType *SpecParam
1281d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor        = cast<TemplateSpecializationType>(Param);
12821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1283de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      // Try to deduce template arguments from the template-id.
1284de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      Sema::TemplateDeductionResult Result
1285a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg,
1286de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                  Info, Deduced);
12871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12884a5c15f75f76b95e1c2ceb6fa2737dcadd5f4be1Douglas Gregor      if (Result && (TDF & TDF_DerivedClass)) {
1289de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        // C++ [temp.deduct.call]p3b3:
1290de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   If P is a class, and P has the form template-id, then A can be a
1291de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   derived class of the deduced A. Likewise, if P is a pointer to a
12921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        //   class of the form template-id, A can be a pointer to a derived
1293de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   class pointed to by the deduced A.
1294de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //
1295de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        // More importantly:
12961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        //   These alternatives are considered only if type deduction would
1297de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   otherwise fail.
1298a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        if (const RecordType *RecordT = Arg->getAs<RecordType>()) {
1299a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          // We cannot inspect base classes as part of deduction when the type
1300a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          // is incomplete, so either instantiate any templates necessary to
1301a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          // complete the type, or skip over it if it cannot be completed.
13025769d6195087229770d7ac90449443e026c47103John McCall          if (S.RequireCompleteType(Info.getLocation(), Arg, 0))
1303a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            return Result;
1304a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth
1305de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          // Use data recursion to crawl through the list of base classes.
13061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          // Visited contains the set of nodes we have already visited, while
1307de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          // ToVisit is our stack of records that we still need to visit.
1308de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          llvm::SmallPtrSet<const RecordType *, 8> Visited;
1309de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          llvm::SmallVector<const RecordType *, 8> ToVisit;
1310de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          ToVisit.push_back(RecordT);
1311de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          bool Successful = false;
1312053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor          llvm::SmallVectorImpl<DeducedTemplateArgument> DeducedOrig(0);
1313053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor          DeducedOrig = Deduced;
1314de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          while (!ToVisit.empty()) {
1315de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // Retrieve the next class in the inheritance hierarchy.
1316de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            const RecordType *NextT = ToVisit.back();
1317de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            ToVisit.pop_back();
13181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1319de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // If we have already seen this type, skip it.
1320de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            if (!Visited.insert(NextT))
1321de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              continue;
13221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1323de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // If this is a base class, try to perform template argument
1324de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // deduction from it.
1325de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            if (NextT != RecordT) {
1326de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              Sema::TemplateDeductionResult BaseResult
1327a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth                = DeduceTemplateArguments(S, TemplateParams, SpecParam,
1328de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                          QualType(NextT, 0), Info, Deduced);
13291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1330de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              // If template argument deduction for this base was successful,
1331053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor              // note that we had some success. Otherwise, ignore any deductions
1332053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor              // from this base class.
1333053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor              if (BaseResult == Sema::TDK_Success) {
1334de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                Successful = true;
1335053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor                DeducedOrig = Deduced;
1336053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor              }
1337053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor              else
1338053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor                Deduced = DeducedOrig;
1339de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            }
13401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1341de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // Visit base classes
1342de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
1343de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
1344de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                                 BaseEnd = Next->bases_end();
13459994a34f6cf842721ba7723edc0b9036229fe387Sebastian Redl                 Base != BaseEnd; ++Base) {
13461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump              assert(Base->getType()->isRecordType() &&
1347de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                     "Base class that isn't a record?");
13486217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek              ToVisit.push_back(Base->getType()->getAs<RecordType>());
1349de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            }
1350de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          }
13511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1352de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          if (Successful)
1353de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            return Sema::TDK_Success;
1354de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        }
13551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1356de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      }
13571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1358de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      return Result;
1359d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor    }
1360d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor
1361637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T type::*
1362637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T T::*
1363637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (type::*)()
1364637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     type (T::*)()
1365637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     type (type::*)(T)
1366637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     type (T::*)(T)
1367637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (type::*)(T)
1368637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (T::*)()
1369637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (T::*)(T)
1370637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    case Type::MemberPointer: {
1371637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
1372637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
1373637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      if (!MemPtrArg)
1374f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
1375f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
1376f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (Sema::TemplateDeductionResult Result
1377a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            = DeduceTemplateArguments(S, TemplateParams,
1378f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      MemPtrParam->getPointeeType(),
1379f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      MemPtrArg->getPointeeType(),
1380508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                      Info, Deduced,
1381508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                      TDF & TDF_IgnoreQualifiers))
1382f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Result;
1383f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
1384a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
1385f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                     QualType(MemPtrParam->getClass(), 0),
1386f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                     QualType(MemPtrArg->getClass(), 0),
1387508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
1388637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    }
1389637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor
13909a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    //     (clang extension)
13919a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    //
13921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     type(^)(T)
13931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(^)()
13941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(^)(T)
1395859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson    case Type::BlockPointer: {
1396859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson      const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
1397859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson      const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
13981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1399859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson      if (!BlockPtrArg)
1400f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
14011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1402a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
1403859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson                                     BlockPtrParam->getPointeeType(),
1404f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                     BlockPtrArg->getPointeeType(), Info,
1405508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Deduced, 0);
1406859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson    }
1407859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson
14084ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    //     (clang extension)
14094ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    //
14104ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    //     T __attribute__(((ext_vector_type(<integral constant>))))
14114ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    case Type::ExtVector: {
14124ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      const ExtVectorType *VectorParam = cast<ExtVectorType>(Param);
14134ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) {
14144ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        // Make sure that the vectors have the same number of elements.
14154ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        if (VectorParam->getNumElements() != VectorArg->getNumElements())
14164ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor          return Sema::TDK_NonDeducedMismatch;
14174ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
14184ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        // Perform deduction on the element types.
14194ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        return DeduceTemplateArguments(S, TemplateParams,
14204ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                       VectorParam->getElementType(),
14214ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                       VectorArg->getElementType(),
14224ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                       Info, Deduced,
14234ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                       TDF);
14244ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      }
14254ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
14264ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      if (const DependentSizedExtVectorType *VectorArg
14274ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                = dyn_cast<DependentSizedExtVectorType>(Arg)) {
14284ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        // We can't check the number of elements, since the argument has a
14294ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        // dependent number of elements. This can only occur during partial
14304ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        // ordering.
14314ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
14324ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        // Perform deduction on the element types.
14334ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        return DeduceTemplateArguments(S, TemplateParams,
14344ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                       VectorParam->getElementType(),
14354ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                       VectorArg->getElementType(),
14364ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                       Info, Deduced,
14374ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                       TDF);
14384ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      }
14394ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
14404ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      return Sema::TDK_NonDeducedMismatch;
14414ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    }
14424ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
14434ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    //     (clang extension)
14444ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    //
14454ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    //     T __attribute__(((ext_vector_type(N))))
14464ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    case Type::DependentSizedExtVector: {
14474ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      const DependentSizedExtVectorType *VectorParam
14484ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        = cast<DependentSizedExtVectorType>(Param);
14494ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
14504ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) {
14514ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        // Perform deduction on the element types.
14524ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        if (Sema::TemplateDeductionResult Result
14534ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor              = DeduceTemplateArguments(S, TemplateParams,
14544ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                        VectorParam->getElementType(),
14554ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                        VectorArg->getElementType(),
14564ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                        Info, Deduced,
14574ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                        TDF))
14584ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor          return Result;
14594ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
14604ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        // Perform deduction on the vector size, if we can.
14614ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        NonTypeTemplateParmDecl *NTTP
14624ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor          = getDeducedParameterFromExpr(VectorParam->getSizeExpr());
14634ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        if (!NTTP)
14644ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor          return Sema::TDK_Success;
14654ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
14664ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false);
14674ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        ArgSize = VectorArg->getNumElements();
14684ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        return DeduceNonTypeTemplateArgument(S, NTTP, ArgSize, S.Context.IntTy,
14694ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                             false, Info, Deduced);
14704ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      }
14714ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
14724ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      if (const DependentSizedExtVectorType *VectorArg
14734ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                = dyn_cast<DependentSizedExtVectorType>(Arg)) {
14744ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        // Perform deduction on the element types.
14754ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        if (Sema::TemplateDeductionResult Result
14764ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor            = DeduceTemplateArguments(S, TemplateParams,
14774ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                      VectorParam->getElementType(),
14784ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                      VectorArg->getElementType(),
14794ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                      Info, Deduced,
14804ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                      TDF))
14814ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor          return Result;
14824ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
14834ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        // Perform deduction on the vector size, if we can.
14844ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        NonTypeTemplateParmDecl *NTTP
14854ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor          = getDeducedParameterFromExpr(VectorParam->getSizeExpr());
14864ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        if (!NTTP)
14874ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor          return Sema::TDK_Success;
14884ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
14894ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor        return DeduceNonTypeTemplateArgument(S, NTTP, VectorArg->getSizeExpr(),
14904ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor                                             Info, Deduced);
14914ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      }
14924ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
14934ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor      return Sema::TDK_NonDeducedMismatch;
14944ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    }
14954ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor
1496637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    case Type::TypeOfExpr:
1497637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    case Type::TypeOf:
14984714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    case Type::DependentName:
14994ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    case Type::UnresolvedUsing:
15004ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    case Type::Decltype:
15014ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    case Type::UnaryTransform:
15024ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    case Type::Auto:
15034ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    case Type::DependentTemplateSpecialization:
15044ac01401b1ec602a1f58c217544d3dcb5fcbd7f1Douglas Gregor    case Type::PackExpansion:
1505637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      // No template argument deduction for these types
1506f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_Success;
15070b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
15080b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
1509f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  return Sema::TDK_Success;
15100b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
15110b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
1512f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
1513a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
1514f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateParameterList *TemplateParams,
1515f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        const TemplateArgument &Param,
151677d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor                        TemplateArgument Arg,
15172a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                        TemplateDeductionInfo &Info,
151802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
151977d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor  // If the template argument is a pack expansion, perform template argument
152077d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor  // deduction against the pattern of that expansion. This only occurs during
152177d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor  // partial ordering.
152277d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor  if (Arg.isPackExpansion())
152377d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor    Arg = Arg.getPackExpansionPattern();
1524dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
15250b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  switch (Param.getKind()) {
1526199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Null:
1527199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    assert(false && "Null template argument in parameter list");
1528199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    break;
15291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case TemplateArgument::Type:
1531788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    if (Arg.getKind() == TemplateArgument::Type)
1532a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams, Param.getAsType(),
1533788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                                     Arg.getAsType(), Info, Deduced, 0);
1534788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.FirstArg = Param;
1535788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.SecondArg = Arg;
1536788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
1537a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
1538788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  case TemplateArgument::Template:
1539db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    if (Arg.getKind() == TemplateArgument::Template)
1540dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      return DeduceTemplateArguments(S, TemplateParams,
1541788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                                     Param.getAsTemplate(),
1542db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor                                     Arg.getAsTemplate(), Info, Deduced);
1543788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.FirstArg = Param;
1544788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.SecondArg = Arg;
1545788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
1546a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
1547a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor  case TemplateArgument::TemplateExpansion:
1548a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    llvm_unreachable("caller should handle pack expansions");
1549a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    break;
1550dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1551199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Declaration:
1552788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    if (Arg.getKind() == TemplateArgument::Declaration &&
1553788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor        Param.getAsDecl()->getCanonicalDecl() ==
1554788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor          Arg.getAsDecl()->getCanonicalDecl())
1555788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return Sema::TDK_Success;
1556dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1557f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.FirstArg = Param;
1558f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.SecondArg = Arg;
1559f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_NonDeducedMismatch;
15601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1561199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Integral:
1562199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    if (Arg.getKind() == TemplateArgument::Integral) {
15639d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor      if (hasSameExtendedValue(*Param.getAsIntegral(), *Arg.getAsIntegral()))
1564f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_Success;
1565f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
1566f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.FirstArg = Param;
1567f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.SecondArg = Arg;
1568f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
1569f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    }
1570f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
1571f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    if (Arg.getKind() == TemplateArgument::Expression) {
1572f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.FirstArg = Param;
1573f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.SecondArg = Arg;
1574f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
1575199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    }
1576199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
1577f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.FirstArg = Param;
1578f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.SecondArg = Arg;
1579f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_NonDeducedMismatch;
15801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1581199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Expression: {
15821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (NonTypeTemplateParmDecl *NTTP
1583199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor          = getDeducedParameterFromExpr(Param.getAsExpr())) {
1584199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (Arg.getKind() == TemplateArgument::Integral)
1585a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP,
15861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             *Arg.getAsIntegral(),
15879d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor                                             Arg.getIntegralType(),
158802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                             /*ArrayBound=*/false,
1589f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                             Info, Deduced);
1590199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (Arg.getKind() == TemplateArgument::Expression)
1591a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(),
1592f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                             Info, Deduced);
159315755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor      if (Arg.getKind() == TemplateArgument::Declaration)
1594a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(),
159515755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor                                             Info, Deduced);
1596dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1597f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.FirstArg = Param;
1598f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.SecondArg = Arg;
1599f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
1600199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    }
16011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1602199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    // Can't deduce anything, but that's okay.
1603f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_Success;
1604199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  }
1605d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson  case TemplateArgument::Pack:
160620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor    llvm_unreachable("Argument packs should be expanded by the caller!");
16070b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
16081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1609f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  return Sema::TDK_Success;
16100b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
16110b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
161220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// \brief Determine whether there is a template argument to be used for
161320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// deduction.
161420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor///
161520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// This routine "expands" argument packs in-place, overriding its input
161620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// parameters so that \c Args[ArgIdx] will be the available template argument.
161720a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor///
161820a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// \returns true if there is another template argument (which will be at
161920a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// \c Args[ArgIdx]), false otherwise.
1620dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic bool hasTemplateArgumentForDeduction(const TemplateArgument *&Args,
162120a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                                            unsigned &ArgIdx,
162220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                                            unsigned &NumArgs) {
162320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  if (ArgIdx == NumArgs)
162420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor    return false;
1625dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
162620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  const TemplateArgument &Arg = Args[ArgIdx];
162720a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  if (Arg.getKind() != TemplateArgument::Pack)
162820a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor    return true;
162920a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor
163020a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  assert(ArgIdx == NumArgs - 1 && "Pack not at the end of argument list?");
163120a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  Args = Arg.pack_begin();
163220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  NumArgs = Arg.pack_size();
163320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  ArgIdx = 0;
163420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  return ArgIdx < NumArgs;
163520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor}
163620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor
16377b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor/// \brief Determine whether the given set of template arguments has a pack
16387b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor/// expansion that is not the last template argument.
16397b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregorstatic bool hasPackExpansionBeforeEnd(const TemplateArgument *Args,
16407b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor                                      unsigned NumArgs) {
16417b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor  unsigned ArgIdx = 0;
16427b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor  while (ArgIdx < NumArgs) {
16437b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    const TemplateArgument &Arg = Args[ArgIdx];
1644dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
16457b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    // Unwrap argument packs.
16467b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    if (Args[ArgIdx].getKind() == TemplateArgument::Pack) {
16477b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      Args = Arg.pack_begin();
16487b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      NumArgs = Arg.pack_size();
16497b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      ArgIdx = 0;
16507b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      continue;
16517b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    }
1652dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
16537b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    ++ArgIdx;
16547b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    if (ArgIdx == NumArgs)
16557b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      return false;
1656dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
16577b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    if (Arg.isPackExpansion())
16587b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      return true;
16597b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor  }
1660dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
16617b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor  return false;
16627b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor}
16637b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor
16641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic Sema::TemplateDeductionResult
1665a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
1666f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateParameterList *TemplateParams,
166720a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        const TemplateArgument *Params, unsigned NumParams,
166820a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        const TemplateArgument *Args, unsigned NumArgs,
16692a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                        TemplateDeductionInfo &Info,
16700972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
16710972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor                        bool NumberOfArgumentsMustMatch) {
1672e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor  // C++0x [temp.deduct.type]p9:
1673dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   If the template argument list of P contains a pack expansion that is not
1674dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   the last template argument, the entire template argument list is a
1675e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor  //   non-deduced context.
16767b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor  if (hasPackExpansionBeforeEnd(Params, NumParams))
16777b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    return Sema::TDK_Success;
1678dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1679e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor  // C++0x [temp.deduct.type]p9:
1680dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   If P has a form that contains <T> or <i>, then each argument Pi of the
1681dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   respective template argument list P is compared with the corresponding
1682e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor  //   argument Ai of the corresponding template argument list of A.
168320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  unsigned ArgIdx = 0, ParamIdx = 0;
1684dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  for (; hasTemplateArgumentForDeduction(Params, ParamIdx, NumParams);
168520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor       ++ParamIdx) {
168620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor    if (!Params[ParamIdx].isPackExpansion()) {
1687e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      // The simple case: deduce template arguments by matching Pi and Ai.
1688dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
168920a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor      // Check whether we have enough arguments.
169020a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor      if (!hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs))
16913cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor        return NumberOfArgumentsMustMatch? Sema::TDK_NonDeducedMismatch
16920972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor                                         : Sema::TDK_Success;
1693dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
169477d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor      if (Args[ArgIdx].isPackExpansion()) {
169577d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor        // FIXME: We follow the logic of C++0x [temp.deduct.type]p22 here,
169677d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor        // but applied to pack expansions that are template arguments.
169777d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
169877d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor      }
1699dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1700e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      // Perform deduction for this Pi/Ai pair.
170120a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor      if (Sema::TemplateDeductionResult Result
170277d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor            = DeduceTemplateArguments(S, TemplateParams,
170377d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor                                      Params[ParamIdx], Args[ArgIdx],
170477d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor                                      Info, Deduced))
1705dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        return Result;
1706dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
170720a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor      // Move to the next argument.
170820a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor      ++ArgIdx;
170920a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor      continue;
171020a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor    }
1711dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1712e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // The parameter is a pack expansion.
1713dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1714e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // C++0x [temp.deduct.type]p9:
1715dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   If Pi is a pack expansion, then the pattern of Pi is compared with
1716dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   each remaining argument in the template argument list of A. Each
1717dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   comparison deduces template arguments for subsequent positions in the
1718e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    //   template parameter packs expanded by Pi.
1719e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern();
1720dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1721e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // Compute the set of template parameter indices that correspond to
1722e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // parameter packs expanded by the pack expansion.
1723e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    llvm::SmallVector<unsigned, 2> PackIndices;
1724e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    {
1725e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      llvm::BitVector SawIndices(TemplateParams->size());
1726e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
1727e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      S.collectUnexpandedParameterPacks(Pattern, Unexpanded);
1728e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
1729e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor        unsigned Depth, Index;
1730e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor        llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]);
1731e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor        if (Depth == 0 && !SawIndices[Index]) {
1732e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor          SawIndices[Index] = true;
1733e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor          PackIndices.push_back(Index);
1734e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor        }
1735e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      }
1736e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    }
1737e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?");
1738dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1739e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // FIXME: If there are no remaining arguments, we can bail out early
1740e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // and set any deduced parameter packs to an empty argument pack.
1741e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // The latter part of this is a (minor) correctness issue.
1742dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1743e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // Save the deduced template arguments for each parameter pack expanded
1744e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // by this pack expansion, then clear out the deduction.
1745dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    llvm::SmallVector<DeducedTemplateArgument, 2>
1746e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      SavedPacks(PackIndices.size());
17475429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor    llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2>
17485429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor      NewlyDeducedPacks(PackIndices.size());
1749dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    PrepareArgumentPackDeduction(S, Deduced, PackIndices, SavedPacks,
17505429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor                                 NewlyDeducedPacks);
1751e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor
1752e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // Keep track of the deduced template arguments for each parameter pack
1753dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    // expanded by this pack expansion (the outer index) and for each
1754e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // template argument (the inner SmallVectors).
1755e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    bool HasAnyArguments = false;
1756e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    while (hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs)) {
1757e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      HasAnyArguments = true;
1758dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1759e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      // Deduce template arguments from the pattern.
1760dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      if (Sema::TemplateDeductionResult Result
1761e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor            = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx],
1762e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                                      Info, Deduced))
1763e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor        return Result;
1764dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1765e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      // Capture the deduced template arguments for each parameter pack expanded
1766e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      // by this pack expansion, add them to the list of arguments we've deduced
1767e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      // for that pack, then clear out the deduced argument.
1768e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
1769e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor        DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]];
1770e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor        if (!DeducedArg.isNull()) {
1771e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor          NewlyDeducedPacks[I].push_back(DeducedArg);
1772e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor          DeducedArg = DeducedTemplateArgument();
1773e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor        }
1774e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      }
1775dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1776e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      ++ArgIdx;
1777e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    }
1778dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1779e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // Build argument packs for each of the parameter packs expanded by this
1780e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    // pack expansion.
17810216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    if (Sema::TemplateDeductionResult Result
1782dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi          = FinishArgumentPackDeduction(S, TemplateParams, HasAnyArguments,
17830216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                                        Deduced, PackIndices, SavedPacks,
17840216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                                        NewlyDeducedPacks, Info))
1785dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      return Result;
17860b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
1787dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
178820a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor  // If there is an argument remaining, then we had too many arguments.
17890972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor  if (NumberOfArgumentsMustMatch &&
17900972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor      hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs))
17913cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor    return Sema::TDK_NonDeducedMismatch;
1792dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1793f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  return Sema::TDK_Success;
17940b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
17950b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
179620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregorstatic Sema::TemplateDeductionResult
179720a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas GregorDeduceTemplateArguments(Sema &S,
179820a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        TemplateParameterList *TemplateParams,
179920a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        const TemplateArgumentList &ParamList,
180020a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        const TemplateArgumentList &ArgList,
180120a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                        TemplateDeductionInfo &Info,
180220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
1803dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  return DeduceTemplateArguments(S, TemplateParams,
180420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                                 ParamList.data(), ParamList.size(),
180520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                                 ArgList.data(), ArgList.size(),
180620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor                                 Info, Deduced);
180720a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor}
180820a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor
1809f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// \brief Determine whether two template arguments are the same.
18101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool isSameTemplateArg(ASTContext &Context,
1811f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor                              const TemplateArgument &X,
1812f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor                              const TemplateArgument &Y) {
1813f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  if (X.getKind() != Y.getKind())
1814f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    return false;
18151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1816f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  switch (X.getKind()) {
1817f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Null:
1818f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      assert(false && "Comparing NULL template argument");
1819f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      break;
18201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1821f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Type:
1822f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      return Context.getCanonicalType(X.getAsType()) ==
1823f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor             Context.getCanonicalType(Y.getAsType());
18241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1825f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Declaration:
182697fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis      return X.getAsDecl()->getCanonicalDecl() ==
182797fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis             Y.getAsDecl()->getCanonicalDecl();
18281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1829788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Template:
1830a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor    case TemplateArgument::TemplateExpansion:
1831a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor      return Context.getCanonicalTemplateName(
1832a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                    X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() ==
1833a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor             Context.getCanonicalTemplateName(
1834a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor                    Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer();
1835dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1836f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Integral:
1837f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      return *X.getAsIntegral() == *Y.getAsIntegral();
18381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1839788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Expression: {
1840788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      llvm::FoldingSetNodeID XID, YID;
1841788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      X.getAsExpr()->Profile(XID, Context, true);
1842dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      Y.getAsExpr()->Profile(YID, Context, true);
1843788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return XID == YID;
1844788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    }
18451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1846f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Pack:
1847f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      if (X.pack_size() != Y.pack_size())
1848f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor        return false;
18491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
18501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (TemplateArgument::pack_iterator XP = X.pack_begin(),
18511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        XPEnd = X.pack_end(),
1852f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor                                           YP = Y.pack_begin();
18531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump           XP != XPEnd; ++XP, ++YP)
1854f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor        if (!isSameTemplateArg(Context, *XP, *YP))
1855f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor          return false;
1856f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
1857f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      return true;
1858f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  }
1859f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
1860f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  return false;
1861f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor}
1862f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
186354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// \brief Allocate a TemplateArgumentLoc where all locations have
186454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// been initialized to the given location.
186554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor///
186654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// \param S The semantic analysis object.
186754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor///
186854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// \param The template argument we are producing template argument
186954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// location information for.
187054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor///
187154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// \param NTTPType For a declaration template argument, the type of
187254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// the non-type template parameter that corresponds to this template
187354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// argument.
187454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor///
187554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// \param Loc The source location to use for the resulting template
187654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// argument.
1877dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic TemplateArgumentLoc
187854c53cca105ed595e12fecf04e415c3712bda936Douglas GregorgetTrivialTemplateArgumentLoc(Sema &S,
1879dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                              const TemplateArgument &Arg,
188054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                              QualType NTTPType,
188154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                              SourceLocation Loc) {
188254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  switch (Arg.getKind()) {
188354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  case TemplateArgument::Null:
188454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    llvm_unreachable("Can't get a NULL template argument here");
188554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    break;
1886dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
188754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  case TemplateArgument::Type:
1888dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    return TemplateArgumentLoc(Arg,
188954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                     S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
1890dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
189154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  case TemplateArgument::Declaration: {
189254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    Expr *E
1893ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor      = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc)
189454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    .takeAs<Expr>();
189554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    return TemplateArgumentLoc(TemplateArgument(E), E);
189654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  }
1897dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
189854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  case TemplateArgument::Integral: {
189954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    Expr *E
1900ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor      = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).takeAs<Expr>();
190154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    return TemplateArgumentLoc(TemplateArgument(E), E);
190254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  }
1903dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1904b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor    case TemplateArgument::Template:
1905b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor    case TemplateArgument::TemplateExpansion: {
1906b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor      NestedNameSpecifierLocBuilder Builder;
1907b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor      TemplateName Template = Arg.getAsTemplate();
1908b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor      if (DependentTemplateName *DTN = Template.getAsDependentTemplateName())
1909b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor        Builder.MakeTrivial(S.Context, DTN->getQualifier(), Loc);
1910b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor      else if (QualifiedTemplateName *QTN = Template.getAsQualifiedTemplateName())
1911b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor        Builder.MakeTrivial(S.Context, QTN->getQualifier(), Loc);
1912b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor
1913b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor      if (Arg.getKind() == TemplateArgument::Template)
1914b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor        return TemplateArgumentLoc(Arg,
1915b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor                                   Builder.getWithLocInContext(S.Context),
1916b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor                                   Loc);
1917b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor
1918b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor
1919b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor      return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(S.Context),
1920b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor                                 Loc, Loc);
1921b6744efecba58792cce20d2d7b9ee39927c5422eDouglas Gregor    }
1922a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor
192354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  case TemplateArgument::Expression:
192454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    return TemplateArgumentLoc(Arg, Arg.getAsExpr());
1925dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
192654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  case TemplateArgument::Pack:
192754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo());
192854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  }
1929dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
193054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  return TemplateArgumentLoc();
193154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor}
193254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor
193354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor
193454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// \brief Convert the given deduced template argument and add it to the set of
193554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// fully-converted template arguments.
1936dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumistatic bool ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param,
193754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                           DeducedTemplateArgument Arg,
1938dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                           NamedDecl *Template,
1939dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                           QualType NTTPType,
19406952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                           unsigned ArgumentPackIndex,
194154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                           TemplateDeductionInfo &Info,
194254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                           bool InFunctionTemplate,
194354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                             llvm::SmallVectorImpl<TemplateArgument> &Output) {
194454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  if (Arg.getKind() == TemplateArgument::Pack) {
194554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    // This is a template argument pack, so check each of its arguments against
194654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    // the template parameter.
194754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    llvm::SmallVector<TemplateArgument, 2> PackedArgsBuilder;
1948dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    for (TemplateArgument::pack_iterator PA = Arg.pack_begin(),
1949135ffa7375fb5802b92f42774e02d0e6e4c78e5bDouglas Gregor                                      PAEnd = Arg.pack_end();
195054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor         PA != PAEnd; ++PA) {
1951d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor      // When converting the deduced template argument, append it to the
1952d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor      // general output list. We need to do this so that the template argument
1953d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor      // checking logic has all of the prior template arguments available.
195454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor      DeducedTemplateArgument InnerArg(*PA);
195554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor      InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound());
1956dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      if (ConvertDeducedTemplateArgument(S, Param, InnerArg, Template,
19576952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                         NTTPType, PackedArgsBuilder.size(),
19586952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                         Info, InFunctionTemplate, Output))
195954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor        return true;
1960dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
1961d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor      // Move the converted template argument into our argument pack.
1962d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor      PackedArgsBuilder.push_back(Output.back());
1963d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor      Output.pop_back();
196454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    }
1965dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
196654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    // Create the resulting argument pack.
1967dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    Output.push_back(TemplateArgument::CreatePackCopy(S.Context,
1968203e6a322ae29d577acafcb1572a57ec16e1e730Douglas Gregor                                                      PackedArgsBuilder.data(),
1969203e6a322ae29d577acafcb1572a57ec16e1e730Douglas Gregor                                                     PackedArgsBuilder.size()));
197054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    return false;
197154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  }
1972dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
197354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  // Convert the deduced template argument into a template
197454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  // argument that we can check, almost as if the user had written
197554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  // the template argument explicitly.
197654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  TemplateArgumentLoc ArgLoc = getTrivialTemplateArgumentLoc(S, Arg, NTTPType,
197754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                                             Info.getLocation());
1978dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
197954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  // Check the template argument, converting it as necessary.
198054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  return S.CheckTemplateArgument(Param, ArgLoc,
198154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                 Template,
198254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                 Template->getLocation(),
198354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                 Template->getSourceRange().getEnd(),
19846952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                 ArgumentPackIndex,
198554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                 Output,
198654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                 InFunctionTemplate
198754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                  ? (Arg.wasDeducedFromArrayBound()
1988dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                       ? Sema::CTAK_DeducedFromArrayBound
198954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                       : Sema::CTAK_Deduced)
199054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                 : Sema::CTAK_Specified);
199154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor}
199254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor
199331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// Complete template argument deduction for a class template partial
199431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// specialization.
199531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregorstatic Sema::TemplateDeductionResult
1996dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA TakumiFinishTemplateArgumentDeduction(Sema &S,
199731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                ClassTemplatePartialSpecializationDecl *Partial,
199831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                const TemplateArgumentList &TemplateArgs,
199931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                      llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
20002a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall                                TemplateDeductionInfo &Info) {
200131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // Trap errors.
200231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  Sema::SFINAETrap Trap(S);
2003dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
200431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  Sema::ContextRAII SavedContext(S, Partial);
2005f5813826802c2e76cdc13cae834ebfd4518d74a6John McCall
200602cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // C++ [temp.deduct.type]p2:
200702cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  //   [...] or if any template argument remains neither deduced nor
200802cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  //   explicitly specified, template argument deduction fails.
2009910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor  llvm::SmallVector<TemplateArgument, 4> Builder;
2010033a3cad525e5b221ba035ffc3e0abfa1241d90eDouglas Gregor  TemplateParameterList *PartialParams = Partial->getTemplateParameters();
2011033a3cad525e5b221ba035ffc3e0abfa1241d90eDouglas Gregor  for (unsigned I = 0, N = PartialParams->size(); I != N; ++I) {
201254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    NamedDecl *Param = PartialParams->getParam(I);
2013f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    if (Deduced[I].isNull()) {
201454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor      Info.Param = makeTemplateParameter(Param);
201531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor      return Sema::TDK_Incomplete;
2016f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    }
2017dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
201854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    // We have deduced this argument, so it still needs to be
201954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    // checked and converted.
2020dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
202154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    // First, for a non-type template parameter type that is
202254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    // initialized by a declaration, we need the type of the
202354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    // corresponding non-type template parameter.
202454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    QualType NTTPType;
2025dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    if (NonTypeTemplateParmDecl *NTTP
2026d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor                                  = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
202754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor      NTTPType = NTTP->getType();
2028d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor      if (NTTPType->isDependentType()) {
2029dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
2030d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor                                          Builder.data(), Builder.size());
2031d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor        NTTPType = S.SubstType(NTTPType,
2032d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor                               MultiLevelTemplateArgumentList(TemplateArgs),
2033d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor                               NTTP->getLocation(),
2034d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor                               NTTP->getDeclName());
2035d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor        if (NTTPType.isNull()) {
2036d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor          Info.Param = makeTemplateParameter(Param);
2037d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor          // FIXME: These template arguments are temporary. Free them!
2038dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi          Info.reset(TemplateArgumentList::CreateCopy(S.Context,
2039dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                      Builder.data(),
2040d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor                                                      Builder.size()));
2041d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor          return Sema::TDK_SubstitutionFailure;
2042d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor        }
2043d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor      }
2044d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor    }
2045d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor
204654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    if (ConvertDeducedTemplateArgument(S, Param, Deduced[I],
20476952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                       Partial, NTTPType, 0, Info, false,
204854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                       Builder)) {
204954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor      Info.Param = makeTemplateParameter(Param);
205054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor      // FIXME: These template arguments are temporary. Free them!
2051dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
2052dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                  Builder.size()));
205354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor      return Sema::TDK_SubstitutionFailure;
205454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    }
205502cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  }
2056dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
205702cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // Form the template argument list from the deduced template arguments.
20581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentList *DeducedArgumentList
2059dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    = TemplateArgumentList::CreateCopy(S.Context, Builder.data(),
2060910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                       Builder.size());
2061910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor
2062f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  Info.reset(DeducedArgumentList);
206302cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor
206402cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // Substitute the deduced template arguments into the template
206502cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // arguments of the class template partial specialization, and
206602cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // verify that the instantiated template arguments are both valid
206702cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // and are equivalent to the template arguments originally provided
20681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to the class template.
20692a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope InstScope(S);
207002cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
2071833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  const TemplateArgumentLoc *PartialTemplateArgs
2072833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    = Partial->getTemplateArgsAsWritten();
2073d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
2074d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  // Note that we don't provide the langle and rangle locations.
2075d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstArgs;
2076d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
2077e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor  if (S.Subst(PartialTemplateArgs,
2078e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor              Partial->getNumTemplateArgsAsWritten(),
2079e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor              InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
2080e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx;
2081e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    if (ParamIdx >= Partial->getTemplateParameters()->size())
2082e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      ParamIdx = Partial->getTemplateParameters()->size() - 1;
2083e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor
2084e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    Decl *Param
2085e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor      = const_cast<NamedDecl *>(
2086e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor                          Partial->getTemplateParameters()->getParam(ParamIdx));
2087e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    Info.Param = makeTemplateParameter(Param);
2088e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument();
2089e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor    return Sema::TDK_SubstitutionFailure;
2090833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  }
2091833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
2092910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor  llvm::SmallVector<TemplateArgument, 4> ConvertedInstArgs;
209331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
209454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                  InstArgs, false, ConvertedInstArgs))
209531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return Sema::TDK_SubstitutionFailure;
2096dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
209754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  TemplateParameterList *TemplateParams
209854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    = ClassTemplate->getTemplateParameters();
209954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) {
2100910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor    TemplateArgument InstArg = ConvertedInstArgs.data()[I];
210131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) {
21022fdc5e8199e1e239620f2faae88997153703e16fDouglas Gregor      Info.Param = makeTemplateParameter(TemplateParams->getParam(I));
2103f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Info.FirstArg = TemplateArgs[I];
2104f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Info.SecondArg = InstArg;
210531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor      return Sema::TDK_NonDeducedMismatch;
2106f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    }
210702cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  }
210802cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor
2109bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor  if (Trap.hasErrorOccurred())
211031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return Sema::TDK_SubstitutionFailure;
2111bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor
211231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  return Sema::TDK_Success;
211331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor}
211431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor
211531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// \brief Perform template argument deduction to determine whether
211631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// the given template arguments match the given class template
211731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// partial specialization per C++ [temp.class.spec.match].
211831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas GregorSema::TemplateDeductionResult
211931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas GregorSema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
212031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                              const TemplateArgumentList &TemplateArgs,
212131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                              TemplateDeductionInfo &Info) {
212231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // C++ [temp.class.spec.match]p2:
212331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  //   A partial specialization matches a given actual template
212431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  //   argument list if the template arguments of the partial
212531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  //   specialization can be deduced from the actual template argument
212631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  //   list (14.8.2).
212731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  SFINAETrap Trap(*this);
212831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
212931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  Deduced.resize(Partial->getTemplateParameters()->size());
213031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  if (TemplateDeductionResult Result
213131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor        = ::DeduceTemplateArguments(*this,
213231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                    Partial->getTemplateParameters(),
213331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                    Partial->getTemplateArgs(),
213431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                    TemplateArgs, Info, Deduced))
213531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return Result;
213631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor
213731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
21389b623639378d53a675921ddfa7316034d571881eDouglas Gregor                             Deduced.data(), Deduced.size(), Info);
213931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  if (Inst)
214031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return TDK_InstantiationDepth;
214131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor
214231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  if (Trap.hasErrorOccurred())
214331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return Sema::TDK_SubstitutionFailure;
2144dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2145dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs,
214631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                           Deduced, Info);
21470b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
2148031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
21494112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor/// \brief Determine whether the given type T is a simple-template-id type.
21504112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregorstatic bool isSimpleTemplateIdType(QualType T) {
21511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const TemplateSpecializationType *Spec
2152183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall        = T->getAs<TemplateSpecializationType>())
21534112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    return Spec->getTemplateName().getAsTemplateDecl() != 0;
21541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
21554112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor  return false;
21564112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor}
215783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
215883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \brief Substitute the explicitly-provided template arguments into the
215983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// given function template according to C++ [temp.arg.explicit].
216083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
216183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionTemplate the function template into which the explicit
216283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template arguments will be substituted.
216383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
21641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ExplicitTemplateArguments the explicitly-specified template
216583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// arguments.
216683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
21671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param Deduced the deduced template arguments, which will be populated
216883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// with the converted and checked explicit template arguments.
216983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
21701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ParamTypes will be populated with the instantiated function
217183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// parameters.
217283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
217383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionType if non-NULL, the result type of the function template
217483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// will also be instantiated and the pointed-to value will be updated with
217583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// the instantiated function type.
217683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
217783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Info if substitution fails for any reason, this object will be
217883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// populated with more information about the failure.
217983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
218083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \returns TDK_Success if substitution was successful, or some failure
218183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// condition.
218283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::TemplateDeductionResult
218383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::SubstituteExplicitTemplateArguments(
218483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      FunctionTemplateDecl *FunctionTemplate,
218567714230a191bc3c01f33378f34f34ef377991a6Douglas Gregor                               TemplateArgumentListInfo &ExplicitTemplateArgs,
218602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                       llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
218783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                 llvm::SmallVectorImpl<QualType> &ParamTypes,
218883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          QualType *FunctionType,
218983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          TemplateDeductionInfo &Info) {
219083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
219183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateParameterList *TemplateParams
219283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = FunctionTemplate->getTemplateParameters();
219383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
2194d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (ExplicitTemplateArgs.size() == 0) {
219583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    // No arguments to substitute; just copy over the parameter types and
219683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    // fill in the function type.
219783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    for (FunctionDecl::param_iterator P = Function->param_begin(),
219883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                   PEnd = Function->param_end();
219983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor         P != PEnd;
220083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor         ++P)
220183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      ParamTypes.push_back((*P)->getType());
22021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
220383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (FunctionType)
220483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      *FunctionType = Function->getType();
220583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_Success;
220683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
22071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
220883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Substitution of the explicit template arguments into a function template
220983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  /// is a SFINAE context. Trap any errors that might occur.
22101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SFINAETrap Trap(*this);
22111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
221283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // C++ [temp.arg.explicit]p3:
22131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   Template arguments that are present shall be specified in the
22141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   declaration order of their corresponding template-parameters. The
221583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   template argument list shall not specify more template-arguments than
22161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   there are corresponding template-parameters.
2217910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor  llvm::SmallVector<TemplateArgument, 4> Builder;
22181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Enter a new template instantiation context where we check the
222083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // explicitly-specified template arguments against this function template,
222183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // and then substitute them into the function parameter types.
22221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
222383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                             FunctionTemplate, Deduced.data(), Deduced.size(),
22249b623639378d53a675921ddfa7316034d571881eDouglas Gregor           ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution,
22259b623639378d53a675921ddfa7316034d571881eDouglas Gregor                             Info);
222683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (Inst)
222783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_InstantiationDepth;
22281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
222983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (CheckTemplateArgumentList(FunctionTemplate,
223083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                SourceLocation(),
2231d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                ExplicitTemplateArgs,
223283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                true,
2233f1a8445036a2d047c7165d4170e3058cdeaba6ebDouglas Gregor                                Builder) || Trap.hasErrorOccurred()) {
2234910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor    unsigned Index = Builder.size();
2235fe52c91dcd1ecda90b579f789baf70f7a992e3c9Douglas Gregor    if (Index >= TemplateParams->size())
2236fe52c91dcd1ecda90b579f789baf70f7a992e3c9Douglas Gregor      Index = TemplateParams->size() - 1;
2237fe52c91dcd1ecda90b579f789baf70f7a992e3c9Douglas Gregor    Info.Param = makeTemplateParameter(TemplateParams->getParam(Index));
223883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_InvalidExplicitArguments;
2239f1a8445036a2d047c7165d4170e3058cdeaba6ebDouglas Gregor  }
22401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
224183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Form the template argument list from the explicitly-specified
224283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // template arguments.
22431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentList *ExplicitArgumentList
2244910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor    = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size());
224583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Info.reset(ExplicitArgumentList);
2246dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2247df41f18936693f7c62e457eefb9fad5b2d2fe3cdJohn McCall  // Template argument deduction and the final substitution should be
2248df41f18936693f7c62e457eefb9fad5b2d2fe3cdJohn McCall  // done in the context of the templated declaration.  Explicit
2249df41f18936693f7c62e457eefb9fad5b2d2fe3cdJohn McCall  // argument substitution, on the other hand, needs to happen in the
2250df41f18936693f7c62e457eefb9fad5b2d2fe3cdJohn McCall  // calling context.
2251df41f18936693f7c62e457eefb9fad5b2d2fe3cdJohn McCall  ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
2252df41f18936693f7c62e457eefb9fad5b2d2fe3cdJohn McCall
2253dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // If we deduced template arguments for a template parameter pack,
2254d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  // note that the template argument pack is partially substituted and record
2255d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  // the explicit template arguments. They'll be used as part of deduction
2256d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  // for this template parameter pack.
2257d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  for (unsigned I = 0, N = Builder.size(); I != N; ++I) {
2258d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    const TemplateArgument &Arg = Builder[I];
2259d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    if (Arg.getKind() == TemplateArgument::Pack) {
2260d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      CurrentInstantiationScope->SetPartiallySubstitutedPack(
2261dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                 TemplateParams->getParam(I),
2262d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                                             Arg.pack_begin(),
2263d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                                             Arg.pack_size());
2264d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      break;
2265d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    }
2266d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  }
2267d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor
226883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Instantiate the types of each of the function parameters given the
226983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // explicitly-specified template arguments.
2270dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  if (SubstParmTypes(Function->getLocation(),
2271a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor                     Function->param_begin(), Function->getNumParams(),
2272a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor                     MultiLevelTemplateArgumentList(*ExplicitArgumentList),
2273a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor                     ParamTypes))
2274a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor    return TDK_SubstitutionFailure;
227583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
227683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // If the caller wants a full function type back, instantiate the return
227783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // type and form that function type.
227883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (FunctionType) {
227983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    // FIXME: exception-specifications?
22801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const FunctionProtoType *Proto
2281183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall      = Function->getType()->getAs<FunctionProtoType>();
228283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    assert(Proto && "Function template does not have a prototype?");
22831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    QualType ResultType
2285357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor      = SubstType(Proto->getResultType(),
2286357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  MultiLevelTemplateArgumentList(*ExplicitArgumentList),
2287357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  Function->getTypeSpecStartLoc(),
2288357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  Function->getDeclName());
228983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (ResultType.isNull() || Trap.hasErrorOccurred())
229083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_SubstitutionFailure;
22911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
22921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    *FunctionType = BuildFunctionType(ResultType,
229383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      ParamTypes.data(), ParamTypes.size(),
229483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      Proto->isVariadic(),
229583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      Proto->getTypeQuals(),
2296c938c1668b4fd12af154e965dd935a89e4801a70Douglas Gregor                                      Proto->getRefQualifier(),
229783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      Function->getLocation(),
2298fa869547eb1cab12d7e0c0dfa8ba594e336b9b32Eli Friedman                                      Function->getDeclName(),
2299fa869547eb1cab12d7e0c0dfa8ba594e336b9b32Eli Friedman                                      Proto->getExtInfo());
230083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (FunctionType->isNull() || Trap.hasErrorOccurred())
230183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_SubstitutionFailure;
230283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
23031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
230483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // C++ [temp.arg.explicit]p2:
23051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   Trailing template arguments that can be deduced (14.8.2) may be
23061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   omitted from the list of explicit template-arguments. If all of the
230783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   template arguments can be deduced, they may all be omitted; in this
230883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   case, the empty template argument list <> itself may also be omitted.
230983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //
2310d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  // Take all of the explicitly-specified arguments and put them into
2311d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  // the set of deduced template arguments. Explicitly-specified
2312d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  // parameter packs, however, will be set to NULL since the deduction
2313d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  // mechanisms handle explicitly-specified argument packs directly.
231483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Deduced.reserve(TemplateParams->size());
2315d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) {
2316d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    const TemplateArgument &Arg = ExplicitArgumentList->get(I);
2317d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    if (Arg.getKind() == TemplateArgument::Pack)
2318d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      Deduced.push_back(DeducedTemplateArgument());
2319d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    else
2320d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      Deduced.push_back(Arg);
2321d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor  }
23221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
232383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  return TDK_Success;
232483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor}
232583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
2326b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor/// \brief Check whether the deduced argument type for a call to a function
2327b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor/// template matches the actual argument type per C++ [temp.deduct.call]p4.
2328b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregorstatic bool
2329b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas GregorCheckOriginalCallArgDeduction(Sema &S, Sema::OriginalCallArg OriginalArg,
2330b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor                              QualType DeducedA) {
2331b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  ASTContext &Context = S.Context;
2332b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor
2333b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  QualType A = OriginalArg.OriginalArgType;
2334b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  QualType OriginalParamType = OriginalArg.OriginalParamType;
2335b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor
2336b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  // Check for type equality (top-level cv-qualifiers are ignored).
2337b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  if (Context.hasSameUnqualifiedType(A, DeducedA))
2338b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor    return false;
2339b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor
2340b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  // Strip off references on the argument types; they aren't needed for
2341b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  // the following checks.
2342b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  if (const ReferenceType *DeducedARef = DeducedA->getAs<ReferenceType>())
2343b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor    DeducedA = DeducedARef->getPointeeType();
2344b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  if (const ReferenceType *ARef = A->getAs<ReferenceType>())
2345b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor    A = ARef->getPointeeType();
2346b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor
2347b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  // C++ [temp.deduct.call]p4:
2348b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  //   [...] However, there are three cases that allow a difference:
2349b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  //     - If the original P is a reference type, the deduced A (i.e., the
2350b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  //       type referred to by the reference) can be more cv-qualified than
2351b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  //       the transformed A.
2352b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  if (const ReferenceType *OriginalParamRef
2353b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor      = OriginalParamType->getAs<ReferenceType>()) {
2354b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor    // We don't want to keep the reference around any more.
2355b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor    OriginalParamType = OriginalParamRef->getPointeeType();
2356b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor
2357b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor    Qualifiers AQuals = A.getQualifiers();
2358b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor    Qualifiers DeducedAQuals = DeducedA.getQualifiers();
2359b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor    if (AQuals == DeducedAQuals) {
2360b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor      // Qualifiers match; there's nothing to do.
2361b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor    } else if (!DeducedAQuals.compatiblyIncludes(AQuals)) {
2362c99f0ec89fd9782cfafd14d7b1b0a80151eab24aDouglas Gregor      return true;
2363b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor    } else {
2364b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor      // Qualifiers are compatible, so have the argument type adopt the
2365b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor      // deduced argument type's qualifiers as if we had performed the
2366b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor      // qualification conversion.
2367b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor      A = Context.getQualifiedType(A.getUnqualifiedType(), DeducedAQuals);
2368b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor    }
2369b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  }
2370b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor
2371b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  //    - The transformed A can be another pointer or pointer to member
2372b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  //      type that can be converted to the deduced A via a qualification
2373b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  //      conversion.
2374b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  bool ObjCLifetimeConversion = false;
2375b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  if ((A->isAnyPointerType() || A->isMemberPointerType()) &&
2376b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor      S.IsQualificationConversion(A, DeducedA, false, ObjCLifetimeConversion))
2377b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor    return false;
2378b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor
2379b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor
2380b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  //    - If P is a class and P has the form simple-template-id, then the
2381b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  //      transformed A can be a derived class of the deduced A. [...]
2382b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  //     [...] Likewise, if P is a pointer to a class of the form
2383b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  //      simple-template-id, the transformed A can be a pointer to a
2384b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  //      derived class pointed to by the deduced A.
2385b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  if (const PointerType *OriginalParamPtr
2386b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor      = OriginalParamType->getAs<PointerType>()) {
2387b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor    if (const PointerType *DeducedAPtr = DeducedA->getAs<PointerType>()) {
2388b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor      if (const PointerType *APtr = A->getAs<PointerType>()) {
2389b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor        if (A->getPointeeType()->isRecordType()) {
2390b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor          OriginalParamType = OriginalParamPtr->getPointeeType();
2391b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor          DeducedA = DeducedAPtr->getPointeeType();
2392b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor          A = APtr->getPointeeType();
2393b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor        }
2394b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor      }
2395b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor    }
2396b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  }
2397b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor
2398b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  if (Context.hasSameUnqualifiedType(A, DeducedA))
2399b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor    return false;
2400b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor
2401b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  if (A->isRecordType() && isSimpleTemplateIdType(OriginalParamType) &&
2402b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor      S.IsDerivedFrom(A, DeducedA))
2403b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor    return false;
2404b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor
2405b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor  return true;
2406b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor}
2407b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor
24081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Finish template argument deduction for a function template,
240983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// checking the deduced template arguments for completeness and forming
241083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// the function template specialization.
2411dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor///
2412dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor/// \param OriginalCallArgs If non-NULL, the original call arguments against
2413dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor/// which the deduced argument types should be compared.
24141eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSema::TemplateDeductionResult
241583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
241602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                       llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
241702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                      unsigned NumExplicitlySpecified,
241883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      FunctionDecl *&Specialization,
2419dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor                                      TemplateDeductionInfo &Info,
2420dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor        llvm::SmallVectorImpl<OriginalCallArg> const *OriginalCallArgs) {
242183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateParameterList *TemplateParams
242283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = FunctionTemplate->getTemplateParameters();
24231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
242451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // Template argument deduction for function templates in a SFINAE context.
242551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // Trap any errors that might occur.
242651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  SFINAETrap Trap(*this);
242751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
242851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // Enter a new template instantiation context while we instantiate the
242951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // actual function declaration.
243051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
243151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                             FunctionTemplate, Deduced.data(), Deduced.size(),
24329b623639378d53a675921ddfa7316034d571881eDouglas Gregor              ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution,
24339b623639378d53a675921ddfa7316034d571881eDouglas Gregor                             Info);
243451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  if (Inst)
243551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    return TDK_InstantiationDepth;
243651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
243796db310ab7ca59e1890ddef25a3701bc2909d20fJohn McCall  ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
2438f5813826802c2e76cdc13cae834ebfd4518d74a6John McCall
243983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // C++ [temp.deduct.type]p2:
244083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   [...] or if any template argument remains neither deduced nor
244183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   explicitly specified, template argument deduction fails.
2442910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor  llvm::SmallVector<TemplateArgument, 4> Builder;
2443b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor  for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) {
2444b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor    NamedDecl *Param = TemplateParams->getParam(I);
2445dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
244651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    if (!Deduced[I].isNull()) {
24473273b0cea879c7af345d6bf98502bbf73fc4fde1Douglas Gregor      if (I < NumExplicitlySpecified) {
244802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        // We have already fully type-checked and converted this
2449dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        // argument, because it was explicitly-specified. Just record the
24503273b0cea879c7af345d6bf98502bbf73fc4fde1Douglas Gregor        // presence of this argument.
2451910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor        Builder.push_back(Deduced[I]);
245202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        continue;
245302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      }
245402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
245502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // We have deduced this argument, so it still needs to be
245602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // checked and converted.
245702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
245802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // First, for a non-type template parameter type that is
245902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // initialized by a declaration, we need the type of the
246002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // corresponding non-type template parameter.
246102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      QualType NTTPType;
2462dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      if (NonTypeTemplateParmDecl *NTTP
2463dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
2464b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor        NTTPType = NTTP->getType();
2465b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor        if (NTTPType->isDependentType()) {
2466dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi          TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack,
2467b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor                                            Builder.data(), Builder.size());
2468b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor          NTTPType = SubstType(NTTPType,
2469b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor                               MultiLevelTemplateArgumentList(TemplateArgs),
2470b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor                               NTTP->getLocation(),
2471b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor                               NTTP->getDeclName());
2472b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor          if (NTTPType.isNull()) {
2473b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor            Info.Param = makeTemplateParameter(Param);
2474b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor            // FIXME: These template arguments are temporary. Free them!
2475dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi            Info.reset(TemplateArgumentList::CreateCopy(Context,
2476dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                        Builder.data(),
2477b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor                                                        Builder.size()));
2478b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor            return TDK_SubstitutionFailure;
247902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor          }
248002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        }
248102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      }
248202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
2483b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor      if (ConvertDeducedTemplateArgument(*this, Param, Deduced[I],
24846952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                                         FunctionTemplate, NTTPType, 0, Info,
248554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor                                         true, Builder)) {
2486b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor        Info.Param = makeTemplateParameter(Param);
2487910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor        // FIXME: These template arguments are temporary. Free them!
2488dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
2489dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                    Builder.size()));
249002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        return TDK_SubstitutionFailure;
249102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      }
249202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
249351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      continue;
249451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    }
2495dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2496ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor    // C++0x [temp.arg.explicit]p3:
2497dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //    A trailing template parameter pack (14.5.3) not otherwise deduced will
2498ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor    //    be deduced to an empty sequence of template arguments.
2499ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor    // FIXME: Where did the word "trailing" come from?
2500ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor    if (Param->isTemplateParameterPack()) {
2501d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      // We may have had explicitly-specified template arguments for this
2502d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      // template parameter pack. If so, our empty deduction extends the
2503d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      // explicitly-specified set (C++0x [temp.arg.explicit]p9).
2504d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      const TemplateArgument *ExplicitArgs;
2505d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      unsigned NumExplicitArgs;
2506d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      if (CurrentInstantiationScope->getPartiallySubstitutedPack(&ExplicitArgs,
2507d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor                                                             &NumExplicitArgs)
2508d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor          == Param)
2509d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor        Builder.push_back(TemplateArgument(ExplicitArgs, NumExplicitArgs));
2510dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      else
2511d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor        Builder.push_back(TemplateArgument(0, 0));
2512dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2513ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor      continue;
2514ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor    }
251551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
2516dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    // Substitute into the default template argument, if available.
251751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    TemplateArgumentLoc DefArg
251851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
251951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                              FunctionTemplate->getLocation(),
252051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                  FunctionTemplate->getSourceRange().getEnd(),
252151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                                Param,
252251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                                Builder);
252351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
252451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // If there was no default argument, deduction is incomplete.
252551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    if (DefArg.getArgument().isNull()) {
252683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      Info.Param = makeTemplateParameter(
252751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                         const_cast<NamedDecl *>(TemplateParams->getParam(I)));
252883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_Incomplete;
252983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    }
2530dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
253151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // Check whether we can actually use the default argument.
253251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    if (CheckTemplateArgument(Param, DefArg,
253351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                              FunctionTemplate,
253451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                              FunctionTemplate->getLocation(),
253551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                              FunctionTemplate->getSourceRange().getEnd(),
25366952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor                              0, Builder,
25378735b294a257a07ca158c28094d7324f0adf889aDouglas Gregor                              CTAK_Specified)) {
253851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      Info.Param = makeTemplateParameter(
253951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                         const_cast<NamedDecl *>(TemplateParams->getParam(I)));
2540910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor      // FIXME: These template arguments are temporary. Free them!
2541dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(),
2542910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor                                                  Builder.size()));
254351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      return TDK_SubstitutionFailure;
254451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    }
25451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
254651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // If we get here, we successfully used the default template argument.
254783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
25481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
254983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Form the template argument list from the deduced template arguments.
25501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentList *DeducedArgumentList
2551910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor    = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size());
255283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Info.reset(DeducedArgumentList);
25531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
25541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Substitute the deduced template arguments into the function template
255583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // declaration to produce the function template specialization.
2556d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor  DeclContext *Owner = FunctionTemplate->getDeclContext();
2557d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor  if (FunctionTemplate->getFriendObjectKind())
2558d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor    Owner = FunctionTemplate->getLexicalDeclContext();
255983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Specialization = cast_or_null<FunctionDecl>(
2560d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor                      SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner,
2561357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                         MultiLevelTemplateArgumentList(*DeducedArgumentList)));
256283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (!Specialization)
256383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_SubstitutionFailure;
25641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2565dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
2566f882574cf640d5c8355965e1c486f9d8d8ffcf47Douglas Gregor         FunctionTemplate->getCanonicalDecl());
2567dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
25681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If the template argument list is owned by the function template
256983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // specialization, release it.
2570ec20f46740a59758b12c22108002395bcf5b6f9bDouglas Gregor  if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList &&
2571ec20f46740a59758b12c22108002395bcf5b6f9bDouglas Gregor      !Trap.hasErrorOccurred())
257283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    Info.take();
25731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2574dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor  if (OriginalCallArgs) {
2575dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor    // C++ [temp.deduct.call]p4:
2576dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor    //   In general, the deduction process attempts to find template argument
2577dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor    //   values that will make the deduced A identical to A (after the type A
2578dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor    //   is transformed as described above). [...]
2579dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor    for (unsigned I = 0, N = OriginalCallArgs->size(); I != N; ++I) {
2580dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor      OriginalCallArg OriginalArg = (*OriginalCallArgs)[I];
2581dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor      unsigned ParamIdx = OriginalArg.ArgIdx;
2582dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor
2583dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor      if (ParamIdx >= Specialization->getNumParams())
2584dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor        continue;
2585dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor
2586dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor      QualType DeducedA = Specialization->getParamDecl(ParamIdx)->getType();
2587b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor      if (CheckOriginalCallArgDeduction(*this, OriginalArg, DeducedA))
2588b7edc4f372f1abe6c422ea711a99f9906a7464daDouglas Gregor        return Sema::TDK_SubstitutionFailure;
2589dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor    }
2590dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor  }
2591dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor
259283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // There may have been an error that did not prevent us from constructing a
259383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // declaration. Mark the declaration invalid and return with a substitution
259483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // failure.
259583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (Trap.hasErrorOccurred()) {
259683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    Specialization->setInvalidDecl(true);
259783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_SubstitutionFailure;
259883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
25991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
26009b623639378d53a675921ddfa7316034d571881eDouglas Gregor  // If we suppressed any diagnostics while performing template argument
26019b623639378d53a675921ddfa7316034d571881eDouglas Gregor  // deduction, and if we haven't already instantiated this declaration,
26029b623639378d53a675921ddfa7316034d571881eDouglas Gregor  // keep track of these diagnostics. They'll be emitted if this specialization
26039b623639378d53a675921ddfa7316034d571881eDouglas Gregor  // is actually used.
26049b623639378d53a675921ddfa7316034d571881eDouglas Gregor  if (Info.diag_begin() != Info.diag_end()) {
26059b623639378d53a675921ddfa7316034d571881eDouglas Gregor    llvm::DenseMap<Decl *, llvm::SmallVector<PartialDiagnosticAt, 1> >::iterator
26069b623639378d53a675921ddfa7316034d571881eDouglas Gregor      Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl());
26079b623639378d53a675921ddfa7316034d571881eDouglas Gregor    if (Pos == SuppressedDiagnostics.end())
26089b623639378d53a675921ddfa7316034d571881eDouglas Gregor        SuppressedDiagnostics[Specialization->getCanonicalDecl()]
26099b623639378d53a675921ddfa7316034d571881eDouglas Gregor          .append(Info.diag_begin(), Info.diag_end());
2610dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  }
26119b623639378d53a675921ddfa7316034d571881eDouglas Gregor
26121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return TDK_Success;
261383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor}
261483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
26159c72c6088d591ace8503b842d39448c2040f3033John McCall/// Gets the type of a function for template-argument-deducton
26169c72c6088d591ace8503b842d39448c2040f3033John McCall/// purposes when it's considered as part of an overload set.
2617eff92135d32039c9874dc356f3e93143af6069c1John McCallstatic QualType GetTypeOfFunction(ASTContext &Context,
26189c72c6088d591ace8503b842d39448c2040f3033John McCall                                  const OverloadExpr::FindResult &R,
2619eff92135d32039c9874dc356f3e93143af6069c1John McCall                                  FunctionDecl *Fn) {
2620eff92135d32039c9874dc356f3e93143af6069c1John McCall  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
26219c72c6088d591ace8503b842d39448c2040f3033John McCall    if (Method->isInstance()) {
26229c72c6088d591ace8503b842d39448c2040f3033John McCall      // An instance method that's referenced in a form that doesn't
26239c72c6088d591ace8503b842d39448c2040f3033John McCall      // look like a member pointer is just invalid.
26249c72c6088d591ace8503b842d39448c2040f3033John McCall      if (!R.HasFormOfMemberPointer) return QualType();
26259c72c6088d591ace8503b842d39448c2040f3033John McCall
2626eff92135d32039c9874dc356f3e93143af6069c1John McCall      return Context.getMemberPointerType(Fn->getType(),
2627eff92135d32039c9874dc356f3e93143af6069c1John McCall               Context.getTypeDeclType(Method->getParent()).getTypePtr());
26289c72c6088d591ace8503b842d39448c2040f3033John McCall    }
26299c72c6088d591ace8503b842d39448c2040f3033John McCall
26309c72c6088d591ace8503b842d39448c2040f3033John McCall  if (!R.IsAddressOfOperand) return Fn->getType();
2631eff92135d32039c9874dc356f3e93143af6069c1John McCall  return Context.getPointerType(Fn->getType());
2632eff92135d32039c9874dc356f3e93143af6069c1John McCall}
2633eff92135d32039c9874dc356f3e93143af6069c1John McCall
2634eff92135d32039c9874dc356f3e93143af6069c1John McCall/// Apply the deduction rules for overload sets.
2635eff92135d32039c9874dc356f3e93143af6069c1John McCall///
2636eff92135d32039c9874dc356f3e93143af6069c1John McCall/// \return the null type if this argument should be treated as an
2637eff92135d32039c9874dc356f3e93143af6069c1John McCall/// undeduced context
2638eff92135d32039c9874dc356f3e93143af6069c1John McCallstatic QualType
2639eff92135d32039c9874dc356f3e93143af6069c1John McCallResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
264075f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor                            Expr *Arg, QualType ParamType,
264175f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor                            bool ParamWasReference) {
2642dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
26439c72c6088d591ace8503b842d39448c2040f3033John McCall  OverloadExpr::FindResult R = OverloadExpr::find(Arg);
2644eff92135d32039c9874dc356f3e93143af6069c1John McCall
26459c72c6088d591ace8503b842d39448c2040f3033John McCall  OverloadExpr *Ovl = R.Expression;
2646eff92135d32039c9874dc356f3e93143af6069c1John McCall
264775f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor  // C++0x [temp.deduct.call]p4
264875f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor  unsigned TDF = 0;
264975f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor  if (ParamWasReference)
265075f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor    TDF |= TDF_ParamWithReferenceType;
265175f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor  if (R.IsAddressOfOperand)
265275f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor    TDF |= TDF_IgnoreQualifiers;
265375f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor
2654eff92135d32039c9874dc356f3e93143af6069c1John McCall  // If there were explicit template arguments, we can only find
2655eff92135d32039c9874dc356f3e93143af6069c1John McCall  // something via C++ [temp.arg.explicit]p3, i.e. if the arguments
2656eff92135d32039c9874dc356f3e93143af6069c1John McCall  // unambiguously name a full specialization.
26577bb12da2b0749eeebb21854c77877736969e59f2John McCall  if (Ovl->hasExplicitTemplateArgs()) {
2658eff92135d32039c9874dc356f3e93143af6069c1John McCall    // But we can still look for an explicit specialization.
2659eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (FunctionDecl *ExplicitSpec
26607bb12da2b0749eeebb21854c77877736969e59f2John McCall          = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
26619c72c6088d591ace8503b842d39448c2040f3033John McCall      return GetTypeOfFunction(S.Context, R, ExplicitSpec);
2662eff92135d32039c9874dc356f3e93143af6069c1John McCall    return QualType();
2663eff92135d32039c9874dc356f3e93143af6069c1John McCall  }
2664eff92135d32039c9874dc356f3e93143af6069c1John McCall
2665eff92135d32039c9874dc356f3e93143af6069c1John McCall  // C++0x [temp.deduct.call]p6:
2666eff92135d32039c9874dc356f3e93143af6069c1John McCall  //   When P is a function type, pointer to function type, or pointer
2667eff92135d32039c9874dc356f3e93143af6069c1John McCall  //   to member function type:
2668eff92135d32039c9874dc356f3e93143af6069c1John McCall
2669eff92135d32039c9874dc356f3e93143af6069c1John McCall  if (!ParamType->isFunctionType() &&
2670eff92135d32039c9874dc356f3e93143af6069c1John McCall      !ParamType->isFunctionPointerType() &&
2671eff92135d32039c9874dc356f3e93143af6069c1John McCall      !ParamType->isMemberFunctionPointerType())
2672eff92135d32039c9874dc356f3e93143af6069c1John McCall    return QualType();
2673eff92135d32039c9874dc356f3e93143af6069c1John McCall
2674eff92135d32039c9874dc356f3e93143af6069c1John McCall  QualType Match;
26757bb12da2b0749eeebb21854c77877736969e59f2John McCall  for (UnresolvedSetIterator I = Ovl->decls_begin(),
26767bb12da2b0749eeebb21854c77877736969e59f2John McCall         E = Ovl->decls_end(); I != E; ++I) {
2677eff92135d32039c9874dc356f3e93143af6069c1John McCall    NamedDecl *D = (*I)->getUnderlyingDecl();
2678eff92135d32039c9874dc356f3e93143af6069c1John McCall
2679eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   - If the argument is an overload set containing one or more
2680eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     function templates, the parameter is treated as a
2681eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     non-deduced context.
2682eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (isa<FunctionTemplateDecl>(D))
2683eff92135d32039c9874dc356f3e93143af6069c1John McCall      return QualType();
2684eff92135d32039c9874dc356f3e93143af6069c1John McCall
2685eff92135d32039c9874dc356f3e93143af6069c1John McCall    FunctionDecl *Fn = cast<FunctionDecl>(D);
26869c72c6088d591ace8503b842d39448c2040f3033John McCall    QualType ArgType = GetTypeOfFunction(S.Context, R, Fn);
26879c72c6088d591ace8503b842d39448c2040f3033John McCall    if (ArgType.isNull()) continue;
2688eff92135d32039c9874dc356f3e93143af6069c1John McCall
268975f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor    // Function-to-pointer conversion.
2690dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    if (!ParamWasReference && ParamType->isPointerType() &&
269175f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor        ArgType->isFunctionType())
269275f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor      ArgType = S.Context.getPointerType(ArgType);
2693dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2694eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   - If the argument is an overload set (not containing function
2695eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     templates), trial argument deduction is attempted using each
2696eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     of the members of the set. If deduction succeeds for only one
2697eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     of the overload set members, that member is used as the
2698eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     argument value for the deduction. If deduction succeeds for
2699eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     more than one member of the overload set the parameter is
2700eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     treated as a non-deduced context.
2701eff92135d32039c9874dc356f3e93143af6069c1John McCall
2702eff92135d32039c9874dc356f3e93143af6069c1John McCall    // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
2703eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   Type deduction is done independently for each P/A pair, and
2704eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   the deduced template argument values are then combined.
2705eff92135d32039c9874dc356f3e93143af6069c1John McCall    // So we do not reject deductions which were made elsewhere.
2706dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    llvm::SmallVector<DeducedTemplateArgument, 8>
270702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      Deduced(TemplateParams->size());
27082a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall    TemplateDeductionInfo Info(S.Context, Ovl->getNameLoc());
2709eff92135d32039c9874dc356f3e93143af6069c1John McCall    Sema::TemplateDeductionResult Result
2710a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      = DeduceTemplateArguments(S, TemplateParams,
2711eff92135d32039c9874dc356f3e93143af6069c1John McCall                                ParamType, ArgType,
2712eff92135d32039c9874dc356f3e93143af6069c1John McCall                                Info, Deduced, TDF);
2713eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (Result) continue;
2714eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (!Match.isNull()) return QualType();
2715eff92135d32039c9874dc356f3e93143af6069c1John McCall    Match = ArgType;
2716eff92135d32039c9874dc356f3e93143af6069c1John McCall  }
2717eff92135d32039c9874dc356f3e93143af6069c1John McCall
2718eff92135d32039c9874dc356f3e93143af6069c1John McCall  return Match;
2719eff92135d32039c9874dc356f3e93143af6069c1John McCall}
2720eff92135d32039c9874dc356f3e93143af6069c1John McCall
2721dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \brief Perform the adjustments to the parameter and argument types
2722f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor/// described in C++ [temp.deduct.call].
2723f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor///
2724f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor/// \returns true if the caller should not attempt to perform any template
2725f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor/// argument deduction based on this P/A pair.
2726f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregorstatic bool AdjustFunctionParmAndArgTypesForDeduction(Sema &S,
2727f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                          TemplateParameterList *TemplateParams,
2728f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                                      QualType &ParamType,
2729f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                                      QualType &ArgType,
2730f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                                      Expr *Arg,
2731f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                                      unsigned &TDF) {
2732f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // C++0x [temp.deduct.call]p3:
27330099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi  //   If P is a cv-qualified type, the top level cv-qualifiers of P's type
2734f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //   are ignored for type deduction.
2735a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor  if (ParamType.hasQualifiers())
2736a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor    ParamType = ParamType.getUnqualifiedType();
2737f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>();
2738f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (ParamRefType) {
273934b41d939a1328f484511c6002ba2456db879a29Richard Smith    QualType PointeeType = ParamRefType->getPointeeType();
274034b41d939a1328f484511c6002ba2456db879a29Richard Smith
2741f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor    // If the argument has incomplete array type, try to complete it's type.
2742f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor    if (ArgType->isIncompleteArrayType() &&
2743f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor        !S.RequireCompleteExprType(Arg, S.PDiag(),
2744f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor                                   std::make_pair(SourceLocation(), S.PDiag())))
2745f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor      ArgType = Arg->getType();
2746f15748a28c8443eef2924ef83689c358c661e9c5Douglas Gregor
27472ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor    //   [C++0x] If P is an rvalue reference to a cv-unqualified
27482ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor    //   template parameter and the argument is an lvalue, the type
27492ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor    //   "lvalue reference to A" is used in place of A for type
27502ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor    //   deduction.
275134b41d939a1328f484511c6002ba2456db879a29Richard Smith    if (isa<RValueReferenceType>(ParamType)) {
275234b41d939a1328f484511c6002ba2456db879a29Richard Smith      if (!PointeeType.getQualifiers() &&
275334b41d939a1328f484511c6002ba2456db879a29Richard Smith          isa<TemplateTypeParmType>(PointeeType) &&
27549625e44c0252485277a340746ed8ac950686156fDouglas Gregor          Arg->Classify(S.Context).isLValue() &&
27559625e44c0252485277a340746ed8ac950686156fDouglas Gregor          Arg->getType() != S.Context.OverloadTy &&
27569625e44c0252485277a340746ed8ac950686156fDouglas Gregor          Arg->getType() != S.Context.BoundMemberTy)
27572ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor        ArgType = S.Context.getLValueReferenceType(ArgType);
27582ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor    }
27592ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor
2760f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //   [...] If P is a reference type, the type referred to by P is used
2761f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //   for type deduction.
276234b41d939a1328f484511c6002ba2456db879a29Richard Smith    ParamType = PointeeType;
2763f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  }
2764dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2765f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // Overload sets usually make this parameter an undeduced
2766f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // context, but there are sometimes special circumstances.
2767f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (ArgType == S.Context.OverloadTy) {
2768f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    ArgType = ResolveOverloadForDeduction(S, TemplateParams,
2769f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                          Arg, ParamType,
2770f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                          ParamRefType != 0);
2771f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    if (ArgType.isNull())
2772f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      return true;
2773f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  }
2774dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2775f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (ParamRefType) {
2776f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    // C++0x [temp.deduct.call]p3:
2777f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //   [...] If P is of the form T&&, where T is a template parameter, and
2778f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //   the argument is an lvalue, the type A& is used in place of A for
2779f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //   type deduction.
2780f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    if (ParamRefType->isRValueReferenceType() &&
2781f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        ParamRefType->getAs<TemplateTypeParmType>() &&
2782f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        Arg->isLValue())
2783f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      ArgType = S.Context.getLValueReferenceType(ArgType);
2784f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  } else {
2785f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    // C++ [temp.deduct.call]p2:
2786f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //   If P is not a reference type:
2787f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //   - If A is an array type, the pointer type produced by the
2788f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //     array-to-pointer standard conversion (4.2) is used in place of
2789f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //     A for type deduction; otherwise,
2790f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    if (ArgType->isArrayType())
2791f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      ArgType = S.Context.getArrayDecayedType(ArgType);
2792f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //   - If A is a function type, the pointer type produced by the
2793f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //     function-to-pointer standard conversion (4.3) is used in place
2794f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    //     of A for type deduction; otherwise,
2795f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    else if (ArgType->isFunctionType())
2796f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      ArgType = S.Context.getPointerType(ArgType);
2797f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    else {
27980099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi      // - If A is a cv-qualified type, the top level cv-qualifiers of A's
2799f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      //   type are ignored for type deduction.
2800a459cc26f6c895ae48742a36203f753cea8b3e91Douglas Gregor      ArgType = ArgType.getUnqualifiedType();
2801f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    }
2802f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  }
2803dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2804f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  // C++0x [temp.deduct.call]p4:
2805f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //   In general, the deduction process attempts to find template argument
2806f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //   values that will make the deduced A identical to A (after the type A
2807f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //   is transformed as described above). [...]
2808f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  TDF = TDF_SkipNonDependent;
2809dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2810f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //     - If the original P is a reference type, the deduced A (i.e., the
2811f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //       type referred to by the reference) can be more cv-qualified than
2812f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //       the transformed A.
2813f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (ParamRefType)
2814f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    TDF |= TDF_ParamWithReferenceType;
2815f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //     - The transformed A can be another pointer or pointer to member
2816f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //       type that can be converted to the deduced A via a qualification
2817f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //       conversion (4.4).
2818f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (ArgType->isPointerType() || ArgType->isMemberPointerType() ||
2819f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      ArgType->isObjCObjectPointerType())
2820f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    TDF |= TDF_IgnoreQualifiers;
2821f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //     - If P is a class and P has the form simple-template-id, then the
2822f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //       transformed A can be a derived class of the deduced A. Likewise,
2823f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //       if P is a pointer to a class of the form simple-template-id, the
2824f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //       transformed A can be a pointer to a derived class pointed to by
2825f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  //       the deduced A.
2826f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  if (isSimpleTemplateIdType(ParamType) ||
2827f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      (isa<PointerType>(ParamType) &&
2828f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor       isSimpleTemplateIdType(
2829f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                              ParamType->getAs<PointerType>()->getPointeeType())))
2830f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    TDF |= TDF_DerivedClass;
2831dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2832f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  return false;
2833f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor}
2834f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor
2835dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregorstatic bool hasDeducibleTemplateParameters(Sema &S,
2836dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor                                           FunctionTemplateDecl *FunctionTemplate,
2837dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor                                           QualType T);
2838dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor
2839e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \brief Perform template argument deduction from a function call
2840e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// (C++ [temp.deduct.call]).
2841e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
2842e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param FunctionTemplate the function template for which we are performing
2843e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// template argument deduction.
2844e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
284548026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// \param ExplicitTemplateArguments the explicit template arguments provided
284648026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// for this call.
28476db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor///
2848e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Args the function call arguments
2849e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
2850e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param NumArgs the number of arguments in Args
2851e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
285248026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// \param Name the name of the function being called. This is only significant
285348026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// when the function template is a conversion function template, in which
285448026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// case this routine will also perform template argument deduction based on
2855dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// the function to which
285648026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor///
2857e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Specialization if template argument deduction was successful,
28581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// this will be set to the function template specialization produced by
2859e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// template argument deduction.
2860e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
2861e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Info the argument will be updated to provide additional information
2862e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// about template argument deduction.
2863e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
2864e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns the result of template argument deduction.
2865e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorSema::TemplateDeductionResult
2866e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
286767714230a191bc3c01f33378f34f34ef377991a6Douglas Gregor                              TemplateArgumentListInfo *ExplicitTemplateArgs,
2868e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                              Expr **Args, unsigned NumArgs,
2869e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                              FunctionDecl *&Specialization,
2870e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                              TemplateDeductionInfo &Info) {
2871e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
28726db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor
2873e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // C++ [temp.deduct.call]p1:
2874e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  //   Template argument deduction is done by comparing each function template
2875e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  //   parameter type (call it P) with the type of the corresponding argument
2876e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  //   of the call (call it A) as described below.
2877e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  unsigned CheckArgs = NumArgs;
28786db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  if (NumArgs < Function->getMinRequiredArguments())
2879e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return TDK_TooFewArguments;
2880e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  else if (NumArgs > Function->getNumParams()) {
28811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const FunctionProtoType *Proto
2882183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall      = Function->getType()->getAs<FunctionProtoType>();
2883f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    if (Proto->isTemplateVariadic())
2884f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      /* Do nothing */;
2885f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    else if (Proto->isVariadic())
2886f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      CheckArgs = Function->getNumParams();
2887dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    else
2888e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return TDK_TooManyArguments;
2889e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  }
28901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
28916db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  // The types of the parameters from which we will perform template argument
28926db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  // deduction.
28932a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope InstScope(*this);
2894e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  TemplateParameterList *TemplateParams
2895e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    = FunctionTemplate->getTemplateParameters();
289602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
28976db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  llvm::SmallVector<QualType, 4> ParamTypes;
289802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  unsigned NumExplicitlySpecified = 0;
2899d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (ExplicitTemplateArgs) {
290083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    TemplateDeductionResult Result =
290183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      SubstituteExplicitTemplateArguments(FunctionTemplate,
2902d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                          *ExplicitTemplateArgs,
290383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          Deduced,
290483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          ParamTypes,
290583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          0,
290683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          Info);
290783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (Result)
290883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return Result;
290902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
291002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    NumExplicitlySpecified = Deduced.size();
29116db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  } else {
29126db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor    // Just fill in the parameter types from the function declaration.
2913f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
29146db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor      ParamTypes.push_back(Function->getParamDecl(I)->getType());
29156db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  }
29161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
29176db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  // Deduce template arguments from the function parameters.
29181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Deduced.resize(TemplateParams->size());
2919f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor  unsigned ArgIdx = 0;
2920dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor  llvm::SmallVector<OriginalCallArg, 4> OriginalCallArgs;
2921dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  for (unsigned ParamIdx = 0, NumParams = ParamTypes.size();
2922f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor       ParamIdx != NumParams; ++ParamIdx) {
2923dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor    QualType OrigParamType = ParamTypes[ParamIdx];
2924dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor    QualType ParamType = OrigParamType;
2925dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor
2926dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    const PackExpansionType *ParamExpansion
2927f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      = dyn_cast<PackExpansionType>(ParamType);
2928f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    if (!ParamExpansion) {
2929f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      // Simple case: matching a function parameter to a function argument.
2930f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      if (ArgIdx >= CheckArgs)
2931f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        break;
2932dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2933f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      Expr *Arg = Args[ArgIdx++];
2934f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      QualType ArgType = Arg->getType();
2935dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor
2936f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      unsigned TDF = 0;
2937f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams,
2938f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                                    ParamType, ArgType, Arg,
2939f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                                    TDF))
2940f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        continue;
2941dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2942dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor      // Keep track of the argument type and corresponding parameter index,
2943dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor      // so we can check for compatibility between the deduced A and A.
2944dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor      if (hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType))
2945dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor        OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx-1,
2946dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor                                                   ArgType));
2947dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor
2948f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      if (TemplateDeductionResult Result
2949f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor          = ::DeduceTemplateArguments(*this, TemplateParams,
2950f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                      ParamType, ArgType, Info, Deduced,
2951f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                      TDF))
2952f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        return Result;
29531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2954f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      continue;
295575f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor    }
2956dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2957f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    // C++0x [temp.deduct.call]p1:
2958dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   For a function parameter pack that occurs at the end of the
2959dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   parameter-declaration-list, the type A of each remaining argument of
2960dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   the call is compared with the type P of the declarator-id of the
2961dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   function parameter pack. Each comparison deduces template arguments
2962dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   for subsequent positions in the template parameter packs expanded by
29637d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    //   the function parameter pack. For a function parameter pack that does
2964dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   not occur at the end of the parameter-declaration-list, the type of
29657d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    //   the parameter pack is a non-deduced context.
29667d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor    if (ParamIdx + 1 < NumParams)
29677d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor      break;
2968dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2969f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    QualType ParamPattern = ParamExpansion->getPattern();
2970f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    llvm::SmallVector<unsigned, 2> PackIndices;
2971f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    {
2972f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      llvm::BitVector SawIndices(TemplateParams->size());
2973f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded;
2974f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      collectUnexpandedParameterPacks(ParamPattern, Unexpanded);
2975f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) {
2976f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        unsigned Depth, Index;
2977f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]);
2978f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        if (Depth == 0 && !SawIndices[Index]) {
2979f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor          SawIndices[Index] = true;
2980f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor          PackIndices.push_back(Index);
2981f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        }
2982f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      }
2983f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    }
2984f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?");
2985dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2986d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    // Keep track of the deduced template arguments for each parameter pack
2987dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    // expanded by this pack expansion (the outer index) and for each
2988d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    // template argument (the inner SmallVectors).
2989d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor    llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2>
2990d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      NewlyDeducedPacks(PackIndices.size());
2991dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    llvm::SmallVector<DeducedTemplateArgument, 2>
2992d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor      SavedPacks(PackIndices.size());
29935429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor    PrepareArgumentPackDeduction(*this, Deduced, PackIndices, SavedPacks,
2994dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                 NewlyDeducedPacks);
2995f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    bool HasAnyArguments = false;
2996f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    for (; ArgIdx < NumArgs; ++ArgIdx) {
2997f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      HasAnyArguments = true;
2998dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
2999dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor      QualType OrigParamType = ParamPattern;
3000dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor      ParamType = OrigParamType;
3001f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      Expr *Arg = Args[ArgIdx];
3002f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      QualType ArgType = Arg->getType();
3003dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor
3004f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      unsigned TDF = 0;
3005f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams,
3006f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                                    ParamType, ArgType, Arg,
3007f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                                    TDF)) {
3008f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        // We can't actually perform any deduction for this argument, so stop
3009f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        // deduction at this point.
3010f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        ++ArgIdx;
3011f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        break;
3012f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      }
3013dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3014dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor      // Keep track of the argument type and corresponding argument index,
3015dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor      // so we can check for compatibility between the deduced A and A.
3016dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor      if (hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType))
3017dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor        OriginalCallArgs.push_back(OriginalCallArg(OrigParamType, ArgIdx,
3018dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor                                                   ArgType));
3019dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor
3020f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      if (TemplateDeductionResult Result
3021f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor          = ::DeduceTemplateArguments(*this, TemplateParams,
3022f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                      ParamType, ArgType, Info, Deduced,
3023f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor                                      TDF))
3024f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        return Result;
3025eff92135d32039c9874dc356f3e93143af6069c1John McCall
3026f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      // Capture the deduced template arguments for each parameter pack expanded
3027f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      // by this pack expansion, add them to the list of arguments we've deduced
3028f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      // for that pack, then clear out the deduced argument.
3029f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor      for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) {
3030f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]];
3031f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        if (!DeducedArg.isNull()) {
3032f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor          NewlyDeducedPacks[I].push_back(DeducedArg);
3033f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor          DeducedArg = DeducedTemplateArgument();
3034f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor        }
3035e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      }
3036e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    }
3037dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3038f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    // Build argument packs for each of the parameter packs expanded by this
3039f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    // pack expansion.
30400216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor    if (Sema::TemplateDeductionResult Result
3041dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi          = FinishArgumentPackDeduction(*this, TemplateParams, HasAnyArguments,
30420216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                                        Deduced, PackIndices, SavedPacks,
30430216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor                                        NewlyDeducedPacks, Info))
3044dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      return Result;
30451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3046f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    // After we've matching against a parameter pack, we're done.
3047f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor    break;
3048e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  }
304965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
30501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
305102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                         NumExplicitlySpecified,
3052dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor                                         Specialization, Info, &OriginalCallArgs);
305383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor}
3054127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor
305583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \brief Deduce template arguments when taking the address of a function
30564b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
30574b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// a template.
305883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
305983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionTemplate the function template for which we are performing
306083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template argument deduction.
306183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
3062dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \param ExplicitTemplateArguments the explicitly-specified template
30634b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// arguments.
306483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
306583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param ArgFunctionType the function type that will be used as the
306683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// "argument" type (A) when performing template argument deduction from the
30674b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// function template's function type. This type may be NULL, if there is no
30684b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
306983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
307083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Specialization if template argument deduction was successful,
30711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// this will be set to the function template specialization produced by
307283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template argument deduction.
307383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
307483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Info the argument will be updated to provide additional information
307583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// about template argument deduction.
307683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
307783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \returns the result of template argument deduction.
307883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::TemplateDeductionResult
307983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
308067714230a191bc3c01f33378f34f34ef377991a6Douglas Gregor                              TemplateArgumentListInfo *ExplicitTemplateArgs,
308183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                              QualType ArgFunctionType,
308283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                              FunctionDecl *&Specialization,
308383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                              TemplateDeductionInfo &Info) {
308483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
308583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateParameterList *TemplateParams
308683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = FunctionTemplate->getTemplateParameters();
308783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  QualType FunctionType = Function->getType();
30881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
308983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Substitute any explicit template arguments.
30902a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope InstScope(*this);
309102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
309202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  unsigned NumExplicitlySpecified = 0;
309383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  llvm::SmallVector<QualType, 4> ParamTypes;
3094d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (ExplicitTemplateArgs) {
30951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (TemplateDeductionResult Result
30961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          = SubstituteExplicitTemplateArguments(FunctionTemplate,
3097d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                *ExplicitTemplateArgs,
30981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                Deduced, ParamTypes,
309983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                                &FunctionType, Info))
310083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return Result;
310102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
310202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    NumExplicitlySpecified = Deduced.size();
3103127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
310483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
310583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Template argument deduction for function templates in a SFINAE context.
310683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Trap any errors that might occur.
31071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SFINAETrap Trap(*this);
31081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3109eff92135d32039c9874dc356f3e93143af6069c1John McCall  Deduced.resize(TemplateParams->size());
3110eff92135d32039c9874dc356f3e93143af6069c1John McCall
31114b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  if (!ArgFunctionType.isNull()) {
31124b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    // Deduce template arguments from the function type.
31134b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (TemplateDeductionResult Result
3114a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          = ::DeduceTemplateArguments(*this, TemplateParams,
31154b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                                      FunctionType, ArgFunctionType, Info,
311673b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor                                      Deduced, TDF_TopLevelParameterTypeList))
31174b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      return Result;
31184b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  }
3119fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor
3120dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  if (TemplateDeductionResult Result
3121fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor        = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
3122fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor                                          NumExplicitlySpecified,
3123fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor                                          Specialization, Info))
3124fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor    return Result;
3125fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor
3126fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor  // If the requested function type does not match the actual type of the
3127fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor  // specialization, template argument deduction fails.
3128fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor  if (!ArgFunctionType.isNull() &&
3129fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor      !Context.hasSameType(ArgFunctionType, Specialization->getType()))
3130fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor    return TDK_NonDeducedMismatch;
3131fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor
3132fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor  return TDK_Success;
3133e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
3134e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
313565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \brief Deduce template arguments for a templated conversion
313665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// function (C++ [temp.deduct.conv]) and, if successful, produce a
313765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// conversion function template specialization.
313865ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::TemplateDeductionResult
313965ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
314065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                              QualType ToType,
314165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                              CXXConversionDecl *&Specialization,
314265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                              TemplateDeductionInfo &Info) {
31431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  CXXConversionDecl *Conv
314465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
314565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  QualType FromType = Conv->getConversionType();
314665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
314765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Canonicalize the types for deduction.
314865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  QualType P = Context.getCanonicalType(FromType);
314965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  QualType A = Context.getCanonicalType(ToType);
315065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
31515453d93ab8668f2d9d0bc02182695b02d207e32dDouglas Gregor  // C++0x [temp.deduct.conv]p2:
315265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   If P is a reference type, the type referred to by P is used for
315365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   type deduction.
315465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (const ReferenceType *PRef = P->getAs<ReferenceType>())
315565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    P = PRef->getPointeeType();
315665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
31575453d93ab8668f2d9d0bc02182695b02d207e32dDouglas Gregor  // C++0x [temp.deduct.conv]p4:
31585453d93ab8668f2d9d0bc02182695b02d207e32dDouglas Gregor  //   [...] If A is a reference type, the type referred to by A is used
315965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   for type deduction.
316065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (const ReferenceType *ARef = A->getAs<ReferenceType>())
31615453d93ab8668f2d9d0bc02182695b02d207e32dDouglas Gregor    A = ARef->getPointeeType().getUnqualifiedType();
31625453d93ab8668f2d9d0bc02182695b02d207e32dDouglas Gregor  // C++ [temp.deduct.conv]p3:
316365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //
31641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   If A is not a reference type:
316565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  else {
316665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    assert(!A->isReferenceType() && "Reference types were handled above");
316765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
316865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   - If P is an array type, the pointer type produced by the
31691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     array-to-pointer standard conversion (4.2) is used in place
317065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     of P for type deduction; otherwise,
317165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    if (P->isArrayType())
317265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      P = Context.getArrayDecayedType(P);
317365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   - If P is a function type, the pointer type produced by the
317465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     function-to-pointer standard conversion (4.3) is used in
317565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     place of P for type deduction; otherwise,
317665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    else if (P->isFunctionType())
317765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      P = Context.getPointerType(P);
317865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   - If P is a cv-qualified type, the top level cv-qualifiers of
31790099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi    //     P's type are ignored for type deduction.
318065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    else
318165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      P = P.getUnqualifiedType();
318265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
31835453d93ab8668f2d9d0bc02182695b02d207e32dDouglas Gregor    // C++0x [temp.deduct.conv]p4:
31840099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi    //   If A is a cv-qualified type, the top level cv-qualifiers of A's
31855453d93ab8668f2d9d0bc02182695b02d207e32dDouglas Gregor    //   type are ignored for type deduction. If A is a reference type, the type
31865453d93ab8668f2d9d0bc02182695b02d207e32dDouglas Gregor    //   referred to by A is used for type deduction.
318765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    A = A.getUnqualifiedType();
318865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  }
318965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
319065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Template argument deduction for function templates in a SFINAE context.
319165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Trap any errors that might occur.
31921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SFINAETrap Trap(*this);
319365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
319465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++ [temp.deduct.conv]p1:
319565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   Template argument deduction is done by comparing the return
319665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   type of the template conversion function (call it P) with the
319765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   type that is required as the result of the conversion (call it
319865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   A) as described in 14.8.2.4.
319965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  TemplateParameterList *TemplateParams
320065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    = FunctionTemplate->getTemplateParameters();
320102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
32021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Deduced.resize(TemplateParams->size());
320365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
320465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++0x [temp.deduct.conv]p4:
320565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   In general, the deduction process attempts to find template
320665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   argument values that will make the deduced A identical to
320765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   A. However, there are two cases that allow a difference:
320865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  unsigned TDF = 0;
320965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //     - If the original A is a reference type, A can be more
321065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       cv-qualified than the deduced A (i.e., the type referred to
321165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       by the reference)
321265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (ToType->isReferenceType())
321365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    TDF |= TDF_ParamWithReferenceType;
321465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //     - The deduced A can be another pointer or pointer to member
32150099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi  //       type that can be converted to A via a qualification
321665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       conversion.
321765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //
321865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
321965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // both P and A are pointers or member pointers. In this case, we
322065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // just ignore cv-qualifiers completely).
322165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if ((P->isPointerType() && A->isPointerType()) ||
322265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      (P->isMemberPointerType() && P->isMemberPointerType()))
322365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    TDF |= TDF_IgnoreQualifiers;
322465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (TemplateDeductionResult Result
3225a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = ::DeduceTemplateArguments(*this, TemplateParams,
322665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                    P, A, Info, Deduced, TDF))
322765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    return Result;
322865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
322965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Finish template argument deduction.
32302a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  LocalInstantiationScope InstScope(*this);
323165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  FunctionDecl *Spec = 0;
323265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  TemplateDeductionResult Result
3233dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 0, Spec,
323402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                      Info);
323565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  Specialization = cast_or_null<CXXConversionDecl>(Spec);
323665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return Result;
323765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor}
323865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
32394b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \brief Deduce template arguments for a function template when there is
32404b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
32414b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
32424b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param FunctionTemplate the function template for which we are performing
32434b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template argument deduction.
32444b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
3245dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \param ExplicitTemplateArguments the explicitly-specified template
32464b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// arguments.
32474b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
32484b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param Specialization if template argument deduction was successful,
32494b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// this will be set to the function template specialization produced by
32504b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template argument deduction.
32514b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
32524b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param Info the argument will be updated to provide additional information
32534b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// about template argument deduction.
32544b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
32554b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \returns the result of template argument deduction.
32564b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas GregorSema::TemplateDeductionResult
32574b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
325867714230a191bc3c01f33378f34f34ef377991a6Douglas Gregor                              TemplateArgumentListInfo *ExplicitTemplateArgs,
32594b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                              FunctionDecl *&Specialization,
32604b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                              TemplateDeductionInfo &Info) {
32614b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
32624b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                                 QualType(), Specialization, Info);
32634b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor}
32644b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor
326534b41d939a1328f484511c6002ba2456db879a29Richard Smithnamespace {
326634b41d939a1328f484511c6002ba2456db879a29Richard Smith  /// Substitute the 'auto' type specifier within a type for a given replacement
326734b41d939a1328f484511c6002ba2456db879a29Richard Smith  /// type.
326834b41d939a1328f484511c6002ba2456db879a29Richard Smith  class SubstituteAutoTransform :
326934b41d939a1328f484511c6002ba2456db879a29Richard Smith    public TreeTransform<SubstituteAutoTransform> {
327034b41d939a1328f484511c6002ba2456db879a29Richard Smith    QualType Replacement;
327134b41d939a1328f484511c6002ba2456db879a29Richard Smith  public:
327234b41d939a1328f484511c6002ba2456db879a29Richard Smith    SubstituteAutoTransform(Sema &SemaRef, QualType Replacement) :
327334b41d939a1328f484511c6002ba2456db879a29Richard Smith      TreeTransform<SubstituteAutoTransform>(SemaRef), Replacement(Replacement) {
327434b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
327534b41d939a1328f484511c6002ba2456db879a29Richard Smith    QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) {
327634b41d939a1328f484511c6002ba2456db879a29Richard Smith      // If we're building the type pattern to deduce against, don't wrap the
327734b41d939a1328f484511c6002ba2456db879a29Richard Smith      // substituted type in an AutoType. Certain template deduction rules
327834b41d939a1328f484511c6002ba2456db879a29Richard Smith      // apply only when a template type parameter appears directly (and not if
327934b41d939a1328f484511c6002ba2456db879a29Richard Smith      // the parameter is found through desugaring). For instance:
328034b41d939a1328f484511c6002ba2456db879a29Richard Smith      //   auto &&lref = lvalue;
328134b41d939a1328f484511c6002ba2456db879a29Richard Smith      // must transform into "rvalue reference to T" not "rvalue reference to
328234b41d939a1328f484511c6002ba2456db879a29Richard Smith      // auto type deduced as T" in order for [temp.deduct.call]p3 to apply.
328334b41d939a1328f484511c6002ba2456db879a29Richard Smith      if (isa<TemplateTypeParmType>(Replacement)) {
328434b41d939a1328f484511c6002ba2456db879a29Richard Smith        QualType Result = Replacement;
328534b41d939a1328f484511c6002ba2456db879a29Richard Smith        TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result);
328634b41d939a1328f484511c6002ba2456db879a29Richard Smith        NewTL.setNameLoc(TL.getNameLoc());
328734b41d939a1328f484511c6002ba2456db879a29Richard Smith        return Result;
328834b41d939a1328f484511c6002ba2456db879a29Richard Smith      } else {
328934b41d939a1328f484511c6002ba2456db879a29Richard Smith        QualType Result = RebuildAutoType(Replacement);
329034b41d939a1328f484511c6002ba2456db879a29Richard Smith        AutoTypeLoc NewTL = TLB.push<AutoTypeLoc>(Result);
329134b41d939a1328f484511c6002ba2456db879a29Richard Smith        NewTL.setNameLoc(TL.getNameLoc());
329234b41d939a1328f484511c6002ba2456db879a29Richard Smith        return Result;
329334b41d939a1328f484511c6002ba2456db879a29Richard Smith      }
329434b41d939a1328f484511c6002ba2456db879a29Richard Smith    }
329534b41d939a1328f484511c6002ba2456db879a29Richard Smith  };
329634b41d939a1328f484511c6002ba2456db879a29Richard Smith}
329734b41d939a1328f484511c6002ba2456db879a29Richard Smith
329834b41d939a1328f484511c6002ba2456db879a29Richard Smith/// \brief Deduce the type for an auto type-specifier (C++0x [dcl.spec.auto]p6)
329934b41d939a1328f484511c6002ba2456db879a29Richard Smith///
330034b41d939a1328f484511c6002ba2456db879a29Richard Smith/// \param Type the type pattern using the auto type-specifier.
330134b41d939a1328f484511c6002ba2456db879a29Richard Smith///
330234b41d939a1328f484511c6002ba2456db879a29Richard Smith/// \param Init the initializer for the variable whose type is to be deduced.
330334b41d939a1328f484511c6002ba2456db879a29Richard Smith///
330434b41d939a1328f484511c6002ba2456db879a29Richard Smith/// \param Result if type deduction was successful, this will be set to the
330534b41d939a1328f484511c6002ba2456db879a29Richard Smith/// deduced type. This may still contain undeduced autos if the type is
3306a085da86242c9b8e3466f8cf6f4397e9f248fd20Richard Smith/// dependent. This will be set to null if deduction succeeded, but auto
3307a085da86242c9b8e3466f8cf6f4397e9f248fd20Richard Smith/// substitution failed; the appropriate diagnostic will already have been
3308a085da86242c9b8e3466f8cf6f4397e9f248fd20Richard Smith/// produced in that case.
330934b41d939a1328f484511c6002ba2456db879a29Richard Smith///
331034b41d939a1328f484511c6002ba2456db879a29Richard Smith/// \returns true if deduction succeeded, false if it failed.
331134b41d939a1328f484511c6002ba2456db879a29Richard Smithbool
3312a085da86242c9b8e3466f8cf6f4397e9f248fd20Richard SmithSema::DeduceAutoType(TypeSourceInfo *Type, Expr *Init,
3313a085da86242c9b8e3466f8cf6f4397e9f248fd20Richard Smith                     TypeSourceInfo *&Result) {
331434b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (Init->isTypeDependent()) {
331534b41d939a1328f484511c6002ba2456db879a29Richard Smith    Result = Type;
331634b41d939a1328f484511c6002ba2456db879a29Richard Smith    return true;
331734b41d939a1328f484511c6002ba2456db879a29Richard Smith  }
331834b41d939a1328f484511c6002ba2456db879a29Richard Smith
331934b41d939a1328f484511c6002ba2456db879a29Richard Smith  SourceLocation Loc = Init->getExprLoc();
332034b41d939a1328f484511c6002ba2456db879a29Richard Smith
332134b41d939a1328f484511c6002ba2456db879a29Richard Smith  LocalInstantiationScope InstScope(*this);
332234b41d939a1328f484511c6002ba2456db879a29Richard Smith
332334b41d939a1328f484511c6002ba2456db879a29Richard Smith  // Build template<class TemplParam> void Func(FuncParam);
33244fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  TemplateTypeParmDecl *TemplParam =
33254fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth    TemplateTypeParmDecl::Create(Context, 0, SourceLocation(), Loc, 0, 0, 0,
33264fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth                                 false, false);
33274fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0);
33284fb86f8c4585e53c21c847ad3de9e3b2de123cd9Chandler Carruth  NamedDecl *TemplParamPtr = TemplParam;
3329483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  FixedSizeTemplateParameterList<1> TemplateParams(Loc, Loc, &TemplParamPtr,
3330483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith                                                   Loc);
3331483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith
3332a085da86242c9b8e3466f8cf6f4397e9f248fd20Richard Smith  TypeSourceInfo *FuncParamInfo =
333334b41d939a1328f484511c6002ba2456db879a29Richard Smith    SubstituteAutoTransform(*this, TemplArg).TransformType(Type);
3334a085da86242c9b8e3466f8cf6f4397e9f248fd20Richard Smith  assert(FuncParamInfo && "substituting template parameter for 'auto' failed");
3335a085da86242c9b8e3466f8cf6f4397e9f248fd20Richard Smith  QualType FuncParam = FuncParamInfo->getType();
333634b41d939a1328f484511c6002ba2456db879a29Richard Smith
333734b41d939a1328f484511c6002ba2456db879a29Richard Smith  // Deduce type of TemplParam in Func(Init)
333834b41d939a1328f484511c6002ba2456db879a29Richard Smith  llvm::SmallVector<DeducedTemplateArgument, 1> Deduced;
333934b41d939a1328f484511c6002ba2456db879a29Richard Smith  Deduced.resize(1);
334034b41d939a1328f484511c6002ba2456db879a29Richard Smith  QualType InitType = Init->getType();
334134b41d939a1328f484511c6002ba2456db879a29Richard Smith  unsigned TDF = 0;
3342483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  if (AdjustFunctionParmAndArgTypesForDeduction(*this, &TemplateParams,
334334b41d939a1328f484511c6002ba2456db879a29Richard Smith                                                FuncParam, InitType, Init,
334434b41d939a1328f484511c6002ba2456db879a29Richard Smith                                                TDF))
334534b41d939a1328f484511c6002ba2456db879a29Richard Smith    return false;
334634b41d939a1328f484511c6002ba2456db879a29Richard Smith
334734b41d939a1328f484511c6002ba2456db879a29Richard Smith  TemplateDeductionInfo Info(Context, Loc);
3348483b9f3bc05c5409e2c6643f1c9d91e21c8ff9d2Richard Smith  if (::DeduceTemplateArguments(*this, &TemplateParams,
334934b41d939a1328f484511c6002ba2456db879a29Richard Smith                                FuncParam, InitType, Info, Deduced,
335034b41d939a1328f484511c6002ba2456db879a29Richard Smith                                TDF))
335134b41d939a1328f484511c6002ba2456db879a29Richard Smith    return false;
335234b41d939a1328f484511c6002ba2456db879a29Richard Smith
335334b41d939a1328f484511c6002ba2456db879a29Richard Smith  QualType DeducedType = Deduced[0].getAsType();
335434b41d939a1328f484511c6002ba2456db879a29Richard Smith  if (DeducedType.isNull())
335534b41d939a1328f484511c6002ba2456db879a29Richard Smith    return false;
33569a636e8403287af0d4db8fe5bf49dee719f5b754Douglas Gregor
335734b41d939a1328f484511c6002ba2456db879a29Richard Smith  Result = SubstituteAutoTransform(*this, DeducedType).TransformType(Type);
33589a636e8403287af0d4db8fe5bf49dee719f5b754Douglas Gregor
33599a636e8403287af0d4db8fe5bf49dee719f5b754Douglas Gregor  // Check that the deduced argument type is compatible with the original
33609a636e8403287af0d4db8fe5bf49dee719f5b754Douglas Gregor  // argument type per C++ [temp.deduct.call]p4.
33619a636e8403287af0d4db8fe5bf49dee719f5b754Douglas Gregor  if (Result &&
33629a636e8403287af0d4db8fe5bf49dee719f5b754Douglas Gregor      CheckOriginalCallArgDeduction(*this,
33639a636e8403287af0d4db8fe5bf49dee719f5b754Douglas Gregor                                    Sema::OriginalCallArg(FuncParam,0,InitType),
33649a636e8403287af0d4db8fe5bf49dee719f5b754Douglas Gregor                                    Result->getType())) {
33659a636e8403287af0d4db8fe5bf49dee719f5b754Douglas Gregor    Result = 0;
33669a636e8403287af0d4db8fe5bf49dee719f5b754Douglas Gregor    return false;
33679a636e8403287af0d4db8fe5bf49dee719f5b754Douglas Gregor  }
33689a636e8403287af0d4db8fe5bf49dee719f5b754Douglas Gregor
336934b41d939a1328f484511c6002ba2456db879a29Richard Smith  return true;
337034b41d939a1328f484511c6002ba2456db879a29Richard Smith}
337134b41d939a1328f484511c6002ba2456db879a29Richard Smith
33728a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic void
3373e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, QualType T,
3374e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
3375ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Level,
3376e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Deduced);
3377dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3378dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \brief If this is a non-static member function,
337977bc572138543ef97ebec137522b763d6a6ee909Douglas Gregorstatic void MaybeAddImplicitObjectParameterType(ASTContext &Context,
338077bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor                                                CXXMethodDecl *Method,
338177bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor                                 llvm::SmallVectorImpl<QualType> &ArgTypes) {
338277bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  if (Method->isStatic())
338377bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor    return;
338477bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor
338577bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  // C++ [over.match.funcs]p4:
338677bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  //
338777bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  //   For non-static member functions, the type of the implicit
338877bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  //   object parameter is
33890099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi  //     - "lvalue reference to cv X" for functions declared without a
339077bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  //       ref-qualifier or with the & ref-qualifier
339177bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  //     - "rvalue reference to cv X" for functions declared with the
339277bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  //       && ref-qualifier
339377bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  //
339477bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  // FIXME: We don't have ref-qualifiers yet, so we don't do that part.
339577bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  QualType ArgTy = Context.getTypeDeclType(Method->getParent());
339677bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  ArgTy = Context.getQualifiedType(ArgTy,
339777bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor                        Qualifiers::fromCVRMask(Method->getTypeQualifiers()));
339877bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  ArgTy = Context.getLValueReferenceType(ArgTy);
339977bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor  ArgTypes.push_back(ArgTy);
340077bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor}
340177bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor
34028a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Determine whether the function template \p FT1 is at least as
34038a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// specialized as \p FT2.
34048a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic bool isAtLeastAsSpecializedAs(Sema &S,
34055769d6195087229770d7ac90449443e026c47103John McCall                                     SourceLocation Loc,
34068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                     FunctionTemplateDecl *FT1,
34078a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                     FunctionTemplateDecl *FT2,
34088a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                     TemplatePartialOrderingContext TPOC,
3409dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                     unsigned NumCallArguments,
3410b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *RefParamComparisons) {
34118a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  FunctionDecl *FD1 = FT1->getTemplatedDecl();
3412dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  FunctionDecl *FD2 = FT2->getTemplatedDecl();
34138a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
34148a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
3415dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
34168a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  assert(Proto1 && Proto2 && "Function templates must have prototypes");
34178a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
341802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
34198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Deduced.resize(TemplateParams->size());
34208a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
34218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p3:
34228a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   The types used to determine the ordering depend on the context in which
34238a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   the partial ordering is done:
34242a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  TemplateDeductionInfo Info(S.Context, Loc);
34258d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor  CXXMethodDecl *Method1 = 0;
34268d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor  CXXMethodDecl *Method2 = 0;
34278d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor  bool IsNonStatic2 = false;
34288d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor  bool IsNonStatic1 = false;
34298d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor  unsigned Skip2 = 0;
34308a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  switch (TPOC) {
34318a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Call: {
34328a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   - In the context of a function call, the function parameter types are
34338a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //     used.
34348d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    Method1 = dyn_cast<CXXMethodDecl>(FD1);
34358d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    Method2 = dyn_cast<CXXMethodDecl>(FD2);
34368d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    IsNonStatic1 = Method1 && !Method1->isStatic();
34378d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    IsNonStatic2 = Method2 && !Method2->isStatic();
34388d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor
34398d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    // C++0x [temp.func.order]p3:
34408d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    //   [...] If only one of the function templates is a non-static
34418d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    //   member, that function template is considered to have a new
34428d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    //   first parameter inserted in its function parameter list. The
34438d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    //   new parameter is of type "reference to cv A," where cv are
34448d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    //   the cv-qualifiers of the function template (if any) and A is
34458d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    //   the class of which the function template is a member.
34468d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    //
34478d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    // C++98/03 doesn't have this provision, so instead we drop the
34488d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    // first argument of the free function or static member, which
34498d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    // seems to match existing practice.
345077bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor    llvm::SmallVector<QualType, 4> Args1;
3451dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    unsigned Skip1 = !S.getLangOptions().CPlusPlus0x &&
34528d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor      IsNonStatic2 && !IsNonStatic1;
34538d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    if (S.getLangOptions().CPlusPlus0x && IsNonStatic1 && !IsNonStatic2)
3454dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      MaybeAddImplicitObjectParameterType(S.Context, Method1, Args1);
3455dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    Args1.insert(Args1.end(),
34568d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor                 Proto1->arg_type_begin() + Skip1, Proto1->arg_type_end());
345777bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor
345877bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor    llvm::SmallVector<QualType, 4> Args2;
3459dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    Skip2 = !S.getLangOptions().CPlusPlus0x &&
34608d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor      IsNonStatic1 && !IsNonStatic2;
34618d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    if (S.getLangOptions().CPlusPlus0x && IsNonStatic2 && !IsNonStatic1)
346277bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor      MaybeAddImplicitObjectParameterType(S.Context, Method2, Args2);
3463dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    Args2.insert(Args2.end(),
34648d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor                 Proto2->arg_type_begin() + Skip2, Proto2->arg_type_end());
3465dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
34665c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    // C++ [temp.func.order]p5:
34675c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //   The presence of unused ellipsis and default arguments has no effect on
34685c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    //   the partial ordering of function templates.
34695c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    if (Args1.size() > NumCallArguments)
34705c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      Args1.resize(NumCallArguments);
34715c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    if (Args2.size() > NumCallArguments)
34725c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor      Args2.resize(NumCallArguments);
34735c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(),
34745c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                Args1.data(), Args1.size(), Info, Deduced,
34755c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                TDF_None, /*PartialOrdering=*/true,
3476b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                                RefParamComparisons))
34778a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        return false;
3478dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
34798a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
34808a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
3481dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
34828a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Conversion:
34838a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   - In the context of a call to a conversion operator, the return types
34848a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //     of the conversion function templates are used.
34855c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor    if (DeduceTemplateArguments(S, TemplateParams, Proto2->getResultType(),
34865c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                Proto1->getResultType(), Info, Deduced,
34875c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                TDF_None, /*PartialOrdering=*/true,
3488b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                                RefParamComparisons))
34898a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      return false;
34908a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
3491dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
34928a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Other:
34930099530a2288df7c2140dd8992b7310b9f6930a9NAKAMURA Takumi    //   - In other contexts (14.6.6.2) the function template's function type
34948a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //     is used.
3495dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    if (DeduceTemplateArguments(S, TemplateParams, FD2->getType(),
34965c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                FD1->getType(), Info, Deduced, TDF_None,
3497b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                                /*PartialOrdering=*/true, RefParamComparisons))
34988a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      return false;
34998a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
35008a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
3501dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
35028a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p11:
3503dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   In most cases, all template parameters must have values in order for
3504dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   deduction to succeed, but for partial ordering purposes a template
3505dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   parameter may remain without a value provided it is not used in the
35068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   types being used for partial ordering. [ Note: a template parameter used
35078a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   in a non-deduced context is considered used. -end note]
35088a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  unsigned ArgIdx = 0, NumArgs = Deduced.size();
35098a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  for (; ArgIdx != NumArgs; ++ArgIdx)
35108a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    if (Deduced[ArgIdx].isNull())
35118a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      break;
35128a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
35138a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  if (ArgIdx == NumArgs) {
3514dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    // All template arguments were deduced. FT1 is at least as specialized
35158a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // as FT2.
35168a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return true;
35178a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
35188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
3519e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // Figure out which template parameters were used.
35208a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  llvm::SmallVector<bool, 4> UsedParameters;
35218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  UsedParameters.resize(TemplateParams->size());
35228a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  switch (TPOC) {
35238a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Call: {
3524dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    unsigned NumParams = std::min(NumCallArguments,
3525dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                  std::min(Proto1->getNumArgs(),
35265c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                           Proto2->getNumArgs()));
35278d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    if (S.getLangOptions().CPlusPlus0x && IsNonStatic2 && !IsNonStatic1)
3528dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      ::MarkUsedTemplateParameters(S, Method2->getThisType(S.Context), false,
35298d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor                                   TemplateParams->getDepth(), UsedParameters);
35308d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor    for (unsigned I = Skip2; I < NumParams; ++I)
3531dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
3532ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                   TemplateParams->getDepth(),
3533e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                   UsedParameters);
35348a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
35358a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
3536dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
35378a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Conversion:
3538dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
3539ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 TemplateParams->getDepth(),
3540e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                 UsedParameters);
35418a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
3542dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
35438a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Other:
3544dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    ::MarkUsedTemplateParameters(S, FD2->getType(), false,
3545ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 TemplateParams->getDepth(),
3546ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 UsedParameters);
35478a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
35488a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
3549dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
35508a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  for (; ArgIdx != NumArgs; ++ArgIdx)
35518a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // If this argument had no value deduced but was used in one of the types
35528a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // used for partial ordering, then deduction fails.
35538a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
35548a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      return false;
3555dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
35568a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  return true;
35578a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor}
3558dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
35599da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor/// \brief Determine whether this a function template whose parameter-type-list
35609da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor/// ends with a function parameter pack.
35619da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregorstatic bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) {
35629da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  FunctionDecl *Function = FunTmpl->getTemplatedDecl();
35639da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  unsigned NumParams = Function->getNumParams();
35649da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  if (NumParams == 0)
35659da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor    return false;
3566dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
35679da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  ParmVarDecl *Last = Function->getParamDecl(NumParams - 1);
35689da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  if (!Last->isParameterPack())
35699da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor    return false;
3570dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
35719da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  // Make sure that no previous parameter is a parameter pack.
35729da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  while (--NumParams > 0) {
35739da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor    if (Function->getParamDecl(NumParams - 1)->isParameterPack())
35749da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor      return false;
35759da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  }
3576dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
35779da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  return true;
35789da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor}
3579dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3580bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \brief Returns the more specialized function template according
358165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// to the rules of function template partial ordering (C++ [temp.func.order]).
358265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor///
358365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \param FT1 the first function template
358465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor///
358565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \param FT2 the second function template
358665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor///
35878a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param TPOC the context in which we are performing partial ordering of
35888a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// function templates.
35891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
35905c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// \param NumCallArguments The number of arguments in a call, used only
35915c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// when \c TPOC is \c TPOC_Call.
35925c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor///
3593bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \returns the more specialized function template. If neither
359465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// template is more specialized, returns NULL.
359565ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorFunctionTemplateDecl *
359665ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
359765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                 FunctionTemplateDecl *FT2,
35985769d6195087229770d7ac90449443e026c47103John McCall                                 SourceLocation Loc,
35995c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                 TemplatePartialOrderingContext TPOC,
36005c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                 unsigned NumCallArguments) {
3601b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor  llvm::SmallVector<RefParamPartialOrderingComparison, 4> RefParamComparisons;
3602dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC,
36035c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                          NumCallArguments, 0);
3604dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC,
36055c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                          NumCallArguments,
3606b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                                          &RefParamComparisons);
3607dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
36088a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  if (Better1 != Better2) // We have a clear winner
36098a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return Better1? FT1 : FT2;
3610dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
36118a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  if (!Better1 && !Better2) // Neither is better than the other
361265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    return 0;
36138a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
36148a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p10:
3615dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   If for each type being considered a given template is at least as
36168a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   specialized for all types and more specialized for some set of types and
3617dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   the other template is not more specialized for any types or is not at
36188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   least as specialized for any types, then the given template is more
36198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   specialized than the other template. Otherwise, neither template is more
36208a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   specialized than the other.
36218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Better1 = false;
36228a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Better2 = false;
3623b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor  for (unsigned I = 0, N = RefParamComparisons.size(); I != N; ++I) {
36248a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // C++0x [temp.deduct.partial]p9:
36258a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   If, for a given type, deduction succeeds in both directions (i.e., the
3626b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //   types are identical after the transformations above) and both P and A
3627b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //   were reference types (before being replaced with the type referred to
3628b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //   above):
3629b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor
3630dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //     -- if the type from the argument template was an lvalue reference
3631b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //        and the type from the parameter template was not, the argument
3632b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //        type is considered to be more specialized than the other;
3633b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //        otherwise,
3634b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    if (!RefParamComparisons[I].ArgIsRvalueRef &&
3635b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor        RefParamComparisons[I].ParamIsRvalueRef) {
3636b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      Better2 = true;
3637b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      if (Better1)
3638b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor        return 0;
3639b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      continue;
3640b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    } else if (!RefParamComparisons[I].ParamIsRvalueRef &&
3641b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor               RefParamComparisons[I].ArgIsRvalueRef) {
3642b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      Better1 = true;
3643b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      if (Better2)
3644b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor        return 0;
3645b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      continue;
3646b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    }
3647dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3648b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //     -- if the type from the argument template is more cv-qualified than
3649b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //        the type from the parameter template (as described above), the
3650b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //        argument type is considered to be more specialized than the
3651b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //        other; otherwise,
3652b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    switch (RefParamComparisons[I].Qualifiers) {
3653b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    case NeitherMoreQualified:
3654b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      break;
3655dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3656b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    case ParamMoreQualified:
3657b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      Better1 = true;
3658b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      if (Better2)
3659b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor        return 0;
3660b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      continue;
3661dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3662b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    case ArgMoreQualified:
3663b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      Better2 = true;
3664b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      if (Better1)
3665b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor        return 0;
3666b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor      continue;
36678a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    }
3668dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3669b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor    //     -- neither type is more specialized than the other.
36708a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
3671dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
36728a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  assert(!(Better1 && Better2) && "Should have broken out in the loop above");
367365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (Better1)
367465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    return FT1;
36758a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  else if (Better2)
36768a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return FT2;
3677dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
36789da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  // FIXME: This mimics what GCC implements, but doesn't match up with the
36799da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  // proposed resolution for core issue 692. This area needs to be sorted out,
36809da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  // but for now we attempt to maintain compatibility.
36819da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  bool Variadic1 = isVariadicFunctionTemplate(FT1);
36829da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  bool Variadic2 = isVariadicFunctionTemplate(FT2);
36839da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  if (Variadic1 != Variadic2)
36849da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor    return Variadic1? FT2 : FT1;
3685dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
36869da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor  return 0;
368765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor}
368883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
3689d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \brief Determine if the two templates are equivalent.
3690d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregorstatic bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
3691d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  if (T1 == T2)
3692d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    return true;
3693dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3694d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  if (!T1 || !T2)
3695d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    return false;
3696dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3697d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  return T1->getCanonicalDecl() == T2->getCanonicalDecl();
3698d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor}
3699d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
3700d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \brief Retrieve the most specialized of the given function template
3701d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specializations.
3702d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3703c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// \param SpecBegin the start iterator of the function template
3704c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// specializations that we will be comparing.
3705d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3706c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// \param SpecEnd the end iterator of the function template
3707c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// specializations, paired with \p SpecBegin.
3708d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3709d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param TPOC the partial ordering context to use to compare the function
3710d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// template specializations.
3711d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
37125c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// \param NumCallArguments The number of arguments in a call, used only
37135c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// when \c TPOC is \c TPOC_Call.
37145c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor///
3715dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \param Loc the location where the ambiguity or no-specializations
3716d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// diagnostic should occur.
3717d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3718d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param NoneDiag partial diagnostic used to diagnose cases where there are
3719d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// no matching candidates.
3720d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3721d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
3722d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// occurs.
3723d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3724d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param CandidateDiag partial diagnostic used for each function template
3725d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specialization that is a candidate in the ambiguous ordering. One parameter
3726d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// in this diagnostic should be unbound, which will correspond to the string
3727d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// describing the template arguments for the function template specialization.
3728d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3729dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \param Index if non-NULL and the result of this function is non-nULL,
3730d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// receives the index corresponding to the resulting function template
3731d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specialization.
3732d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3733dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \returns the most specialized function template specialization, if
3734c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// found. Otherwise, returns SpecEnd.
3735d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
3736dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
3737d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// template argument deduction.
3738c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallUnresolvedSetIterator
3739c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallSema::getMostSpecialized(UnresolvedSetIterator SpecBegin,
37405c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                        UnresolvedSetIterator SpecEnd,
3741c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         TemplatePartialOrderingContext TPOC,
37425c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                         unsigned NumCallArguments,
3743c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         SourceLocation Loc,
3744c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         const PartialDiagnostic &NoneDiag,
3745c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         const PartialDiagnostic &AmbigDiag,
37461be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor                         const PartialDiagnostic &CandidateDiag,
37471be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor                         bool Complain) {
3748c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  if (SpecBegin == SpecEnd) {
37491be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor    if (Complain)
37501be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor      Diag(Loc, NoneDiag);
3751c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    return SpecEnd;
3752d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
3753dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3754dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  if (SpecBegin + 1 == SpecEnd)
3755c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    return SpecBegin;
3756dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3757d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // Find the function template that is better than all of the templates it
3758d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // has been compared to.
3759c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  UnresolvedSetIterator Best = SpecBegin;
3760dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  FunctionTemplateDecl *BestTemplate
3761c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
3762d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  assert(BestTemplate && "Not a function template specialization?");
3763c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
3764c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    FunctionTemplateDecl *Challenger
3765c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall      = cast<FunctionDecl>(*I)->getPrimaryTemplate();
3766d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    assert(Challenger && "Not a function template specialization?");
3767c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
37685c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                                  Loc, TPOC, NumCallArguments),
3769d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor                       Challenger)) {
3770d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      Best = I;
3771d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      BestTemplate = Challenger;
3772d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    }
3773d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
3774dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3775d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // Make sure that the "best" function template is more specialized than all
3776d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // of the others.
3777d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  bool Ambiguous = false;
3778c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
3779c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    FunctionTemplateDecl *Challenger
3780c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall      = cast<FunctionDecl>(*I)->getPrimaryTemplate();
3781d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    if (I != Best &&
3782dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi        !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
37835c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                                   Loc, TPOC, NumCallArguments),
3784d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor                        BestTemplate)) {
3785d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      Ambiguous = true;
3786d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      break;
3787d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    }
3788d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
3789dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3790d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  if (!Ambiguous) {
3791d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    // We found an answer. Return it.
3792c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    return Best;
3793d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
3794dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3795d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // Diagnose the ambiguity.
37961be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor  if (Complain)
37971be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor    Diag(Loc, AmbigDiag);
3798dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
37991be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor  if (Complain)
3800d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // FIXME: Can we order the candidates in some sane way?
38011be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor    for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I)
38021be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor      Diag((*I)->getLocation(), CandidateDiag)
38031be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor        << getTemplateArgumentBindingsText(
38041be8eec3ddd2a23c19b453c2639226174eb5d4a8Douglas Gregor          cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
3805c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                    *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
3806dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3807c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  return SpecEnd;
3808d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor}
3809d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
3810bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \brief Returns the more specialized class template partial specialization
3811bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// according to the rules of partial ordering of class template partial
3812bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// specializations (C++ [temp.class.order]).
3813bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor///
3814bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \param PS1 the first class template partial specialization
3815bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor///
3816bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \param PS2 the second class template partial specialization
3817bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor///
3818bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \returns the more specialized class template partial specialization. If
3819bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// neither partial specialization is more specialized, returns NULL.
3820bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas GregorClassTemplatePartialSpecializationDecl *
3821bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas GregorSema::getMoreSpecializedPartialSpecialization(
3822bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                  ClassTemplatePartialSpecializationDecl *PS1,
38235769d6195087229770d7ac90449443e026c47103John McCall                                  ClassTemplatePartialSpecializationDecl *PS2,
38245769d6195087229770d7ac90449443e026c47103John McCall                                              SourceLocation Loc) {
3825bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // C++ [temp.class.order]p1:
3826bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   For two class template partial specializations, the first is at least as
3827dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   specialized as the second if, given the following rewrite to two
3828dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   function templates, the first function template is at least as
3829dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   specialized as the second according to the ordering rules for function
3830bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   templates (14.6.6.2):
3831bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //     - the first function template has the same template parameters as the
3832dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //       first partial specialization and has a single function parameter
3833dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //       whose type is a class template specialization with the template
3834bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       arguments of the first partial specialization, and
3835bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //     - the second function template has the same template parameters as the
3836dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //       second partial specialization and has a single function parameter
3837dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //       whose type is a class template specialization with the template
3838bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       arguments of the second partial specialization.
3839bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //
384031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // Rather than synthesize function templates, we merely perform the
384131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // equivalent partial ordering by performing deduction directly on
384231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // the template arguments of the class template partial
384331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // specializations. This computation is slightly simpler than the
384431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // general problem of function template partial ordering, because
384531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // class template partial specializations are more constrained. We
384631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // know that every template parameter is deducible from the class
384731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // template partial specialization's template arguments, for
384831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // example.
384902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
38502a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall  TemplateDeductionInfo Info(Context, Loc);
385131f17ecbef57b5679c017c375db330546b7b5145John McCall
385231f17ecbef57b5679c017c375db330546b7b5145John McCall  QualType PT1 = PS1->getInjectedSpecializationType();
385331f17ecbef57b5679c017c375db330546b7b5145John McCall  QualType PT2 = PS2->getInjectedSpecializationType();
3854dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3855bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // Determine whether PS1 is at least as specialized as PS2
3856bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  Deduced.resize(PS2->getTemplateParameters()->size());
38575c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor  bool Better1 = !::DeduceTemplateArguments(*this, PS2->getTemplateParameters(),
38585c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                            PT2, PT1, Info, Deduced, TDF_None,
3859dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                            /*PartialOrdering=*/true,
3860b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                                            /*RefParamComparisons=*/0);
38612c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis  if (Better1) {
38622c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis    InstantiatingTemplate Inst(*this, PS2->getLocation(), PS2,
38632c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis                               Deduced.data(), Deduced.size(), Info);
3864dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    Better1 = !::FinishTemplateArgumentDeduction(*this, PS2,
3865dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                 PS1->getTemplateArgs(),
3866516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor                                                 Deduced, Info);
38672c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis  }
3868dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3869bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // Determine whether PS2 is at least as specialized as PS1
3870db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Deduced.clear();
3871bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  Deduced.resize(PS1->getTemplateParameters()->size());
38725c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor  bool Better2 = !::DeduceTemplateArguments(*this, PS1->getTemplateParameters(),
38735c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                            PT1, PT2, Info, Deduced, TDF_None,
38745c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor                                            /*PartialOrdering=*/true,
3875b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor                                            /*RefParamComparisons=*/0);
38762c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis  if (Better2) {
38772c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis    InstantiatingTemplate Inst(*this, PS1->getLocation(), PS1,
38782c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis                               Deduced.data(), Deduced.size(), Info);
3879dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    Better2 = !::FinishTemplateArgumentDeduction(*this, PS1,
3880dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                                                 PS2->getTemplateArgs(),
3881516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor                                                 Deduced, Info);
38822c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis  }
3883dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3884bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  if (Better1 == Better2)
3885bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    return 0;
3886dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3887bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  return Better1? PS1 : PS2;
3888bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor}
3889bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor
38901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
3891e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
3892e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           const TemplateArgument &TemplateArg,
3893e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
3894ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
3895e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used);
3896031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3897e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
3898031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// expression.
38991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
3900e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
3901e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           const Expr *E,
3902e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
3903ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
3904e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
3905be230c36e32142cbdcdbe9c97511d097beeecbabDouglas Gregor  // We can deduce from a pack expansion.
3906be230c36e32142cbdcdbe9c97511d097beeecbabDouglas Gregor  if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E))
3907be230c36e32142cbdcdbe9c97511d097beeecbabDouglas Gregor    E = Expansion->getPattern();
3908dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
390954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  // Skip through any implicit casts we added while type-checking.
391054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor  while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E))
391154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor    E = ICE->getSubExpr();
3912dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3913dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
3914e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // find other occurrences of template parameters.
3915f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor  const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
3916c781f9cd854f3d5d1c826f4a13382c6abca4cff7Douglas Gregor  if (!DRE)
3917031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    return;
3918031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
39191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const NonTypeTemplateParmDecl *NTTP
3920031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
3921031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  if (!NTTP)
3922031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    return;
3923031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3924ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (NTTP->getDepth() == Depth)
3925ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Used[NTTP->getIndex()] = true;
3926031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
3927031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3928e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
3929e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// nested name specifier.
3930e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregorstatic void
3931e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
3932e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           NestedNameSpecifier *NNS,
3933e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
3934ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
3935e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
3936e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (!NNS)
3937e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    return;
3938dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3939ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
3940ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             Used);
3941dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
3942ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             OnlyDeduced, Depth, Used);
3943e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor}
3944dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3945e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
3946e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// template name.
3947e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregorstatic void
3948e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
3949e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           TemplateName Name,
3950e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
3951ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
3952e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
3953e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
3954e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    if (TemplateTemplateParmDecl *TTP
3955ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor          = dyn_cast<TemplateTemplateParmDecl>(Template)) {
3956ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      if (TTP->getDepth() == Depth)
3957ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        Used[TTP->getIndex()] = true;
3958ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    }
3959e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    return;
3960e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  }
3961dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3962788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
3963dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
3964788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                               Depth, Used);
3965e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
3966dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
3967ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
3968e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor}
3969e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
3970e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
3971031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// type.
39721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
3973e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, QualType T,
3974e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
3975ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
3976e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
3977e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (T.isNull())
3978e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    return;
3979dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
3980031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  // Non-dependent types have nothing deducible
3981031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  if (!T->isDependentType())
3982031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    return;
3983031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3984031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  T = SemaRef.Context.getCanonicalType(T);
3985031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  switch (T->getTypeClass()) {
3986031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Pointer:
3987e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
3988e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<PointerType>(T)->getPointeeType(),
3989e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               OnlyDeduced,
3990ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth,
3991e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               Used);
3992031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
3993031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
3994031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::BlockPointer:
3995e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
3996e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<BlockPointerType>(T)->getPointeeType(),
3997e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               OnlyDeduced,
3998ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth,
3999e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               Used);
4000031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
4001031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
4002031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::LValueReference:
4003031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::RValueReference:
4004e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
4005e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<ReferenceType>(T)->getPointeeType(),
4006e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               OnlyDeduced,
4007ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth,
4008e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               Used);
4009031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
4010031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
4011031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::MemberPointer: {
4012031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
4013e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
4014ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
4015e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
4016ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
4017031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
4018031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
4019031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
4020031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::DependentSizedArray:
4021e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
4022e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<DependentSizedArrayType>(T)->getSizeExpr(),
4023ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
4024031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    // Fall through to check the element type
4025031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
4026031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::ConstantArray:
4027031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::IncompleteArray:
4028e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
4029e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<ArrayType>(T)->getElementType(),
4030ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
4031031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
4032031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
4033031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Vector:
4034031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::ExtVector:
4035e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
4036e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<VectorType>(T)->getElementType(),
4037ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
4038031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
4039031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
40409cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  case Type::DependentSizedExtVector: {
40419cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    const DependentSizedExtVectorType *VecType
4042f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor      = cast<DependentSizedExtVectorType>(T);
4043e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
4044ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
4045dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
4046ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
40479cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    break;
40489cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
40499cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
4050031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::FunctionProto: {
4051f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor    const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
4052e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
4053ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
4054031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
4055e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor      MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
4056ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 Depth, Used);
4057031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
4058031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
4059031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
4060ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  case Type::TemplateTypeParm: {
4061ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
4062ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (TTP->getDepth() == Depth)
4063ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      Used[TTP->getIndex()] = true;
4064031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
4065ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
4066031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
40670bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  case Type::SubstTemplateTypeParmPack: {
40680bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    const SubstTemplateTypeParmPackType *Subst
40690bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor      = cast<SubstTemplateTypeParmPackType>(T);
4070dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    MarkUsedTemplateParameters(SemaRef,
40710bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor                               QualType(Subst->getReplacedParameter(), 0),
40720bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor                               OnlyDeduced, Depth, Used);
40730bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    MarkUsedTemplateParameters(SemaRef, Subst->getArgumentPack(),
40740bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor                               OnlyDeduced, Depth, Used);
40750bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor    break;
40760bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor  }
40770bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor
407831f17ecbef57b5679c017c375db330546b7b5145John McCall  case Type::InjectedClassName:
407931f17ecbef57b5679c017c375db330546b7b5145John McCall    T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
408031f17ecbef57b5679c017c375db330546b7b5145John McCall    // fall through
408131f17ecbef57b5679c017c375db330546b7b5145John McCall
4082031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::TemplateSpecialization: {
40831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const TemplateSpecializationType *Spec
4084f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor      = cast<TemplateSpecializationType>(T);
4085e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
4086ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
4087dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
40887b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    // C++0x [temp.deduct.type]p9:
4089dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   If the template argument list of P contains a pack expansion that is not
4090dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   the last template argument, the entire template argument list is a
40917b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    //   non-deduced context.
4092dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    if (OnlyDeduced &&
40937b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor        hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs()))
40947b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      break;
40957b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor
4096e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
4097ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
4098ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 Used);
4099e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    break;
4100e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  }
41011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4102e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  case Type::Complex:
4103e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    if (!OnlyDeduced)
4104dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi      MarkUsedTemplateParameters(SemaRef,
4105e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                 cast<ComplexType>(T)->getElementType(),
4106ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 OnlyDeduced, Depth, Used);
4107e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    break;
4108031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
41094714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  case Type::DependentName:
4110e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    if (!OnlyDeduced)
4111e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor      MarkUsedTemplateParameters(SemaRef,
41124714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor                                 cast<DependentNameType>(T)->getQualifier(),
4113ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 OnlyDeduced, Depth, Used);
4114031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
4115031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
411633500955d731c73717af52088b7fc0e7a85681e7John McCall  case Type::DependentTemplateSpecialization: {
411733500955d731c73717af52088b7fc0e7a85681e7John McCall    const DependentTemplateSpecializationType *Spec
411833500955d731c73717af52088b7fc0e7a85681e7John McCall      = cast<DependentTemplateSpecializationType>(T);
411933500955d731c73717af52088b7fc0e7a85681e7John McCall    if (!OnlyDeduced)
412033500955d731c73717af52088b7fc0e7a85681e7John McCall      MarkUsedTemplateParameters(SemaRef, Spec->getQualifier(),
412133500955d731c73717af52088b7fc0e7a85681e7John McCall                                 OnlyDeduced, Depth, Used);
4122dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
41237b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    // C++0x [temp.deduct.type]p9:
4124dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   If the template argument list of P contains a pack expansion that is not
4125dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    //   the last template argument, the entire template argument list is a
41267b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    //   non-deduced context.
4127dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    if (OnlyDeduced &&
41287b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor        hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs()))
41297b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      break;
41307b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor
413133500955d731c73717af52088b7fc0e7a85681e7John McCall    for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
413233500955d731c73717af52088b7fc0e7a85681e7John McCall      MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
413333500955d731c73717af52088b7fc0e7a85681e7John McCall                                 Used);
413433500955d731c73717af52088b7fc0e7a85681e7John McCall    break;
413533500955d731c73717af52088b7fc0e7a85681e7John McCall  }
413633500955d731c73717af52088b7fc0e7a85681e7John McCall
4137ad5e73887052193afda72db8efcb812bd083a4a8John McCall  case Type::TypeOf:
4138ad5e73887052193afda72db8efcb812bd083a4a8John McCall    if (!OnlyDeduced)
4139ad5e73887052193afda72db8efcb812bd083a4a8John McCall      MarkUsedTemplateParameters(SemaRef,
4140ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 cast<TypeOfType>(T)->getUnderlyingType(),
4141ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 OnlyDeduced, Depth, Used);
4142ad5e73887052193afda72db8efcb812bd083a4a8John McCall    break;
4143ad5e73887052193afda72db8efcb812bd083a4a8John McCall
4144ad5e73887052193afda72db8efcb812bd083a4a8John McCall  case Type::TypeOfExpr:
4145ad5e73887052193afda72db8efcb812bd083a4a8John McCall    if (!OnlyDeduced)
4146ad5e73887052193afda72db8efcb812bd083a4a8John McCall      MarkUsedTemplateParameters(SemaRef,
4147ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 cast<TypeOfExprType>(T)->getUnderlyingExpr(),
4148ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 OnlyDeduced, Depth, Used);
4149ad5e73887052193afda72db8efcb812bd083a4a8John McCall    break;
4150ad5e73887052193afda72db8efcb812bd083a4a8John McCall
4151ad5e73887052193afda72db8efcb812bd083a4a8John McCall  case Type::Decltype:
4152ad5e73887052193afda72db8efcb812bd083a4a8John McCall    if (!OnlyDeduced)
4153ad5e73887052193afda72db8efcb812bd083a4a8John McCall      MarkUsedTemplateParameters(SemaRef,
4154ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 cast<DecltypeType>(T)->getUnderlyingExpr(),
4155ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 OnlyDeduced, Depth, Used);
4156ad5e73887052193afda72db8efcb812bd083a4a8John McCall    break;
4157ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt
4158ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt  case Type::UnaryTransform:
4159ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    if (!OnlyDeduced)
4160ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt      MarkUsedTemplateParameters(SemaRef,
4161ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                               cast<UnaryTransformType>(T)->getUnderlyingType(),
4162ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt                                 OnlyDeduced, Depth, Used);
4163ca63c200346c0ca9e00194ec6e34a5a7b0ed9321Sean Hunt    break;
4164ad5e73887052193afda72db8efcb812bd083a4a8John McCall
41657536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor  case Type::PackExpansion:
4166dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    MarkUsedTemplateParameters(SemaRef,
41677536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor                               cast<PackExpansionType>(T)->getPattern(),
41687536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor                               OnlyDeduced, Depth, Used);
41697536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor    break;
41707536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor
417134b41d939a1328f484511c6002ba2456db879a29Richard Smith  case Type::Auto:
417234b41d939a1328f484511c6002ba2456db879a29Richard Smith    MarkUsedTemplateParameters(SemaRef,
417334b41d939a1328f484511c6002ba2456db879a29Richard Smith                               cast<AutoType>(T)->getDeducedType(),
417434b41d939a1328f484511c6002ba2456db879a29Richard Smith                               OnlyDeduced, Depth, Used);
417534b41d939a1328f484511c6002ba2456db879a29Richard Smith
4176e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // None of these types have any template parameters in them.
4177031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Builtin:
4178031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::VariableArray:
4179031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::FunctionNoProto:
4180031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Record:
4181031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Enum:
4182031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::ObjCInterface:
4183c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject:
4184d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  case Type::ObjCObjectPointer:
4185ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  case Type::UnresolvedUsing:
4186031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define TYPE(Class, Base)
4187031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
4188031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define DEPENDENT_TYPE(Class, Base)
4189031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
4190031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#include "clang/AST/TypeNodes.def"
4191031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
4192031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
4193031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
4194031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
4195e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by this
4196031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// template argument.
41971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
4198e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
4199e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           const TemplateArgument &TemplateArg,
4200e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
4201ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
4202e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
4203031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  switch (TemplateArg.getKind()) {
4204031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Null:
4205031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Integral:
4206788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Declaration:
4207031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
42081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4209031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Type:
4210e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
4211ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
4212031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
4213031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
4214788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  case TemplateArgument::Template:
4215a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor  case TemplateArgument::TemplateExpansion:
4216dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    MarkUsedTemplateParameters(SemaRef,
4217dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi                               TemplateArg.getAsTemplateOrTemplatePattern(),
4218788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                               OnlyDeduced, Depth, Used);
4219031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
4220031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
4221031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Expression:
4222dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
4223ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
4224031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
4225dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
4226d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson  case TemplateArgument::Pack:
4227e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
4228e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                      PEnd = TemplateArg.pack_end();
4229e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor         P != PEnd; ++P)
4230ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
4231d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson    break;
4232031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
4233031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
4234031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
4235031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \brief Mark the template parameters can be deduced by the given
4236031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// template argument list.
4237031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor///
4238031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \param TemplateArgs the template argument list from which template
4239031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// parameters will be deduced.
4240031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor///
4241031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \param Deduced a bit vector whose elements will be set to \c true
4242031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// to indicate when the corresponding template parameter will be
4243031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// deduced.
42441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
4245e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorSema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
4246ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 bool OnlyDeduced, unsigned Depth,
4247e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                 llvm::SmallVectorImpl<bool> &Used) {
42487b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor  // C++0x [temp.deduct.type]p9:
4249dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   If the template argument list of P contains a pack expansion that is not
4250dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  //   the last template argument, the entire template argument list is a
42517b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor  //   non-deduced context.
4252dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  if (OnlyDeduced &&
42537b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor      hasPackExpansionBeforeEnd(TemplateArgs.data(), TemplateArgs.size()))
42547b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor    return;
42557b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor
4256031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
4257dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi    ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
4258ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 Depth, Used);
4259031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
426063f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor
426163f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor/// \brief Marks all of the template parameters that will be deduced by a
426263f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor/// call to the given function template.
4263dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumivoid
426402024a9f0d8e6c898de276193af604c42ee41269Douglas GregorSema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
426502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                    llvm::SmallVectorImpl<bool> &Deduced) {
4266dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi  TemplateParameterList *TemplateParams
426763f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor    = FunctionTemplate->getTemplateParameters();
426863f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  Deduced.clear();
426963f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  Deduced.resize(TemplateParams->size());
4270dfbb02a16ac8c764b5ba1742450513d6212d2f9fNAKAMURA Takumi
427163f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
427263f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
427363f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor    ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
4274ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 true, TemplateParams->getDepth(), Deduced);
427563f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor}
4276dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor
4277dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregorbool hasDeducibleTemplateParameters(Sema &S,
4278dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor                                    FunctionTemplateDecl *FunctionTemplate,
4279dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor                                    QualType T) {
4280dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor  if (!T->isDependentType())
4281dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor    return false;
4282dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor
4283dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor  TemplateParameterList *TemplateParams
4284dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor    = FunctionTemplate->getTemplateParameters();
4285dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor  llvm::SmallVector<bool, 4> Deduced;
4286dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor  Deduced.resize(TemplateParams->size());
4287dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor  ::MarkUsedTemplateParameters(S, T, true, TemplateParams->getDepth(),
4288dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor                               Deduced);
4289dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor
4290dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor  for (unsigned I = 0, N = Deduced.size(); I != N; ++I)
4291dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor    if (Deduced[I])
4292dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor      return true;
4293dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor
4294dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor  return false;
4295dbfb371e297794e827ad2e5b33b45fafbfc46b29Douglas Gregor}
4296