SemaTemplateDeduction.cpp revision 57e97786433e70197a089360228d8f0d82e3ad4c
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
130b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "Sema.h"
140b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/ASTContext.h"
150b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/DeclTemplate.h"
160b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/StmtVisitor.h"
170b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/Expr.h"
180b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/ExprCXX.h"
190b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/Parse/DeclSpec.h"
208a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor#include <algorithm>
21508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor
22508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregornamespace clang {
23508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  /// \brief Various flags that control template argument deduction.
24508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  ///
25508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  /// These flags can be bitwise-OR'd together.
26508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  enum TemplateDeductionFlags {
27508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// \brief No template argument deduction flags, which indicates the
28508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// strictest results for template argument deduction (as used for, e.g.,
29508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// matching class template partial specializations).
30508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    TDF_None = 0,
31508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// \brief Within template argument deduction from a function call, we are
32508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// matching with a parameter type for which the original parameter was
33508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// a reference.
34508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    TDF_ParamWithReferenceType = 0x1,
35508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// \brief Within template argument deduction from a function call, we
36508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// are matching in a case where we ignore cv-qualifiers.
37508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    TDF_IgnoreQualifiers = 0x02,
38508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// \brief Within template argument deduction from a function call,
39508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    /// we are matching in a case where we can perform template argument
404112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    /// deduction from a template-id of a derived class of the argument type.
411282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor    TDF_DerivedClass = 0x04,
421282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor    /// \brief Allow non-dependent types to differ, e.g., when performing
431282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor    /// template argument deduction from a function call where conversions
441282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor    /// may apply.
451282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor    TDF_SkipNonDependent = 0x08
46508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  };
47508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor}
48508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor
490b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregorusing namespace clang;
500b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
519d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor/// \brief Compare two APSInts, extending and switching the sign as
529d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor/// necessary to compare their values regardless of underlying type.
539d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregorstatic bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) {
549d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor  if (Y.getBitWidth() > X.getBitWidth())
559d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor    X.extend(Y.getBitWidth());
569d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor  else if (Y.getBitWidth() < X.getBitWidth())
579d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor    Y.extend(X.getBitWidth());
589d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor
599d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor  // If there is a signedness mismatch, correct it.
609d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor  if (X.isSigned() != Y.isSigned()) {
619d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor    // If the signed value is negative, then the values cannot be the same.
629d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor    if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative()))
639d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor      return false;
649d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor
659d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor    Y.setIsSigned(true);
669d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor    X.setIsSigned(true);
679d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor  }
689d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor
699d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor  return X == Y;
709d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor}
719d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor
72f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
73a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
74f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateParameterList *TemplateParams,
75f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        const TemplateArgument &Param,
76d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor                        const TemplateArgument &Arg,
77f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        Sema::TemplateDeductionInfo &Info,
7802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced);
79d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor
80199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// \brief If the given expression is of a form that permits the deduction
81199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// of a non-type template parameter, return the declaration of that
82199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// non-type template parameter.
83199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregorstatic NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
84199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
85199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    E = IC->getSubExpr();
861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
87199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
88199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
90199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  return 0;
91199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor}
92199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Deduce the value of the given non-type template parameter
94199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// from the given constant.
95f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
96a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceNonTypeTemplateArgument(Sema &S,
971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                              NonTypeTemplateParmDecl *NTTP,
989d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor                              llvm::APSInt Value, QualType ValueType,
9902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                              bool DeducedFromArrayBound,
100f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                              Sema::TemplateDeductionInfo &Info,
10102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
1021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(NTTP->getDepth() == 0 &&
103199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor         "Cannot deduce non-type template argument with depth > 0");
1041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
105199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  if (Deduced[NTTP->getIndex()].isNull()) {
10602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    Deduced[NTTP->getIndex()] = DeducedTemplateArgument(Value, ValueType,
10702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                                        DeducedFromArrayBound);
108f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_Success;
109199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  }
1101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1119d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor  if (Deduced[NTTP->getIndex()].getKind() != TemplateArgument::Integral) {
112f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.Param = NTTP;
113f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.FirstArg = Deduced[NTTP->getIndex()];
1149d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor    Info.SecondArg = TemplateArgument(Value, ValueType);
1159d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor    return Sema::TDK_Inconsistent;
116f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  }
117f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
1189d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor  // Extent the smaller of the two values.
1199d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor  llvm::APSInt PrevValue = *Deduced[NTTP->getIndex()].getAsIntegral();
1209d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor  if (!hasSameExtendedValue(PrevValue, Value)) {
121f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.Param = NTTP;
122f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.FirstArg = Deduced[NTTP->getIndex()];
1239d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor    Info.SecondArg = TemplateArgument(Value, ValueType);
124f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_Inconsistent;
125f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  }
126f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
12702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  if (!DeducedFromArrayBound)
12802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    Deduced[NTTP->getIndex()].setDeducedFromArrayBound(false);
12902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
130f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  return Sema::TDK_Success;
131199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor}
132199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
1331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Deduce the value of the given non-type template parameter
134199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// from the given type- or value-dependent expression.
135199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor///
136199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// \returns true if deduction succeeded, false otherwise.
137f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
138a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceNonTypeTemplateArgument(Sema &S,
139f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                              NonTypeTemplateParmDecl *NTTP,
140f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                              Expr *Value,
141f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                              Sema::TemplateDeductionInfo &Info,
14202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
1431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(NTTP->getDepth() == 0 &&
144199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor         "Cannot deduce non-type template argument with depth > 0");
145199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  assert((Value->isTypeDependent() || Value->isValueDependent()) &&
146199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor         "Expression template argument must be type- or value-dependent.");
1471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
148199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  if (Deduced[NTTP->getIndex()].isNull()) {
1499d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor    Deduced[NTTP->getIndex()] = TemplateArgument(Value->Retain());
150f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_Success;
151199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  }
1521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
153199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) {
1541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Okay, we deduced a constant in one case and a dependent expression
1551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // in another case. FIXME: Later, we will check that instantiating the
156199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    // dependent expression gives us the constant value.
157f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_Success;
158199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  }
1591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1609eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor  if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
1619eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor    // Compare the expressions for equality
1629eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor    llvm::FoldingSetNodeID ID1, ID2;
163a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth    Deduced[NTTP->getIndex()].getAsExpr()->Profile(ID1, S.Context, true);
164a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth    Value->Profile(ID2, S.Context, true);
1659eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor    if (ID1 == ID2)
1669eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor      return Sema::TDK_Success;
1679eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor
1689eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor    // FIXME: Fill in argument mismatch information
1699eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
1709eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor  }
1719eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor
172f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  return Sema::TDK_Success;
173199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor}
174199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
17515755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor/// \brief Deduce the value of the given non-type template parameter
17615755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor/// from the given declaration.
17715755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor///
17815755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor/// \returns true if deduction succeeded, false otherwise.
17915755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregorstatic Sema::TemplateDeductionResult
180a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceNonTypeTemplateArgument(Sema &S,
18115755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor                              NonTypeTemplateParmDecl *NTTP,
18215755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor                              Decl *D,
18315755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor                              Sema::TemplateDeductionInfo &Info,
18402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
18515755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor  assert(NTTP->getDepth() == 0 &&
18615755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor         "Cannot deduce non-type template argument with depth > 0");
18715755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor
18815755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor  if (Deduced[NTTP->getIndex()].isNull()) {
18915755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor    Deduced[NTTP->getIndex()] = TemplateArgument(D->getCanonicalDecl());
19015755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor    return Sema::TDK_Success;
19115755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor  }
19215755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor
19315755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor  if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
19415755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor    // Okay, we deduced a declaration in one case and a dependent expression
19515755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor    // in another case.
19615755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor    return Sema::TDK_Success;
19715755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor  }
19815755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor
19915755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor  if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Declaration) {
20015755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor    // Compare the declarations for equality
20115755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor    if (Deduced[NTTP->getIndex()].getAsDecl()->getCanonicalDecl() ==
20215755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor          D->getCanonicalDecl())
20315755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor      return Sema::TDK_Success;
20415755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor
20515755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor    // FIXME: Fill in argument mismatch information
20615755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
20715755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor  }
20815755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor
20915755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor  return Sema::TDK_Success;
21015755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor}
21115755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor
212f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
213a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
214db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor                        TemplateParameterList *TemplateParams,
215f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateName Param,
216f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateName Arg,
217f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        Sema::TemplateDeductionInfo &Info,
21802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
219d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor  TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
220db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  if (!ParamDecl) {
221db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    // The parameter type is dependent and is not a template template parameter,
222db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    // so there is nothing that we can deduce.
223db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    return Sema::TDK_Success;
224f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  }
225db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor
226db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  if (TemplateTemplateParmDecl *TempParam
227db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor        = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) {
228db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    // Bind the template template parameter to the given template name.
229db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    TemplateArgument &ExistingArg = Deduced[TempParam->getIndex()];
230db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    if (ExistingArg.isNull()) {
231db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor      // This is the first deduction for this template template parameter.
232a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      ExistingArg = TemplateArgument(S.Context.getCanonicalTemplateName(Arg));
233db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor      return Sema::TDK_Success;
234db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    }
235db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor
236db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    // Verify that the previous binding matches this deduction.
237db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    assert(ExistingArg.getKind() == TemplateArgument::Template);
238a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth    if (S.Context.hasSameTemplateName(ExistingArg.getAsTemplate(), Arg))
239db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor      return Sema::TDK_Success;
240db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor
241db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    // Inconsistent deduction.
242db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    Info.Param = TempParam;
243db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    Info.FirstArg = ExistingArg;
244db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    Info.SecondArg = TemplateArgument(Arg);
245f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_Inconsistent;
246f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  }
247db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor
248db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  // Verify that the two template names are equivalent.
249a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  if (S.Context.hasSameTemplateName(Param, Arg))
250db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    return Sema::TDK_Success;
251db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor
252db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  // Mismatch of non-dependent template parameter to argument.
253db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Info.FirstArg = TemplateArgument(Param);
254db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Info.SecondArg = TemplateArgument(Arg);
255db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  return Sema::TDK_NonDeducedMismatch;
256d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor}
257d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor
2581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Deduce the template arguments by comparing the template parameter
259de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// type (which is a template-id) with the template argument type.
260de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
261a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth/// \param S the Sema
262de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
263de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param TemplateParams the template parameters that we are deducing
264de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
265de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Param the parameter type
266de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
267de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Arg the argument type
268de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
269de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Info information about the template argument deduction itself
270de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
271de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Deduced the deduced template arguments
272de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
273de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \returns the result of template argument deduction so far. Note that a
274de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// "success" result means that template argument deduction has not yet failed,
275de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// but it may still fail, later, for other reasons.
276de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregorstatic Sema::TemplateDeductionResult
277a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
278de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                        TemplateParameterList *TemplateParams,
279de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                        const TemplateSpecializationType *Param,
280de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                        QualType Arg,
281de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                        Sema::TemplateDeductionInfo &Info,
28202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
283467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(Arg.isCanonical() && "Argument type must be canonical");
2841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
285de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  // Check whether the template argument is a dependent template-id.
2861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const TemplateSpecializationType *SpecArg
287de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        = dyn_cast<TemplateSpecializationType>(Arg)) {
288de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    // Perform template argument deduction for the template name.
289de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    if (Sema::TemplateDeductionResult Result
290a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          = DeduceTemplateArguments(S, TemplateParams,
291de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                    Param->getTemplateName(),
292de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                    SpecArg->getTemplateName(),
293de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                    Info, Deduced))
294de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      return Result;
2951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
297de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    // Perform template argument deduction on each template
298de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    // argument.
299db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    unsigned NumArgs = std::min(SpecArg->getNumArgs(), Param->getNumArgs());
300de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    for (unsigned I = 0; I != NumArgs; ++I)
301de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      if (Sema::TemplateDeductionResult Result
302a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            = DeduceTemplateArguments(S, TemplateParams,
303de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                      Param->getArg(I),
304de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                      SpecArg->getArg(I),
305de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                      Info, Deduced))
306de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        return Result;
3071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
308de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    return Sema::TDK_Success;
309de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  }
3101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
311de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  // If the argument type is a class template specialization, we
312de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  // perform template argument deduction using its template
313de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  // arguments.
314de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
315de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  if (!RecordArg)
316de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
3171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ClassTemplateSpecializationDecl *SpecArg
319de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
320de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  if (!SpecArg)
321de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
3221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
323de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  // Perform template argument deduction for the template name.
324de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  if (Sema::TemplateDeductionResult Result
325a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = DeduceTemplateArguments(S,
326db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor                                  TemplateParams,
327de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                  Param->getTemplateName(),
328de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                               TemplateName(SpecArg->getSpecializedTemplate()),
329de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                  Info, Deduced))
330de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    return Result;
3311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
332de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  unsigned NumArgs = Param->getNumArgs();
333de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs();
334de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  if (NumArgs != ArgArgs.size())
335de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
3361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
337de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  for (unsigned I = 0; I != NumArgs; ++I)
3381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (Sema::TemplateDeductionResult Result
339a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          = DeduceTemplateArguments(S, TemplateParams,
340de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                    Param->getArg(I),
341de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                    ArgArgs.get(I),
342de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                    Info, Deduced))
343de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      return Result;
3441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
345de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  return Sema::TDK_Success;
346de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor}
347de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor
348500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \brief Deduce the template arguments by comparing the parameter type and
349500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// the argument type (C++ [temp.deduct.type]).
350500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
351a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth/// \param S the semantic analysis object within which we are deducing
352500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
353500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param TemplateParams the template parameters that we are deducing
354500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
355500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param ParamIn the parameter type
356500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
357500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param ArgIn the argument type
358500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
359500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param Info information about the template argument deduction itself
360500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
361500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param Deduced the deduced template arguments
362500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
363508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
3641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// how template argument deduction is performed.
365500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
366500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \returns the result of template argument deduction so far. Note that a
367500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// "success" result means that template argument deduction has not yet failed,
368500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// but it may still fail, later, for other reasons.
369f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
370a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
371f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateParameterList *TemplateParams,
372f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        QualType ParamIn, QualType ArgIn,
373f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        Sema::TemplateDeductionInfo &Info,
37402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                     llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
375508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                        unsigned TDF) {
3760b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  // We only want to look at the canonical types, since typedefs and
3770b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  // sugar are not part of template argument deduction.
378a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  QualType Param = S.Context.getCanonicalType(ParamIn);
379a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  QualType Arg = S.Context.getCanonicalType(ArgIn);
3800b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
381500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor  // C++0x [temp.deduct.call]p4 bullet 1:
382500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor  //   - If the original P is a reference type, the deduced A (i.e., the type
3831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //     referred to by the reference) can be more cv-qualified than the
384500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor  //     transformed A.
385508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  if (TDF & TDF_ParamWithReferenceType) {
386e724246b9f655801bd96b727daf9dddc44beef4dChandler Carruth    Qualifiers Quals;
387a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth    QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals);
388e724246b9f655801bd96b727daf9dddc44beef4dChandler Carruth    Quals.setCVRQualifiers(Quals.getCVRQualifiers() &
389e724246b9f655801bd96b727daf9dddc44beef4dChandler Carruth                           Arg.getCVRQualifiersThroughArrayTypes());
390a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth    Param = S.Context.getQualifiedType(UnqualParam, Quals);
391500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor  }
3921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
393f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  // If the parameter type is not dependent, there is nothing to deduce.
3941282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor  if (!Param->isDependentType()) {
3951282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor    if (!(TDF & TDF_SkipNonDependent) && Param != Arg) {
3961282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor
3971282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor      return Sema::TDK_NonDeducedMismatch;
3981282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor    }
3991282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor
400f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    return Sema::TDK_Success;
4011282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor  }
4020b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
403199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  // C++ [temp.deduct.type]p9:
4041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   A template type argument T, a template template argument TT or a
4051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   template non-type argument i can be deduced if P and A have one of
406199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  //   the following forms:
407199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  //
408199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  //     T
409199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  //     cv-list T
4101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const TemplateTypeParmType *TemplateTypeParm
411183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall        = Param->getAs<TemplateTypeParmType>()) {
412f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    unsigned Index = TemplateTypeParm->getIndex();
413f290e0db835a619014538c41ed552696efc0e977Douglas Gregor    bool RecanonicalizeArg = false;
4141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4159e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor    // If the argument type is an array type, move the qualifiers up to the
4169e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor    // top level, so they can be matched with the qualifiers on the parameter.
4179e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor    // FIXME: address spaces, ObjC GC qualifiers
418f290e0db835a619014538c41ed552696efc0e977Douglas Gregor    if (isa<ArrayType>(Arg)) {
4190953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      Qualifiers Quals;
420a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      Arg = S.Context.getUnqualifiedArrayType(Arg, Quals);
4210953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (Quals) {
422a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        Arg = S.Context.getQualifiedType(Arg, Quals);
423f290e0db835a619014538c41ed552696efc0e977Douglas Gregor        RecanonicalizeArg = true;
424f290e0db835a619014538c41ed552696efc0e977Douglas Gregor      }
425f290e0db835a619014538c41ed552696efc0e977Douglas Gregor    }
4261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4270b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor    // The argument type can not be less qualified than the parameter
4280b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor    // type.
429508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) {
430f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
43157e97786433e70197a089360228d8f0d82e3ad4cJohn McCall      Info.FirstArg = TemplateArgument(Param);
432833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      Info.SecondArg = TemplateArgument(Arg);
43357e97786433e70197a089360228d8f0d82e3ad4cJohn McCall      return Sema::TDK_Underqualified;
434f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    }
4350b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
4360b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor    assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
437a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth    assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function");
4380953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    QualType DeducedType = Arg;
4390953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    DeducedType.removeCVRQualifiers(Param.getCVRQualifiers());
440f290e0db835a619014538c41ed552696efc0e977Douglas Gregor    if (RecanonicalizeArg)
441a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      DeducedType = S.Context.getCanonicalType(DeducedType);
4421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4430b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor    if (Deduced[Index].isNull())
444833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      Deduced[Index] = TemplateArgument(DeducedType);
4450b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor    else {
4461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // C++ [temp.deduct.type]p2:
4470b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor      //   [...] If type deduction cannot be done for any P/A pair, or if for
4481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   any pair the deduction leads to more than one possible set of
4491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   deduced values, or if different pairs yield different deduced
4501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   values, or if any template argument remains neither deduced nor
4510b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor      //   explicitly specified, template argument deduction fails.
452f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (Deduced[Index].getAsType() != DeducedType) {
4531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        Info.Param
454f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor          = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
455f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        Info.FirstArg = Deduced[Index];
456833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        Info.SecondArg = TemplateArgument(Arg);
457f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_Inconsistent;
458f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      }
4590b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor    }
460f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_Success;
4610b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
4620b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
463f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  // Set up the template argument deduction information for a failure.
464833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  Info.FirstArg = TemplateArgument(ParamIn);
465833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  Info.SecondArg = TemplateArgument(ArgIn);
466f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
467508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  // Check the cv-qualifiers on the parameter and argument types.
468508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  if (!(TDF & TDF_IgnoreQualifiers)) {
469508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    if (TDF & TDF_ParamWithReferenceType) {
470508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor      if (Param.isMoreQualifiedThan(Arg))
471508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
472508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    } else {
473508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor      if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
4741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        return Sema::TDK_NonDeducedMismatch;
475508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    }
476508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  }
4770b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
478d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor  switch (Param->getTypeClass()) {
479199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    // No deduction possible for these types
480199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    case Type::Builtin:
481f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
4821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
483199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T *
484d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    case Type::Pointer: {
485c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall      QualType PointeeType;
486c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall      if (const PointerType *PointerArg = Arg->getAs<PointerType>()) {
487c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall        PointeeType = PointerArg->getPointeeType();
488c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall      } else if (const ObjCObjectPointerType *PointerArg
489c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall                   = Arg->getAs<ObjCObjectPointerType>()) {
490c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall        PointeeType = PointerArg->getPointeeType();
491c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall      } else {
492f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
493c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall      }
4941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4954112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor      unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
496a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
497d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                                   cast<PointerType>(Param)->getPointeeType(),
498c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall                                     PointeeType,
4994112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor                                     Info, Deduced, SubTDF);
500d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    }
5011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
502199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T &
503d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    case Type::LValueReference: {
5046217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
505d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor      if (!ReferenceArg)
506f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
5071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
508a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
509d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                           cast<LValueReferenceType>(Param)->getPointeeType(),
510d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                                     ReferenceArg->getPointeeType(),
511508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
512d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    }
5130b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
514199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T && [C++0x]
515d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    case Type::RValueReference: {
5166217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
517d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor      if (!ReferenceArg)
518f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
5191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
520a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
521d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                           cast<RValueReferenceType>(Param)->getPointeeType(),
522d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                                     ReferenceArg->getPointeeType(),
523508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
524d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    }
5251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
526199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T [] (implied, but not stated explicitly)
5274d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson    case Type::IncompleteArray: {
5281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const IncompleteArrayType *IncompleteArrayArg =
529a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        S.Context.getAsIncompleteArrayType(Arg);
5304d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson      if (!IncompleteArrayArg)
531f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
5321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
533a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
534a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth                     S.Context.getAsIncompleteArrayType(Param)->getElementType(),
5354d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson                                     IncompleteArrayArg->getElementType(),
536508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
5374d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson    }
538199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
539199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T [integer-constant]
5404d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson    case Type::ConstantArray: {
5411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const ConstantArrayType *ConstantArrayArg =
542a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        S.Context.getAsConstantArrayType(Arg);
5434d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson      if (!ConstantArrayArg)
544f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
5451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const ConstantArrayType *ConstantArrayParm =
547a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        S.Context.getAsConstantArrayType(Param);
5484d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson      if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
549f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
5501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
551a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
5524d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson                                     ConstantArrayParm->getElementType(),
5534d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson                                     ConstantArrayArg->getElementType(),
554508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
5554d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson    }
5564d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson
557199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     type [i]
558199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    case Type::DependentSizedArray: {
559a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg);
560199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (!ArrayArg)
561f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
5621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
563199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // Check the element type of the arrays
564199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      const DependentSizedArrayType *DependentArrayParm
565a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = S.Context.getAsDependentSizedArrayType(Param);
566f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (Sema::TemplateDeductionResult Result
567a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            = DeduceTemplateArguments(S, TemplateParams,
568f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      DependentArrayParm->getElementType(),
569f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      ArrayArg->getElementType(),
570508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                      Info, Deduced, 0))
571f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Result;
5721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
573199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // Determine the array bound is something we can deduce.
5741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NonTypeTemplateParmDecl *NTTP
575199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor        = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
576199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (!NTTP)
577f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_Success;
5781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We can perform template argument deduction for the given non-type
580199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // template parameter.
5811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(NTTP->getDepth() == 0 &&
582199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor             "Cannot deduce non-type template argument at depth > 0");
5831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (const ConstantArrayType *ConstantArrayArg
584335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson            = dyn_cast<ConstantArrayType>(ArrayArg)) {
585335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson        llvm::APSInt Size(ConstantArrayArg->getSize());
5869d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor        return DeduceNonTypeTemplateArgument(S, NTTP, Size,
5879d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor                                             S.Context.getSizeType(),
58802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                             /*ArrayBound=*/true,
589f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                             Info, Deduced);
590335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson      }
591199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (const DependentSizedArrayType *DependentArrayArg
592199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor            = dyn_cast<DependentSizedArrayType>(ArrayArg))
593a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP,
594199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor                                             DependentArrayArg->getSizeExpr(),
595f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                             Info, Deduced);
5961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
597199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // Incomplete type does not match a dependently-sized array type
598f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
599199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    }
6001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     type(*)(T)
6021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(*)()
6031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(*)(T)
604a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson    case Type::FunctionProto: {
6051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const FunctionProtoType *FunctionProtoArg =
606a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson        dyn_cast<FunctionProtoType>(Arg);
607a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson      if (!FunctionProtoArg)
608f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
6091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const FunctionProtoType *FunctionProtoParam =
611a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson        cast<FunctionProtoType>(Param);
612994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson
6131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (FunctionProtoParam->getTypeQuals() !=
614994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson          FunctionProtoArg->getTypeQuals())
615f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
6161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
617994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson      if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
618f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
6191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
620994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson      if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
621f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
622994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson
623a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson      // Check return types.
624f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (Sema::TemplateDeductionResult Result
625a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            = DeduceTemplateArguments(S, TemplateParams,
626f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      FunctionProtoParam->getResultType(),
627f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      FunctionProtoArg->getResultType(),
628508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                      Info, Deduced, 0))
629f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Result;
6301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
631a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson      for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
632a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson        // Check argument types.
633f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        if (Sema::TemplateDeductionResult Result
634a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth              = DeduceTemplateArguments(S, TemplateParams,
635f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                        FunctionProtoParam->getArgType(I),
636f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                        FunctionProtoArg->getArgType(I),
637508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                        Info, Deduced, 0))
638f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor          return Result;
639a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson      }
6401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
641f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_Success;
642a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson    }
6431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6443cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    case Type::InjectedClassName: {
6453cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // Treat a template's injected-class-name as if the template
6463cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // specialization type had been used.
64731f17ecbef57b5679c017c375db330546b7b5145John McCall      Param = cast<InjectedClassNameType>(Param)
64831f17ecbef57b5679c017c375db330546b7b5145John McCall        ->getInjectedSpecializationType();
6493cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      assert(isa<TemplateSpecializationType>(Param) &&
6503cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall             "injected class name is not a template specialization type");
6513cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // fall through
6523cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    }
6533cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
654f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    //     template-name<T> (where template-name refers to a class template)
655d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor    //     template-name<i>
656db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    //     TT<T>
657db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    //     TT<i>
658db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    //     TT<>
659d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor    case Type::TemplateSpecialization: {
660d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor      const TemplateSpecializationType *SpecParam
661d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor        = cast<TemplateSpecializationType>(Param);
6621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
663de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      // Try to deduce template arguments from the template-id.
664de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      Sema::TemplateDeductionResult Result
665a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg,
666de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                  Info, Deduced);
6671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6684a5c15f75f76b95e1c2ceb6fa2737dcadd5f4be1Douglas Gregor      if (Result && (TDF & TDF_DerivedClass)) {
669de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        // C++ [temp.deduct.call]p3b3:
670de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   If P is a class, and P has the form template-id, then A can be a
671de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   derived class of the deduced A. Likewise, if P is a pointer to a
6721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        //   class of the form template-id, A can be a pointer to a derived
673de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   class pointed to by the deduced A.
674de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //
675de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        // More importantly:
6761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        //   These alternatives are considered only if type deduction would
677de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   otherwise fail.
678a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        if (const RecordType *RecordT = Arg->getAs<RecordType>()) {
679a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          // We cannot inspect base classes as part of deduction when the type
680a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          // is incomplete, so either instantiate any templates necessary to
681a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          // complete the type, or skip over it if it cannot be completed.
6825769d6195087229770d7ac90449443e026c47103John McCall          if (S.RequireCompleteType(Info.getLocation(), Arg, 0))
683a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            return Result;
684a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth
685de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          // Use data recursion to crawl through the list of base classes.
6861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          // Visited contains the set of nodes we have already visited, while
687de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          // ToVisit is our stack of records that we still need to visit.
688de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          llvm::SmallPtrSet<const RecordType *, 8> Visited;
689de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          llvm::SmallVector<const RecordType *, 8> ToVisit;
690de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          ToVisit.push_back(RecordT);
691de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          bool Successful = false;
692de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          while (!ToVisit.empty()) {
693de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // Retrieve the next class in the inheritance hierarchy.
694de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            const RecordType *NextT = ToVisit.back();
695de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            ToVisit.pop_back();
6961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
697de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // If we have already seen this type, skip it.
698de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            if (!Visited.insert(NextT))
699de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              continue;
7001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
701de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // If this is a base class, try to perform template argument
702de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // deduction from it.
703de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            if (NextT != RecordT) {
704de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              Sema::TemplateDeductionResult BaseResult
705a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth                = DeduceTemplateArguments(S, TemplateParams, SpecParam,
706de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                          QualType(NextT, 0), Info, Deduced);
7071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
708de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              // If template argument deduction for this base was successful,
709de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              // note that we had some success.
710de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              if (BaseResult == Sema::TDK_Success)
711de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                Successful = true;
712de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            }
7131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
714de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // Visit base classes
715de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
716de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
717de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                                 BaseEnd = Next->bases_end();
7189994a34f6cf842721ba7723edc0b9036229fe387Sebastian Redl                 Base != BaseEnd; ++Base) {
7191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump              assert(Base->getType()->isRecordType() &&
720de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                     "Base class that isn't a record?");
7216217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek              ToVisit.push_back(Base->getType()->getAs<RecordType>());
722de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            }
723de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          }
7241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
725de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          if (Successful)
726de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            return Sema::TDK_Success;
727de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        }
7281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
729de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      }
7301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
731de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      return Result;
732d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor    }
733d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor
734637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T type::*
735637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T T::*
736637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (type::*)()
737637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     type (T::*)()
738637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     type (type::*)(T)
739637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     type (T::*)(T)
740637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (type::*)(T)
741637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (T::*)()
742637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (T::*)(T)
743637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    case Type::MemberPointer: {
744637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
745637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
746637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      if (!MemPtrArg)
747f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
748f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
749f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (Sema::TemplateDeductionResult Result
750a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            = DeduceTemplateArguments(S, TemplateParams,
751f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      MemPtrParam->getPointeeType(),
752f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      MemPtrArg->getPointeeType(),
753508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                      Info, Deduced,
754508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                      TDF & TDF_IgnoreQualifiers))
755f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Result;
756f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
757a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
758f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                     QualType(MemPtrParam->getClass(), 0),
759f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                     QualType(MemPtrArg->getClass(), 0),
760508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
761637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    }
762637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor
7639a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    //     (clang extension)
7649a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    //
7651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     type(^)(T)
7661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(^)()
7671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(^)(T)
768859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson    case Type::BlockPointer: {
769859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson      const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
770859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson      const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
7711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
772859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson      if (!BlockPtrArg)
773f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
7741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
775a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
776859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson                                     BlockPtrParam->getPointeeType(),
777f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                     BlockPtrArg->getPointeeType(), Info,
778508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Deduced, 0);
779859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson    }
780859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson
781637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    case Type::TypeOfExpr:
782637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    case Type::TypeOf:
7834714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    case Type::DependentName:
784637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      // No template argument deduction for these types
785f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_Success;
786637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor
787d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    default:
788d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor      break;
7890b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
7900b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
7910b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  // FIXME: Many more cases to go (to go).
792f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  return Sema::TDK_Success;
7930b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
7940b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
795f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
796a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
797f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateParameterList *TemplateParams,
798f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        const TemplateArgument &Param,
7990b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor                        const TemplateArgument &Arg,
800f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        Sema::TemplateDeductionInfo &Info,
80102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
8020b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  switch (Param.getKind()) {
803199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Null:
804199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    assert(false && "Null template argument in parameter list");
805199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    break;
8061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case TemplateArgument::Type:
808788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    if (Arg.getKind() == TemplateArgument::Type)
809a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams, Param.getAsType(),
810788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                                     Arg.getAsType(), Info, Deduced, 0);
811788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.FirstArg = Param;
812788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.SecondArg = Arg;
813788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
814788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor
815788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  case TemplateArgument::Template:
816db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    if (Arg.getKind() == TemplateArgument::Template)
817a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
818788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                                     Param.getAsTemplate(),
819db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor                                     Arg.getAsTemplate(), Info, Deduced);
820788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.FirstArg = Param;
821788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.SecondArg = Arg;
822788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
823788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor
824199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Declaration:
825788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    if (Arg.getKind() == TemplateArgument::Declaration &&
826788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor        Param.getAsDecl()->getCanonicalDecl() ==
827788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor          Arg.getAsDecl()->getCanonicalDecl())
828788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return Sema::TDK_Success;
829788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor
830f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.FirstArg = Param;
831f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.SecondArg = Arg;
832f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_NonDeducedMismatch;
8331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
834199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Integral:
835199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    if (Arg.getKind() == TemplateArgument::Integral) {
8369d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor      if (hasSameExtendedValue(*Param.getAsIntegral(), *Arg.getAsIntegral()))
837f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_Success;
838f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
839f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.FirstArg = Param;
840f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.SecondArg = Arg;
841f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
842f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    }
843f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
844f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    if (Arg.getKind() == TemplateArgument::Expression) {
845f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.FirstArg = Param;
846f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.SecondArg = Arg;
847f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
848199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    }
849199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
850199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    assert(false && "Type/value mismatch");
851f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.FirstArg = Param;
852f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.SecondArg = Arg;
853f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_NonDeducedMismatch;
8541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
855199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Expression: {
8561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (NonTypeTemplateParmDecl *NTTP
857199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor          = getDeducedParameterFromExpr(Param.getAsExpr())) {
858199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (Arg.getKind() == TemplateArgument::Integral)
859a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP,
8601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             *Arg.getAsIntegral(),
8619d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor                                             Arg.getIntegralType(),
86202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                             /*ArrayBound=*/false,
863f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                             Info, Deduced);
864199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (Arg.getKind() == TemplateArgument::Expression)
865a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(),
866f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                             Info, Deduced);
86715755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor      if (Arg.getKind() == TemplateArgument::Declaration)
868a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(),
86915755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor                                             Info, Deduced);
87015755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor
871199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      assert(false && "Type/value mismatch");
872f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.FirstArg = Param;
873f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.SecondArg = Arg;
874f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
875199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    }
8761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
877199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    // Can't deduce anything, but that's okay.
878f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_Success;
879199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  }
880d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson  case TemplateArgument::Pack:
881d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson    assert(0 && "FIXME: Implement!");
882d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson    break;
8830b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
8841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
885f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  return Sema::TDK_Success;
8860b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
8870b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
8881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic Sema::TemplateDeductionResult
889a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
890f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateParameterList *TemplateParams,
8910b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor                        const TemplateArgumentList &ParamList,
8920b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor                        const TemplateArgumentList &ArgList,
893f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        Sema::TemplateDeductionInfo &Info,
89402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
8950b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  assert(ParamList.size() == ArgList.size());
8960b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
897f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    if (Sema::TemplateDeductionResult Result
898a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          = DeduceTemplateArguments(S, TemplateParams,
8991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                    ParamList[I], ArgList[I],
900f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                    Info, Deduced))
901f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Result;
9020b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
903f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  return Sema::TDK_Success;
9040b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
9050b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
906f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// \brief Determine whether two template arguments are the same.
9071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool isSameTemplateArg(ASTContext &Context,
908f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor                              const TemplateArgument &X,
909f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor                              const TemplateArgument &Y) {
910f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  if (X.getKind() != Y.getKind())
911f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    return false;
9121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
913f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  switch (X.getKind()) {
914f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Null:
915f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      assert(false && "Comparing NULL template argument");
916f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      break;
9171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
918f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Type:
919f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      return Context.getCanonicalType(X.getAsType()) ==
920f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor             Context.getCanonicalType(Y.getAsType());
9211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
922f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Declaration:
92397fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis      return X.getAsDecl()->getCanonicalDecl() ==
92497fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis             Y.getAsDecl()->getCanonicalDecl();
9251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
926788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Template:
927788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return Context.getCanonicalTemplateName(X.getAsTemplate())
928788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor               .getAsVoidPointer() ==
929788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor             Context.getCanonicalTemplateName(Y.getAsTemplate())
930788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor               .getAsVoidPointer();
931788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor
932f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Integral:
933f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      return *X.getAsIntegral() == *Y.getAsIntegral();
9341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
935788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Expression: {
936788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      llvm::FoldingSetNodeID XID, YID;
937788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      X.getAsExpr()->Profile(XID, Context, true);
938788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      Y.getAsExpr()->Profile(YID, Context, true);
939788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return XID == YID;
940788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    }
9411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
942f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Pack:
943f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      if (X.pack_size() != Y.pack_size())
944f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor        return false;
9451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (TemplateArgument::pack_iterator XP = X.pack_begin(),
9471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        XPEnd = X.pack_end(),
948f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor                                           YP = Y.pack_begin();
9491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump           XP != XPEnd; ++XP, ++YP)
950f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor        if (!isSameTemplateArg(Context, *XP, *YP))
951f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor          return false;
952f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
953f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      return true;
954f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  }
955f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
956f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  return false;
957f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor}
958f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
959f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// \brief Helper function to build a TemplateParameter when we don't
960f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// know its type statically.
961f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregorstatic TemplateParameter makeTemplateParameter(Decl *D) {
962f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
963f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    return TemplateParameter(TTP);
964f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
965f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    return TemplateParameter(NTTP);
9661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
967f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
968f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor}
969f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
97031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// Complete template argument deduction for a class template partial
97131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// specialization.
97231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregorstatic Sema::TemplateDeductionResult
97331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas GregorFinishTemplateArgumentDeduction(Sema &S,
97431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                ClassTemplatePartialSpecializationDecl *Partial,
97531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                const TemplateArgumentList &TemplateArgs,
97631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                      llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
97731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                Sema::TemplateDeductionInfo &Info) {
97831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // Trap errors.
97931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  Sema::SFINAETrap Trap(S);
98031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor
98131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  Sema::ContextRAII SavedContext(S, Partial);
982f5813826802c2e76cdc13cae834ebfd4518d74a6John McCall
98302cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // C++ [temp.deduct.type]p2:
98402cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  //   [...] or if any template argument remains neither deduced nor
98502cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  //   explicitly specified, template argument deduction fails.
986fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson  TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
987fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson                                      Deduced.size());
98802cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
989f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    if (Deduced[I].isNull()) {
9901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Decl *Param
99131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor      = const_cast<NamedDecl *>(
992bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                Partial->getTemplateParameters()->getParam(I));
9932b0749a4f8965d0205bf77322db150c13c39e3beDouglas Gregor      Info.Param = makeTemplateParameter(Param);
99431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor      return Sema::TDK_Incomplete;
995f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    }
99631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor
997fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson    Builder.Append(Deduced[I]);
99802cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  }
99931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor
100002cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // Form the template argument list from the deduced template arguments.
10011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentList *DeducedArgumentList
100231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    = new (S.Context) TemplateArgumentList(S.Context, Builder,
100331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                           /*TakeArgs=*/true);
1004f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  Info.reset(DeducedArgumentList);
100502cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor
100602cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // Substitute the deduced template arguments into the template
100702cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // arguments of the class template partial specialization, and
100802cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // verify that the instantiated template arguments are both valid
100902cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // and are equivalent to the template arguments originally provided
10101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to the class template.
101102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  // FIXME: Do we have to correct the types of deduced non-type template
101202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  // arguments (in particular, integral non-type template arguments?).
101331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  Sema::LocalInstantiationScope InstScope(S);
101402cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
1015833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  const TemplateArgumentLoc *PartialTemplateArgs
1016833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    = Partial->getTemplateArgsAsWritten();
1017833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  unsigned N = Partial->getNumTemplateArgsAsWritten();
1018d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
1019d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  // Note that we don't provide the langle and rangle locations.
1020d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstArgs;
1021d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
1022833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned I = 0; I != N; ++I) {
1023bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    Decl *Param = const_cast<NamedDecl *>(
1024c9e5d25ea33616c896a2ce5fbd6d68186eaddc5cDouglas Gregor                    ClassTemplate->getTemplateParameters()->getParam(I));
1025d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    TemplateArgumentLoc InstArg;
102631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    if (S.Subst(PartialTemplateArgs[I], InstArg,
102731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
1028f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Info.Param = makeTemplateParameter(Param);
1029833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      Info.FirstArg = PartialTemplateArgs[I].getArgument();
103031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor      return Sema::TDK_SubstitutionFailure;
1031f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    }
1032d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    InstArgs.addArgument(InstArg);
1033833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  }
1034833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1035833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  TemplateArgumentListBuilder ConvertedInstArgs(
1036833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall                                  ClassTemplate->getTemplateParameters(), N);
1037833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
103831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
1039ec20f46740a59758b12c22108002395bcf5b6f9bDouglas Gregor                                InstArgs, false, ConvertedInstArgs))
104031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return Sema::TDK_SubstitutionFailure;
1041833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1042833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned I = 0, E = ConvertedInstArgs.flatSize(); I != E; ++I) {
1043d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    TemplateArgument InstArg = ConvertedInstArgs.getFlatArguments()[I];
1044833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1045833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    Decl *Param = const_cast<NamedDecl *>(
1046833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall                    ClassTemplate->getTemplateParameters()->getParam(I));
10471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1048f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    if (InstArg.getKind() == TemplateArgument::Expression) {
10491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // When the argument is an expression, check the expression result
1050f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      // against the actual template parameter to get down to the canonical
1051f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      // template argument.
1052f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Expr *InstExpr = InstArg.getAsExpr();
10531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (NonTypeTemplateParmDecl *NTTP
1054f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor            = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
105531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor        if (S.CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
1056f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor          Info.Param = makeTemplateParameter(Param);
1057833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall          Info.FirstArg = Partial->getTemplateArgs()[I];
105831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor          return Sema::TDK_SubstitutionFailure;
1059f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor        }
1060f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      }
106102cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor    }
10621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
106331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) {
1064f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Info.Param = makeTemplateParameter(Param);
1065f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Info.FirstArg = TemplateArgs[I];
1066f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Info.SecondArg = InstArg;
106731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor      return Sema::TDK_NonDeducedMismatch;
1068f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    }
106902cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  }
107002cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor
1071bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor  if (Trap.hasErrorOccurred())
107231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return Sema::TDK_SubstitutionFailure;
1073bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor
107431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  return Sema::TDK_Success;
107531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor}
107631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor
107731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// \brief Perform template argument deduction to determine whether
107831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// the given template arguments match the given class template
107931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// partial specialization per C++ [temp.class.spec.match].
108031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas GregorSema::TemplateDeductionResult
108131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas GregorSema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
108231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                              const TemplateArgumentList &TemplateArgs,
108331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                              TemplateDeductionInfo &Info) {
108431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // C++ [temp.class.spec.match]p2:
108531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  //   A partial specialization matches a given actual template
108631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  //   argument list if the template arguments of the partial
108731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  //   specialization can be deduced from the actual template argument
108831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  //   list (14.8.2).
108931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  SFINAETrap Trap(*this);
109031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
109131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  Deduced.resize(Partial->getTemplateParameters()->size());
109231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  if (TemplateDeductionResult Result
109331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor        = ::DeduceTemplateArguments(*this,
109431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                    Partial->getTemplateParameters(),
109531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                    Partial->getTemplateArgs(),
109631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                    TemplateArgs, Info, Deduced))
109731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return Result;
109831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor
109931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
110031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                             Deduced.data(), Deduced.size());
110131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  if (Inst)
110231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return TDK_InstantiationDepth;
110331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor
110431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  if (Trap.hasErrorOccurred())
110531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return Sema::TDK_SubstitutionFailure;
110631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor
110731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs,
110831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                           Deduced, Info);
11090b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
1110031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
11114112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor/// \brief Determine whether the given type T is a simple-template-id type.
11124112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregorstatic bool isSimpleTemplateIdType(QualType T) {
11131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const TemplateSpecializationType *Spec
1114183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall        = T->getAs<TemplateSpecializationType>())
11154112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    return Spec->getTemplateName().getAsTemplateDecl() != 0;
11161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11174112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor  return false;
11184112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor}
111983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
112083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \brief Substitute the explicitly-provided template arguments into the
112183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// given function template according to C++ [temp.arg.explicit].
112283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
112383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionTemplate the function template into which the explicit
112483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template arguments will be substituted.
112583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
11261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ExplicitTemplateArguments the explicitly-specified template
112783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// arguments.
112883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
11291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param Deduced the deduced template arguments, which will be populated
113083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// with the converted and checked explicit template arguments.
113183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
11321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ParamTypes will be populated with the instantiated function
113383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// parameters.
113483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
113583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionType if non-NULL, the result type of the function template
113683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// will also be instantiated and the pointed-to value will be updated with
113783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// the instantiated function type.
113883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
113983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Info if substitution fails for any reason, this object will be
114083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// populated with more information about the failure.
114183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
114283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \returns TDK_Success if substitution was successful, or some failure
114383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// condition.
114483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::TemplateDeductionResult
114583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::SubstituteExplicitTemplateArguments(
114683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      FunctionTemplateDecl *FunctionTemplate,
1147d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                        const TemplateArgumentListInfo &ExplicitTemplateArgs,
114802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                       llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
114983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                 llvm::SmallVectorImpl<QualType> &ParamTypes,
115083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          QualType *FunctionType,
115183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          TemplateDeductionInfo &Info) {
115283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
115383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateParameterList *TemplateParams
115483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = FunctionTemplate->getTemplateParameters();
115583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
1156d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (ExplicitTemplateArgs.size() == 0) {
115783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    // No arguments to substitute; just copy over the parameter types and
115883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    // fill in the function type.
115983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    for (FunctionDecl::param_iterator P = Function->param_begin(),
116083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                   PEnd = Function->param_end();
116183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor         P != PEnd;
116283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor         ++P)
116383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      ParamTypes.push_back((*P)->getType());
11641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
116583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (FunctionType)
116683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      *FunctionType = Function->getType();
116783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_Success;
116883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
11691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
117083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Substitution of the explicit template arguments into a function template
117183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  /// is a SFINAE context. Trap any errors that might occur.
11721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SFINAETrap Trap(*this);
11731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
117483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // C++ [temp.arg.explicit]p3:
11751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   Template arguments that are present shall be specified in the
11761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   declaration order of their corresponding template-parameters. The
117783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   template argument list shall not specify more template-arguments than
11781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   there are corresponding template-parameters.
11791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentListBuilder Builder(TemplateParams,
1180d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                      ExplicitTemplateArgs.size());
11811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Enter a new template instantiation context where we check the
118383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // explicitly-specified template arguments against this function template,
118483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // and then substitute them into the function parameter types.
11851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
118683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                             FunctionTemplate, Deduced.data(), Deduced.size(),
118783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor           ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution);
118883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (Inst)
118983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_InstantiationDepth;
11901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
119196db310ab7ca59e1890ddef25a3701bc2909d20fJohn McCall  ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
1192f5813826802c2e76cdc13cae834ebfd4518d74a6John McCall
119383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (CheckTemplateArgumentList(FunctionTemplate,
119483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                SourceLocation(),
1195d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                ExplicitTemplateArgs,
119683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                true,
1197f1a8445036a2d047c7165d4170e3058cdeaba6ebDouglas Gregor                                Builder) || Trap.hasErrorOccurred()) {
1198fe52c91dcd1ecda90b579f789baf70f7a992e3c9Douglas Gregor    unsigned Index = Builder.structuredSize();
1199fe52c91dcd1ecda90b579f789baf70f7a992e3c9Douglas Gregor    if (Index >= TemplateParams->size())
1200fe52c91dcd1ecda90b579f789baf70f7a992e3c9Douglas Gregor      Index = TemplateParams->size() - 1;
1201fe52c91dcd1ecda90b579f789baf70f7a992e3c9Douglas Gregor    Info.Param = makeTemplateParameter(TemplateParams->getParam(Index));
120283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_InvalidExplicitArguments;
1203f1a8445036a2d047c7165d4170e3058cdeaba6ebDouglas Gregor  }
12041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
120583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Form the template argument list from the explicitly-specified
120683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // template arguments.
12071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentList *ExplicitArgumentList
120883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
120983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Info.reset(ExplicitArgumentList);
12101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
121183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Instantiate the types of each of the function parameters given the
121283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // explicitly-specified template arguments.
121383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  for (FunctionDecl::param_iterator P = Function->param_begin(),
121483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                PEnd = Function->param_end();
121583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor       P != PEnd;
121683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor       ++P) {
12171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    QualType ParamType
12181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      = SubstType((*P)->getType(),
1219357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1220357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  (*P)->getLocation(), (*P)->getDeclName());
122183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (ParamType.isNull() || Trap.hasErrorOccurred())
122283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_SubstitutionFailure;
12231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
122483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    ParamTypes.push_back(ParamType);
122583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
122683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
122783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // If the caller wants a full function type back, instantiate the return
122883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // type and form that function type.
122983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (FunctionType) {
123083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    // FIXME: exception-specifications?
12311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const FunctionProtoType *Proto
1232183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall      = Function->getType()->getAs<FunctionProtoType>();
123383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    assert(Proto && "Function template does not have a prototype?");
12341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    QualType ResultType
1236357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor      = SubstType(Proto->getResultType(),
1237357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1238357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  Function->getTypeSpecStartLoc(),
1239357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  Function->getDeclName());
124083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (ResultType.isNull() || Trap.hasErrorOccurred())
124183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_SubstitutionFailure;
12421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    *FunctionType = BuildFunctionType(ResultType,
124483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      ParamTypes.data(), ParamTypes.size(),
124583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      Proto->isVariadic(),
124683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      Proto->getTypeQuals(),
124783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      Function->getLocation(),
1248fa869547eb1cab12d7e0c0dfa8ba594e336b9b32Eli Friedman                                      Function->getDeclName(),
1249fa869547eb1cab12d7e0c0dfa8ba594e336b9b32Eli Friedman                                      Proto->getExtInfo());
125083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (FunctionType->isNull() || Trap.hasErrorOccurred())
125183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_SubstitutionFailure;
125283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
12531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
125483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // C++ [temp.arg.explicit]p2:
12551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   Trailing template arguments that can be deduced (14.8.2) may be
12561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   omitted from the list of explicit template-arguments. If all of the
125783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   template arguments can be deduced, they may all be omitted; in this
125883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   case, the empty template argument list <> itself may also be omitted.
125983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //
126083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Take all of the explicitly-specified arguments and put them into the
12611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // set of deduced template arguments.
126283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Deduced.reserve(TemplateParams->size());
126383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I)
12641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Deduced.push_back(ExplicitArgumentList->get(I));
12651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
126683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  return TDK_Success;
126783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor}
126883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
126902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// \brief Allocate a TemplateArgumentLoc where all locations have
127002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// been initialized to the given location.
127102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor///
127202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// \param S The semantic analysis object.
127302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor///
127402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// \param The template argument we are producing template argument
127502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// location information for.
127602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor///
127702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// \param NTTPType For a declaration template argument, the type of
127802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// the non-type template parameter that corresponds to this template
127902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// argument.
128002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor///
128102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// \param Loc The source location to use for the resulting template
128202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// argument.
128302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregorstatic TemplateArgumentLoc
128402024a9f0d8e6c898de276193af604c42ee41269Douglas GregorgetTrivialTemplateArgumentLoc(Sema &S,
128502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                              const TemplateArgument &Arg,
128602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                              QualType NTTPType,
128702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                              SourceLocation Loc) {
128802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  switch (Arg.getKind()) {
128902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  case TemplateArgument::Null:
129002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    llvm_unreachable("Can't get a NULL template argument here");
129102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    break;
129202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
129302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  case TemplateArgument::Type:
129402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    return TemplateArgumentLoc(Arg,
129502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
129602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
129702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  case TemplateArgument::Declaration: {
129802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    Expr *E
129902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc)
130002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                                              .takeAs<Expr>();
130102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    return TemplateArgumentLoc(TemplateArgument(E), E);
130202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  }
130302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
130402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  case TemplateArgument::Integral: {
130502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    Expr *E
130602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).takeAs<Expr>();
130702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    return TemplateArgumentLoc(TemplateArgument(E), E);
130802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  }
130902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
131002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  case TemplateArgument::Template:
131102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    return TemplateArgumentLoc(Arg, SourceRange(), Loc);
131202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
131302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  case TemplateArgument::Expression:
131402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    return TemplateArgumentLoc(Arg, Arg.getAsExpr());
131502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
131602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  case TemplateArgument::Pack:
131702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    llvm_unreachable("Template parameter packs are not yet supported");
131802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  }
131902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
132002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  return TemplateArgumentLoc();
132102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor}
132202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
13231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Finish template argument deduction for a function template,
132483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// checking the deduced template arguments for completeness and forming
132583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// the function template specialization.
13261eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSema::TemplateDeductionResult
132783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
132802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                       llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
132902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                      unsigned NumExplicitlySpecified,
133083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      FunctionDecl *&Specialization,
133183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      TemplateDeductionInfo &Info) {
133283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateParameterList *TemplateParams
133383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = FunctionTemplate->getTemplateParameters();
13341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
133551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // Template argument deduction for function templates in a SFINAE context.
133651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // Trap any errors that might occur.
133751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  SFINAETrap Trap(*this);
133851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
133951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // Enter a new template instantiation context while we instantiate the
134051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // actual function declaration.
134151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
134251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                             FunctionTemplate, Deduced.data(), Deduced.size(),
134351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor              ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution);
134451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  if (Inst)
134551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    return TDK_InstantiationDepth;
134651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
134796db310ab7ca59e1890ddef25a3701bc2909d20fJohn McCall  ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
1348f5813826802c2e76cdc13cae834ebfd4518d74a6John McCall
134983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // C++ [temp.deduct.type]p2:
135083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   [...] or if any template argument remains neither deduced nor
135183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   explicitly specified, template argument deduction fails.
135283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
135383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
135402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    NamedDecl *Param = FunctionTemplate->getTemplateParameters()->getParam(I);
135551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    if (!Deduced[I].isNull()) {
135602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      if (I < NumExplicitlySpecified ||
135702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor          Deduced[I].getKind() == TemplateArgument::Type) {
135802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        // We have already fully type-checked and converted this
135902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        // argument (because it was explicitly-specified) or no
136002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        // additional checking is necessary (because it's a template
136102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        // type parameter). Just record the presence of this
136202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        // parameter.
136302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        Builder.Append(Deduced[I]);
136402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        continue;
136502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      }
136602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
136702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // We have deduced this argument, so it still needs to be
136802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // checked and converted.
136902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
137002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // First, for a non-type template parameter type that is
137102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // initialized by a declaration, we need the type of the
137202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // corresponding non-type template parameter.
137302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      QualType NTTPType;
137402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      if (NonTypeTemplateParmDecl *NTTP
137502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
137602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        if (Deduced[I].getKind() == TemplateArgument::Declaration) {
137702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor          NTTPType = NTTP->getType();
137802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor          if (NTTPType->isDependentType()) {
137902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor            TemplateArgumentList TemplateArgs(Context, Builder,
138002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                              /*TakeArgs=*/false);
138102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor            NTTPType = SubstType(NTTPType,
138202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                 MultiLevelTemplateArgumentList(TemplateArgs),
138302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                 NTTP->getLocation(),
138402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                 NTTP->getDeclName());
138502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor            if (NTTPType.isNull()) {
138602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor              Info.Param = makeTemplateParameter(Param);
1387ec20f46740a59758b12c22108002395bcf5b6f9bDouglas Gregor              Info.reset(new (Context) TemplateArgumentList(Context, Builder,
1388ec20f46740a59758b12c22108002395bcf5b6f9bDouglas Gregor                                                            /*TakeArgs=*/true));
138902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor              return TDK_SubstitutionFailure;
139002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor            }
139102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor          }
139202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        }
139302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      }
139402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
139502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // Convert the deduced template argument into a template
139602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // argument that we can check, almost as if the user had written
139702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // the template argument explicitly.
139802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      TemplateArgumentLoc Arg = getTrivialTemplateArgumentLoc(*this,
139902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                                              Deduced[I],
140002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                                              NTTPType,
140102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                                           SourceLocation());
140202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
140302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // Check the template argument, converting it as necessary.
140402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      if (CheckTemplateArgument(Param, Arg,
140502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                FunctionTemplate,
140602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                FunctionTemplate->getLocation(),
140702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                FunctionTemplate->getSourceRange().getEnd(),
140802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                Builder,
140902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                Deduced[I].wasDeducedFromArrayBound()
141002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                  ? CTAK_DeducedFromArrayBound
141102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                  : CTAK_Deduced)) {
141202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        Info.Param = makeTemplateParameter(
141302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                         const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1414ec20f46740a59758b12c22108002395bcf5b6f9bDouglas Gregor        Info.reset(new (Context) TemplateArgumentList(Context, Builder,
1415ec20f46740a59758b12c22108002395bcf5b6f9bDouglas Gregor                                                      /*TakeArgs=*/true));
141602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        return TDK_SubstitutionFailure;
141702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      }
141802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
141951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      continue;
142051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    }
142151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
142251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // Substitute into the default template argument, if available.
142351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    TemplateArgumentLoc DefArg
142451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
142551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                              FunctionTemplate->getLocation(),
142651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                  FunctionTemplate->getSourceRange().getEnd(),
142751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                                Param,
142851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                                Builder);
142951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
143051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // If there was no default argument, deduction is incomplete.
143151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    if (DefArg.getArgument().isNull()) {
143283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      Info.Param = makeTemplateParameter(
143351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                         const_cast<NamedDecl *>(TemplateParams->getParam(I)));
143483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_Incomplete;
143583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    }
143651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
143751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // Check whether we can actually use the default argument.
143851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    if (CheckTemplateArgument(Param, DefArg,
143951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                              FunctionTemplate,
144051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                              FunctionTemplate->getLocation(),
144151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                              FunctionTemplate->getSourceRange().getEnd(),
144202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                              Builder,
144302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                              CTAK_Deduced)) {
144451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      Info.Param = makeTemplateParameter(
144551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                         const_cast<NamedDecl *>(TemplateParams->getParam(I)));
1446ec20f46740a59758b12c22108002395bcf5b6f9bDouglas Gregor      Info.reset(new (Context) TemplateArgumentList(Context, Builder,
1447ec20f46740a59758b12c22108002395bcf5b6f9bDouglas Gregor                                                    /*TakeArgs=*/true));
144851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      return TDK_SubstitutionFailure;
144951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    }
14501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
145151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // If we get here, we successfully used the default template argument.
145283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
14531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
145483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Form the template argument list from the deduced template arguments.
14551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentList *DeducedArgumentList
145683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
145783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Info.reset(DeducedArgumentList);
14581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Substitute the deduced template arguments into the function template
146083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // declaration to produce the function template specialization.
1461d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor  DeclContext *Owner = FunctionTemplate->getDeclContext();
1462d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor  if (FunctionTemplate->getFriendObjectKind())
1463d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor    Owner = FunctionTemplate->getLexicalDeclContext();
146483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Specialization = cast_or_null<FunctionDecl>(
1465d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor                      SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner,
1466357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                         MultiLevelTemplateArgumentList(*DeducedArgumentList)));
146783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (!Specialization)
146883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_SubstitutionFailure;
14691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1470f882574cf640d5c8355965e1c486f9d8d8ffcf47Douglas Gregor  assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
1471f882574cf640d5c8355965e1c486f9d8d8ffcf47Douglas Gregor         FunctionTemplate->getCanonicalDecl());
1472f882574cf640d5c8355965e1c486f9d8d8ffcf47Douglas Gregor
14731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If the template argument list is owned by the function template
147483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // specialization, release it.
1475ec20f46740a59758b12c22108002395bcf5b6f9bDouglas Gregor  if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList &&
1476ec20f46740a59758b12c22108002395bcf5b6f9bDouglas Gregor      !Trap.hasErrorOccurred())
147783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    Info.take();
14781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
147983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // There may have been an error that did not prevent us from constructing a
148083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // declaration. Mark the declaration invalid and return with a substitution
148183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // failure.
148283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (Trap.hasErrorOccurred()) {
148383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    Specialization->setInvalidDecl(true);
148483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_SubstitutionFailure;
148583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
14861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return TDK_Success;
148883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor}
148983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
1490eff92135d32039c9874dc356f3e93143af6069c1John McCallstatic QualType GetTypeOfFunction(ASTContext &Context,
1491eff92135d32039c9874dc356f3e93143af6069c1John McCall                                  bool isAddressOfOperand,
1492eff92135d32039c9874dc356f3e93143af6069c1John McCall                                  FunctionDecl *Fn) {
1493eff92135d32039c9874dc356f3e93143af6069c1John McCall  if (!isAddressOfOperand) return Fn->getType();
1494eff92135d32039c9874dc356f3e93143af6069c1John McCall  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
1495eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (Method->isInstance())
1496eff92135d32039c9874dc356f3e93143af6069c1John McCall      return Context.getMemberPointerType(Fn->getType(),
1497eff92135d32039c9874dc356f3e93143af6069c1John McCall               Context.getTypeDeclType(Method->getParent()).getTypePtr());
1498eff92135d32039c9874dc356f3e93143af6069c1John McCall  return Context.getPointerType(Fn->getType());
1499eff92135d32039c9874dc356f3e93143af6069c1John McCall}
1500eff92135d32039c9874dc356f3e93143af6069c1John McCall
1501eff92135d32039c9874dc356f3e93143af6069c1John McCall/// Apply the deduction rules for overload sets.
1502eff92135d32039c9874dc356f3e93143af6069c1John McCall///
1503eff92135d32039c9874dc356f3e93143af6069c1John McCall/// \return the null type if this argument should be treated as an
1504eff92135d32039c9874dc356f3e93143af6069c1John McCall/// undeduced context
1505eff92135d32039c9874dc356f3e93143af6069c1John McCallstatic QualType
1506eff92135d32039c9874dc356f3e93143af6069c1John McCallResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
1507eff92135d32039c9874dc356f3e93143af6069c1John McCall                            Expr *Arg, QualType ParamType) {
15087bb12da2b0749eeebb21854c77877736969e59f2John McCall  llvm::PointerIntPair<OverloadExpr*,1> R = OverloadExpr::find(Arg);
1509eff92135d32039c9874dc356f3e93143af6069c1John McCall
15107bb12da2b0749eeebb21854c77877736969e59f2John McCall  bool isAddressOfOperand = bool(R.getInt());
15117bb12da2b0749eeebb21854c77877736969e59f2John McCall  OverloadExpr *Ovl = R.getPointer();
1512eff92135d32039c9874dc356f3e93143af6069c1John McCall
1513eff92135d32039c9874dc356f3e93143af6069c1John McCall  // If there were explicit template arguments, we can only find
1514eff92135d32039c9874dc356f3e93143af6069c1John McCall  // something via C++ [temp.arg.explicit]p3, i.e. if the arguments
1515eff92135d32039c9874dc356f3e93143af6069c1John McCall  // unambiguously name a full specialization.
15167bb12da2b0749eeebb21854c77877736969e59f2John McCall  if (Ovl->hasExplicitTemplateArgs()) {
1517eff92135d32039c9874dc356f3e93143af6069c1John McCall    // But we can still look for an explicit specialization.
1518eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (FunctionDecl *ExplicitSpec
15197bb12da2b0749eeebb21854c77877736969e59f2John McCall          = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
15207bb12da2b0749eeebb21854c77877736969e59f2John McCall      return GetTypeOfFunction(S.Context, isAddressOfOperand, ExplicitSpec);
1521eff92135d32039c9874dc356f3e93143af6069c1John McCall    return QualType();
1522eff92135d32039c9874dc356f3e93143af6069c1John McCall  }
1523eff92135d32039c9874dc356f3e93143af6069c1John McCall
1524eff92135d32039c9874dc356f3e93143af6069c1John McCall  // C++0x [temp.deduct.call]p6:
1525eff92135d32039c9874dc356f3e93143af6069c1John McCall  //   When P is a function type, pointer to function type, or pointer
1526eff92135d32039c9874dc356f3e93143af6069c1John McCall  //   to member function type:
1527eff92135d32039c9874dc356f3e93143af6069c1John McCall
1528eff92135d32039c9874dc356f3e93143af6069c1John McCall  if (!ParamType->isFunctionType() &&
1529eff92135d32039c9874dc356f3e93143af6069c1John McCall      !ParamType->isFunctionPointerType() &&
1530eff92135d32039c9874dc356f3e93143af6069c1John McCall      !ParamType->isMemberFunctionPointerType())
1531eff92135d32039c9874dc356f3e93143af6069c1John McCall    return QualType();
1532eff92135d32039c9874dc356f3e93143af6069c1John McCall
1533eff92135d32039c9874dc356f3e93143af6069c1John McCall  QualType Match;
15347bb12da2b0749eeebb21854c77877736969e59f2John McCall  for (UnresolvedSetIterator I = Ovl->decls_begin(),
15357bb12da2b0749eeebb21854c77877736969e59f2John McCall         E = Ovl->decls_end(); I != E; ++I) {
1536eff92135d32039c9874dc356f3e93143af6069c1John McCall    NamedDecl *D = (*I)->getUnderlyingDecl();
1537eff92135d32039c9874dc356f3e93143af6069c1John McCall
1538eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   - If the argument is an overload set containing one or more
1539eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     function templates, the parameter is treated as a
1540eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     non-deduced context.
1541eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (isa<FunctionTemplateDecl>(D))
1542eff92135d32039c9874dc356f3e93143af6069c1John McCall      return QualType();
1543eff92135d32039c9874dc356f3e93143af6069c1John McCall
1544eff92135d32039c9874dc356f3e93143af6069c1John McCall    FunctionDecl *Fn = cast<FunctionDecl>(D);
1545eff92135d32039c9874dc356f3e93143af6069c1John McCall    QualType ArgType = GetTypeOfFunction(S.Context, isAddressOfOperand, Fn);
1546eff92135d32039c9874dc356f3e93143af6069c1John McCall
1547eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   - If the argument is an overload set (not containing function
1548eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     templates), trial argument deduction is attempted using each
1549eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     of the members of the set. If deduction succeeds for only one
1550eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     of the overload set members, that member is used as the
1551eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     argument value for the deduction. If deduction succeeds for
1552eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     more than one member of the overload set the parameter is
1553eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     treated as a non-deduced context.
1554eff92135d32039c9874dc356f3e93143af6069c1John McCall
1555eff92135d32039c9874dc356f3e93143af6069c1John McCall    // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
1556eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   Type deduction is done independently for each P/A pair, and
1557eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   the deduced template argument values are then combined.
1558eff92135d32039c9874dc356f3e93143af6069c1John McCall    // So we do not reject deductions which were made elsewhere.
155902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    llvm::SmallVector<DeducedTemplateArgument, 8>
156002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      Deduced(TemplateParams->size());
15615769d6195087229770d7ac90449443e026c47103John McCall    Sema::TemplateDeductionInfo Info(S.Context, Ovl->getNameLoc());
1562eff92135d32039c9874dc356f3e93143af6069c1John McCall    unsigned TDF = 0;
1563eff92135d32039c9874dc356f3e93143af6069c1John McCall
1564eff92135d32039c9874dc356f3e93143af6069c1John McCall    Sema::TemplateDeductionResult Result
1565a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      = DeduceTemplateArguments(S, TemplateParams,
1566eff92135d32039c9874dc356f3e93143af6069c1John McCall                                ParamType, ArgType,
1567eff92135d32039c9874dc356f3e93143af6069c1John McCall                                Info, Deduced, TDF);
1568eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (Result) continue;
1569eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (!Match.isNull()) return QualType();
1570eff92135d32039c9874dc356f3e93143af6069c1John McCall    Match = ArgType;
1571eff92135d32039c9874dc356f3e93143af6069c1John McCall  }
1572eff92135d32039c9874dc356f3e93143af6069c1John McCall
1573eff92135d32039c9874dc356f3e93143af6069c1John McCall  return Match;
1574eff92135d32039c9874dc356f3e93143af6069c1John McCall}
1575eff92135d32039c9874dc356f3e93143af6069c1John McCall
1576e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \brief Perform template argument deduction from a function call
1577e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// (C++ [temp.deduct.call]).
1578e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1579e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param FunctionTemplate the function template for which we are performing
1580e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// template argument deduction.
1581e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
158248026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// \param ExplicitTemplateArguments the explicit template arguments provided
158348026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// for this call.
15846db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor///
1585e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Args the function call arguments
1586e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1587e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param NumArgs the number of arguments in Args
1588e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
158948026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// \param Name the name of the function being called. This is only significant
159048026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// when the function template is a conversion function template, in which
159148026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// case this routine will also perform template argument deduction based on
159248026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// the function to which
159348026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor///
1594e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Specialization if template argument deduction was successful,
15951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// this will be set to the function template specialization produced by
1596e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// template argument deduction.
1597e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1598e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Info the argument will be updated to provide additional information
1599e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// about template argument deduction.
1600e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1601e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns the result of template argument deduction.
1602e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorSema::TemplateDeductionResult
1603e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
160448026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor                          const TemplateArgumentListInfo *ExplicitTemplateArgs,
1605e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                              Expr **Args, unsigned NumArgs,
1606e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                              FunctionDecl *&Specialization,
1607e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                              TemplateDeductionInfo &Info) {
1608e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
16096db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor
1610e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // C++ [temp.deduct.call]p1:
1611e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  //   Template argument deduction is done by comparing each function template
1612e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  //   parameter type (call it P) with the type of the corresponding argument
1613e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  //   of the call (call it A) as described below.
1614e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  unsigned CheckArgs = NumArgs;
16156db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  if (NumArgs < Function->getMinRequiredArguments())
1616e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return TDK_TooFewArguments;
1617e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  else if (NumArgs > Function->getNumParams()) {
16181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const FunctionProtoType *Proto
1619183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall      = Function->getType()->getAs<FunctionProtoType>();
1620e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (!Proto->isVariadic())
1621e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return TDK_TooManyArguments;
16221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1623e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    CheckArgs = Function->getNumParams();
1624e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  }
16251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16266db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  // The types of the parameters from which we will perform template argument
16276db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  // deduction.
16282b0749a4f8965d0205bf77322db150c13c39e3beDouglas Gregor  Sema::LocalInstantiationScope InstScope(*this);
1629e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  TemplateParameterList *TemplateParams
1630e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    = FunctionTemplate->getTemplateParameters();
163102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
16326db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  llvm::SmallVector<QualType, 4> ParamTypes;
163302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  unsigned NumExplicitlySpecified = 0;
1634d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (ExplicitTemplateArgs) {
163583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    TemplateDeductionResult Result =
163683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      SubstituteExplicitTemplateArguments(FunctionTemplate,
1637d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                          *ExplicitTemplateArgs,
163883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          Deduced,
163983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          ParamTypes,
164083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          0,
164183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          Info);
164283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (Result)
164383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return Result;
164402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
164502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    NumExplicitlySpecified = Deduced.size();
16466db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  } else {
16476db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor    // Just fill in the parameter types from the function declaration.
16486db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor    for (unsigned I = 0; I != CheckArgs; ++I)
16496db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor      ParamTypes.push_back(Function->getParamDecl(I)->getType());
16506db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  }
16511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16526db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  // Deduce template arguments from the function parameters.
16531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Deduced.resize(TemplateParams->size());
1654e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  for (unsigned I = 0; I != CheckArgs; ++I) {
16556db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor    QualType ParamType = ParamTypes[I];
1656e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    QualType ArgType = Args[I]->getType();
16571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1658eff92135d32039c9874dc356f3e93143af6069c1John McCall    // Overload sets usually make this parameter an undeduced
1659eff92135d32039c9874dc356f3e93143af6069c1John McCall    // context, but there are sometimes special circumstances.
1660eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (ArgType == Context.OverloadTy) {
1661eff92135d32039c9874dc356f3e93143af6069c1John McCall      ArgType = ResolveOverloadForDeduction(*this, TemplateParams,
1662eff92135d32039c9874dc356f3e93143af6069c1John McCall                                            Args[I], ParamType);
1663eff92135d32039c9874dc356f3e93143af6069c1John McCall      if (ArgType.isNull())
1664eff92135d32039c9874dc356f3e93143af6069c1John McCall        continue;
1665eff92135d32039c9874dc356f3e93143af6069c1John McCall    }
1666eff92135d32039c9874dc356f3e93143af6069c1John McCall
1667e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    // C++ [temp.deduct.call]p2:
1668e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    //   If P is not a reference type:
1669e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    QualType CanonParamType = Context.getCanonicalType(ParamType);
1670500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor    bool ParamWasReference = isa<ReferenceType>(CanonParamType);
1671500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor    if (!ParamWasReference) {
16721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   - If A is an array type, the pointer type produced by the
16731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //     array-to-pointer standard conversion (4.2) is used in place of
1674e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      //     A for type deduction; otherwise,
1675e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      if (ArgType->isArrayType())
1676e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        ArgType = Context.getArrayDecayedType(ArgType);
16771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   - If A is a function type, the pointer type produced by the
16781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //     function-to-pointer standard conversion (4.3) is used in place
1679e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      //     of A for type deduction; otherwise,
1680e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      else if (ArgType->isFunctionType())
1681e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        ArgType = Context.getPointerType(ArgType);
1682e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      else {
1683e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
1684e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        //   type are ignored for type deduction.
1685e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        QualType CanonArgType = Context.getCanonicalType(ArgType);
1686a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor        if (CanonArgType.getLocalCVRQualifiers())
1687a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor          ArgType = CanonArgType.getLocalUnqualifiedType();
1688e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      }
1689e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    }
16901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1691e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    // C++0x [temp.deduct.call]p3:
1692e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    //   If P is a cv-qualified type, the top level cv-qualifiers of P’s type
16931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //   are ignored for type deduction.
1694a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor    if (CanonParamType.getLocalCVRQualifiers())
1695a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor      ParamType = CanonParamType.getLocalUnqualifiedType();
16966217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
16971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   [...] If P is a reference type, the type referred to by P is used
16981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   for type deduction.
1699e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      ParamType = ParamRefType->getPointeeType();
17001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
17011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   [...] If P is of the form T&&, where T is a template parameter, and
17021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   the argument is an lvalue, the type A& is used in place of A for
1703e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      //   type deduction.
1704e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      if (isa<RValueReferenceType>(ParamRefType) &&
1705183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall          ParamRefType->getAs<TemplateTypeParmType>() &&
1706e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor          Args[I]->isLvalue(Context) == Expr::LV_Valid)
1707e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        ArgType = Context.getLValueReferenceType(ArgType);
1708e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    }
17091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1710e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    // C++0x [temp.deduct.call]p4:
1711e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    //   In general, the deduction process attempts to find template argument
1712e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    //   values that will make the deduced A identical to A (after the type A
1713e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    //   is transformed as described above). [...]
17141282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor    unsigned TDF = TDF_SkipNonDependent;
17151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1716508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    //     - If the original P is a reference type, the deduced A (i.e., the
1717508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    //       type referred to by the reference) can be more cv-qualified than
1718508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    //       the transformed A.
1719508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    if (ParamWasReference)
1720508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor      TDF |= TDF_ParamWithReferenceType;
17211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     - The transformed A can be another pointer or pointer to member
17221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //       type that can be converted to the deduced A via a qualification
1723508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    //       conversion (4.4).
1724db0bc4706c4bda8e9166f40b5c7cfda656100818John McCall    if (ArgType->isPointerType() || ArgType->isMemberPointerType() ||
1725db0bc4706c4bda8e9166f40b5c7cfda656100818John McCall        ArgType->isObjCObjectPointerType())
1726508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor      TDF |= TDF_IgnoreQualifiers;
17271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     - If P is a class and P has the form simple-template-id, then the
17284112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    //       transformed A can be a derived class of the deduced A. Likewise,
17294112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    //       if P is a pointer to a class of the form simple-template-id, the
17304112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    //       transformed A can be a pointer to a derived class pointed to by
17314112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    //       the deduced A.
17324112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    if (isSimpleTemplateIdType(ParamType) ||
17331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        (isa<PointerType>(ParamType) &&
17344112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor         isSimpleTemplateIdType(
17356217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek                              ParamType->getAs<PointerType>()->getPointeeType())))
17364112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor      TDF |= TDF_DerivedClass;
17371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1738e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (TemplateDeductionResult Result
1739a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = ::DeduceTemplateArguments(*this, TemplateParams,
1740500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor                                    ParamType, ArgType, Info, Deduced,
1741508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                    TDF))
1742e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return Result;
17431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
174465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    // FIXME: we need to check that the deduced A is the same as A,
174565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    // modulo the various allowed differences.
1746e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  }
174765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
17481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
174902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                         NumExplicitlySpecified,
175083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                         Specialization, Info);
175183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor}
1752127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor
175383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \brief Deduce template arguments when taking the address of a function
17544b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
17554b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// a template.
175683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
175783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionTemplate the function template for which we are performing
175883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template argument deduction.
175983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
17604b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param ExplicitTemplateArguments the explicitly-specified template
17614b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// arguments.
176283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
176383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param ArgFunctionType the function type that will be used as the
176483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// "argument" type (A) when performing template argument deduction from the
17654b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// function template's function type. This type may be NULL, if there is no
17664b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
176783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
176883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Specialization if template argument deduction was successful,
17691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// this will be set to the function template specialization produced by
177083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template argument deduction.
177183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
177283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Info the argument will be updated to provide additional information
177383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// about template argument deduction.
177483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
177583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \returns the result of template argument deduction.
177683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::TemplateDeductionResult
177783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1778d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                        const TemplateArgumentListInfo *ExplicitTemplateArgs,
177983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                              QualType ArgFunctionType,
178083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                              FunctionDecl *&Specialization,
178183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                              TemplateDeductionInfo &Info) {
178283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
178383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateParameterList *TemplateParams
178483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = FunctionTemplate->getTemplateParameters();
178583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  QualType FunctionType = Function->getType();
17861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
178783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Substitute any explicit template arguments.
17882b0749a4f8965d0205bf77322db150c13c39e3beDouglas Gregor  Sema::LocalInstantiationScope InstScope(*this);
178902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
179002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  unsigned NumExplicitlySpecified = 0;
179183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  llvm::SmallVector<QualType, 4> ParamTypes;
1792d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (ExplicitTemplateArgs) {
17931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (TemplateDeductionResult Result
17941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          = SubstituteExplicitTemplateArguments(FunctionTemplate,
1795d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                *ExplicitTemplateArgs,
17961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                Deduced, ParamTypes,
179783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                                &FunctionType, Info))
179883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return Result;
179902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
180002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    NumExplicitlySpecified = Deduced.size();
1801127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
180283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
180383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Template argument deduction for function templates in a SFINAE context.
180483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Trap any errors that might occur.
18051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SFINAETrap Trap(*this);
18061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1807eff92135d32039c9874dc356f3e93143af6069c1John McCall  Deduced.resize(TemplateParams->size());
1808eff92135d32039c9874dc356f3e93143af6069c1John McCall
18094b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  if (!ArgFunctionType.isNull()) {
18104b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    // Deduce template arguments from the function type.
18114b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (TemplateDeductionResult Result
1812a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          = ::DeduceTemplateArguments(*this, TemplateParams,
18134b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                                      FunctionType, ArgFunctionType, Info,
18144b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                                      Deduced, 0))
18154b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      return Result;
18164b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  }
18174b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor
18181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
181902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                         NumExplicitlySpecified,
182083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                         Specialization, Info);
1821e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
1822e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
182365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \brief Deduce template arguments for a templated conversion
182465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// function (C++ [temp.deduct.conv]) and, if successful, produce a
182565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// conversion function template specialization.
182665ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::TemplateDeductionResult
182765ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
182865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                              QualType ToType,
182965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                              CXXConversionDecl *&Specialization,
183065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                              TemplateDeductionInfo &Info) {
18311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  CXXConversionDecl *Conv
183265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
183365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  QualType FromType = Conv->getConversionType();
183465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
183565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Canonicalize the types for deduction.
183665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  QualType P = Context.getCanonicalType(FromType);
183765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  QualType A = Context.getCanonicalType(ToType);
183865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
183965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++0x [temp.deduct.conv]p3:
184065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   If P is a reference type, the type referred to by P is used for
184165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   type deduction.
184265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (const ReferenceType *PRef = P->getAs<ReferenceType>())
184365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    P = PRef->getPointeeType();
184465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
184565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++0x [temp.deduct.conv]p3:
184665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   If A is a reference type, the type referred to by A is used
184765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   for type deduction.
184865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (const ReferenceType *ARef = A->getAs<ReferenceType>())
184965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    A = ARef->getPointeeType();
185065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++ [temp.deduct.conv]p2:
185165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //
18521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   If A is not a reference type:
185365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  else {
185465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    assert(!A->isReferenceType() && "Reference types were handled above");
185565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
185665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   - If P is an array type, the pointer type produced by the
18571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     array-to-pointer standard conversion (4.2) is used in place
185865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     of P for type deduction; otherwise,
185965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    if (P->isArrayType())
186065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      P = Context.getArrayDecayedType(P);
186165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   - If P is a function type, the pointer type produced by the
186265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     function-to-pointer standard conversion (4.3) is used in
186365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     place of P for type deduction; otherwise,
186465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    else if (P->isFunctionType())
186565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      P = Context.getPointerType(P);
186665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   - If P is a cv-qualified type, the top level cv-qualifiers of
186765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     P’s type are ignored for type deduction.
186865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    else
186965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      P = P.getUnqualifiedType();
187065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
187165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    // C++0x [temp.deduct.conv]p3:
187265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   If A is a cv-qualified type, the top level cv-qualifiers of A’s
187365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   type are ignored for type deduction.
187465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    A = A.getUnqualifiedType();
187565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  }
187665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
187765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Template argument deduction for function templates in a SFINAE context.
187865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Trap any errors that might occur.
18791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SFINAETrap Trap(*this);
188065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
188165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++ [temp.deduct.conv]p1:
188265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   Template argument deduction is done by comparing the return
188365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   type of the template conversion function (call it P) with the
188465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   type that is required as the result of the conversion (call it
188565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   A) as described in 14.8.2.4.
188665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  TemplateParameterList *TemplateParams
188765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    = FunctionTemplate->getTemplateParameters();
188802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
18891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Deduced.resize(TemplateParams->size());
189065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
189165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++0x [temp.deduct.conv]p4:
189265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   In general, the deduction process attempts to find template
189365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   argument values that will make the deduced A identical to
189465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   A. However, there are two cases that allow a difference:
189565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  unsigned TDF = 0;
189665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //     - If the original A is a reference type, A can be more
189765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       cv-qualified than the deduced A (i.e., the type referred to
189865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       by the reference)
189965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (ToType->isReferenceType())
190065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    TDF |= TDF_ParamWithReferenceType;
190165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //     - The deduced A can be another pointer or pointer to member
190265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       type that can be converted to A via a qualification
190365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       conversion.
190465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //
190565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
190665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // both P and A are pointers or member pointers. In this case, we
190765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // just ignore cv-qualifiers completely).
190865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if ((P->isPointerType() && A->isPointerType()) ||
190965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      (P->isMemberPointerType() && P->isMemberPointerType()))
191065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    TDF |= TDF_IgnoreQualifiers;
191165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (TemplateDeductionResult Result
1912a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = ::DeduceTemplateArguments(*this, TemplateParams,
191365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                    P, A, Info, Deduced, TDF))
191465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    return Result;
191565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
191665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // FIXME: we need to check that the deduced A is the same as A,
191765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // modulo the various allowed differences.
19181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
191965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Finish template argument deduction.
19202b0749a4f8965d0205bf77322db150c13c39e3beDouglas Gregor  Sema::LocalInstantiationScope InstScope(*this);
192165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  FunctionDecl *Spec = 0;
192265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  TemplateDeductionResult Result
192302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 0, Spec,
192402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                      Info);
192565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  Specialization = cast_or_null<CXXConversionDecl>(Spec);
192665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return Result;
192765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor}
192865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
19294b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \brief Deduce template arguments for a function template when there is
19304b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
19314b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
19324b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param FunctionTemplate the function template for which we are performing
19334b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template argument deduction.
19344b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
19354b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param ExplicitTemplateArguments the explicitly-specified template
19364b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// arguments.
19374b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
19384b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param Specialization if template argument deduction was successful,
19394b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// this will be set to the function template specialization produced by
19404b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template argument deduction.
19414b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
19424b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param Info the argument will be updated to provide additional information
19434b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// about template argument deduction.
19444b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
19454b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \returns the result of template argument deduction.
19464b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas GregorSema::TemplateDeductionResult
19474b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
19484b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                           const TemplateArgumentListInfo *ExplicitTemplateArgs,
19494b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                              FunctionDecl *&Specialization,
19504b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                              TemplateDeductionInfo &Info) {
19514b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
19524b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                                 QualType(), Specialization, Info);
19534b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor}
19544b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor
19558a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Stores the result of comparing the qualifiers of two types.
19568a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorenum DeductionQualifierComparison {
19578a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  NeitherMoreQualified = 0,
19588a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  ParamMoreQualified,
19598a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  ArgMoreQualified
19608a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor};
19618a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19628a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Deduce the template arguments during partial ordering by comparing
19638a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// the parameter type and the argument type (C++0x [temp.deduct.partial]).
19648a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
1965a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth/// \param S the semantic analysis object within which we are deducing
19668a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
19678a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param TemplateParams the template parameters that we are deducing
19688a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
19698a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param ParamIn the parameter type
19708a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
19718a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param ArgIn the argument type
19728a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
19738a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param Info information about the template argument deduction itself
19748a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
19758a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param Deduced the deduced template arguments
19768a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
19778a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \returns the result of template argument deduction so far. Note that a
19788a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// "success" result means that template argument deduction has not yet failed,
19798a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// but it may still fail, later, for other reasons.
19808a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic Sema::TemplateDeductionResult
1981a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArgumentsDuringPartialOrdering(Sema &S,
198202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                        TemplateParameterList *TemplateParams,
19838a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                             QualType ParamIn, QualType ArgIn,
19848a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                             Sema::TemplateDeductionInfo &Info,
198502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                      llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
198602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor   llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
1987a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  CanQualType Param = S.Context.getCanonicalType(ParamIn);
1988a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  CanQualType Arg = S.Context.getCanonicalType(ArgIn);
19898a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19908a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p5:
19918a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   Before the partial ordering is done, certain transformations are
19928a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   performed on the types used for partial ordering:
19938a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //     - If P is a reference type, P is replaced by the type referred to.
19948a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>();
1995e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  if (!ParamRef.isNull())
19968a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    Param = ParamRef->getPointeeType();
19978a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19988a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //     - If A is a reference type, A is replaced by the type referred to.
19998a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>();
2000e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  if (!ArgRef.isNull())
20018a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    Arg = ArgRef->getPointeeType();
20028a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
2003e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) {
20048a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // C++0x [temp.deduct.partial]p6:
20058a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   If both P and A were reference types (before being replaced with the
20068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   type referred to above), determine which of the two types (if any) is
20078a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   more cv-qualified than the other; otherwise the types are considered to
20088a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   be equally cv-qualified for partial ordering purposes. The result of this
20098a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   determination will be used below.
20108a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //
20118a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // We save this information for later, using it only when deduction
20128a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // succeeds in both directions.
20138a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    DeductionQualifierComparison QualifierResult = NeitherMoreQualified;
20148a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    if (Param.isMoreQualifiedThan(Arg))
20158a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      QualifierResult = ParamMoreQualified;
20168a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    else if (Arg.isMoreQualifiedThan(Param))
20178a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      QualifierResult = ArgMoreQualified;
20188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    QualifierComparisons->push_back(QualifierResult);
20198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
20208a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p7:
20228a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   Remove any top-level cv-qualifiers:
20238a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //     - If P is a cv-qualified type, P is replaced by the cv-unqualified
20248a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //       version of P.
20258a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Param = Param.getUnqualifiedType();
20268a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //     - If A is a cv-qualified type, A is replaced by the cv-unqualified
20278a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //       version of A.
20288a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Arg = Arg.getUnqualifiedType();
20298a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20308a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p8:
20318a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   Using the resulting types P and A the deduction is then done as
20328a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   described in 14.9.2.5. If deduction succeeds for a given type, the type
20338a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   from the argument template is considered to be at least as specialized
20348a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   as the type from the parameter template.
2035a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  return DeduceTemplateArguments(S, TemplateParams, Param, Arg, Info,
20368a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                 Deduced, TDF_None);
20378a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor}
20388a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20398a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic void
2040e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2041e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2042ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Level,
2043e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Deduced);
20448a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20458a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Determine whether the function template \p FT1 is at least as
20468a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// specialized as \p FT2.
20478a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic bool isAtLeastAsSpecializedAs(Sema &S,
20485769d6195087229770d7ac90449443e026c47103John McCall                                     SourceLocation Loc,
20498a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                     FunctionTemplateDecl *FT1,
20508a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                     FunctionTemplateDecl *FT2,
20518a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                     TemplatePartialOrderingContext TPOC,
20528a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
20538a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  FunctionDecl *FD1 = FT1->getTemplatedDecl();
20548a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  FunctionDecl *FD2 = FT2->getTemplatedDecl();
20558a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
20568a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
20578a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20588a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  assert(Proto1 && Proto2 && "Function templates must have prototypes");
20598a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
206002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
20618a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Deduced.resize(TemplateParams->size());
20628a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20638a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p3:
20648a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   The types used to determine the ordering depend on the context in which
20658a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   the partial ordering is done:
20665769d6195087229770d7ac90449443e026c47103John McCall  Sema::TemplateDeductionInfo Info(S.Context, Loc);
20678a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  switch (TPOC) {
20688a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Call: {
20698a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   - In the context of a function call, the function parameter types are
20708a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //     used.
20718a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
20728a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    for (unsigned I = 0; I != NumParams; ++I)
2073a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      if (DeduceTemplateArgumentsDuringPartialOrdering(S,
20748a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                       TemplateParams,
20758a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                       Proto2->getArgType(I),
20768a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                       Proto1->getArgType(I),
20778a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                       Info,
20788a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                       Deduced,
20798a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                       QualifierComparisons))
20808a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        return false;
20818a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20828a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
20838a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
20848a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20858a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Conversion:
20868a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   - In the context of a call to a conversion operator, the return types
20878a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //     of the conversion function templates are used.
2088a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth    if (DeduceTemplateArgumentsDuringPartialOrdering(S,
20898a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     TemplateParams,
20908a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     Proto2->getResultType(),
20918a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     Proto1->getResultType(),
20928a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     Info,
20938a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     Deduced,
20948a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     QualifierComparisons))
20958a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      return false;
20968a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
20978a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20988a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Other:
20998a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   - In other contexts (14.6.6.2) the function template’s function type
21008a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //     is used.
2101a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth    if (DeduceTemplateArgumentsDuringPartialOrdering(S,
21028a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     TemplateParams,
21038a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     FD2->getType(),
21048a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     FD1->getType(),
21058a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     Info,
21068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     Deduced,
21078a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     QualifierComparisons))
21088a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      return false;
21098a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
21108a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
21118a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
21128a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p11:
21138a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   In most cases, all template parameters must have values in order for
21148a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   deduction to succeed, but for partial ordering purposes a template
21158a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   parameter may remain without a value provided it is not used in the
21168a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   types being used for partial ordering. [ Note: a template parameter used
21178a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   in a non-deduced context is considered used. -end note]
21188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  unsigned ArgIdx = 0, NumArgs = Deduced.size();
21198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  for (; ArgIdx != NumArgs; ++ArgIdx)
21208a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    if (Deduced[ArgIdx].isNull())
21218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      break;
21228a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
21238a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  if (ArgIdx == NumArgs) {
21248a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // All template arguments were deduced. FT1 is at least as specialized
21258a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // as FT2.
21268a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return true;
21278a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
21288a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
2129e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // Figure out which template parameters were used.
21308a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  llvm::SmallVector<bool, 4> UsedParameters;
21318a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  UsedParameters.resize(TemplateParams->size());
21328a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  switch (TPOC) {
21338a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Call: {
21348a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
21358a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    for (unsigned I = 0; I != NumParams; ++I)
2136ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
2137ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                   TemplateParams->getDepth(),
2138e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                   UsedParameters);
21398a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
21408a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
21418a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
21428a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Conversion:
2143ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
2144ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 TemplateParams->getDepth(),
2145e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                 UsedParameters);
21468a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
21478a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
21488a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Other:
2149ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    ::MarkUsedTemplateParameters(S, FD2->getType(), false,
2150ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 TemplateParams->getDepth(),
2151ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 UsedParameters);
21528a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
21538a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
21548a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
21558a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  for (; ArgIdx != NumArgs; ++ArgIdx)
21568a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // If this argument had no value deduced but was used in one of the types
21578a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // used for partial ordering, then deduction fails.
21588a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
21598a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      return false;
21608a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
21618a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  return true;
21628a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor}
21638a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
21648a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
2165bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \brief Returns the more specialized function template according
216665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// to the rules of function template partial ordering (C++ [temp.func.order]).
216765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor///
216865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \param FT1 the first function template
216965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor///
217065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \param FT2 the second function template
217165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor///
21728a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param TPOC the context in which we are performing partial ordering of
21738a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// function templates.
21741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
2175bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \returns the more specialized function template. If neither
217665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// template is more specialized, returns NULL.
217765ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorFunctionTemplateDecl *
217865ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
217965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                 FunctionTemplateDecl *FT2,
21805769d6195087229770d7ac90449443e026c47103John McCall                                 SourceLocation Loc,
21818a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                 TemplatePartialOrderingContext TPOC) {
21828a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons;
21835769d6195087229770d7ac90449443e026c47103John McCall  bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, 0);
21845769d6195087229770d7ac90449443e026c47103John McCall  bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC,
21858a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                          &QualifierComparisons);
21868a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
21878a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  if (Better1 != Better2) // We have a clear winner
21888a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return Better1? FT1 : FT2;
21898a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
21908a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  if (!Better1 && !Better2) // Neither is better than the other
219165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    return 0;
21928a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
21938a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
21948a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p10:
21958a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   If for each type being considered a given template is at least as
21968a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   specialized for all types and more specialized for some set of types and
21978a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   the other template is not more specialized for any types or is not at
21988a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   least as specialized for any types, then the given template is more
21998a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   specialized than the other template. Otherwise, neither template is more
22008a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   specialized than the other.
22018a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Better1 = false;
22028a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Better2 = false;
22038a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) {
22048a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // C++0x [temp.deduct.partial]p9:
22058a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   If, for a given type, deduction succeeds in both directions (i.e., the
22068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   types are identical after the transformations above) and if the type
22078a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   from the argument template is more cv-qualified than the type from the
22088a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   parameter template (as described above) that type is considered to be
22098a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   more specialized than the other. If neither type is more cv-qualified
22108a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   than the other then neither type is more specialized than the other.
22118a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    switch (QualifierComparisons[I]) {
22128a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      case NeitherMoreQualified:
22138a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        break;
22148a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
22158a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      case ParamMoreQualified:
22168a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        Better1 = true;
22178a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        if (Better2)
22188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor          return 0;
22198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        break;
22208a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
22218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      case ArgMoreQualified:
22228a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        Better2 = true;
22238a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        if (Better1)
22248a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor          return 0;
22258a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        break;
22268a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    }
22278a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
22288a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
22298a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  assert(!(Better1 && Better2) && "Should have broken out in the loop above");
223065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (Better1)
223165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    return FT1;
22328a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  else if (Better2)
22338a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return FT2;
22348a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  else
22358a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return 0;
223665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor}
223783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
2238d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \brief Determine if the two templates are equivalent.
2239d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregorstatic bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
2240d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  if (T1 == T2)
2241d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    return true;
2242d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2243d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  if (!T1 || !T2)
2244d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    return false;
2245d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2246d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  return T1->getCanonicalDecl() == T2->getCanonicalDecl();
2247d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor}
2248d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2249d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \brief Retrieve the most specialized of the given function template
2250d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specializations.
2251d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2252c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// \param SpecBegin the start iterator of the function template
2253c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// specializations that we will be comparing.
2254d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2255c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// \param SpecEnd the end iterator of the function template
2256c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// specializations, paired with \p SpecBegin.
2257d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2258d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param TPOC the partial ordering context to use to compare the function
2259d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// template specializations.
2260d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2261d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param Loc the location where the ambiguity or no-specializations
2262d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// diagnostic should occur.
2263d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2264d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param NoneDiag partial diagnostic used to diagnose cases where there are
2265d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// no matching candidates.
2266d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2267d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
2268d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// occurs.
2269d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2270d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param CandidateDiag partial diagnostic used for each function template
2271d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specialization that is a candidate in the ambiguous ordering. One parameter
2272d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// in this diagnostic should be unbound, which will correspond to the string
2273d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// describing the template arguments for the function template specialization.
2274d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2275d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param Index if non-NULL and the result of this function is non-nULL,
2276d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// receives the index corresponding to the resulting function template
2277d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specialization.
2278d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2279d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \returns the most specialized function template specialization, if
2280c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// found. Otherwise, returns SpecEnd.
2281d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2282d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
2283d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// template argument deduction.
2284c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallUnresolvedSetIterator
2285c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallSema::getMostSpecialized(UnresolvedSetIterator SpecBegin,
2286c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         UnresolvedSetIterator SpecEnd,
2287c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         TemplatePartialOrderingContext TPOC,
2288c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         SourceLocation Loc,
2289c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         const PartialDiagnostic &NoneDiag,
2290c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         const PartialDiagnostic &AmbigDiag,
2291c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         const PartialDiagnostic &CandidateDiag) {
2292c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  if (SpecBegin == SpecEnd) {
2293d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    Diag(Loc, NoneDiag);
2294c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    return SpecEnd;
2295d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
2296d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2297c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  if (SpecBegin + 1 == SpecEnd)
2298c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    return SpecBegin;
2299d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2300d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // Find the function template that is better than all of the templates it
2301d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // has been compared to.
2302c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  UnresolvedSetIterator Best = SpecBegin;
2303d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  FunctionTemplateDecl *BestTemplate
2304c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
2305d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  assert(BestTemplate && "Not a function template specialization?");
2306c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
2307c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    FunctionTemplateDecl *Challenger
2308c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall      = cast<FunctionDecl>(*I)->getPrimaryTemplate();
2309d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    assert(Challenger && "Not a function template specialization?");
2310c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
23115769d6195087229770d7ac90449443e026c47103John McCall                                                  Loc, TPOC),
2312d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor                       Challenger)) {
2313d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      Best = I;
2314d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      BestTemplate = Challenger;
2315d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    }
2316d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
2317d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2318d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // Make sure that the "best" function template is more specialized than all
2319d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // of the others.
2320d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  bool Ambiguous = false;
2321c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
2322c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    FunctionTemplateDecl *Challenger
2323c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall      = cast<FunctionDecl>(*I)->getPrimaryTemplate();
2324d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    if (I != Best &&
2325d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor        !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
23265769d6195087229770d7ac90449443e026c47103John McCall                                                   Loc, TPOC),
2327d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor                        BestTemplate)) {
2328d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      Ambiguous = true;
2329d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      break;
2330d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    }
2331d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
2332d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2333d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  if (!Ambiguous) {
2334d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    // We found an answer. Return it.
2335c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    return Best;
2336d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
2337d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2338d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // Diagnose the ambiguity.
2339d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  Diag(Loc, AmbigDiag);
2340d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2341d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // FIXME: Can we order the candidates in some sane way?
2342c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I)
2343c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    Diag((*I)->getLocation(), CandidateDiag)
2344d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      << getTemplateArgumentBindingsText(
2345c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall        cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
2346c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                    *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
2347d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2348c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  return SpecEnd;
2349d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor}
2350d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2351bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \brief Returns the more specialized class template partial specialization
2352bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// according to the rules of partial ordering of class template partial
2353bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// specializations (C++ [temp.class.order]).
2354bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor///
2355bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \param PS1 the first class template partial specialization
2356bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor///
2357bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \param PS2 the second class template partial specialization
2358bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor///
2359bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \returns the more specialized class template partial specialization. If
2360bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// neither partial specialization is more specialized, returns NULL.
2361bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas GregorClassTemplatePartialSpecializationDecl *
2362bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas GregorSema::getMoreSpecializedPartialSpecialization(
2363bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                  ClassTemplatePartialSpecializationDecl *PS1,
23645769d6195087229770d7ac90449443e026c47103John McCall                                  ClassTemplatePartialSpecializationDecl *PS2,
23655769d6195087229770d7ac90449443e026c47103John McCall                                              SourceLocation Loc) {
2366bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // C++ [temp.class.order]p1:
2367bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   For two class template partial specializations, the first is at least as
2368bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   specialized as the second if, given the following rewrite to two
2369bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   function templates, the first function template is at least as
2370bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   specialized as the second according to the ordering rules for function
2371bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   templates (14.6.6.2):
2372bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //     - the first function template has the same template parameters as the
2373bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       first partial specialization and has a single function parameter
2374bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       whose type is a class template specialization with the template
2375bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       arguments of the first partial specialization, and
2376bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //     - the second function template has the same template parameters as the
2377bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       second partial specialization and has a single function parameter
2378bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       whose type is a class template specialization with the template
2379bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       arguments of the second partial specialization.
2380bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //
238131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // Rather than synthesize function templates, we merely perform the
238231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // equivalent partial ordering by performing deduction directly on
238331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // the template arguments of the class template partial
238431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // specializations. This computation is slightly simpler than the
238531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // general problem of function template partial ordering, because
238631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // class template partial specializations are more constrained. We
238731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // know that every template parameter is deducible from the class
238831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // template partial specialization's template arguments, for
238931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // example.
239002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
23915769d6195087229770d7ac90449443e026c47103John McCall  Sema::TemplateDeductionInfo Info(Context, Loc);
239231f17ecbef57b5679c017c375db330546b7b5145John McCall
239331f17ecbef57b5679c017c375db330546b7b5145John McCall  QualType PT1 = PS1->getInjectedSpecializationType();
239431f17ecbef57b5679c017c375db330546b7b5145John McCall  QualType PT2 = PS2->getInjectedSpecializationType();
2395bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor
2396bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // Determine whether PS1 is at least as specialized as PS2
2397bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  Deduced.resize(PS2->getTemplateParameters()->size());
2398a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
2399bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                  PS2->getTemplateParameters(),
240031f17ecbef57b5679c017c375db330546b7b5145John McCall                                                               PT2,
240131f17ecbef57b5679c017c375db330546b7b5145John McCall                                                               PT1,
2402bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                               Info,
2403bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                               Deduced,
2404bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                               0);
2405516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor  if (Better1)
2406516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor    Better1 = !::FinishTemplateArgumentDeduction(*this, PS2,
2407516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor                                                 PS1->getTemplateArgs(),
2408516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor                                                 Deduced, Info);
2409516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor
2410bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // Determine whether PS2 is at least as specialized as PS1
2411db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Deduced.clear();
2412bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  Deduced.resize(PS1->getTemplateParameters()->size());
2413a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
2414bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                  PS1->getTemplateParameters(),
241531f17ecbef57b5679c017c375db330546b7b5145John McCall                                                               PT1,
241631f17ecbef57b5679c017c375db330546b7b5145John McCall                                                               PT2,
2417bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                               Info,
2418bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                               Deduced,
2419bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                               0);
2420516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor  if (Better2)
2421516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor    Better2 = !::FinishTemplateArgumentDeduction(*this, PS1,
2422516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor                                                 PS2->getTemplateArgs(),
2423516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor                                                 Deduced, Info);
2424bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor
2425bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  if (Better1 == Better2)
2426bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    return 0;
2427bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor
2428bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  return Better1? PS1 : PS2;
2429bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor}
2430bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor
24311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
2432e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
2433e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           const TemplateArgument &TemplateArg,
2434e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2435ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
2436e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used);
2437031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2438e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
2439031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// expression.
24401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
2441e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
2442e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           const Expr *E,
2443e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2444ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
2445e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
2446e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
2447e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // find other occurrences of template parameters.
2448f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor  const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
2449c781f9cd854f3d5d1c826f4a13382c6abca4cff7Douglas Gregor  if (!DRE)
2450031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    return;
2451031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
24521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const NonTypeTemplateParmDecl *NTTP
2453031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
2454031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  if (!NTTP)
2455031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    return;
2456031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2457ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (NTTP->getDepth() == Depth)
2458ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Used[NTTP->getIndex()] = true;
2459031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
2460031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2461e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
2462e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// nested name specifier.
2463e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregorstatic void
2464e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
2465e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           NestedNameSpecifier *NNS,
2466e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2467ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
2468e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
2469e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (!NNS)
2470e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    return;
2471e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
2472ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
2473ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             Used);
2474e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
2475ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             OnlyDeduced, Depth, Used);
2476e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor}
2477e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
2478e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
2479e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// template name.
2480e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregorstatic void
2481e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
2482e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           TemplateName Name,
2483e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2484ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
2485e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
2486e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2487e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    if (TemplateTemplateParmDecl *TTP
2488ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor          = dyn_cast<TemplateTemplateParmDecl>(Template)) {
2489ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      if (TTP->getDepth() == Depth)
2490ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        Used[TTP->getIndex()] = true;
2491ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    }
2492e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    return;
2493e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  }
2494e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
2495788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
2496788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
2497788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                               Depth, Used);
2498e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
2499ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
2500ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2501e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor}
2502e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
2503e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
2504031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// type.
25051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
2506e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2507e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2508ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
2509e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
2510e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (T.isNull())
2511e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    return;
2512e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
2513031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  // Non-dependent types have nothing deducible
2514031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  if (!T->isDependentType())
2515031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    return;
2516031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2517031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  T = SemaRef.Context.getCanonicalType(T);
2518031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  switch (T->getTypeClass()) {
2519031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Pointer:
2520e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
2521e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<PointerType>(T)->getPointeeType(),
2522e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               OnlyDeduced,
2523ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth,
2524e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               Used);
2525031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2526031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2527031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::BlockPointer:
2528e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
2529e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<BlockPointerType>(T)->getPointeeType(),
2530e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               OnlyDeduced,
2531ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth,
2532e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               Used);
2533031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2534031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2535031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::LValueReference:
2536031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::RValueReference:
2537e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
2538e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<ReferenceType>(T)->getPointeeType(),
2539e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               OnlyDeduced,
2540ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth,
2541e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               Used);
2542031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2543031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2544031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::MemberPointer: {
2545031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
2546e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
2547ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2548e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
2549ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
2550031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2551031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
2552031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2553031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::DependentSizedArray:
2554e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
2555e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<DependentSizedArrayType>(T)->getSizeExpr(),
2556ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
2557031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    // Fall through to check the element type
2558031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2559031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::ConstantArray:
2560031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::IncompleteArray:
2561e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
2562e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<ArrayType>(T)->getElementType(),
2563ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
2564031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2565031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2566031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Vector:
2567031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::ExtVector:
2568e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
2569e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<VectorType>(T)->getElementType(),
2570ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
2571031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2572031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
25739cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  case Type::DependentSizedExtVector: {
25749cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    const DependentSizedExtVectorType *VecType
2575f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor      = cast<DependentSizedExtVectorType>(T);
2576e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
2577ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2578e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
2579ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
25809cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    break;
25819cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
25829cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
2583031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::FunctionProto: {
2584f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor    const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
2585e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
2586ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2587031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
2588e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor      MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
2589ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 Depth, Used);
2590031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2591031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
2592031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2593ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  case Type::TemplateTypeParm: {
2594ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
2595ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (TTP->getDepth() == Depth)
2596ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      Used[TTP->getIndex()] = true;
2597031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2598ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
2599031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
260031f17ecbef57b5679c017c375db330546b7b5145John McCall  case Type::InjectedClassName:
260131f17ecbef57b5679c017c375db330546b7b5145John McCall    T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
260231f17ecbef57b5679c017c375db330546b7b5145John McCall    // fall through
260331f17ecbef57b5679c017c375db330546b7b5145John McCall
2604031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::TemplateSpecialization: {
26051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const TemplateSpecializationType *Spec
2606f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor      = cast<TemplateSpecializationType>(T);
2607e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
2608ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2609e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
2610ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2611ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 Used);
2612e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    break;
2613e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  }
26141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2615e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  case Type::Complex:
2616e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    if (!OnlyDeduced)
2617e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor      MarkUsedTemplateParameters(SemaRef,
2618e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                 cast<ComplexType>(T)->getElementType(),
2619ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 OnlyDeduced, Depth, Used);
2620e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    break;
2621031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
26224714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  case Type::DependentName:
2623e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    if (!OnlyDeduced)
2624e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor      MarkUsedTemplateParameters(SemaRef,
26254714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor                                 cast<DependentNameType>(T)->getQualifier(),
2626ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 OnlyDeduced, Depth, Used);
2627031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2628031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
262933500955d731c73717af52088b7fc0e7a85681e7John McCall  case Type::DependentTemplateSpecialization: {
263033500955d731c73717af52088b7fc0e7a85681e7John McCall    const DependentTemplateSpecializationType *Spec
263133500955d731c73717af52088b7fc0e7a85681e7John McCall      = cast<DependentTemplateSpecializationType>(T);
263233500955d731c73717af52088b7fc0e7a85681e7John McCall    if (!OnlyDeduced)
263333500955d731c73717af52088b7fc0e7a85681e7John McCall      MarkUsedTemplateParameters(SemaRef, Spec->getQualifier(),
263433500955d731c73717af52088b7fc0e7a85681e7John McCall                                 OnlyDeduced, Depth, Used);
263533500955d731c73717af52088b7fc0e7a85681e7John McCall    for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
263633500955d731c73717af52088b7fc0e7a85681e7John McCall      MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
263733500955d731c73717af52088b7fc0e7a85681e7John McCall                                 Used);
263833500955d731c73717af52088b7fc0e7a85681e7John McCall    break;
263933500955d731c73717af52088b7fc0e7a85681e7John McCall  }
264033500955d731c73717af52088b7fc0e7a85681e7John McCall
2641ad5e73887052193afda72db8efcb812bd083a4a8John McCall  case Type::TypeOf:
2642ad5e73887052193afda72db8efcb812bd083a4a8John McCall    if (!OnlyDeduced)
2643ad5e73887052193afda72db8efcb812bd083a4a8John McCall      MarkUsedTemplateParameters(SemaRef,
2644ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 cast<TypeOfType>(T)->getUnderlyingType(),
2645ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 OnlyDeduced, Depth, Used);
2646ad5e73887052193afda72db8efcb812bd083a4a8John McCall    break;
2647ad5e73887052193afda72db8efcb812bd083a4a8John McCall
2648ad5e73887052193afda72db8efcb812bd083a4a8John McCall  case Type::TypeOfExpr:
2649ad5e73887052193afda72db8efcb812bd083a4a8John McCall    if (!OnlyDeduced)
2650ad5e73887052193afda72db8efcb812bd083a4a8John McCall      MarkUsedTemplateParameters(SemaRef,
2651ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 cast<TypeOfExprType>(T)->getUnderlyingExpr(),
2652ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 OnlyDeduced, Depth, Used);
2653ad5e73887052193afda72db8efcb812bd083a4a8John McCall    break;
2654ad5e73887052193afda72db8efcb812bd083a4a8John McCall
2655ad5e73887052193afda72db8efcb812bd083a4a8John McCall  case Type::Decltype:
2656ad5e73887052193afda72db8efcb812bd083a4a8John McCall    if (!OnlyDeduced)
2657ad5e73887052193afda72db8efcb812bd083a4a8John McCall      MarkUsedTemplateParameters(SemaRef,
2658ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 cast<DecltypeType>(T)->getUnderlyingExpr(),
2659ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 OnlyDeduced, Depth, Used);
2660ad5e73887052193afda72db8efcb812bd083a4a8John McCall    break;
2661ad5e73887052193afda72db8efcb812bd083a4a8John McCall
2662e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // None of these types have any template parameters in them.
2663031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Builtin:
2664031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::VariableArray:
2665031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::FunctionNoProto:
2666031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Record:
2667031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Enum:
2668031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::ObjCInterface:
2669c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall  case Type::ObjCObject:
2670d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  case Type::ObjCObjectPointer:
2671ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  case Type::UnresolvedUsing:
2672031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define TYPE(Class, Base)
2673031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
2674031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define DEPENDENT_TYPE(Class, Base)
2675031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2676031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#include "clang/AST/TypeNodes.def"
2677031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2678031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
2679031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
2680031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2681e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by this
2682031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// template argument.
26831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
2684e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
2685e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           const TemplateArgument &TemplateArg,
2686e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2687ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
2688e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
2689031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  switch (TemplateArg.getKind()) {
2690031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Null:
2691031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Integral:
2692788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Declaration:
2693031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
26941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2695031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Type:
2696e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
2697ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2698031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2699031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2700788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  case TemplateArgument::Template:
2701788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsTemplate(),
2702788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                               OnlyDeduced, Depth, Used);
2703031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2704031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2705031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Expression:
2706e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
2707ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2708031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2709e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
2710d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson  case TemplateArgument::Pack:
2711e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
2712e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                      PEnd = TemplateArg.pack_end();
2713e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor         P != PEnd; ++P)
2714ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
2715d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson    break;
2716031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
2717031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
2718031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2719031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \brief Mark the template parameters can be deduced by the given
2720031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// template argument list.
2721031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor///
2722031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \param TemplateArgs the template argument list from which template
2723031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// parameters will be deduced.
2724031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor///
2725031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \param Deduced a bit vector whose elements will be set to \c true
2726031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// to indicate when the corresponding template parameter will be
2727031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// deduced.
27281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
2729e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorSema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
2730ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 bool OnlyDeduced, unsigned Depth,
2731e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                 llvm::SmallVectorImpl<bool> &Used) {
2732031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
2733ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
2734ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 Depth, Used);
2735031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
273663f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor
273763f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor/// \brief Marks all of the template parameters that will be deduced by a
273863f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor/// call to the given function template.
273902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregorvoid
274002024a9f0d8e6c898de276193af604c42ee41269Douglas GregorSema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
274102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                    llvm::SmallVectorImpl<bool> &Deduced) {
274263f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  TemplateParameterList *TemplateParams
274363f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor    = FunctionTemplate->getTemplateParameters();
274463f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  Deduced.clear();
274563f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  Deduced.resize(TemplateParams->size());
274663f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor
274763f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
274863f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
274963f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor    ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
2750ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 true, TemplateParams->getDepth(), Deduced);
275163f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor}
2752