SemaTemplateDeduction.cpp revision 516e6e09821a61e8975c787e189949723249e7c5
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));
431f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.FirstArg = Deduced[Index];
432833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      Info.SecondArg = TemplateArgument(Arg);
433f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_InconsistentQuals;
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: {
4856217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      const PointerType *PointerArg = Arg->getAs<PointerType>();
486d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor      if (!PointerArg)
487f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
4881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4894112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor      unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
490a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
491d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                                   cast<PointerType>(Param)->getPointeeType(),
492d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                                     PointerArg->getPointeeType(),
4934112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor                                     Info, Deduced, SubTDF);
494d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    }
4951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
496199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T &
497d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    case Type::LValueReference: {
4986217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
499d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor      if (!ReferenceArg)
500f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
5011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
502a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
503d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                           cast<LValueReferenceType>(Param)->getPointeeType(),
504d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                                     ReferenceArg->getPointeeType(),
505508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
506d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    }
5070b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
508199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T && [C++0x]
509d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    case Type::RValueReference: {
5106217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
511d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor      if (!ReferenceArg)
512f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
5131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
514a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
515d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                           cast<RValueReferenceType>(Param)->getPointeeType(),
516d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                                     ReferenceArg->getPointeeType(),
517508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
518d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    }
5191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
520199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T [] (implied, but not stated explicitly)
5214d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson    case Type::IncompleteArray: {
5221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const IncompleteArrayType *IncompleteArrayArg =
523a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        S.Context.getAsIncompleteArrayType(Arg);
5244d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson      if (!IncompleteArrayArg)
525f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
5261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
527a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
528a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth                     S.Context.getAsIncompleteArrayType(Param)->getElementType(),
5294d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson                                     IncompleteArrayArg->getElementType(),
530508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
5314d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson    }
532199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
533199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T [integer-constant]
5344d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson    case Type::ConstantArray: {
5351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const ConstantArrayType *ConstantArrayArg =
536a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        S.Context.getAsConstantArrayType(Arg);
5374d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson      if (!ConstantArrayArg)
538f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
5391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const ConstantArrayType *ConstantArrayParm =
541a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        S.Context.getAsConstantArrayType(Param);
5424d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson      if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
543f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
5441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
545a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
5464d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson                                     ConstantArrayParm->getElementType(),
5474d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson                                     ConstantArrayArg->getElementType(),
548508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
5494d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson    }
5504d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson
551199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     type [i]
552199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    case Type::DependentSizedArray: {
553a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg);
554199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (!ArrayArg)
555f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
5561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
557199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // Check the element type of the arrays
558199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      const DependentSizedArrayType *DependentArrayParm
559a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = S.Context.getAsDependentSizedArrayType(Param);
560f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (Sema::TemplateDeductionResult Result
561a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            = DeduceTemplateArguments(S, TemplateParams,
562f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      DependentArrayParm->getElementType(),
563f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      ArrayArg->getElementType(),
564508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                      Info, Deduced, 0))
565f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Result;
5661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
567199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // Determine the array bound is something we can deduce.
5681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NonTypeTemplateParmDecl *NTTP
569199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor        = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
570199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (!NTTP)
571f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_Success;
5721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We can perform template argument deduction for the given non-type
574199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // template parameter.
5751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(NTTP->getDepth() == 0 &&
576199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor             "Cannot deduce non-type template argument at depth > 0");
5771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (const ConstantArrayType *ConstantArrayArg
578335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson            = dyn_cast<ConstantArrayType>(ArrayArg)) {
579335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson        llvm::APSInt Size(ConstantArrayArg->getSize());
5809d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor        return DeduceNonTypeTemplateArgument(S, NTTP, Size,
5819d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor                                             S.Context.getSizeType(),
58202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                             /*ArrayBound=*/true,
583f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                             Info, Deduced);
584335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson      }
585199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (const DependentSizedArrayType *DependentArrayArg
586199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor            = dyn_cast<DependentSizedArrayType>(ArrayArg))
587a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP,
588199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor                                             DependentArrayArg->getSizeExpr(),
589f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                             Info, Deduced);
5901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
591199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // Incomplete type does not match a dependently-sized array type
592f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
593199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    }
5941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     type(*)(T)
5961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(*)()
5971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(*)(T)
598a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson    case Type::FunctionProto: {
5991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const FunctionProtoType *FunctionProtoArg =
600a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson        dyn_cast<FunctionProtoType>(Arg);
601a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson      if (!FunctionProtoArg)
602f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
6031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const FunctionProtoType *FunctionProtoParam =
605a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson        cast<FunctionProtoType>(Param);
606994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson
6071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (FunctionProtoParam->getTypeQuals() !=
608994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson          FunctionProtoArg->getTypeQuals())
609f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
6101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
611994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson      if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
612f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
6131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
614994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson      if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
615f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
616994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson
617a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson      // Check return types.
618f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (Sema::TemplateDeductionResult Result
619a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            = DeduceTemplateArguments(S, TemplateParams,
620f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      FunctionProtoParam->getResultType(),
621f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      FunctionProtoArg->getResultType(),
622508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                      Info, Deduced, 0))
623f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Result;
6241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
625a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson      for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
626a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson        // Check argument types.
627f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        if (Sema::TemplateDeductionResult Result
628a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth              = DeduceTemplateArguments(S, TemplateParams,
629f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                        FunctionProtoParam->getArgType(I),
630f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                        FunctionProtoArg->getArgType(I),
631508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                        Info, Deduced, 0))
632f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor          return Result;
633a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson      }
6341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
635f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_Success;
636a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson    }
6371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6383cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    case Type::InjectedClassName: {
6393cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // Treat a template's injected-class-name as if the template
6403cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // specialization type had been used.
64131f17ecbef57b5679c017c375db330546b7b5145John McCall      Param = cast<InjectedClassNameType>(Param)
64231f17ecbef57b5679c017c375db330546b7b5145John McCall        ->getInjectedSpecializationType();
6433cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      assert(isa<TemplateSpecializationType>(Param) &&
6443cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall             "injected class name is not a template specialization type");
6453cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // fall through
6463cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    }
6473cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
648f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    //     template-name<T> (where template-name refers to a class template)
649d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor    //     template-name<i>
650db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    //     TT<T>
651db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    //     TT<i>
652db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    //     TT<>
653d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor    case Type::TemplateSpecialization: {
654d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor      const TemplateSpecializationType *SpecParam
655d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor        = cast<TemplateSpecializationType>(Param);
6561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
657de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      // Try to deduce template arguments from the template-id.
658de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      Sema::TemplateDeductionResult Result
659a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg,
660de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                  Info, Deduced);
6611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6624a5c15f75f76b95e1c2ceb6fa2737dcadd5f4be1Douglas Gregor      if (Result && (TDF & TDF_DerivedClass)) {
663de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        // C++ [temp.deduct.call]p3b3:
664de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   If P is a class, and P has the form template-id, then A can be a
665de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   derived class of the deduced A. Likewise, if P is a pointer to a
6661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        //   class of the form template-id, A can be a pointer to a derived
667de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   class pointed to by the deduced A.
668de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //
669de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        // More importantly:
6701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        //   These alternatives are considered only if type deduction would
671de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   otherwise fail.
672a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        if (const RecordType *RecordT = Arg->getAs<RecordType>()) {
673a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          // We cannot inspect base classes as part of deduction when the type
674a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          // is incomplete, so either instantiate any templates necessary to
675a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          // complete the type, or skip over it if it cannot be completed.
6765769d6195087229770d7ac90449443e026c47103John McCall          if (S.RequireCompleteType(Info.getLocation(), Arg, 0))
677a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            return Result;
678a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth
679de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          // Use data recursion to crawl through the list of base classes.
6801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          // Visited contains the set of nodes we have already visited, while
681de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          // ToVisit is our stack of records that we still need to visit.
682de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          llvm::SmallPtrSet<const RecordType *, 8> Visited;
683de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          llvm::SmallVector<const RecordType *, 8> ToVisit;
684de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          ToVisit.push_back(RecordT);
685de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          bool Successful = false;
686de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          while (!ToVisit.empty()) {
687de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // Retrieve the next class in the inheritance hierarchy.
688de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            const RecordType *NextT = ToVisit.back();
689de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            ToVisit.pop_back();
6901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
691de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // If we have already seen this type, skip it.
692de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            if (!Visited.insert(NextT))
693de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              continue;
6941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
695de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // If this is a base class, try to perform template argument
696de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // deduction from it.
697de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            if (NextT != RecordT) {
698de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              Sema::TemplateDeductionResult BaseResult
699a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth                = DeduceTemplateArguments(S, TemplateParams, SpecParam,
700de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                          QualType(NextT, 0), Info, Deduced);
7011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
702de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              // If template argument deduction for this base was successful,
703de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              // note that we had some success.
704de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              if (BaseResult == Sema::TDK_Success)
705de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                Successful = true;
706de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            }
7071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
708de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // Visit base classes
709de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
710de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
711de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                                 BaseEnd = Next->bases_end();
7129994a34f6cf842721ba7723edc0b9036229fe387Sebastian Redl                 Base != BaseEnd; ++Base) {
7131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump              assert(Base->getType()->isRecordType() &&
714de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                     "Base class that isn't a record?");
7156217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek              ToVisit.push_back(Base->getType()->getAs<RecordType>());
716de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            }
717de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          }
7181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
719de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          if (Successful)
720de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            return Sema::TDK_Success;
721de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        }
7221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
723de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      }
7241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
725de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      return Result;
726d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor    }
727d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor
728637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T type::*
729637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T T::*
730637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (type::*)()
731637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     type (T::*)()
732637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     type (type::*)(T)
733637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     type (T::*)(T)
734637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (type::*)(T)
735637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (T::*)()
736637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (T::*)(T)
737637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    case Type::MemberPointer: {
738637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
739637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
740637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      if (!MemPtrArg)
741f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
742f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
743f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (Sema::TemplateDeductionResult Result
744a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            = DeduceTemplateArguments(S, TemplateParams,
745f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      MemPtrParam->getPointeeType(),
746f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      MemPtrArg->getPointeeType(),
747508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                      Info, Deduced,
748508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                      TDF & TDF_IgnoreQualifiers))
749f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Result;
750f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
751a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
752f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                     QualType(MemPtrParam->getClass(), 0),
753f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                     QualType(MemPtrArg->getClass(), 0),
754508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
755637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    }
756637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor
7579a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    //     (clang extension)
7589a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    //
7591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     type(^)(T)
7601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(^)()
7611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(^)(T)
762859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson    case Type::BlockPointer: {
763859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson      const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
764859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson      const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
7651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
766859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson      if (!BlockPtrArg)
767f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
7681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
769a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
770859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson                                     BlockPtrParam->getPointeeType(),
771f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                     BlockPtrArg->getPointeeType(), Info,
772508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Deduced, 0);
773859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson    }
774859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson
775637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    case Type::TypeOfExpr:
776637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    case Type::TypeOf:
7774714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor    case Type::DependentName:
778637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      // No template argument deduction for these types
779f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_Success;
780637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor
781d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    default:
782d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor      break;
7830b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
7840b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
7850b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  // FIXME: Many more cases to go (to go).
786f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  return Sema::TDK_Success;
7870b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
7880b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
789f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
790a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
791f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateParameterList *TemplateParams,
792f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        const TemplateArgument &Param,
7930b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor                        const TemplateArgument &Arg,
794f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        Sema::TemplateDeductionInfo &Info,
79502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
7960b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  switch (Param.getKind()) {
797199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Null:
798199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    assert(false && "Null template argument in parameter list");
799199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    break;
8001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case TemplateArgument::Type:
802788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    if (Arg.getKind() == TemplateArgument::Type)
803a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams, Param.getAsType(),
804788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                                     Arg.getAsType(), Info, Deduced, 0);
805788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.FirstArg = Param;
806788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.SecondArg = Arg;
807788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
808788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor
809788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  case TemplateArgument::Template:
810db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    if (Arg.getKind() == TemplateArgument::Template)
811a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
812788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                                     Param.getAsTemplate(),
813db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor                                     Arg.getAsTemplate(), Info, Deduced);
814788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.FirstArg = Param;
815788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.SecondArg = Arg;
816788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
817788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor
818199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Declaration:
819788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    if (Arg.getKind() == TemplateArgument::Declaration &&
820788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor        Param.getAsDecl()->getCanonicalDecl() ==
821788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor          Arg.getAsDecl()->getCanonicalDecl())
822788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return Sema::TDK_Success;
823788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor
824f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.FirstArg = Param;
825f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.SecondArg = Arg;
826f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_NonDeducedMismatch;
8271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
828199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Integral:
829199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    if (Arg.getKind() == TemplateArgument::Integral) {
8309d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor      if (hasSameExtendedValue(*Param.getAsIntegral(), *Arg.getAsIntegral()))
831f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_Success;
832f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
833f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.FirstArg = Param;
834f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.SecondArg = Arg;
835f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
836f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    }
837f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
838f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    if (Arg.getKind() == TemplateArgument::Expression) {
839f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.FirstArg = Param;
840f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.SecondArg = Arg;
841f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
842199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    }
843199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
844199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    assert(false && "Type/value mismatch");
845f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.FirstArg = Param;
846f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.SecondArg = Arg;
847f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_NonDeducedMismatch;
8481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
849199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Expression: {
8501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (NonTypeTemplateParmDecl *NTTP
851199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor          = getDeducedParameterFromExpr(Param.getAsExpr())) {
852199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (Arg.getKind() == TemplateArgument::Integral)
853a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP,
8541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             *Arg.getAsIntegral(),
8559d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor                                             Arg.getIntegralType(),
85602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                             /*ArrayBound=*/false,
857f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                             Info, Deduced);
858199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (Arg.getKind() == TemplateArgument::Expression)
859a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(),
860f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                             Info, Deduced);
86115755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor      if (Arg.getKind() == TemplateArgument::Declaration)
862a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(),
86315755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor                                             Info, Deduced);
86415755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor
865199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      assert(false && "Type/value mismatch");
866f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.FirstArg = Param;
867f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.SecondArg = Arg;
868f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
869199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    }
8701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
871199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    // Can't deduce anything, but that's okay.
872f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_Success;
873199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  }
874d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson  case TemplateArgument::Pack:
875d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson    assert(0 && "FIXME: Implement!");
876d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson    break;
8770b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
8781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
879f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  return Sema::TDK_Success;
8800b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
8810b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
8821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic Sema::TemplateDeductionResult
883a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
884f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateParameterList *TemplateParams,
8850b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor                        const TemplateArgumentList &ParamList,
8860b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor                        const TemplateArgumentList &ArgList,
887f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        Sema::TemplateDeductionInfo &Info,
88802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
8890b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  assert(ParamList.size() == ArgList.size());
8900b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
891f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    if (Sema::TemplateDeductionResult Result
892a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          = DeduceTemplateArguments(S, TemplateParams,
8931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                    ParamList[I], ArgList[I],
894f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                    Info, Deduced))
895f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Result;
8960b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
897f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  return Sema::TDK_Success;
8980b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
8990b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
900f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// \brief Determine whether two template arguments are the same.
9011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool isSameTemplateArg(ASTContext &Context,
902f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor                              const TemplateArgument &X,
903f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor                              const TemplateArgument &Y) {
904f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  if (X.getKind() != Y.getKind())
905f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    return false;
9061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
907f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  switch (X.getKind()) {
908f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Null:
909f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      assert(false && "Comparing NULL template argument");
910f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      break;
9111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
912f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Type:
913f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      return Context.getCanonicalType(X.getAsType()) ==
914f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor             Context.getCanonicalType(Y.getAsType());
9151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
916f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Declaration:
91797fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis      return X.getAsDecl()->getCanonicalDecl() ==
91897fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis             Y.getAsDecl()->getCanonicalDecl();
9191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
920788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Template:
921788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return Context.getCanonicalTemplateName(X.getAsTemplate())
922788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor               .getAsVoidPointer() ==
923788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor             Context.getCanonicalTemplateName(Y.getAsTemplate())
924788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor               .getAsVoidPointer();
925788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor
926f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Integral:
927f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      return *X.getAsIntegral() == *Y.getAsIntegral();
9281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
929788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Expression: {
930788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      llvm::FoldingSetNodeID XID, YID;
931788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      X.getAsExpr()->Profile(XID, Context, true);
932788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      Y.getAsExpr()->Profile(YID, Context, true);
933788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return XID == YID;
934788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    }
9351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
936f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Pack:
937f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      if (X.pack_size() != Y.pack_size())
938f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor        return false;
9391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (TemplateArgument::pack_iterator XP = X.pack_begin(),
9411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        XPEnd = X.pack_end(),
942f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor                                           YP = Y.pack_begin();
9431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump           XP != XPEnd; ++XP, ++YP)
944f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor        if (!isSameTemplateArg(Context, *XP, *YP))
945f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor          return false;
946f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
947f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      return true;
948f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  }
949f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
950f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  return false;
951f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor}
952f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
953f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// \brief Helper function to build a TemplateParameter when we don't
954f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// know its type statically.
955f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregorstatic TemplateParameter makeTemplateParameter(Decl *D) {
956f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
957f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    return TemplateParameter(TTP);
958f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
959f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    return TemplateParameter(NTTP);
9601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
961f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
962f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor}
963f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
96431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// Complete template argument deduction for a class template partial
96531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// specialization.
96631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregorstatic Sema::TemplateDeductionResult
96731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas GregorFinishTemplateArgumentDeduction(Sema &S,
96831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                ClassTemplatePartialSpecializationDecl *Partial,
96931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                const TemplateArgumentList &TemplateArgs,
97031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                      llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
97131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                Sema::TemplateDeductionInfo &Info) {
97231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // Trap errors.
97331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  Sema::SFINAETrap Trap(S);
97431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor
97531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  Sema::ContextRAII SavedContext(S, Partial);
976f5813826802c2e76cdc13cae834ebfd4518d74a6John McCall
97702cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // C++ [temp.deduct.type]p2:
97802cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  //   [...] or if any template argument remains neither deduced nor
97902cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  //   explicitly specified, template argument deduction fails.
980fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson  TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
981fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson                                      Deduced.size());
98202cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
983f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    if (Deduced[I].isNull()) {
9841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Decl *Param
98531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor      = const_cast<NamedDecl *>(
986bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                Partial->getTemplateParameters()->getParam(I));
9872b0749a4f8965d0205bf77322db150c13c39e3beDouglas Gregor      Info.Param = makeTemplateParameter(Param);
98831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor      return Sema::TDK_Incomplete;
989f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    }
99031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor
991fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson    Builder.Append(Deduced[I]);
99202cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  }
99331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor
99402cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // Form the template argument list from the deduced template arguments.
9951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentList *DeducedArgumentList
99631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    = new (S.Context) TemplateArgumentList(S.Context, Builder,
99731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                           /*TakeArgs=*/true);
998f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  Info.reset(DeducedArgumentList);
99902cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor
100002cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // Substitute the deduced template arguments into the template
100102cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // arguments of the class template partial specialization, and
100202cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // verify that the instantiated template arguments are both valid
100302cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // and are equivalent to the template arguments originally provided
10041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to the class template.
100502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  // FIXME: Do we have to correct the types of deduced non-type template
100602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  // arguments (in particular, integral non-type template arguments?).
100731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  Sema::LocalInstantiationScope InstScope(S);
100802cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
1009833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  const TemplateArgumentLoc *PartialTemplateArgs
1010833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    = Partial->getTemplateArgsAsWritten();
1011833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  unsigned N = Partial->getNumTemplateArgsAsWritten();
1012d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
1013d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  // Note that we don't provide the langle and rangle locations.
1014d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstArgs;
1015d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
1016833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned I = 0; I != N; ++I) {
1017bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    Decl *Param = const_cast<NamedDecl *>(
1018c9e5d25ea33616c896a2ce5fbd6d68186eaddc5cDouglas Gregor                    ClassTemplate->getTemplateParameters()->getParam(I));
1019d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    TemplateArgumentLoc InstArg;
102031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    if (S.Subst(PartialTemplateArgs[I], InstArg,
102131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
1022f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Info.Param = makeTemplateParameter(Param);
1023833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      Info.FirstArg = PartialTemplateArgs[I].getArgument();
102431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor      return Sema::TDK_SubstitutionFailure;
1025f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    }
1026d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    InstArgs.addArgument(InstArg);
1027833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  }
1028833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1029833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  TemplateArgumentListBuilder ConvertedInstArgs(
1030833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall                                  ClassTemplate->getTemplateParameters(), N);
1031833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
103231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
1033d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                InstArgs, false, ConvertedInstArgs)) {
1034833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    // FIXME: fail with more useful information?
103531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return Sema::TDK_SubstitutionFailure;
1036833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  }
1037833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1038833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned I = 0, E = ConvertedInstArgs.flatSize(); I != E; ++I) {
1039d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    TemplateArgument InstArg = ConvertedInstArgs.getFlatArguments()[I];
1040833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1041833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    Decl *Param = const_cast<NamedDecl *>(
1042833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall                    ClassTemplate->getTemplateParameters()->getParam(I));
10431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1044f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    if (InstArg.getKind() == TemplateArgument::Expression) {
10451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // When the argument is an expression, check the expression result
1046f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      // against the actual template parameter to get down to the canonical
1047f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      // template argument.
1048f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Expr *InstExpr = InstArg.getAsExpr();
10491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (NonTypeTemplateParmDecl *NTTP
1050f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor            = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
105131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor        if (S.CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
1052f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor          Info.Param = makeTemplateParameter(Param);
1053833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall          Info.FirstArg = Partial->getTemplateArgs()[I];
105431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor          return Sema::TDK_SubstitutionFailure;
1055f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor        }
1056f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      }
105702cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor    }
10581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
105931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) {
1060f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Info.Param = makeTemplateParameter(Param);
1061f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Info.FirstArg = TemplateArgs[I];
1062f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Info.SecondArg = InstArg;
106331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor      return Sema::TDK_NonDeducedMismatch;
1064f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    }
106502cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  }
106602cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor
1067bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor  if (Trap.hasErrorOccurred())
106831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return Sema::TDK_SubstitutionFailure;
1069bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor
107031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  return Sema::TDK_Success;
107131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor}
107231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor
107331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// \brief Perform template argument deduction to determine whether
107431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// the given template arguments match the given class template
107531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// partial specialization per C++ [temp.class.spec.match].
107631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas GregorSema::TemplateDeductionResult
107731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas GregorSema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
107831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                              const TemplateArgumentList &TemplateArgs,
107931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                              TemplateDeductionInfo &Info) {
108031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // C++ [temp.class.spec.match]p2:
108131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  //   A partial specialization matches a given actual template
108231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  //   argument list if the template arguments of the partial
108331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  //   specialization can be deduced from the actual template argument
108431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  //   list (14.8.2).
108531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  SFINAETrap Trap(*this);
108631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
108731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  Deduced.resize(Partial->getTemplateParameters()->size());
108831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  if (TemplateDeductionResult Result
108931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor        = ::DeduceTemplateArguments(*this,
109031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                    Partial->getTemplateParameters(),
109131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                    Partial->getTemplateArgs(),
109231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                    TemplateArgs, Info, Deduced))
109331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return Result;
109431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor
109531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
109631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                             Deduced.data(), Deduced.size());
109731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  if (Inst)
109831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return TDK_InstantiationDepth;
109931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor
110031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  if (Trap.hasErrorOccurred())
110131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor    return Sema::TDK_SubstitutionFailure;
110231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor
110331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs,
110431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor                                           Deduced, Info);
11050b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
1106031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
11074112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor/// \brief Determine whether the given type T is a simple-template-id type.
11084112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregorstatic bool isSimpleTemplateIdType(QualType T) {
11091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const TemplateSpecializationType *Spec
1110183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall        = T->getAs<TemplateSpecializationType>())
11114112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    return Spec->getTemplateName().getAsTemplateDecl() != 0;
11121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11134112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor  return false;
11144112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor}
111583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
111683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \brief Substitute the explicitly-provided template arguments into the
111783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// given function template according to C++ [temp.arg.explicit].
111883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
111983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionTemplate the function template into which the explicit
112083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template arguments will be substituted.
112183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
11221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ExplicitTemplateArguments the explicitly-specified template
112383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// arguments.
112483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
11251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param Deduced the deduced template arguments, which will be populated
112683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// with the converted and checked explicit template arguments.
112783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
11281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ParamTypes will be populated with the instantiated function
112983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// parameters.
113083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
113183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionType if non-NULL, the result type of the function template
113283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// will also be instantiated and the pointed-to value will be updated with
113383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// the instantiated function type.
113483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
113583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Info if substitution fails for any reason, this object will be
113683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// populated with more information about the failure.
113783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
113883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \returns TDK_Success if substitution was successful, or some failure
113983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// condition.
114083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::TemplateDeductionResult
114183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::SubstituteExplicitTemplateArguments(
114283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      FunctionTemplateDecl *FunctionTemplate,
1143d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                        const TemplateArgumentListInfo &ExplicitTemplateArgs,
114402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                       llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
114583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                 llvm::SmallVectorImpl<QualType> &ParamTypes,
114683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          QualType *FunctionType,
114783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          TemplateDeductionInfo &Info) {
114883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
114983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateParameterList *TemplateParams
115083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = FunctionTemplate->getTemplateParameters();
115183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
1152d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (ExplicitTemplateArgs.size() == 0) {
115383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    // No arguments to substitute; just copy over the parameter types and
115483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    // fill in the function type.
115583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    for (FunctionDecl::param_iterator P = Function->param_begin(),
115683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                   PEnd = Function->param_end();
115783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor         P != PEnd;
115883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor         ++P)
115983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      ParamTypes.push_back((*P)->getType());
11601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
116183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (FunctionType)
116283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      *FunctionType = Function->getType();
116383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_Success;
116483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
11651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
116683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Substitution of the explicit template arguments into a function template
116783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  /// is a SFINAE context. Trap any errors that might occur.
11681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SFINAETrap Trap(*this);
11691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
117083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // C++ [temp.arg.explicit]p3:
11711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   Template arguments that are present shall be specified in the
11721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   declaration order of their corresponding template-parameters. The
117383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   template argument list shall not specify more template-arguments than
11741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   there are corresponding template-parameters.
11751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentListBuilder Builder(TemplateParams,
1176d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                      ExplicitTemplateArgs.size());
11771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Enter a new template instantiation context where we check the
117983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // explicitly-specified template arguments against this function template,
118083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // and then substitute them into the function parameter types.
11811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
118283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                             FunctionTemplate, Deduced.data(), Deduced.size(),
118383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor           ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution);
118483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (Inst)
118583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_InstantiationDepth;
11861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
118796db310ab7ca59e1890ddef25a3701bc2909d20fJohn McCall  ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
1188f5813826802c2e76cdc13cae834ebfd4518d74a6John McCall
118983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (CheckTemplateArgumentList(FunctionTemplate,
119083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                SourceLocation(),
1191d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                ExplicitTemplateArgs,
119283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                true,
119383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                Builder) || Trap.hasErrorOccurred())
119483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_InvalidExplicitArguments;
11951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
119683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Form the template argument list from the explicitly-specified
119783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // template arguments.
11981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentList *ExplicitArgumentList
119983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
120083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Info.reset(ExplicitArgumentList);
12011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
120283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Instantiate the types of each of the function parameters given the
120383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // explicitly-specified template arguments.
120483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  for (FunctionDecl::param_iterator P = Function->param_begin(),
120583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                PEnd = Function->param_end();
120683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor       P != PEnd;
120783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor       ++P) {
12081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    QualType ParamType
12091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      = SubstType((*P)->getType(),
1210357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1211357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  (*P)->getLocation(), (*P)->getDeclName());
121283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (ParamType.isNull() || Trap.hasErrorOccurred())
121383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_SubstitutionFailure;
12141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
121583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    ParamTypes.push_back(ParamType);
121683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
121783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
121883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // If the caller wants a full function type back, instantiate the return
121983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // type and form that function type.
122083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (FunctionType) {
122183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    // FIXME: exception-specifications?
12221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const FunctionProtoType *Proto
1223183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall      = Function->getType()->getAs<FunctionProtoType>();
122483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    assert(Proto && "Function template does not have a prototype?");
12251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    QualType ResultType
1227357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor      = SubstType(Proto->getResultType(),
1228357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1229357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  Function->getTypeSpecStartLoc(),
1230357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  Function->getDeclName());
123183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (ResultType.isNull() || Trap.hasErrorOccurred())
123283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_SubstitutionFailure;
12331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    *FunctionType = BuildFunctionType(ResultType,
123583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      ParamTypes.data(), ParamTypes.size(),
123683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      Proto->isVariadic(),
123783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      Proto->getTypeQuals(),
123883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      Function->getLocation(),
123983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      Function->getDeclName());
124083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (FunctionType->isNull() || Trap.hasErrorOccurred())
124183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_SubstitutionFailure;
124283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
12431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
124483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // C++ [temp.arg.explicit]p2:
12451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   Trailing template arguments that can be deduced (14.8.2) may be
12461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   omitted from the list of explicit template-arguments. If all of the
124783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   template arguments can be deduced, they may all be omitted; in this
124883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   case, the empty template argument list <> itself may also be omitted.
124983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //
125083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Take all of the explicitly-specified arguments and put them into the
12511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // set of deduced template arguments.
125283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Deduced.reserve(TemplateParams->size());
125383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I)
12541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Deduced.push_back(ExplicitArgumentList->get(I));
12551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
125683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  return TDK_Success;
125783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor}
125883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
125902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// \brief Allocate a TemplateArgumentLoc where all locations have
126002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// been initialized to the given location.
126102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor///
126202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// \param S The semantic analysis object.
126302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor///
126402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// \param The template argument we are producing template argument
126502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// location information for.
126602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor///
126702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// \param NTTPType For a declaration template argument, the type of
126802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// the non-type template parameter that corresponds to this template
126902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// argument.
127002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor///
127102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// \param Loc The source location to use for the resulting template
127202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// argument.
127302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregorstatic TemplateArgumentLoc
127402024a9f0d8e6c898de276193af604c42ee41269Douglas GregorgetTrivialTemplateArgumentLoc(Sema &S,
127502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                              const TemplateArgument &Arg,
127602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                              QualType NTTPType,
127702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                              SourceLocation Loc) {
127802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  switch (Arg.getKind()) {
127902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  case TemplateArgument::Null:
128002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    llvm_unreachable("Can't get a NULL template argument here");
128102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    break;
128202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
128302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  case TemplateArgument::Type:
128402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    return TemplateArgumentLoc(Arg,
128502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                    S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc));
128602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
128702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  case TemplateArgument::Declaration: {
128802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    Expr *E
128902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc)
129002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                                              .takeAs<Expr>();
129102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    return TemplateArgumentLoc(TemplateArgument(E), E);
129202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  }
129302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
129402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  case TemplateArgument::Integral: {
129502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    Expr *E
129602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).takeAs<Expr>();
129702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    return TemplateArgumentLoc(TemplateArgument(E), E);
129802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  }
129902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
130002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  case TemplateArgument::Template:
130102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    return TemplateArgumentLoc(Arg, SourceRange(), Loc);
130202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
130302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  case TemplateArgument::Expression:
130402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    return TemplateArgumentLoc(Arg, Arg.getAsExpr());
130502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
130602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  case TemplateArgument::Pack:
130702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    llvm_unreachable("Template parameter packs are not yet supported");
130802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  }
130902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
131002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  return TemplateArgumentLoc();
131102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor}
131202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
13131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Finish template argument deduction for a function template,
131483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// checking the deduced template arguments for completeness and forming
131583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// the function template specialization.
13161eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSema::TemplateDeductionResult
131783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
131802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                       llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
131902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                      unsigned NumExplicitlySpecified,
132083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      FunctionDecl *&Specialization,
132183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      TemplateDeductionInfo &Info) {
132283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateParameterList *TemplateParams
132383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = FunctionTemplate->getTemplateParameters();
13241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
132551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // Template argument deduction for function templates in a SFINAE context.
132651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // Trap any errors that might occur.
132751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  SFINAETrap Trap(*this);
132851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
132951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // Enter a new template instantiation context while we instantiate the
133051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // actual function declaration.
133151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
133251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                             FunctionTemplate, Deduced.data(), Deduced.size(),
133351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor              ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution);
133451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  if (Inst)
133551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    return TDK_InstantiationDepth;
133651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
133796db310ab7ca59e1890ddef25a3701bc2909d20fJohn McCall  ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl());
1338f5813826802c2e76cdc13cae834ebfd4518d74a6John McCall
133983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // C++ [temp.deduct.type]p2:
134083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   [...] or if any template argument remains neither deduced nor
134183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   explicitly specified, template argument deduction fails.
134283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
134383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
134402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    NamedDecl *Param = FunctionTemplate->getTemplateParameters()->getParam(I);
134551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    if (!Deduced[I].isNull()) {
134602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      if (I < NumExplicitlySpecified ||
134702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor          Deduced[I].getKind() == TemplateArgument::Type) {
134802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        // We have already fully type-checked and converted this
134902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        // argument (because it was explicitly-specified) or no
135002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        // additional checking is necessary (because it's a template
135102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        // type parameter). Just record the presence of this
135202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        // parameter.
135302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        Builder.Append(Deduced[I]);
135402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        continue;
135502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      }
135602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
135702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // We have deduced this argument, so it still needs to be
135802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // checked and converted.
135902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
136002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // First, for a non-type template parameter type that is
136102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // initialized by a declaration, we need the type of the
136202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // corresponding non-type template parameter.
136302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      QualType NTTPType;
136402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      if (NonTypeTemplateParmDecl *NTTP
136502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
136602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        if (Deduced[I].getKind() == TemplateArgument::Declaration) {
136702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor          NTTPType = NTTP->getType();
136802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor          if (NTTPType->isDependentType()) {
136902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor            TemplateArgumentList TemplateArgs(Context, Builder,
137002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                              /*TakeArgs=*/false);
137102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor            NTTPType = SubstType(NTTPType,
137202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                 MultiLevelTemplateArgumentList(TemplateArgs),
137302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                 NTTP->getLocation(),
137402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                 NTTP->getDeclName());
137502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor            if (NTTPType.isNull()) {
137602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor              Info.Param = makeTemplateParameter(Param);
137702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor              return TDK_SubstitutionFailure;
137802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor            }
137902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor          }
138002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        }
138102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      }
138202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
138302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // Convert the deduced template argument into a template
138402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // argument that we can check, almost as if the user had written
138502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // the template argument explicitly.
138602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      TemplateArgumentLoc Arg = getTrivialTemplateArgumentLoc(*this,
138702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                                              Deduced[I],
138802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                                              NTTPType,
138902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                                           SourceLocation());
139002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
139102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      // Check the template argument, converting it as necessary.
139202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      if (CheckTemplateArgument(Param, Arg,
139302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                FunctionTemplate,
139402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                FunctionTemplate->getLocation(),
139502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                FunctionTemplate->getSourceRange().getEnd(),
139602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                Builder,
139702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                Deduced[I].wasDeducedFromArrayBound()
139802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                  ? CTAK_DeducedFromArrayBound
139902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                  : CTAK_Deduced)) {
140002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        Info.Param = makeTemplateParameter(
140102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                         const_cast<NamedDecl *>(TemplateParams->getParam(I)));
140202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor        return TDK_SubstitutionFailure;
140302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      }
140402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
140551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      continue;
140651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    }
140751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
140851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // Substitute into the default template argument, if available.
140951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    TemplateArgumentLoc DefArg
141051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
141151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                              FunctionTemplate->getLocation(),
141251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                  FunctionTemplate->getSourceRange().getEnd(),
141351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                                Param,
141451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                                Builder);
141551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
141651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // If there was no default argument, deduction is incomplete.
141751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    if (DefArg.getArgument().isNull()) {
141883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      Info.Param = makeTemplateParameter(
141951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                         const_cast<NamedDecl *>(TemplateParams->getParam(I)));
142083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_Incomplete;
142183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    }
142251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
142351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // Check whether we can actually use the default argument.
142451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    if (CheckTemplateArgument(Param, DefArg,
142551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                              FunctionTemplate,
142651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                              FunctionTemplate->getLocation(),
142751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                              FunctionTemplate->getSourceRange().getEnd(),
142802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                              Builder,
142902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                              CTAK_Deduced)) {
143051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      Info.Param = makeTemplateParameter(
143151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                         const_cast<NamedDecl *>(TemplateParams->getParam(I)));
143251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      return TDK_SubstitutionFailure;
143351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    }
14341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
143551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // If we get here, we successfully used the default template argument.
143683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
14371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
143883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Form the template argument list from the deduced template arguments.
14391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentList *DeducedArgumentList
144083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
144183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Info.reset(DeducedArgumentList);
14421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Substitute the deduced template arguments into the function template
144483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // declaration to produce the function template specialization.
1445d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor  DeclContext *Owner = FunctionTemplate->getDeclContext();
1446d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor  if (FunctionTemplate->getFriendObjectKind())
1447d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor    Owner = FunctionTemplate->getLexicalDeclContext();
144883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Specialization = cast_or_null<FunctionDecl>(
1449d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor                      SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner,
1450357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                         MultiLevelTemplateArgumentList(*DeducedArgumentList)));
145183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (!Specialization)
145283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_SubstitutionFailure;
14531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1454f882574cf640d5c8355965e1c486f9d8d8ffcf47Douglas Gregor  assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
1455f882574cf640d5c8355965e1c486f9d8d8ffcf47Douglas Gregor         FunctionTemplate->getCanonicalDecl());
1456f882574cf640d5c8355965e1c486f9d8d8ffcf47Douglas Gregor
14571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If the template argument list is owned by the function template
145883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // specialization, release it.
145983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList)
146083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    Info.take();
14611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
146283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // There may have been an error that did not prevent us from constructing a
146383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // declaration. Mark the declaration invalid and return with a substitution
146483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // failure.
146583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (Trap.hasErrorOccurred()) {
146683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    Specialization->setInvalidDecl(true);
146783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_SubstitutionFailure;
146883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
14691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return TDK_Success;
147183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor}
147283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
1473eff92135d32039c9874dc356f3e93143af6069c1John McCallstatic QualType GetTypeOfFunction(ASTContext &Context,
1474eff92135d32039c9874dc356f3e93143af6069c1John McCall                                  bool isAddressOfOperand,
1475eff92135d32039c9874dc356f3e93143af6069c1John McCall                                  FunctionDecl *Fn) {
1476eff92135d32039c9874dc356f3e93143af6069c1John McCall  if (!isAddressOfOperand) return Fn->getType();
1477eff92135d32039c9874dc356f3e93143af6069c1John McCall  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
1478eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (Method->isInstance())
1479eff92135d32039c9874dc356f3e93143af6069c1John McCall      return Context.getMemberPointerType(Fn->getType(),
1480eff92135d32039c9874dc356f3e93143af6069c1John McCall               Context.getTypeDeclType(Method->getParent()).getTypePtr());
1481eff92135d32039c9874dc356f3e93143af6069c1John McCall  return Context.getPointerType(Fn->getType());
1482eff92135d32039c9874dc356f3e93143af6069c1John McCall}
1483eff92135d32039c9874dc356f3e93143af6069c1John McCall
1484eff92135d32039c9874dc356f3e93143af6069c1John McCall/// Apply the deduction rules for overload sets.
1485eff92135d32039c9874dc356f3e93143af6069c1John McCall///
1486eff92135d32039c9874dc356f3e93143af6069c1John McCall/// \return the null type if this argument should be treated as an
1487eff92135d32039c9874dc356f3e93143af6069c1John McCall/// undeduced context
1488eff92135d32039c9874dc356f3e93143af6069c1John McCallstatic QualType
1489eff92135d32039c9874dc356f3e93143af6069c1John McCallResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
1490eff92135d32039c9874dc356f3e93143af6069c1John McCall                            Expr *Arg, QualType ParamType) {
14917bb12da2b0749eeebb21854c77877736969e59f2John McCall  llvm::PointerIntPair<OverloadExpr*,1> R = OverloadExpr::find(Arg);
1492eff92135d32039c9874dc356f3e93143af6069c1John McCall
14937bb12da2b0749eeebb21854c77877736969e59f2John McCall  bool isAddressOfOperand = bool(R.getInt());
14947bb12da2b0749eeebb21854c77877736969e59f2John McCall  OverloadExpr *Ovl = R.getPointer();
1495eff92135d32039c9874dc356f3e93143af6069c1John McCall
1496eff92135d32039c9874dc356f3e93143af6069c1John McCall  // If there were explicit template arguments, we can only find
1497eff92135d32039c9874dc356f3e93143af6069c1John McCall  // something via C++ [temp.arg.explicit]p3, i.e. if the arguments
1498eff92135d32039c9874dc356f3e93143af6069c1John McCall  // unambiguously name a full specialization.
14997bb12da2b0749eeebb21854c77877736969e59f2John McCall  if (Ovl->hasExplicitTemplateArgs()) {
1500eff92135d32039c9874dc356f3e93143af6069c1John McCall    // But we can still look for an explicit specialization.
1501eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (FunctionDecl *ExplicitSpec
15027bb12da2b0749eeebb21854c77877736969e59f2John McCall          = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
15037bb12da2b0749eeebb21854c77877736969e59f2John McCall      return GetTypeOfFunction(S.Context, isAddressOfOperand, ExplicitSpec);
1504eff92135d32039c9874dc356f3e93143af6069c1John McCall    return QualType();
1505eff92135d32039c9874dc356f3e93143af6069c1John McCall  }
1506eff92135d32039c9874dc356f3e93143af6069c1John McCall
1507eff92135d32039c9874dc356f3e93143af6069c1John McCall  // C++0x [temp.deduct.call]p6:
1508eff92135d32039c9874dc356f3e93143af6069c1John McCall  //   When P is a function type, pointer to function type, or pointer
1509eff92135d32039c9874dc356f3e93143af6069c1John McCall  //   to member function type:
1510eff92135d32039c9874dc356f3e93143af6069c1John McCall
1511eff92135d32039c9874dc356f3e93143af6069c1John McCall  if (!ParamType->isFunctionType() &&
1512eff92135d32039c9874dc356f3e93143af6069c1John McCall      !ParamType->isFunctionPointerType() &&
1513eff92135d32039c9874dc356f3e93143af6069c1John McCall      !ParamType->isMemberFunctionPointerType())
1514eff92135d32039c9874dc356f3e93143af6069c1John McCall    return QualType();
1515eff92135d32039c9874dc356f3e93143af6069c1John McCall
1516eff92135d32039c9874dc356f3e93143af6069c1John McCall  QualType Match;
15177bb12da2b0749eeebb21854c77877736969e59f2John McCall  for (UnresolvedSetIterator I = Ovl->decls_begin(),
15187bb12da2b0749eeebb21854c77877736969e59f2John McCall         E = Ovl->decls_end(); I != E; ++I) {
1519eff92135d32039c9874dc356f3e93143af6069c1John McCall    NamedDecl *D = (*I)->getUnderlyingDecl();
1520eff92135d32039c9874dc356f3e93143af6069c1John McCall
1521eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   - If the argument is an overload set containing one or more
1522eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     function templates, the parameter is treated as a
1523eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     non-deduced context.
1524eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (isa<FunctionTemplateDecl>(D))
1525eff92135d32039c9874dc356f3e93143af6069c1John McCall      return QualType();
1526eff92135d32039c9874dc356f3e93143af6069c1John McCall
1527eff92135d32039c9874dc356f3e93143af6069c1John McCall    FunctionDecl *Fn = cast<FunctionDecl>(D);
1528eff92135d32039c9874dc356f3e93143af6069c1John McCall    QualType ArgType = GetTypeOfFunction(S.Context, isAddressOfOperand, Fn);
1529eff92135d32039c9874dc356f3e93143af6069c1John McCall
1530eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   - If the argument is an overload set (not containing function
1531eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     templates), trial argument deduction is attempted using each
1532eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     of the members of the set. If deduction succeeds for only one
1533eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     of the overload set members, that member is used as the
1534eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     argument value for the deduction. If deduction succeeds for
1535eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     more than one member of the overload set the parameter is
1536eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     treated as a non-deduced context.
1537eff92135d32039c9874dc356f3e93143af6069c1John McCall
1538eff92135d32039c9874dc356f3e93143af6069c1John McCall    // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
1539eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   Type deduction is done independently for each P/A pair, and
1540eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   the deduced template argument values are then combined.
1541eff92135d32039c9874dc356f3e93143af6069c1John McCall    // So we do not reject deductions which were made elsewhere.
154202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    llvm::SmallVector<DeducedTemplateArgument, 8>
154302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor      Deduced(TemplateParams->size());
15445769d6195087229770d7ac90449443e026c47103John McCall    Sema::TemplateDeductionInfo Info(S.Context, Ovl->getNameLoc());
1545eff92135d32039c9874dc356f3e93143af6069c1John McCall    unsigned TDF = 0;
1546eff92135d32039c9874dc356f3e93143af6069c1John McCall
1547eff92135d32039c9874dc356f3e93143af6069c1John McCall    Sema::TemplateDeductionResult Result
1548a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      = DeduceTemplateArguments(S, TemplateParams,
1549eff92135d32039c9874dc356f3e93143af6069c1John McCall                                ParamType, ArgType,
1550eff92135d32039c9874dc356f3e93143af6069c1John McCall                                Info, Deduced, TDF);
1551eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (Result) continue;
1552eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (!Match.isNull()) return QualType();
1553eff92135d32039c9874dc356f3e93143af6069c1John McCall    Match = ArgType;
1554eff92135d32039c9874dc356f3e93143af6069c1John McCall  }
1555eff92135d32039c9874dc356f3e93143af6069c1John McCall
1556eff92135d32039c9874dc356f3e93143af6069c1John McCall  return Match;
1557eff92135d32039c9874dc356f3e93143af6069c1John McCall}
1558eff92135d32039c9874dc356f3e93143af6069c1John McCall
1559e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \brief Perform template argument deduction from a function call
1560e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// (C++ [temp.deduct.call]).
1561e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1562e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param FunctionTemplate the function template for which we are performing
1563e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// template argument deduction.
1564e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
156548026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// \param ExplicitTemplateArguments the explicit template arguments provided
156648026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// for this call.
15676db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor///
1568e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Args the function call arguments
1569e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1570e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param NumArgs the number of arguments in Args
1571e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
157248026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// \param Name the name of the function being called. This is only significant
157348026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// when the function template is a conversion function template, in which
157448026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// case this routine will also perform template argument deduction based on
157548026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// the function to which
157648026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor///
1577e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Specialization if template argument deduction was successful,
15781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// this will be set to the function template specialization produced by
1579e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// template argument deduction.
1580e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1581e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Info the argument will be updated to provide additional information
1582e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// about template argument deduction.
1583e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1584e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns the result of template argument deduction.
1585e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorSema::TemplateDeductionResult
1586e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
158748026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor                          const TemplateArgumentListInfo *ExplicitTemplateArgs,
1588e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                              Expr **Args, unsigned NumArgs,
1589e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                              FunctionDecl *&Specialization,
1590e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                              TemplateDeductionInfo &Info) {
1591e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
15926db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor
1593e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // C++ [temp.deduct.call]p1:
1594e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  //   Template argument deduction is done by comparing each function template
1595e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  //   parameter type (call it P) with the type of the corresponding argument
1596e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  //   of the call (call it A) as described below.
1597e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  unsigned CheckArgs = NumArgs;
15986db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  if (NumArgs < Function->getMinRequiredArguments())
1599e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return TDK_TooFewArguments;
1600e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  else if (NumArgs > Function->getNumParams()) {
16011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const FunctionProtoType *Proto
1602183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall      = Function->getType()->getAs<FunctionProtoType>();
1603e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (!Proto->isVariadic())
1604e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return TDK_TooManyArguments;
16051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1606e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    CheckArgs = Function->getNumParams();
1607e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  }
16081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16096db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  // The types of the parameters from which we will perform template argument
16106db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  // deduction.
16112b0749a4f8965d0205bf77322db150c13c39e3beDouglas Gregor  Sema::LocalInstantiationScope InstScope(*this);
1612e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  TemplateParameterList *TemplateParams
1613e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    = FunctionTemplate->getTemplateParameters();
161402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
16156db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  llvm::SmallVector<QualType, 4> ParamTypes;
161602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  unsigned NumExplicitlySpecified = 0;
1617d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (ExplicitTemplateArgs) {
161883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    TemplateDeductionResult Result =
161983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      SubstituteExplicitTemplateArguments(FunctionTemplate,
1620d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                          *ExplicitTemplateArgs,
162183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          Deduced,
162283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          ParamTypes,
162383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          0,
162483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          Info);
162583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (Result)
162683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return Result;
162702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
162802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    NumExplicitlySpecified = Deduced.size();
16296db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  } else {
16306db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor    // Just fill in the parameter types from the function declaration.
16316db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor    for (unsigned I = 0; I != CheckArgs; ++I)
16326db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor      ParamTypes.push_back(Function->getParamDecl(I)->getType());
16336db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  }
16341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16356db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  // Deduce template arguments from the function parameters.
16361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Deduced.resize(TemplateParams->size());
1637e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  for (unsigned I = 0; I != CheckArgs; ++I) {
16386db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor    QualType ParamType = ParamTypes[I];
1639e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    QualType ArgType = Args[I]->getType();
16401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1641eff92135d32039c9874dc356f3e93143af6069c1John McCall    // Overload sets usually make this parameter an undeduced
1642eff92135d32039c9874dc356f3e93143af6069c1John McCall    // context, but there are sometimes special circumstances.
1643eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (ArgType == Context.OverloadTy) {
1644eff92135d32039c9874dc356f3e93143af6069c1John McCall      ArgType = ResolveOverloadForDeduction(*this, TemplateParams,
1645eff92135d32039c9874dc356f3e93143af6069c1John McCall                                            Args[I], ParamType);
1646eff92135d32039c9874dc356f3e93143af6069c1John McCall      if (ArgType.isNull())
1647eff92135d32039c9874dc356f3e93143af6069c1John McCall        continue;
1648eff92135d32039c9874dc356f3e93143af6069c1John McCall    }
1649eff92135d32039c9874dc356f3e93143af6069c1John McCall
1650e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    // C++ [temp.deduct.call]p2:
1651e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    //   If P is not a reference type:
1652e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    QualType CanonParamType = Context.getCanonicalType(ParamType);
1653500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor    bool ParamWasReference = isa<ReferenceType>(CanonParamType);
1654500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor    if (!ParamWasReference) {
16551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   - If A is an array type, the pointer type produced by the
16561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //     array-to-pointer standard conversion (4.2) is used in place of
1657e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      //     A for type deduction; otherwise,
1658e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      if (ArgType->isArrayType())
1659e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        ArgType = Context.getArrayDecayedType(ArgType);
16601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   - If A is a function type, the pointer type produced by the
16611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //     function-to-pointer standard conversion (4.3) is used in place
1662e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      //     of A for type deduction; otherwise,
1663e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      else if (ArgType->isFunctionType())
1664e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        ArgType = Context.getPointerType(ArgType);
1665e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      else {
1666e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
1667e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        //   type are ignored for type deduction.
1668e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        QualType CanonArgType = Context.getCanonicalType(ArgType);
1669a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor        if (CanonArgType.getLocalCVRQualifiers())
1670a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor          ArgType = CanonArgType.getLocalUnqualifiedType();
1671e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      }
1672e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    }
16731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1674e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    // C++0x [temp.deduct.call]p3:
1675e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    //   If P is a cv-qualified type, the top level cv-qualifiers of P’s type
16761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //   are ignored for type deduction.
1677a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor    if (CanonParamType.getLocalCVRQualifiers())
1678a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor      ParamType = CanonParamType.getLocalUnqualifiedType();
16796217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
16801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   [...] If P is a reference type, the type referred to by P is used
16811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   for type deduction.
1682e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      ParamType = ParamRefType->getPointeeType();
16831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
16841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   [...] If P is of the form T&&, where T is a template parameter, and
16851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   the argument is an lvalue, the type A& is used in place of A for
1686e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      //   type deduction.
1687e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      if (isa<RValueReferenceType>(ParamRefType) &&
1688183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall          ParamRefType->getAs<TemplateTypeParmType>() &&
1689e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor          Args[I]->isLvalue(Context) == Expr::LV_Valid)
1690e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        ArgType = Context.getLValueReferenceType(ArgType);
1691e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    }
16921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1693e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    // C++0x [temp.deduct.call]p4:
1694e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    //   In general, the deduction process attempts to find template argument
1695e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    //   values that will make the deduced A identical to A (after the type A
1696e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    //   is transformed as described above). [...]
16971282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor    unsigned TDF = TDF_SkipNonDependent;
16981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1699508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    //     - If the original P is a reference type, the deduced A (i.e., the
1700508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    //       type referred to by the reference) can be more cv-qualified than
1701508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    //       the transformed A.
1702508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    if (ParamWasReference)
1703508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor      TDF |= TDF_ParamWithReferenceType;
17041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     - The transformed A can be another pointer or pointer to member
17051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //       type that can be converted to the deduced A via a qualification
1706508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    //       conversion (4.4).
1707508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    if (ArgType->isPointerType() || ArgType->isMemberPointerType())
1708508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor      TDF |= TDF_IgnoreQualifiers;
17091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     - If P is a class and P has the form simple-template-id, then the
17104112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    //       transformed A can be a derived class of the deduced A. Likewise,
17114112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    //       if P is a pointer to a class of the form simple-template-id, the
17124112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    //       transformed A can be a pointer to a derived class pointed to by
17134112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    //       the deduced A.
17144112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    if (isSimpleTemplateIdType(ParamType) ||
17151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        (isa<PointerType>(ParamType) &&
17164112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor         isSimpleTemplateIdType(
17176217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek                              ParamType->getAs<PointerType>()->getPointeeType())))
17184112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor      TDF |= TDF_DerivedClass;
17191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1720e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (TemplateDeductionResult Result
1721a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = ::DeduceTemplateArguments(*this, TemplateParams,
1722500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor                                    ParamType, ArgType, Info, Deduced,
1723508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                    TDF))
1724e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return Result;
17251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
172665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    // FIXME: we need to check that the deduced A is the same as A,
172765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    // modulo the various allowed differences.
1728e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  }
172965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
17301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
173102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                         NumExplicitlySpecified,
173283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                         Specialization, Info);
173383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor}
1734127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor
173583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \brief Deduce template arguments when taking the address of a function
17364b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
17374b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// a template.
173883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
173983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionTemplate the function template for which we are performing
174083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template argument deduction.
174183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
17424b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param ExplicitTemplateArguments the explicitly-specified template
17434b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// arguments.
174483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
174583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param ArgFunctionType the function type that will be used as the
174683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// "argument" type (A) when performing template argument deduction from the
17474b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// function template's function type. This type may be NULL, if there is no
17484b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
174983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
175083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Specialization if template argument deduction was successful,
17511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// this will be set to the function template specialization produced by
175283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template argument deduction.
175383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
175483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Info the argument will be updated to provide additional information
175583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// about template argument deduction.
175683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
175783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \returns the result of template argument deduction.
175883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::TemplateDeductionResult
175983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1760d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                        const TemplateArgumentListInfo *ExplicitTemplateArgs,
176183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                              QualType ArgFunctionType,
176283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                              FunctionDecl *&Specialization,
176383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                              TemplateDeductionInfo &Info) {
176483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
176583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateParameterList *TemplateParams
176683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = FunctionTemplate->getTemplateParameters();
176783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  QualType FunctionType = Function->getType();
17681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
176983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Substitute any explicit template arguments.
17702b0749a4f8965d0205bf77322db150c13c39e3beDouglas Gregor  Sema::LocalInstantiationScope InstScope(*this);
177102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
177202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  unsigned NumExplicitlySpecified = 0;
177383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  llvm::SmallVector<QualType, 4> ParamTypes;
1774d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (ExplicitTemplateArgs) {
17751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (TemplateDeductionResult Result
17761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          = SubstituteExplicitTemplateArguments(FunctionTemplate,
1777d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                *ExplicitTemplateArgs,
17781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                Deduced, ParamTypes,
177983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                                &FunctionType, Info))
178083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return Result;
178102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor
178202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    NumExplicitlySpecified = Deduced.size();
1783127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
178483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
178583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Template argument deduction for function templates in a SFINAE context.
178683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Trap any errors that might occur.
17871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SFINAETrap Trap(*this);
17881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1789eff92135d32039c9874dc356f3e93143af6069c1John McCall  Deduced.resize(TemplateParams->size());
1790eff92135d32039c9874dc356f3e93143af6069c1John McCall
17914b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  if (!ArgFunctionType.isNull()) {
17924b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    // Deduce template arguments from the function type.
17934b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (TemplateDeductionResult Result
1794a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          = ::DeduceTemplateArguments(*this, TemplateParams,
17954b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                                      FunctionType, ArgFunctionType, Info,
17964b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                                      Deduced, 0))
17974b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      return Result;
17984b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  }
17994b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor
18001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
180102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                         NumExplicitlySpecified,
180283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                         Specialization, Info);
1803e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
1804e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
180565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \brief Deduce template arguments for a templated conversion
180665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// function (C++ [temp.deduct.conv]) and, if successful, produce a
180765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// conversion function template specialization.
180865ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::TemplateDeductionResult
180965ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
181065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                              QualType ToType,
181165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                              CXXConversionDecl *&Specialization,
181265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                              TemplateDeductionInfo &Info) {
18131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  CXXConversionDecl *Conv
181465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
181565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  QualType FromType = Conv->getConversionType();
181665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
181765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Canonicalize the types for deduction.
181865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  QualType P = Context.getCanonicalType(FromType);
181965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  QualType A = Context.getCanonicalType(ToType);
182065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
182165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++0x [temp.deduct.conv]p3:
182265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   If P is a reference type, the type referred to by P is used for
182365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   type deduction.
182465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (const ReferenceType *PRef = P->getAs<ReferenceType>())
182565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    P = PRef->getPointeeType();
182665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
182765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++0x [temp.deduct.conv]p3:
182865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   If A is a reference type, the type referred to by A is used
182965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   for type deduction.
183065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (const ReferenceType *ARef = A->getAs<ReferenceType>())
183165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    A = ARef->getPointeeType();
183265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++ [temp.deduct.conv]p2:
183365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //
18341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   If A is not a reference type:
183565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  else {
183665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    assert(!A->isReferenceType() && "Reference types were handled above");
183765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
183865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   - If P is an array type, the pointer type produced by the
18391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     array-to-pointer standard conversion (4.2) is used in place
184065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     of P for type deduction; otherwise,
184165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    if (P->isArrayType())
184265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      P = Context.getArrayDecayedType(P);
184365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   - If P is a function type, the pointer type produced by the
184465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     function-to-pointer standard conversion (4.3) is used in
184565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     place of P for type deduction; otherwise,
184665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    else if (P->isFunctionType())
184765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      P = Context.getPointerType(P);
184865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   - If P is a cv-qualified type, the top level cv-qualifiers of
184965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     P’s type are ignored for type deduction.
185065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    else
185165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      P = P.getUnqualifiedType();
185265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
185365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    // C++0x [temp.deduct.conv]p3:
185465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   If A is a cv-qualified type, the top level cv-qualifiers of A’s
185565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   type are ignored for type deduction.
185665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    A = A.getUnqualifiedType();
185765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  }
185865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
185965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Template argument deduction for function templates in a SFINAE context.
186065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Trap any errors that might occur.
18611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SFINAETrap Trap(*this);
186265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
186365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++ [temp.deduct.conv]p1:
186465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   Template argument deduction is done by comparing the return
186565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   type of the template conversion function (call it P) with the
186665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   type that is required as the result of the conversion (call it
186765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   A) as described in 14.8.2.4.
186865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  TemplateParameterList *TemplateParams
186965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    = FunctionTemplate->getTemplateParameters();
187002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
18711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Deduced.resize(TemplateParams->size());
187265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
187365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++0x [temp.deduct.conv]p4:
187465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   In general, the deduction process attempts to find template
187565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   argument values that will make the deduced A identical to
187665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   A. However, there are two cases that allow a difference:
187765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  unsigned TDF = 0;
187865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //     - If the original A is a reference type, A can be more
187965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       cv-qualified than the deduced A (i.e., the type referred to
188065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       by the reference)
188165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (ToType->isReferenceType())
188265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    TDF |= TDF_ParamWithReferenceType;
188365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //     - The deduced A can be another pointer or pointer to member
188465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       type that can be converted to A via a qualification
188565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       conversion.
188665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //
188765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
188865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // both P and A are pointers or member pointers. In this case, we
188965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // just ignore cv-qualifiers completely).
189065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if ((P->isPointerType() && A->isPointerType()) ||
189165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      (P->isMemberPointerType() && P->isMemberPointerType()))
189265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    TDF |= TDF_IgnoreQualifiers;
189365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (TemplateDeductionResult Result
1894a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = ::DeduceTemplateArguments(*this, TemplateParams,
189565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                    P, A, Info, Deduced, TDF))
189665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    return Result;
189765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
189865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // FIXME: we need to check that the deduced A is the same as A,
189965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // modulo the various allowed differences.
19001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
190165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Finish template argument deduction.
19022b0749a4f8965d0205bf77322db150c13c39e3beDouglas Gregor  Sema::LocalInstantiationScope InstScope(*this);
190365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  FunctionDecl *Spec = 0;
190465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  TemplateDeductionResult Result
190502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor    = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 0, Spec,
190602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                      Info);
190765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  Specialization = cast_or_null<CXXConversionDecl>(Spec);
190865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return Result;
190965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor}
191065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
19114b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \brief Deduce template arguments for a function template when there is
19124b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
19134b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
19144b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param FunctionTemplate the function template for which we are performing
19154b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template argument deduction.
19164b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
19174b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param ExplicitTemplateArguments the explicitly-specified template
19184b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// arguments.
19194b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
19204b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param Specialization if template argument deduction was successful,
19214b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// this will be set to the function template specialization produced by
19224b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template argument deduction.
19234b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
19244b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param Info the argument will be updated to provide additional information
19254b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// about template argument deduction.
19264b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
19274b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \returns the result of template argument deduction.
19284b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas GregorSema::TemplateDeductionResult
19294b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
19304b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                           const TemplateArgumentListInfo *ExplicitTemplateArgs,
19314b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                              FunctionDecl *&Specialization,
19324b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                              TemplateDeductionInfo &Info) {
19334b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
19344b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                                 QualType(), Specialization, Info);
19354b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor}
19364b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor
19378a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Stores the result of comparing the qualifiers of two types.
19388a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorenum DeductionQualifierComparison {
19398a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  NeitherMoreQualified = 0,
19408a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  ParamMoreQualified,
19418a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  ArgMoreQualified
19428a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor};
19438a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19448a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Deduce the template arguments during partial ordering by comparing
19458a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// the parameter type and the argument type (C++0x [temp.deduct.partial]).
19468a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
1947a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth/// \param S the semantic analysis object within which we are deducing
19488a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
19498a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param TemplateParams the template parameters that we are deducing
19508a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
19518a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param ParamIn the parameter type
19528a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
19538a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param ArgIn the argument type
19548a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
19558a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param Info information about the template argument deduction itself
19568a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
19578a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param Deduced the deduced template arguments
19588a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
19598a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \returns the result of template argument deduction so far. Note that a
19608a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// "success" result means that template argument deduction has not yet failed,
19618a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// but it may still fail, later, for other reasons.
19628a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic Sema::TemplateDeductionResult
1963a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArgumentsDuringPartialOrdering(Sema &S,
196402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                        TemplateParameterList *TemplateParams,
19658a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                             QualType ParamIn, QualType ArgIn,
19668a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                             Sema::TemplateDeductionInfo &Info,
196702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                      llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced,
196802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor   llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
1969a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  CanQualType Param = S.Context.getCanonicalType(ParamIn);
1970a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  CanQualType Arg = S.Context.getCanonicalType(ArgIn);
19718a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19728a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p5:
19738a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   Before the partial ordering is done, certain transformations are
19748a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   performed on the types used for partial ordering:
19758a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //     - If P is a reference type, P is replaced by the type referred to.
19768a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>();
1977e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  if (!ParamRef.isNull())
19788a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    Param = ParamRef->getPointeeType();
19798a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19808a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //     - If A is a reference type, A is replaced by the type referred to.
19818a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>();
1982e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  if (!ArgRef.isNull())
19838a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    Arg = ArgRef->getPointeeType();
19848a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
1985e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) {
19868a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // C++0x [temp.deduct.partial]p6:
19878a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   If both P and A were reference types (before being replaced with the
19888a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   type referred to above), determine which of the two types (if any) is
19898a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   more cv-qualified than the other; otherwise the types are considered to
19908a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   be equally cv-qualified for partial ordering purposes. The result of this
19918a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   determination will be used below.
19928a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //
19938a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // We save this information for later, using it only when deduction
19948a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // succeeds in both directions.
19958a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    DeductionQualifierComparison QualifierResult = NeitherMoreQualified;
19968a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    if (Param.isMoreQualifiedThan(Arg))
19978a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      QualifierResult = ParamMoreQualified;
19988a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    else if (Arg.isMoreQualifiedThan(Param))
19998a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      QualifierResult = ArgMoreQualified;
20008a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    QualifierComparisons->push_back(QualifierResult);
20018a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
20028a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20038a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p7:
20048a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   Remove any top-level cv-qualifiers:
20058a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //     - If P is a cv-qualified type, P is replaced by the cv-unqualified
20068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //       version of P.
20078a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Param = Param.getUnqualifiedType();
20088a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //     - If A is a cv-qualified type, A is replaced by the cv-unqualified
20098a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //       version of A.
20108a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Arg = Arg.getUnqualifiedType();
20118a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20128a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p8:
20138a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   Using the resulting types P and A the deduction is then done as
20148a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   described in 14.9.2.5. If deduction succeeds for a given type, the type
20158a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   from the argument template is considered to be at least as specialized
20168a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   as the type from the parameter template.
2017a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  return DeduceTemplateArguments(S, TemplateParams, Param, Arg, Info,
20188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                 Deduced, TDF_None);
20198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor}
20208a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic void
2022e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2023e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2024ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Level,
2025e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Deduced);
20268a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20278a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Determine whether the function template \p FT1 is at least as
20288a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// specialized as \p FT2.
20298a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic bool isAtLeastAsSpecializedAs(Sema &S,
20305769d6195087229770d7ac90449443e026c47103John McCall                                     SourceLocation Loc,
20318a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                     FunctionTemplateDecl *FT1,
20328a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                     FunctionTemplateDecl *FT2,
20338a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                     TemplatePartialOrderingContext TPOC,
20348a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
20358a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  FunctionDecl *FD1 = FT1->getTemplatedDecl();
20368a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  FunctionDecl *FD2 = FT2->getTemplatedDecl();
20378a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
20388a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
20398a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20408a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  assert(Proto1 && Proto2 && "Function templates must have prototypes");
20418a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
204202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
20438a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Deduced.resize(TemplateParams->size());
20448a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20458a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p3:
20468a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   The types used to determine the ordering depend on the context in which
20478a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   the partial ordering is done:
20485769d6195087229770d7ac90449443e026c47103John McCall  Sema::TemplateDeductionInfo Info(S.Context, Loc);
20498a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  switch (TPOC) {
20508a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Call: {
20518a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   - In the context of a function call, the function parameter types are
20528a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //     used.
20538a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
20548a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    for (unsigned I = 0; I != NumParams; ++I)
2055a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      if (DeduceTemplateArgumentsDuringPartialOrdering(S,
20568a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                       TemplateParams,
20578a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                       Proto2->getArgType(I),
20588a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                       Proto1->getArgType(I),
20598a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                       Info,
20608a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                       Deduced,
20618a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                       QualifierComparisons))
20628a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        return false;
20638a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20648a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
20658a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
20668a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20678a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Conversion:
20688a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   - In the context of a call to a conversion operator, the return types
20698a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //     of the conversion function templates are used.
2070a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth    if (DeduceTemplateArgumentsDuringPartialOrdering(S,
20718a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     TemplateParams,
20728a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     Proto2->getResultType(),
20738a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     Proto1->getResultType(),
20748a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     Info,
20758a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     Deduced,
20768a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     QualifierComparisons))
20778a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      return false;
20788a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
20798a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20808a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Other:
20818a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   - In other contexts (14.6.6.2) the function template’s function type
20828a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //     is used.
2083a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth    if (DeduceTemplateArgumentsDuringPartialOrdering(S,
20848a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     TemplateParams,
20858a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     FD2->getType(),
20868a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     FD1->getType(),
20878a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     Info,
20888a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     Deduced,
20898a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     QualifierComparisons))
20908a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      return false;
20918a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
20928a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
20938a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20948a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p11:
20958a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   In most cases, all template parameters must have values in order for
20968a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   deduction to succeed, but for partial ordering purposes a template
20978a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   parameter may remain without a value provided it is not used in the
20988a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   types being used for partial ordering. [ Note: a template parameter used
20998a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   in a non-deduced context is considered used. -end note]
21008a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  unsigned ArgIdx = 0, NumArgs = Deduced.size();
21018a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  for (; ArgIdx != NumArgs; ++ArgIdx)
21028a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    if (Deduced[ArgIdx].isNull())
21038a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      break;
21048a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
21058a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  if (ArgIdx == NumArgs) {
21068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // All template arguments were deduced. FT1 is at least as specialized
21078a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // as FT2.
21088a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return true;
21098a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
21108a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
2111e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // Figure out which template parameters were used.
21128a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  llvm::SmallVector<bool, 4> UsedParameters;
21138a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  UsedParameters.resize(TemplateParams->size());
21148a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  switch (TPOC) {
21158a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Call: {
21168a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
21178a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    for (unsigned I = 0; I != NumParams; ++I)
2118ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
2119ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                   TemplateParams->getDepth(),
2120e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                   UsedParameters);
21218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
21228a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
21238a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
21248a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Conversion:
2125ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
2126ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 TemplateParams->getDepth(),
2127e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                 UsedParameters);
21288a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
21298a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
21308a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Other:
2131ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    ::MarkUsedTemplateParameters(S, FD2->getType(), false,
2132ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 TemplateParams->getDepth(),
2133ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 UsedParameters);
21348a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
21358a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
21368a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
21378a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  for (; ArgIdx != NumArgs; ++ArgIdx)
21388a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // If this argument had no value deduced but was used in one of the types
21398a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // used for partial ordering, then deduction fails.
21408a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
21418a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      return false;
21428a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
21438a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  return true;
21448a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor}
21458a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
21468a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
2147bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \brief Returns the more specialized function template according
214865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// to the rules of function template partial ordering (C++ [temp.func.order]).
214965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor///
215065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \param FT1 the first function template
215165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor///
215265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \param FT2 the second function template
215365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor///
21548a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param TPOC the context in which we are performing partial ordering of
21558a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// function templates.
21561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
2157bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \returns the more specialized function template. If neither
215865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// template is more specialized, returns NULL.
215965ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorFunctionTemplateDecl *
216065ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
216165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                 FunctionTemplateDecl *FT2,
21625769d6195087229770d7ac90449443e026c47103John McCall                                 SourceLocation Loc,
21638a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                 TemplatePartialOrderingContext TPOC) {
21648a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons;
21655769d6195087229770d7ac90449443e026c47103John McCall  bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, 0);
21665769d6195087229770d7ac90449443e026c47103John McCall  bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC,
21678a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                          &QualifierComparisons);
21688a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
21698a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  if (Better1 != Better2) // We have a clear winner
21708a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return Better1? FT1 : FT2;
21718a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
21728a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  if (!Better1 && !Better2) // Neither is better than the other
217365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    return 0;
21748a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
21758a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
21768a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p10:
21778a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   If for each type being considered a given template is at least as
21788a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   specialized for all types and more specialized for some set of types and
21798a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   the other template is not more specialized for any types or is not at
21808a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   least as specialized for any types, then the given template is more
21818a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   specialized than the other template. Otherwise, neither template is more
21828a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   specialized than the other.
21838a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Better1 = false;
21848a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Better2 = false;
21858a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) {
21868a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // C++0x [temp.deduct.partial]p9:
21878a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   If, for a given type, deduction succeeds in both directions (i.e., the
21888a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   types are identical after the transformations above) and if the type
21898a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   from the argument template is more cv-qualified than the type from the
21908a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   parameter template (as described above) that type is considered to be
21918a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   more specialized than the other. If neither type is more cv-qualified
21928a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   than the other then neither type is more specialized than the other.
21938a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    switch (QualifierComparisons[I]) {
21948a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      case NeitherMoreQualified:
21958a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        break;
21968a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
21978a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      case ParamMoreQualified:
21988a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        Better1 = true;
21998a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        if (Better2)
22008a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor          return 0;
22018a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        break;
22028a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
22038a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      case ArgMoreQualified:
22048a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        Better2 = true;
22058a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        if (Better1)
22068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor          return 0;
22078a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        break;
22088a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    }
22098a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
22108a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
22118a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  assert(!(Better1 && Better2) && "Should have broken out in the loop above");
221265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (Better1)
221365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    return FT1;
22148a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  else if (Better2)
22158a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return FT2;
22168a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  else
22178a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return 0;
221865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor}
221983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
2220d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \brief Determine if the two templates are equivalent.
2221d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregorstatic bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
2222d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  if (T1 == T2)
2223d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    return true;
2224d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2225d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  if (!T1 || !T2)
2226d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    return false;
2227d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2228d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  return T1->getCanonicalDecl() == T2->getCanonicalDecl();
2229d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor}
2230d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2231d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \brief Retrieve the most specialized of the given function template
2232d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specializations.
2233d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2234c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// \param SpecBegin the start iterator of the function template
2235c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// specializations that we will be comparing.
2236d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2237c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// \param SpecEnd the end iterator of the function template
2238c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// specializations, paired with \p SpecBegin.
2239d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2240d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param TPOC the partial ordering context to use to compare the function
2241d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// template specializations.
2242d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2243d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param Loc the location where the ambiguity or no-specializations
2244d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// diagnostic should occur.
2245d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2246d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param NoneDiag partial diagnostic used to diagnose cases where there are
2247d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// no matching candidates.
2248d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2249d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
2250d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// occurs.
2251d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2252d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param CandidateDiag partial diagnostic used for each function template
2253d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specialization that is a candidate in the ambiguous ordering. One parameter
2254d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// in this diagnostic should be unbound, which will correspond to the string
2255d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// describing the template arguments for the function template specialization.
2256d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2257d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param Index if non-NULL and the result of this function is non-nULL,
2258d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// receives the index corresponding to the resulting function template
2259d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specialization.
2260d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2261d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \returns the most specialized function template specialization, if
2262c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// found. Otherwise, returns SpecEnd.
2263d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2264d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
2265d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// template argument deduction.
2266c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallUnresolvedSetIterator
2267c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallSema::getMostSpecialized(UnresolvedSetIterator SpecBegin,
2268c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         UnresolvedSetIterator SpecEnd,
2269c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         TemplatePartialOrderingContext TPOC,
2270c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         SourceLocation Loc,
2271c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         const PartialDiagnostic &NoneDiag,
2272c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         const PartialDiagnostic &AmbigDiag,
2273c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         const PartialDiagnostic &CandidateDiag) {
2274c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  if (SpecBegin == SpecEnd) {
2275d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    Diag(Loc, NoneDiag);
2276c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    return SpecEnd;
2277d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
2278d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2279c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  if (SpecBegin + 1 == SpecEnd)
2280c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    return SpecBegin;
2281d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2282d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // Find the function template that is better than all of the templates it
2283d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // has been compared to.
2284c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  UnresolvedSetIterator Best = SpecBegin;
2285d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  FunctionTemplateDecl *BestTemplate
2286c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
2287d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  assert(BestTemplate && "Not a function template specialization?");
2288c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
2289c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    FunctionTemplateDecl *Challenger
2290c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall      = cast<FunctionDecl>(*I)->getPrimaryTemplate();
2291d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    assert(Challenger && "Not a function template specialization?");
2292c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
22935769d6195087229770d7ac90449443e026c47103John McCall                                                  Loc, TPOC),
2294d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor                       Challenger)) {
2295d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      Best = I;
2296d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      BestTemplate = Challenger;
2297d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    }
2298d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
2299d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2300d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // Make sure that the "best" function template is more specialized than all
2301d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // of the others.
2302d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  bool Ambiguous = false;
2303c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
2304c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    FunctionTemplateDecl *Challenger
2305c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall      = cast<FunctionDecl>(*I)->getPrimaryTemplate();
2306d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    if (I != Best &&
2307d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor        !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
23085769d6195087229770d7ac90449443e026c47103John McCall                                                   Loc, TPOC),
2309d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor                        BestTemplate)) {
2310d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      Ambiguous = true;
2311d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      break;
2312d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    }
2313d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
2314d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2315d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  if (!Ambiguous) {
2316d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    // We found an answer. Return it.
2317c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    return Best;
2318d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
2319d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2320d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // Diagnose the ambiguity.
2321d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  Diag(Loc, AmbigDiag);
2322d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2323d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // FIXME: Can we order the candidates in some sane way?
2324c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I)
2325c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    Diag((*I)->getLocation(), CandidateDiag)
2326d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      << getTemplateArgumentBindingsText(
2327c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall        cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
2328c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                    *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
2329d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2330c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  return SpecEnd;
2331d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor}
2332d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2333bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \brief Returns the more specialized class template partial specialization
2334bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// according to the rules of partial ordering of class template partial
2335bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// specializations (C++ [temp.class.order]).
2336bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor///
2337bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \param PS1 the first class template partial specialization
2338bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor///
2339bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \param PS2 the second class template partial specialization
2340bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor///
2341bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \returns the more specialized class template partial specialization. If
2342bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// neither partial specialization is more specialized, returns NULL.
2343bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas GregorClassTemplatePartialSpecializationDecl *
2344bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas GregorSema::getMoreSpecializedPartialSpecialization(
2345bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                  ClassTemplatePartialSpecializationDecl *PS1,
23465769d6195087229770d7ac90449443e026c47103John McCall                                  ClassTemplatePartialSpecializationDecl *PS2,
23475769d6195087229770d7ac90449443e026c47103John McCall                                              SourceLocation Loc) {
2348bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // C++ [temp.class.order]p1:
2349bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   For two class template partial specializations, the first is at least as
2350bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   specialized as the second if, given the following rewrite to two
2351bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   function templates, the first function template is at least as
2352bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   specialized as the second according to the ordering rules for function
2353bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   templates (14.6.6.2):
2354bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //     - the first function template has the same template parameters as the
2355bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       first partial specialization and has a single function parameter
2356bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       whose type is a class template specialization with the template
2357bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       arguments of the first partial specialization, and
2358bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //     - the second function template has the same template parameters as the
2359bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       second partial specialization and has a single function parameter
2360bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       whose type is a class template specialization with the template
2361bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       arguments of the second partial specialization.
2362bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //
236331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // Rather than synthesize function templates, we merely perform the
236431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // equivalent partial ordering by performing deduction directly on
236531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // the template arguments of the class template partial
236631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // specializations. This computation is slightly simpler than the
236731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // general problem of function template partial ordering, because
236831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // class template partial specializations are more constrained. We
236931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // know that every template parameter is deducible from the class
237031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // template partial specialization's template arguments, for
237131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor  // example.
237202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor  llvm::SmallVector<DeducedTemplateArgument, 4> Deduced;
23735769d6195087229770d7ac90449443e026c47103John McCall  Sema::TemplateDeductionInfo Info(Context, Loc);
237431f17ecbef57b5679c017c375db330546b7b5145John McCall
237531f17ecbef57b5679c017c375db330546b7b5145John McCall  QualType PT1 = PS1->getInjectedSpecializationType();
237631f17ecbef57b5679c017c375db330546b7b5145John McCall  QualType PT2 = PS2->getInjectedSpecializationType();
2377bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor
2378bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // Determine whether PS1 is at least as specialized as PS2
2379bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  Deduced.resize(PS2->getTemplateParameters()->size());
2380a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
2381bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                  PS2->getTemplateParameters(),
238231f17ecbef57b5679c017c375db330546b7b5145John McCall                                                               PT2,
238331f17ecbef57b5679c017c375db330546b7b5145John McCall                                                               PT1,
2384bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                               Info,
2385bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                               Deduced,
2386bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                               0);
2387516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor  if (Better1)
2388516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor    Better1 = !::FinishTemplateArgumentDeduction(*this, PS2,
2389516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor                                                 PS1->getTemplateArgs(),
2390516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor                                                 Deduced, Info);
2391516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor
2392bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // Determine whether PS2 is at least as specialized as PS1
2393db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Deduced.clear();
2394bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  Deduced.resize(PS1->getTemplateParameters()->size());
2395a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
2396bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                  PS1->getTemplateParameters(),
239731f17ecbef57b5679c017c375db330546b7b5145John McCall                                                               PT1,
239831f17ecbef57b5679c017c375db330546b7b5145John McCall                                                               PT2,
2399bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                               Info,
2400bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                               Deduced,
2401bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                               0);
2402516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor  if (Better2)
2403516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor    Better2 = !::FinishTemplateArgumentDeduction(*this, PS1,
2404516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor                                                 PS2->getTemplateArgs(),
2405516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor                                                 Deduced, Info);
2406bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor
2407bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  if (Better1 == Better2)
2408bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    return 0;
2409bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor
2410bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  return Better1? PS1 : PS2;
2411bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor}
2412bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor
24131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
2414e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
2415e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           const TemplateArgument &TemplateArg,
2416e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2417ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
2418e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used);
2419031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2420e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
2421031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// expression.
24221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
2423e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
2424e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           const Expr *E,
2425e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2426ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
2427e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
2428e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
2429e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // find other occurrences of template parameters.
2430f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor  const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
2431c781f9cd854f3d5d1c826f4a13382c6abca4cff7Douglas Gregor  if (!DRE)
2432031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    return;
2433031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
24341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const NonTypeTemplateParmDecl *NTTP
2435031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
2436031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  if (!NTTP)
2437031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    return;
2438031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2439ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (NTTP->getDepth() == Depth)
2440ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Used[NTTP->getIndex()] = true;
2441031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
2442031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2443e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
2444e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// nested name specifier.
2445e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregorstatic void
2446e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
2447e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           NestedNameSpecifier *NNS,
2448e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2449ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
2450e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
2451e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (!NNS)
2452e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    return;
2453e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
2454ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
2455ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             Used);
2456e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
2457ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             OnlyDeduced, Depth, Used);
2458e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor}
2459e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
2460e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
2461e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// template name.
2462e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregorstatic void
2463e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
2464e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           TemplateName Name,
2465e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2466ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
2467e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
2468e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2469e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    if (TemplateTemplateParmDecl *TTP
2470ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor          = dyn_cast<TemplateTemplateParmDecl>(Template)) {
2471ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      if (TTP->getDepth() == Depth)
2472ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        Used[TTP->getIndex()] = true;
2473ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    }
2474e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    return;
2475e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  }
2476e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
2477788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
2478788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
2479788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                               Depth, Used);
2480e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
2481ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
2482ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2483e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor}
2484e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
2485e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
2486031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// type.
24871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
2488e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2489e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2490ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
2491e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
2492e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (T.isNull())
2493e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    return;
2494e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
2495031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  // Non-dependent types have nothing deducible
2496031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  if (!T->isDependentType())
2497031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    return;
2498031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2499031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  T = SemaRef.Context.getCanonicalType(T);
2500031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  switch (T->getTypeClass()) {
2501031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Pointer:
2502e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
2503e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<PointerType>(T)->getPointeeType(),
2504e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               OnlyDeduced,
2505ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth,
2506e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               Used);
2507031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2508031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2509031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::BlockPointer:
2510e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
2511e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<BlockPointerType>(T)->getPointeeType(),
2512e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               OnlyDeduced,
2513ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth,
2514e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               Used);
2515031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2516031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2517031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::LValueReference:
2518031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::RValueReference:
2519e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
2520e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<ReferenceType>(T)->getPointeeType(),
2521e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               OnlyDeduced,
2522ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth,
2523e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               Used);
2524031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2525031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2526031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::MemberPointer: {
2527031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
2528e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
2529ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2530e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
2531ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
2532031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2533031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
2534031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2535031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::DependentSizedArray:
2536e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
2537e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<DependentSizedArrayType>(T)->getSizeExpr(),
2538ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
2539031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    // Fall through to check the element type
2540031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2541031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::ConstantArray:
2542031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::IncompleteArray:
2543e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
2544e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<ArrayType>(T)->getElementType(),
2545ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
2546031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2547031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2548031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Vector:
2549031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::ExtVector:
2550e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
2551e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<VectorType>(T)->getElementType(),
2552ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
2553031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2554031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
25559cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  case Type::DependentSizedExtVector: {
25569cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    const DependentSizedExtVectorType *VecType
2557f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor      = cast<DependentSizedExtVectorType>(T);
2558e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
2559ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2560e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
2561ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
25629cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    break;
25639cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
25649cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
2565031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::FunctionProto: {
2566f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor    const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
2567e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
2568ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2569031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
2570e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor      MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
2571ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 Depth, Used);
2572031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2573031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
2574031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2575ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  case Type::TemplateTypeParm: {
2576ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
2577ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (TTP->getDepth() == Depth)
2578ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      Used[TTP->getIndex()] = true;
2579031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2580ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
2581031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
258231f17ecbef57b5679c017c375db330546b7b5145John McCall  case Type::InjectedClassName:
258331f17ecbef57b5679c017c375db330546b7b5145John McCall    T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType();
258431f17ecbef57b5679c017c375db330546b7b5145John McCall    // fall through
258531f17ecbef57b5679c017c375db330546b7b5145John McCall
2586031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::TemplateSpecialization: {
25871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const TemplateSpecializationType *Spec
2588f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor      = cast<TemplateSpecializationType>(T);
2589e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
2590ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2591e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
2592ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2593ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 Used);
2594e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    break;
2595e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  }
25961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2597e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  case Type::Complex:
2598e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    if (!OnlyDeduced)
2599e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor      MarkUsedTemplateParameters(SemaRef,
2600e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                 cast<ComplexType>(T)->getElementType(),
2601ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 OnlyDeduced, Depth, Used);
2602e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    break;
2603031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
26044714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor  case Type::DependentName:
2605e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    if (!OnlyDeduced)
2606e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor      MarkUsedTemplateParameters(SemaRef,
26074714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor                                 cast<DependentNameType>(T)->getQualifier(),
2608ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 OnlyDeduced, Depth, Used);
2609031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2610031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2611ad5e73887052193afda72db8efcb812bd083a4a8John McCall  case Type::TypeOf:
2612ad5e73887052193afda72db8efcb812bd083a4a8John McCall    if (!OnlyDeduced)
2613ad5e73887052193afda72db8efcb812bd083a4a8John McCall      MarkUsedTemplateParameters(SemaRef,
2614ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 cast<TypeOfType>(T)->getUnderlyingType(),
2615ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 OnlyDeduced, Depth, Used);
2616ad5e73887052193afda72db8efcb812bd083a4a8John McCall    break;
2617ad5e73887052193afda72db8efcb812bd083a4a8John McCall
2618ad5e73887052193afda72db8efcb812bd083a4a8John McCall  case Type::TypeOfExpr:
2619ad5e73887052193afda72db8efcb812bd083a4a8John McCall    if (!OnlyDeduced)
2620ad5e73887052193afda72db8efcb812bd083a4a8John McCall      MarkUsedTemplateParameters(SemaRef,
2621ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 cast<TypeOfExprType>(T)->getUnderlyingExpr(),
2622ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 OnlyDeduced, Depth, Used);
2623ad5e73887052193afda72db8efcb812bd083a4a8John McCall    break;
2624ad5e73887052193afda72db8efcb812bd083a4a8John McCall
2625ad5e73887052193afda72db8efcb812bd083a4a8John McCall  case Type::Decltype:
2626ad5e73887052193afda72db8efcb812bd083a4a8John McCall    if (!OnlyDeduced)
2627ad5e73887052193afda72db8efcb812bd083a4a8John McCall      MarkUsedTemplateParameters(SemaRef,
2628ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 cast<DecltypeType>(T)->getUnderlyingExpr(),
2629ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 OnlyDeduced, Depth, Used);
2630ad5e73887052193afda72db8efcb812bd083a4a8John McCall    break;
2631ad5e73887052193afda72db8efcb812bd083a4a8John McCall
2632e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // None of these types have any template parameters in them.
2633031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Builtin:
2634031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::VariableArray:
2635031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::FunctionNoProto:
2636031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Record:
2637031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Enum:
2638031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::ObjCInterface:
2639d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  case Type::ObjCObjectPointer:
2640ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  case Type::UnresolvedUsing:
2641031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define TYPE(Class, Base)
2642031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
2643031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define DEPENDENT_TYPE(Class, Base)
2644031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2645031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#include "clang/AST/TypeNodes.def"
2646031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2647031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
2648031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
2649031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2650e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by this
2651031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// template argument.
26521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
2653e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
2654e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           const TemplateArgument &TemplateArg,
2655e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2656ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
2657e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
2658031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  switch (TemplateArg.getKind()) {
2659031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Null:
2660031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Integral:
2661788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Declaration:
2662031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
26631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2664031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Type:
2665e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
2666ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2667031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2668031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2669788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  case TemplateArgument::Template:
2670788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsTemplate(),
2671788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                               OnlyDeduced, Depth, Used);
2672031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2673031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2674031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Expression:
2675e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
2676ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2677031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2678e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
2679d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson  case TemplateArgument::Pack:
2680e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
2681e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                      PEnd = TemplateArg.pack_end();
2682e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor         P != PEnd; ++P)
2683ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
2684d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson    break;
2685031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
2686031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
2687031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2688031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \brief Mark the template parameters can be deduced by the given
2689031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// template argument list.
2690031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor///
2691031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \param TemplateArgs the template argument list from which template
2692031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// parameters will be deduced.
2693031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor///
2694031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \param Deduced a bit vector whose elements will be set to \c true
2695031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// to indicate when the corresponding template parameter will be
2696031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// deduced.
26971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
2698e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorSema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
2699ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 bool OnlyDeduced, unsigned Depth,
2700e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                 llvm::SmallVectorImpl<bool> &Used) {
2701031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
2702ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
2703ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 Depth, Used);
2704031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
270563f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor
270663f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor/// \brief Marks all of the template parameters that will be deduced by a
270763f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor/// call to the given function template.
270802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregorvoid
270902024a9f0d8e6c898de276193af604c42ee41269Douglas GregorSema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
271002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor                                    llvm::SmallVectorImpl<bool> &Deduced) {
271163f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  TemplateParameterList *TemplateParams
271263f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor    = FunctionTemplate->getTemplateParameters();
271363f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  Deduced.clear();
271463f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  Deduced.resize(TemplateParams->size());
271563f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor
271663f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
271763f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
271863f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor    ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
2719ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 true, TemplateParams->getDepth(), Deduced);
272063f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor}
2721