SemaTemplateDeduction.cpp revision 3cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4
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
51f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
52a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
53f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateParameterList *TemplateParams,
54f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        const TemplateArgument &Param,
55d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor                        const TemplateArgument &Arg,
56f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        Sema::TemplateDeductionInfo &Info,
57d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor                        llvm::SmallVectorImpl<TemplateArgument> &Deduced);
58d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor
59199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// \brief If the given expression is of a form that permits the deduction
60199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// of a non-type template parameter, return the declaration of that
61199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// non-type template parameter.
62199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregorstatic NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) {
63199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E))
64199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    E = IC->getSubExpr();
651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
66199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E))
67199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
69199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  return 0;
70199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor}
71199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Deduce the value of the given non-type template parameter
73199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// from the given constant.
74f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
75a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceNonTypeTemplateArgument(Sema &S,
761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                              NonTypeTemplateParmDecl *NTTP,
77335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson                              llvm::APSInt Value,
78f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                              Sema::TemplateDeductionInfo &Info,
79f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                              llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(NTTP->getDepth() == 0 &&
81199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor         "Cannot deduce non-type template argument with depth > 0");
821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
83199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  if (Deduced[NTTP->getIndex()].isNull()) {
8425af1ed6ffa46a333886264299be98a838097b34Anders Carlsson    QualType T = NTTP->getType();
851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
8625af1ed6ffa46a333886264299be98a838097b34Anders Carlsson    // FIXME: Make sure we didn't overflow our data type!
87a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth    unsigned AllowedBits = S.Context.getTypeSize(T);
8825af1ed6ffa46a333886264299be98a838097b34Anders Carlsson    if (Value.getBitWidth() != AllowedBits)
8925af1ed6ffa46a333886264299be98a838097b34Anders Carlsson      Value.extOrTrunc(AllowedBits);
9025af1ed6ffa46a333886264299be98a838097b34Anders Carlsson    Value.setIsSigned(T->isSignedIntegerType());
9125af1ed6ffa46a333886264299be98a838097b34Anders Carlsson
92833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    Deduced[NTTP->getIndex()] = TemplateArgument(Value, T);
93f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_Success;
94199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  }
951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
96f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  assert(Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral);
971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If the template argument was previously deduced to a negative value,
99199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  // then our deduction fails.
100199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  const llvm::APSInt *PrevValuePtr = Deduced[NTTP->getIndex()].getAsIntegral();
101335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson  if (PrevValuePtr->isNegative()) {
102f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.Param = NTTP;
103f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.FirstArg = Deduced[NTTP->getIndex()];
104833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    Info.SecondArg = TemplateArgument(Value, NTTP->getType());
105f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_Inconsistent;
106f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  }
107f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
108335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson  llvm::APSInt PrevValue = *PrevValuePtr;
109199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  if (Value.getBitWidth() > PrevValue.getBitWidth())
110199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    PrevValue.zext(Value.getBitWidth());
111199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  else if (Value.getBitWidth() < PrevValue.getBitWidth())
112199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    Value.zext(PrevValue.getBitWidth());
113f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
114f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  if (Value != PrevValue) {
115f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.Param = NTTP;
116f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.FirstArg = Deduced[NTTP->getIndex()];
117833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    Info.SecondArg = TemplateArgument(Value, NTTP->getType());
118f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_Inconsistent;
119f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  }
120f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
121f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  return Sema::TDK_Success;
122199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor}
123199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
1241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Deduce the value of the given non-type template parameter
125199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// from the given type- or value-dependent expression.
126199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor///
127199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// \returns true if deduction succeeded, false otherwise.
128f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
129a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceNonTypeTemplateArgument(Sema &S,
130f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                              NonTypeTemplateParmDecl *NTTP,
131f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                              Expr *Value,
132f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                              Sema::TemplateDeductionInfo &Info,
133f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                           llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
1341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  assert(NTTP->getDepth() == 0 &&
135199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor         "Cannot deduce non-type template argument with depth > 0");
136199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  assert((Value->isTypeDependent() || Value->isValueDependent()) &&
137199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor         "Expression template argument must be type- or value-dependent.");
1381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
139199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  if (Deduced[NTTP->getIndex()].isNull()) {
140199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    // FIXME: Clone the Value?
141199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    Deduced[NTTP->getIndex()] = TemplateArgument(Value);
142f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_Success;
143199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  }
1441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
145199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) {
1461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // Okay, we deduced a constant in one case and a dependent expression
1471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    // in another case. FIXME: Later, we will check that instantiating the
148199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    // dependent expression gives us the constant value.
149f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_Success;
150199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  }
1511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1529eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor  if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
1539eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor    // Compare the expressions for equality
1549eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor    llvm::FoldingSetNodeID ID1, ID2;
155a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth    Deduced[NTTP->getIndex()].getAsExpr()->Profile(ID1, S.Context, true);
156a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth    Value->Profile(ID2, S.Context, true);
1579eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor    if (ID1 == ID2)
1589eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor      return Sema::TDK_Success;
1599eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor
1609eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor    // FIXME: Fill in argument mismatch information
1619eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
1629eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor  }
1639eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor
164f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  return Sema::TDK_Success;
165199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor}
166199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
16715755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor/// \brief Deduce the value of the given non-type template parameter
16815755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor/// from the given declaration.
16915755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor///
17015755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor/// \returns true if deduction succeeded, false otherwise.
17115755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregorstatic Sema::TemplateDeductionResult
172a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceNonTypeTemplateArgument(Sema &S,
17315755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor                              NonTypeTemplateParmDecl *NTTP,
17415755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor                              Decl *D,
17515755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor                              Sema::TemplateDeductionInfo &Info,
17615755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor                              llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
17715755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor  assert(NTTP->getDepth() == 0 &&
17815755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor         "Cannot deduce non-type template argument with depth > 0");
17915755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor
18015755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor  if (Deduced[NTTP->getIndex()].isNull()) {
18115755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor    Deduced[NTTP->getIndex()] = TemplateArgument(D->getCanonicalDecl());
18215755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor    return Sema::TDK_Success;
18315755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor  }
18415755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor
18515755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor  if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) {
18615755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor    // Okay, we deduced a declaration in one case and a dependent expression
18715755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor    // in another case.
18815755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor    return Sema::TDK_Success;
18915755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor  }
19015755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor
19115755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor  if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Declaration) {
19215755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor    // Compare the declarations for equality
19315755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor    if (Deduced[NTTP->getIndex()].getAsDecl()->getCanonicalDecl() ==
19415755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor          D->getCanonicalDecl())
19515755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor      return Sema::TDK_Success;
19615755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor
19715755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor    // FIXME: Fill in argument mismatch information
19815755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
19915755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor  }
20015755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor
20115755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor  return Sema::TDK_Success;
20215755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor}
20315755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor
204f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
205a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
206db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor                        TemplateParameterList *TemplateParams,
207f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateName Param,
208f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateName Arg,
209f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        Sema::TemplateDeductionInfo &Info,
210f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
211d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor  TemplateDecl *ParamDecl = Param.getAsTemplateDecl();
212db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  if (!ParamDecl) {
213db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    // The parameter type is dependent and is not a template template parameter,
214db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    // so there is nothing that we can deduce.
215db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    return Sema::TDK_Success;
216f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  }
217db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor
218db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  if (TemplateTemplateParmDecl *TempParam
219db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor        = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) {
220db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    // Bind the template template parameter to the given template name.
221db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    TemplateArgument &ExistingArg = Deduced[TempParam->getIndex()];
222db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    if (ExistingArg.isNull()) {
223db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor      // This is the first deduction for this template template parameter.
224a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      ExistingArg = TemplateArgument(S.Context.getCanonicalTemplateName(Arg));
225db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor      return Sema::TDK_Success;
226db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    }
227db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor
228db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    // Verify that the previous binding matches this deduction.
229db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    assert(ExistingArg.getKind() == TemplateArgument::Template);
230a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth    if (S.Context.hasSameTemplateName(ExistingArg.getAsTemplate(), Arg))
231db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor      return Sema::TDK_Success;
232db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor
233db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    // Inconsistent deduction.
234db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    Info.Param = TempParam;
235db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    Info.FirstArg = ExistingArg;
236db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    Info.SecondArg = TemplateArgument(Arg);
237f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_Inconsistent;
238f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  }
239db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor
240db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  // Verify that the two template names are equivalent.
241a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  if (S.Context.hasSameTemplateName(Param, Arg))
242db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    return Sema::TDK_Success;
243db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor
244db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  // Mismatch of non-dependent template parameter to argument.
245db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Info.FirstArg = TemplateArgument(Param);
246db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Info.SecondArg = TemplateArgument(Arg);
247db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  return Sema::TDK_NonDeducedMismatch;
248d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor}
249d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor
2501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Deduce the template arguments by comparing the template parameter
251de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// type (which is a template-id) with the template argument type.
252de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
253a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth/// \param S the Sema
254de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
255de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param TemplateParams the template parameters that we are deducing
256de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
257de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Param the parameter type
258de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
259de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Arg the argument type
260de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
261de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Info information about the template argument deduction itself
262de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
263de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Deduced the deduced template arguments
264de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor///
265de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \returns the result of template argument deduction so far. Note that a
266de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// "success" result means that template argument deduction has not yet failed,
267de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// but it may still fail, later, for other reasons.
268de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregorstatic Sema::TemplateDeductionResult
269a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
270de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                        TemplateParameterList *TemplateParams,
271de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                        const TemplateSpecializationType *Param,
272de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                        QualType Arg,
273de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                        Sema::TemplateDeductionInfo &Info,
274de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                        llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
275467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall  assert(Arg.isCanonical() && "Argument type must be canonical");
2761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
277de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  // Check whether the template argument is a dependent template-id.
2781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const TemplateSpecializationType *SpecArg
279de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        = dyn_cast<TemplateSpecializationType>(Arg)) {
280de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    // Perform template argument deduction for the template name.
281de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    if (Sema::TemplateDeductionResult Result
282a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          = DeduceTemplateArguments(S, TemplateParams,
283de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                    Param->getTemplateName(),
284de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                    SpecArg->getTemplateName(),
285de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                    Info, Deduced))
286de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      return Result;
2871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
289de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    // Perform template argument deduction on each template
290de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    // argument.
291db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    unsigned NumArgs = std::min(SpecArg->getNumArgs(), Param->getNumArgs());
292de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    for (unsigned I = 0; I != NumArgs; ++I)
293de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      if (Sema::TemplateDeductionResult Result
294a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            = DeduceTemplateArguments(S, TemplateParams,
295de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                      Param->getArg(I),
296de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                      SpecArg->getArg(I),
297de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                      Info, Deduced))
298de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        return Result;
2991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
300de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    return Sema::TDK_Success;
301de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  }
3021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
303de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  // If the argument type is a class template specialization, we
304de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  // perform template argument deduction using its template
305de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  // arguments.
306de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  const RecordType *RecordArg = dyn_cast<RecordType>(Arg);
307de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  if (!RecordArg)
308de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
3091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
3101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  ClassTemplateSpecializationDecl *SpecArg
311de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl());
312de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  if (!SpecArg)
313de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
3141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
315de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  // Perform template argument deduction for the template name.
316de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  if (Sema::TemplateDeductionResult Result
317a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = DeduceTemplateArguments(S,
318db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor                                  TemplateParams,
319de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                  Param->getTemplateName(),
320de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                               TemplateName(SpecArg->getSpecializedTemplate()),
321de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                  Info, Deduced))
322de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    return Result;
3231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
324de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  unsigned NumArgs = Param->getNumArgs();
325de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs();
326de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  if (NumArgs != ArgArgs.size())
327de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
3281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
329de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  for (unsigned I = 0; I != NumArgs; ++I)
3301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (Sema::TemplateDeductionResult Result
331a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          = DeduceTemplateArguments(S, TemplateParams,
332de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                    Param->getArg(I),
333de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                    ArgArgs.get(I),
334de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                    Info, Deduced))
335de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      return Result;
3361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
337de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor  return Sema::TDK_Success;
338de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor}
339de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor
340500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \brief Deduce the template arguments by comparing the parameter type and
341500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// the argument type (C++ [temp.deduct.type]).
342500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
343a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth/// \param S the semantic analysis object within which we are deducing
344500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
345500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param TemplateParams the template parameters that we are deducing
346500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
347500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param ParamIn the parameter type
348500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
349500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param ArgIn the argument type
350500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
351500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param Info information about the template argument deduction itself
352500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
353500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param Deduced the deduced template arguments
354500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
355508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe
3561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// how template argument deduction is performed.
357500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor///
358500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \returns the result of template argument deduction so far. Note that a
359500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// "success" result means that template argument deduction has not yet failed,
360500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// but it may still fail, later, for other reasons.
361f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
362a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
363f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateParameterList *TemplateParams,
364f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        QualType ParamIn, QualType ArgIn,
365f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        Sema::TemplateDeductionInfo &Info,
366500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor                        llvm::SmallVectorImpl<TemplateArgument> &Deduced,
367508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                        unsigned TDF) {
3680b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  // We only want to look at the canonical types, since typedefs and
3690b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  // sugar are not part of template argument deduction.
370a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  QualType Param = S.Context.getCanonicalType(ParamIn);
371a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  QualType Arg = S.Context.getCanonicalType(ArgIn);
3720b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
373500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor  // C++0x [temp.deduct.call]p4 bullet 1:
374500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor  //   - If the original P is a reference type, the deduced A (i.e., the type
3751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //     referred to by the reference) can be more cv-qualified than the
376500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor  //     transformed A.
377508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  if (TDF & TDF_ParamWithReferenceType) {
378e724246b9f655801bd96b727daf9dddc44beef4dChandler Carruth    Qualifiers Quals;
379a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth    QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals);
380e724246b9f655801bd96b727daf9dddc44beef4dChandler Carruth    Quals.setCVRQualifiers(Quals.getCVRQualifiers() &
381e724246b9f655801bd96b727daf9dddc44beef4dChandler Carruth                           Arg.getCVRQualifiersThroughArrayTypes());
382a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth    Param = S.Context.getQualifiedType(UnqualParam, Quals);
383500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor  }
3841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
385f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  // If the parameter type is not dependent, there is nothing to deduce.
3861282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor  if (!Param->isDependentType()) {
3871282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor    if (!(TDF & TDF_SkipNonDependent) && Param != Arg) {
3881282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor
3891282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor      return Sema::TDK_NonDeducedMismatch;
3901282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor    }
3911282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor
392f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    return Sema::TDK_Success;
3931282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor  }
3940b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
395199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  // C++ [temp.deduct.type]p9:
3961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   A template type argument T, a template template argument TT or a
3971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   template non-type argument i can be deduced if P and A have one of
398199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  //   the following forms:
399199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  //
400199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  //     T
401199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  //     cv-list T
4021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const TemplateTypeParmType *TemplateTypeParm
403183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall        = Param->getAs<TemplateTypeParmType>()) {
404f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    unsigned Index = TemplateTypeParm->getIndex();
405f290e0db835a619014538c41ed552696efc0e977Douglas Gregor    bool RecanonicalizeArg = false;
4061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4079e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor    // If the argument type is an array type, move the qualifiers up to the
4089e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor    // top level, so they can be matched with the qualifiers on the parameter.
4099e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor    // FIXME: address spaces, ObjC GC qualifiers
410f290e0db835a619014538c41ed552696efc0e977Douglas Gregor    if (isa<ArrayType>(Arg)) {
4110953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      Qualifiers Quals;
412a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      Arg = S.Context.getUnqualifiedArrayType(Arg, Quals);
4130953e767ff7817f97b3ab20896b229891eeff45bJohn McCall      if (Quals) {
414a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        Arg = S.Context.getQualifiedType(Arg, Quals);
415f290e0db835a619014538c41ed552696efc0e977Douglas Gregor        RecanonicalizeArg = true;
416f290e0db835a619014538c41ed552696efc0e977Douglas Gregor      }
417f290e0db835a619014538c41ed552696efc0e977Douglas Gregor    }
4181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4190b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor    // The argument type can not be less qualified than the parameter
4200b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor    // type.
421508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) {
422f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
423f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.FirstArg = Deduced[Index];
424833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      Info.SecondArg = TemplateArgument(Arg);
425f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_InconsistentQuals;
426f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    }
4270b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
4280b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor    assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0");
429a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth    assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function");
4300953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    QualType DeducedType = Arg;
4310953e767ff7817f97b3ab20896b229891eeff45bJohn McCall    DeducedType.removeCVRQualifiers(Param.getCVRQualifiers());
432f290e0db835a619014538c41ed552696efc0e977Douglas Gregor    if (RecanonicalizeArg)
433a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      DeducedType = S.Context.getCanonicalType(DeducedType);
4341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4350b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor    if (Deduced[Index].isNull())
436833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      Deduced[Index] = TemplateArgument(DeducedType);
4370b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor    else {
4381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // C++ [temp.deduct.type]p2:
4390b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor      //   [...] If type deduction cannot be done for any P/A pair, or if for
4401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   any pair the deduction leads to more than one possible set of
4411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   deduced values, or if different pairs yield different deduced
4421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   values, or if any template argument remains neither deduced nor
4430b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor      //   explicitly specified, template argument deduction fails.
444f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (Deduced[Index].getAsType() != DeducedType) {
4451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        Info.Param
446f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor          = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index));
447f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        Info.FirstArg = Deduced[Index];
448833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall        Info.SecondArg = TemplateArgument(Arg);
449f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_Inconsistent;
450f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      }
4510b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor    }
452f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_Success;
4530b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
4540b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
455f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  // Set up the template argument deduction information for a failure.
456833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  Info.FirstArg = TemplateArgument(ParamIn);
457833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  Info.SecondArg = TemplateArgument(ArgIn);
458f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
459508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  // Check the cv-qualifiers on the parameter and argument types.
460508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  if (!(TDF & TDF_IgnoreQualifiers)) {
461508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    if (TDF & TDF_ParamWithReferenceType) {
462508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor      if (Param.isMoreQualifiedThan(Arg))
463508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
464508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    } else {
465508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor      if (Param.getCVRQualifiers() != Arg.getCVRQualifiers())
4661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        return Sema::TDK_NonDeducedMismatch;
467508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    }
468508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor  }
4690b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
470d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor  switch (Param->getTypeClass()) {
471199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    // No deduction possible for these types
472199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    case Type::Builtin:
473f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
4741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
475199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T *
476d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    case Type::Pointer: {
4776217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      const PointerType *PointerArg = Arg->getAs<PointerType>();
478d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor      if (!PointerArg)
479f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
4801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
4814112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor      unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass);
482a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
483d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                                   cast<PointerType>(Param)->getPointeeType(),
484d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                                     PointerArg->getPointeeType(),
4854112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor                                     Info, Deduced, SubTDF);
486d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    }
4871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
488199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T &
489d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    case Type::LValueReference: {
4906217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>();
491d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor      if (!ReferenceArg)
492f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
4931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
494a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
495d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                           cast<LValueReferenceType>(Param)->getPointeeType(),
496d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                                     ReferenceArg->getPointeeType(),
497508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
498d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    }
4990b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
500199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T && [C++0x]
501d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    case Type::RValueReference: {
5026217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek      const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>();
503d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor      if (!ReferenceArg)
504f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
5051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
506a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
507d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                           cast<RValueReferenceType>(Param)->getPointeeType(),
508d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor                                     ReferenceArg->getPointeeType(),
509508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
510d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    }
5111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
512199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T [] (implied, but not stated explicitly)
5134d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson    case Type::IncompleteArray: {
5141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const IncompleteArrayType *IncompleteArrayArg =
515a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        S.Context.getAsIncompleteArrayType(Arg);
5164d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson      if (!IncompleteArrayArg)
517f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
5181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
519a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
520a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth                     S.Context.getAsIncompleteArrayType(Param)->getElementType(),
5214d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson                                     IncompleteArrayArg->getElementType(),
522508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
5234d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson    }
524199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
525199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     T [integer-constant]
5264d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson    case Type::ConstantArray: {
5271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const ConstantArrayType *ConstantArrayArg =
528a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        S.Context.getAsConstantArrayType(Arg);
5294d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson      if (!ConstantArrayArg)
530f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
5311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const ConstantArrayType *ConstantArrayParm =
533a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        S.Context.getAsConstantArrayType(Param);
5344d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson      if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize())
535f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
5361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
537a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
5384d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson                                     ConstantArrayParm->getElementType(),
5394d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson                                     ConstantArrayArg->getElementType(),
540508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
5414d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson    }
5424d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson
543199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    //     type [i]
544199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    case Type::DependentSizedArray: {
545a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg);
546199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (!ArrayArg)
547f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
5481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
549199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // Check the element type of the arrays
550199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      const DependentSizedArrayType *DependentArrayParm
551a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = S.Context.getAsDependentSizedArrayType(Param);
552f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (Sema::TemplateDeductionResult Result
553a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            = DeduceTemplateArguments(S, TemplateParams,
554f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      DependentArrayParm->getElementType(),
555f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      ArrayArg->getElementType(),
556508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                      Info, Deduced, 0))
557f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Result;
5581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
559199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // Determine the array bound is something we can deduce.
5601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      NonTypeTemplateParmDecl *NTTP
561199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor        = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr());
562199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (!NTTP)
563f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_Success;
5641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // We can perform template argument deduction for the given non-type
566199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // template parameter.
5671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      assert(NTTP->getDepth() == 0 &&
568199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor             "Cannot deduce non-type template argument at depth > 0");
5691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (const ConstantArrayType *ConstantArrayArg
570335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson            = dyn_cast<ConstantArrayType>(ArrayArg)) {
571335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson        llvm::APSInt Size(ConstantArrayArg->getSize());
572a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP, Size,
573f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                             Info, Deduced);
574335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson      }
575199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (const DependentSizedArrayType *DependentArrayArg
576199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor            = dyn_cast<DependentSizedArrayType>(ArrayArg))
577a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP,
578199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor                                             DependentArrayArg->getSizeExpr(),
579f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                             Info, Deduced);
5801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
581199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // Incomplete type does not match a dependently-sized array type
582f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
583199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    }
5841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     type(*)(T)
5861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(*)()
5871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(*)(T)
588a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson    case Type::FunctionProto: {
5891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const FunctionProtoType *FunctionProtoArg =
590a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson        dyn_cast<FunctionProtoType>(Arg);
591a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson      if (!FunctionProtoArg)
592f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
5931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      const FunctionProtoType *FunctionProtoParam =
595a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson        cast<FunctionProtoType>(Param);
596994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson
5971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (FunctionProtoParam->getTypeQuals() !=
598994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson          FunctionProtoArg->getTypeQuals())
599f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
6001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
601994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson      if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs())
602f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
6031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
604994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson      if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic())
605f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
606994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson
607a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson      // Check return types.
608f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (Sema::TemplateDeductionResult Result
609a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            = DeduceTemplateArguments(S, TemplateParams,
610f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      FunctionProtoParam->getResultType(),
611f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      FunctionProtoArg->getResultType(),
612508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                      Info, Deduced, 0))
613f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Result;
6141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
615a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson      for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) {
616a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson        // Check argument types.
617f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        if (Sema::TemplateDeductionResult Result
618a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth              = DeduceTemplateArguments(S, TemplateParams,
619f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                        FunctionProtoParam->getArgType(I),
620f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                        FunctionProtoArg->getArgType(I),
621508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                        Info, Deduced, 0))
622f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor          return Result;
623a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson      }
6241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
625f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_Success;
626a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson    }
6271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6283cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    case Type::InjectedClassName: {
6293cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // Treat a template's injected-class-name as if the template
6303cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // specialization type had been used.
6313cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      Param = cast<InjectedClassNameType>(Param)->getUnderlyingType();
6323cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      assert(isa<TemplateSpecializationType>(Param) &&
6333cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall             "injected class name is not a template specialization type");
6343cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall      // fall through
6353cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall    }
6363cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall
637f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    //     template-name<T> (where template-name refers to a class template)
638d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor    //     template-name<i>
639db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    //     TT<T>
640db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    //     TT<i>
641db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    //     TT<>
642d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor    case Type::TemplateSpecialization: {
643d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor      const TemplateSpecializationType *SpecParam
644d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor        = cast<TemplateSpecializationType>(Param);
6451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
646de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      // Try to deduce template arguments from the template-id.
647de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      Sema::TemplateDeductionResult Result
648a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg,
649de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                  Info, Deduced);
6501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6514a5c15f75f76b95e1c2ceb6fa2737dcadd5f4be1Douglas Gregor      if (Result && (TDF & TDF_DerivedClass)) {
652de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        // C++ [temp.deduct.call]p3b3:
653de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   If P is a class, and P has the form template-id, then A can be a
654de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   derived class of the deduced A. Likewise, if P is a pointer to a
6551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        //   class of the form template-id, A can be a pointer to a derived
656de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   class pointed to by the deduced A.
657de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //
658de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        // More importantly:
6591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        //   These alternatives are considered only if type deduction would
660de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   otherwise fail.
661a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        if (const RecordType *RecordT = Arg->getAs<RecordType>()) {
662a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          // We cannot inspect base classes as part of deduction when the type
663a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          // is incomplete, so either instantiate any templates necessary to
664a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          // complete the type, or skip over it if it cannot be completed.
6655769d6195087229770d7ac90449443e026c47103John McCall          if (S.RequireCompleteType(Info.getLocation(), Arg, 0))
666a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            return Result;
667a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth
668de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          // Use data recursion to crawl through the list of base classes.
6691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          // Visited contains the set of nodes we have already visited, while
670de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          // ToVisit is our stack of records that we still need to visit.
671de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          llvm::SmallPtrSet<const RecordType *, 8> Visited;
672de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          llvm::SmallVector<const RecordType *, 8> ToVisit;
673de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          ToVisit.push_back(RecordT);
674de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          bool Successful = false;
675de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          while (!ToVisit.empty()) {
676de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // Retrieve the next class in the inheritance hierarchy.
677de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            const RecordType *NextT = ToVisit.back();
678de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            ToVisit.pop_back();
6791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
680de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // If we have already seen this type, skip it.
681de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            if (!Visited.insert(NextT))
682de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              continue;
6831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
684de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // If this is a base class, try to perform template argument
685de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // deduction from it.
686de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            if (NextT != RecordT) {
687de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              Sema::TemplateDeductionResult BaseResult
688a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth                = DeduceTemplateArguments(S, TemplateParams, SpecParam,
689de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                          QualType(NextT, 0), Info, Deduced);
6901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
691de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              // If template argument deduction for this base was successful,
692de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              // note that we had some success.
693de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              if (BaseResult == Sema::TDK_Success)
694de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                Successful = true;
695de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            }
6961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
697de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // Visit base classes
698de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
699de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
700de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                                 BaseEnd = Next->bases_end();
7019994a34f6cf842721ba7723edc0b9036229fe387Sebastian Redl                 Base != BaseEnd; ++Base) {
7021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump              assert(Base->getType()->isRecordType() &&
703de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                     "Base class that isn't a record?");
7046217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek              ToVisit.push_back(Base->getType()->getAs<RecordType>());
705de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            }
706de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          }
7071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
708de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          if (Successful)
709de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            return Sema::TDK_Success;
710de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        }
7111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
712de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      }
7131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
714de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      return Result;
715d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor    }
716d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor
717637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T type::*
718637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T T::*
719637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (type::*)()
720637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     type (T::*)()
721637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     type (type::*)(T)
722637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     type (T::*)(T)
723637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (type::*)(T)
724637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (T::*)()
725637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (T::*)(T)
726637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    case Type::MemberPointer: {
727637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
728637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
729637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      if (!MemPtrArg)
730f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
731f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
732f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (Sema::TemplateDeductionResult Result
733a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            = DeduceTemplateArguments(S, TemplateParams,
734f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      MemPtrParam->getPointeeType(),
735f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      MemPtrArg->getPointeeType(),
736508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                      Info, Deduced,
737508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                      TDF & TDF_IgnoreQualifiers))
738f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Result;
739f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
740a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
741f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                     QualType(MemPtrParam->getClass(), 0),
742f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                     QualType(MemPtrArg->getClass(), 0),
743508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
744637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    }
745637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor
7469a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    //     (clang extension)
7479a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    //
7481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     type(^)(T)
7491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(^)()
7501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(^)(T)
751859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson    case Type::BlockPointer: {
752859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson      const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
753859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson      const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
7541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
755859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson      if (!BlockPtrArg)
756f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
7571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
758a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
759859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson                                     BlockPtrParam->getPointeeType(),
760f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                     BlockPtrArg->getPointeeType(), Info,
761508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Deduced, 0);
762859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson    }
763859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson
764637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    case Type::TypeOfExpr:
765637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    case Type::TypeOf:
766637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    case Type::Typename:
767637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      // No template argument deduction for these types
768f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_Success;
769637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor
770d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    default:
771d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor      break;
7720b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
7730b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
7740b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  // FIXME: Many more cases to go (to go).
775f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  return Sema::TDK_Success;
7760b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
7770b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
778f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
779a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
780f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateParameterList *TemplateParams,
781f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        const TemplateArgument &Param,
7820b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor                        const TemplateArgument &Arg,
783f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        Sema::TemplateDeductionInfo &Info,
7840b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor                        llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
7850b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  switch (Param.getKind()) {
786199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Null:
787199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    assert(false && "Null template argument in parameter list");
788199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    break;
7891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case TemplateArgument::Type:
791788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    if (Arg.getKind() == TemplateArgument::Type)
792a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams, Param.getAsType(),
793788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                                     Arg.getAsType(), Info, Deduced, 0);
794788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.FirstArg = Param;
795788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.SecondArg = Arg;
796788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
797788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor
798788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  case TemplateArgument::Template:
799db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    if (Arg.getKind() == TemplateArgument::Template)
800a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
801788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                                     Param.getAsTemplate(),
802db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor                                     Arg.getAsTemplate(), Info, Deduced);
803788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.FirstArg = Param;
804788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.SecondArg = Arg;
805788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
806788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor
807199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Declaration:
808788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    if (Arg.getKind() == TemplateArgument::Declaration &&
809788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor        Param.getAsDecl()->getCanonicalDecl() ==
810788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor          Arg.getAsDecl()->getCanonicalDecl())
811788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return Sema::TDK_Success;
812788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor
813f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.FirstArg = Param;
814f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.SecondArg = Arg;
815f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_NonDeducedMismatch;
8161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
817199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Integral:
818199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    if (Arg.getKind() == TemplateArgument::Integral) {
819199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // FIXME: Zero extension + sign checking here?
820f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (*Param.getAsIntegral() == *Arg.getAsIntegral())
821f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_Success;
822f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
823f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.FirstArg = Param;
824f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.SecondArg = Arg;
825f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
826f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    }
827f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
828f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    if (Arg.getKind() == TemplateArgument::Expression) {
829f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.FirstArg = Param;
830f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.SecondArg = Arg;
831f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
832199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    }
833199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
834199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    assert(false && "Type/value mismatch");
835f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.FirstArg = Param;
836f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.SecondArg = Arg;
837f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_NonDeducedMismatch;
8381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
839199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Expression: {
8401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (NonTypeTemplateParmDecl *NTTP
841199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor          = getDeducedParameterFromExpr(Param.getAsExpr())) {
842199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (Arg.getKind() == TemplateArgument::Integral)
843199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor        // FIXME: Sign problems here
844a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP,
8451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             *Arg.getAsIntegral(),
846f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                             Info, Deduced);
847199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (Arg.getKind() == TemplateArgument::Expression)
848a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(),
849f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                             Info, Deduced);
85015755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor      if (Arg.getKind() == TemplateArgument::Declaration)
851a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(),
85215755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor                                             Info, Deduced);
85315755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor
854199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      assert(false && "Type/value mismatch");
855f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.FirstArg = Param;
856f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.SecondArg = Arg;
857f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
858199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    }
8591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
860199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    // Can't deduce anything, but that's okay.
861f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_Success;
862199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  }
863d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson  case TemplateArgument::Pack:
864d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson    assert(0 && "FIXME: Implement!");
865d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson    break;
8660b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
8671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
868f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  return Sema::TDK_Success;
8690b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
8700b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
8711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic Sema::TemplateDeductionResult
872a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
873f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateParameterList *TemplateParams,
8740b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor                        const TemplateArgumentList &ParamList,
8750b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor                        const TemplateArgumentList &ArgList,
876f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        Sema::TemplateDeductionInfo &Info,
8770b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor                        llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
8780b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  assert(ParamList.size() == ArgList.size());
8790b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
880f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    if (Sema::TemplateDeductionResult Result
881a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          = DeduceTemplateArguments(S, TemplateParams,
8821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                    ParamList[I], ArgList[I],
883f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                    Info, Deduced))
884f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Result;
8850b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
886f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  return Sema::TDK_Success;
8870b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
8880b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
889f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// \brief Determine whether two template arguments are the same.
8901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool isSameTemplateArg(ASTContext &Context,
891f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor                              const TemplateArgument &X,
892f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor                              const TemplateArgument &Y) {
893f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  if (X.getKind() != Y.getKind())
894f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    return false;
8951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
896f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  switch (X.getKind()) {
897f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Null:
898f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      assert(false && "Comparing NULL template argument");
899f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      break;
9001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
901f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Type:
902f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      return Context.getCanonicalType(X.getAsType()) ==
903f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor             Context.getCanonicalType(Y.getAsType());
9041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
905f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Declaration:
90697fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis      return X.getAsDecl()->getCanonicalDecl() ==
90797fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis             Y.getAsDecl()->getCanonicalDecl();
9081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
909788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Template:
910788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return Context.getCanonicalTemplateName(X.getAsTemplate())
911788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor               .getAsVoidPointer() ==
912788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor             Context.getCanonicalTemplateName(Y.getAsTemplate())
913788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor               .getAsVoidPointer();
914788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor
915f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Integral:
916f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      return *X.getAsIntegral() == *Y.getAsIntegral();
9171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
918788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Expression: {
919788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      llvm::FoldingSetNodeID XID, YID;
920788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      X.getAsExpr()->Profile(XID, Context, true);
921788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      Y.getAsExpr()->Profile(YID, Context, true);
922788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return XID == YID;
923788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    }
9241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
925f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Pack:
926f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      if (X.pack_size() != Y.pack_size())
927f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor        return false;
9281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (TemplateArgument::pack_iterator XP = X.pack_begin(),
9301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        XPEnd = X.pack_end(),
931f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor                                           YP = Y.pack_begin();
9321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump           XP != XPEnd; ++XP, ++YP)
933f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor        if (!isSameTemplateArg(Context, *XP, *YP))
934f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor          return false;
935f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
936f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      return true;
937f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  }
938f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
939f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  return false;
940f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor}
941f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
942f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// \brief Helper function to build a TemplateParameter when we don't
943f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// know its type statically.
944f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregorstatic TemplateParameter makeTemplateParameter(Decl *D) {
945f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
946f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    return TemplateParameter(TTP);
947f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
948f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    return TemplateParameter(NTTP);
9491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
950f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
951f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor}
952f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
953c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor/// \brief Perform template argument deduction to determine whether
954c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor/// the given template arguments match the given class template
955c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor/// partial specialization per C++ [temp.class.spec.match].
956f67875d5addf36b951ad37fb04509ab2b572c88aDouglas GregorSema::TemplateDeductionResult
9570b9247f129401b4849682b2e2bcdea8fc977068aDouglas GregorSema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
958f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                              const TemplateArgumentList &TemplateArgs,
959f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                              TemplateDeductionInfo &Info) {
960c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor  // C++ [temp.class.spec.match]p2:
961c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor  //   A partial specialization matches a given actual template
962c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor  //   argument list if the template arguments of the partial
963c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor  //   specialization can be deduced from the actual template argument
964c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor  //   list (14.8.2).
965bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor  SFINAETrap Trap(*this);
9660b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  llvm::SmallVector<TemplateArgument, 4> Deduced;
9670b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  Deduced.resize(Partial->getTemplateParameters()->size());
968f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  if (TemplateDeductionResult Result
969a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = ::DeduceTemplateArguments(*this,
970f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                    Partial->getTemplateParameters(),
9711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                    Partial->getTemplateArgs(),
972f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                    TemplateArgs, Info, Deduced))
973f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Result;
974637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor
975637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor  InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
976637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor                             Deduced.data(), Deduced.size());
977637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor  if (Inst)
978f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return TDK_InstantiationDepth;
979199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
98002cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // C++ [temp.deduct.type]p2:
98102cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  //   [...] or if any template argument remains neither deduced nor
98202cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  //   explicitly specified, template argument deduction fails.
983fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson  TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
984fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson                                      Deduced.size());
98502cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
986f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    if (Deduced[I].isNull()) {
9871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Decl *Param
988bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor        = const_cast<NamedDecl *>(
989bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                Partial->getTemplateParameters()->getParam(I));
990f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
991f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        Info.Param = TTP;
9921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      else if (NonTypeTemplateParmDecl *NTTP
993f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                 = dyn_cast<NonTypeTemplateParmDecl>(Param))
994f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        Info.Param = NTTP;
995f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      else
996f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        Info.Param = cast<TemplateTemplateParmDecl>(Param);
997f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return TDK_Incomplete;
998f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    }
99902cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor
1000fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson    Builder.Append(Deduced[I]);
100102cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  }
100202cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor
100302cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // Form the template argument list from the deduced template arguments.
10041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentList *DeducedArgumentList
1005fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson    = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
1006f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  Info.reset(DeducedArgumentList);
100702cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor
100802cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // Substitute the deduced template arguments into the template
100902cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // arguments of the class template partial specialization, and
101002cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // verify that the instantiated template arguments are both valid
101102cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // and are equivalent to the template arguments originally provided
10121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to the class template.
101302cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
1014833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  const TemplateArgumentLoc *PartialTemplateArgs
1015833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    = Partial->getTemplateArgsAsWritten();
1016833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  unsigned N = Partial->getNumTemplateArgsAsWritten();
1017d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
1018d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  // Note that we don't provide the langle and rangle locations.
1019d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstArgs;
1020d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
1021833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned I = 0; I != N; ++I) {
1022bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    Decl *Param = const_cast<NamedDecl *>(
1023c9e5d25ea33616c896a2ce5fbd6d68186eaddc5cDouglas Gregor                    ClassTemplate->getTemplateParameters()->getParam(I));
1024d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    TemplateArgumentLoc InstArg;
1025d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    if (Subst(PartialTemplateArgs[I], InstArg,
1026833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall              MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
1027f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Info.Param = makeTemplateParameter(Param);
1028833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      Info.FirstArg = PartialTemplateArgs[I].getArgument();
10291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return TDK_SubstitutionFailure;
1030f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    }
1031d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    InstArgs.addArgument(InstArg);
1032833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  }
1033833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1034833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  TemplateArgumentListBuilder ConvertedInstArgs(
1035833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall                                  ClassTemplate->getTemplateParameters(), N);
1036833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1037833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  if (CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
1038d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                InstArgs, false, ConvertedInstArgs)) {
1039833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    // FIXME: fail with more useful information?
1040833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    return TDK_SubstitutionFailure;
1041833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  }
1042833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1043833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned I = 0, E = ConvertedInstArgs.flatSize(); I != E; ++I) {
1044d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    TemplateArgument InstArg = ConvertedInstArgs.getFlatArguments()[I];
1045833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1046833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    Decl *Param = const_cast<NamedDecl *>(
1047833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall                    ClassTemplate->getTemplateParameters()->getParam(I));
10481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1049f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    if (InstArg.getKind() == TemplateArgument::Expression) {
10501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // When the argument is an expression, check the expression result
1051f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      // against the actual template parameter to get down to the canonical
1052f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      // template argument.
1053f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Expr *InstExpr = InstArg.getAsExpr();
10541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (NonTypeTemplateParmDecl *NTTP
1055f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor            = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1056f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor        if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
1057f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor          Info.Param = makeTemplateParameter(Param);
1058833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall          Info.FirstArg = Partial->getTemplateArgs()[I];
10591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          return TDK_SubstitutionFailure;
1060f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor        }
1061f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      }
106202cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor    }
10631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1064f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) {
1065f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Info.Param = makeTemplateParameter(Param);
1066f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Info.FirstArg = TemplateArgs[I];
1067f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Info.SecondArg = InstArg;
1068f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      return TDK_NonDeducedMismatch;
1069f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    }
107002cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  }
107102cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor
1072bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor  if (Trap.hasErrorOccurred())
1073bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor    return TDK_SubstitutionFailure;
1074bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor
1075f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  return TDK_Success;
10760b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
1077031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
10784112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor/// \brief Determine whether the given type T is a simple-template-id type.
10794112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregorstatic bool isSimpleTemplateIdType(QualType T) {
10801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const TemplateSpecializationType *Spec
1081183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall        = T->getAs<TemplateSpecializationType>())
10824112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    return Spec->getTemplateName().getAsTemplateDecl() != 0;
10831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10844112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor  return false;
10854112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor}
108683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
108783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \brief Substitute the explicitly-provided template arguments into the
108883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// given function template according to C++ [temp.arg.explicit].
108983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
109083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionTemplate the function template into which the explicit
109183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template arguments will be substituted.
109283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
10931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ExplicitTemplateArguments the explicitly-specified template
109483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// arguments.
109583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
10961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param Deduced the deduced template arguments, which will be populated
109783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// with the converted and checked explicit template arguments.
109883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
10991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ParamTypes will be populated with the instantiated function
110083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// parameters.
110183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
110283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionType if non-NULL, the result type of the function template
110383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// will also be instantiated and the pointed-to value will be updated with
110483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// the instantiated function type.
110583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
110683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Info if substitution fails for any reason, this object will be
110783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// populated with more information about the failure.
110883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
110983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \returns TDK_Success if substitution was successful, or some failure
111083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// condition.
111183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::TemplateDeductionResult
111283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::SubstituteExplicitTemplateArguments(
111383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      FunctionTemplateDecl *FunctionTemplate,
1114d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                        const TemplateArgumentListInfo &ExplicitTemplateArgs,
111583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                            llvm::SmallVectorImpl<TemplateArgument> &Deduced,
111683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                 llvm::SmallVectorImpl<QualType> &ParamTypes,
111783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          QualType *FunctionType,
111883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          TemplateDeductionInfo &Info) {
111983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
112083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateParameterList *TemplateParams
112183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = FunctionTemplate->getTemplateParameters();
112283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
1123d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (ExplicitTemplateArgs.size() == 0) {
112483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    // No arguments to substitute; just copy over the parameter types and
112583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    // fill in the function type.
112683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    for (FunctionDecl::param_iterator P = Function->param_begin(),
112783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                   PEnd = Function->param_end();
112883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor         P != PEnd;
112983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor         ++P)
113083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      ParamTypes.push_back((*P)->getType());
11311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
113283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (FunctionType)
113383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      *FunctionType = Function->getType();
113483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_Success;
113583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
11361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
113783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Substitution of the explicit template arguments into a function template
113883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  /// is a SFINAE context. Trap any errors that might occur.
11391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SFINAETrap Trap(*this);
11401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
114183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // C++ [temp.arg.explicit]p3:
11421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   Template arguments that are present shall be specified in the
11431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   declaration order of their corresponding template-parameters. The
114483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   template argument list shall not specify more template-arguments than
11451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   there are corresponding template-parameters.
11461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentListBuilder Builder(TemplateParams,
1147d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                      ExplicitTemplateArgs.size());
11481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Enter a new template instantiation context where we check the
115083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // explicitly-specified template arguments against this function template,
115183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // and then substitute them into the function parameter types.
11521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
115383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                             FunctionTemplate, Deduced.data(), Deduced.size(),
115483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor           ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution);
115583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (Inst)
115683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_InstantiationDepth;
11571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
115883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (CheckTemplateArgumentList(FunctionTemplate,
115983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                SourceLocation(),
1160d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                ExplicitTemplateArgs,
116183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                true,
116283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                Builder) || Trap.hasErrorOccurred())
116383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_InvalidExplicitArguments;
11641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
116583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Form the template argument list from the explicitly-specified
116683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // template arguments.
11671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentList *ExplicitArgumentList
116883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
116983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Info.reset(ExplicitArgumentList);
11701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
117183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Instantiate the types of each of the function parameters given the
117283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // explicitly-specified template arguments.
117383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  for (FunctionDecl::param_iterator P = Function->param_begin(),
117483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                PEnd = Function->param_end();
117583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor       P != PEnd;
117683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor       ++P) {
11771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    QualType ParamType
11781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      = SubstType((*P)->getType(),
1179357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1180357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  (*P)->getLocation(), (*P)->getDeclName());
118183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (ParamType.isNull() || Trap.hasErrorOccurred())
118283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_SubstitutionFailure;
11831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
118483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    ParamTypes.push_back(ParamType);
118583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
118683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
118783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // If the caller wants a full function type back, instantiate the return
118883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // type and form that function type.
118983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (FunctionType) {
119083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    // FIXME: exception-specifications?
11911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const FunctionProtoType *Proto
1192183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall      = Function->getType()->getAs<FunctionProtoType>();
119383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    assert(Proto && "Function template does not have a prototype?");
11941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    QualType ResultType
1196357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor      = SubstType(Proto->getResultType(),
1197357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1198357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  Function->getTypeSpecStartLoc(),
1199357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  Function->getDeclName());
120083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (ResultType.isNull() || Trap.hasErrorOccurred())
120183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_SubstitutionFailure;
12021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    *FunctionType = BuildFunctionType(ResultType,
120483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      ParamTypes.data(), ParamTypes.size(),
120583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      Proto->isVariadic(),
120683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      Proto->getTypeQuals(),
120783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      Function->getLocation(),
120883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      Function->getDeclName());
120983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (FunctionType->isNull() || Trap.hasErrorOccurred())
121083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_SubstitutionFailure;
121183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
12121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
121383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // C++ [temp.arg.explicit]p2:
12141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   Trailing template arguments that can be deduced (14.8.2) may be
12151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   omitted from the list of explicit template-arguments. If all of the
121683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   template arguments can be deduced, they may all be omitted; in this
121783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   case, the empty template argument list <> itself may also be omitted.
121883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //
121983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Take all of the explicitly-specified arguments and put them into the
12201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // set of deduced template arguments.
122183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Deduced.reserve(TemplateParams->size());
122283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I)
12231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Deduced.push_back(ExplicitArgumentList->get(I));
12241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
122583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  return TDK_Success;
122683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor}
122783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
12281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Finish template argument deduction for a function template,
122983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// checking the deduced template arguments for completeness and forming
123083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// the function template specialization.
12311eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSema::TemplateDeductionResult
123283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
123383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                            llvm::SmallVectorImpl<TemplateArgument> &Deduced,
123483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      FunctionDecl *&Specialization,
123583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      TemplateDeductionInfo &Info) {
123683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateParameterList *TemplateParams
123783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = FunctionTemplate->getTemplateParameters();
12381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
123951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // Template argument deduction for function templates in a SFINAE context.
124051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // Trap any errors that might occur.
124151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  SFINAETrap Trap(*this);
124251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
124351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // Enter a new template instantiation context while we instantiate the
124451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // actual function declaration.
124551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
124651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                             FunctionTemplate, Deduced.data(), Deduced.size(),
124751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor              ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution);
124851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  if (Inst)
124951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    return TDK_InstantiationDepth;
125051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
125183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // C++ [temp.deduct.type]p2:
125283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   [...] or if any template argument remains neither deduced nor
125383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   explicitly specified, template argument deduction fails.
125483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
125583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
125651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    if (!Deduced[I].isNull()) {
125751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      Builder.Append(Deduced[I]);
125851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      continue;
125951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    }
126051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
126151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // Substitute into the default template argument, if available.
126251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    NamedDecl *Param = FunctionTemplate->getTemplateParameters()->getParam(I);
126351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    TemplateArgumentLoc DefArg
126451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
126551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                              FunctionTemplate->getLocation(),
126651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                  FunctionTemplate->getSourceRange().getEnd(),
126751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                                Param,
126851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                                Builder);
126951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
127051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // If there was no default argument, deduction is incomplete.
127151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    if (DefArg.getArgument().isNull()) {
127283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      Info.Param = makeTemplateParameter(
127351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                         const_cast<NamedDecl *>(TemplateParams->getParam(I)));
127483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_Incomplete;
127583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    }
127651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
127751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // Check whether we can actually use the default argument.
127851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    if (CheckTemplateArgument(Param, DefArg,
127951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                              FunctionTemplate,
128051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                              FunctionTemplate->getLocation(),
128151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                              FunctionTemplate->getSourceRange().getEnd(),
128251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                              Builder)) {
128351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      Info.Param = makeTemplateParameter(
128451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                         const_cast<NamedDecl *>(TemplateParams->getParam(I)));
128551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      return TDK_SubstitutionFailure;
128651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    }
12871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
128851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // If we get here, we successfully used the default template argument.
128983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
12901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
129183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Form the template argument list from the deduced template arguments.
12921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentList *DeducedArgumentList
129383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
129483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Info.reset(DeducedArgumentList);
12951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Substitute the deduced template arguments into the function template
129783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // declaration to produce the function template specialization.
129883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Specialization = cast_or_null<FunctionDecl>(
1299ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                      SubstDecl(FunctionTemplate->getTemplatedDecl(),
1300ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                                FunctionTemplate->getDeclContext(),
1301357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                         MultiLevelTemplateArgumentList(*DeducedArgumentList)));
130283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (!Specialization)
130383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_SubstitutionFailure;
13041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1305f882574cf640d5c8355965e1c486f9d8d8ffcf47Douglas Gregor  assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
1306f882574cf640d5c8355965e1c486f9d8d8ffcf47Douglas Gregor         FunctionTemplate->getCanonicalDecl());
1307f882574cf640d5c8355965e1c486f9d8d8ffcf47Douglas Gregor
13081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If the template argument list is owned by the function template
130983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // specialization, release it.
131083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList)
131183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    Info.take();
13121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
131383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // There may have been an error that did not prevent us from constructing a
131483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // declaration. Mark the declaration invalid and return with a substitution
131583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // failure.
131683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (Trap.hasErrorOccurred()) {
131783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    Specialization->setInvalidDecl(true);
131883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_SubstitutionFailure;
131983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
13201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return TDK_Success;
132283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor}
132383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
1324eff92135d32039c9874dc356f3e93143af6069c1John McCallstatic QualType GetTypeOfFunction(ASTContext &Context,
1325eff92135d32039c9874dc356f3e93143af6069c1John McCall                                  bool isAddressOfOperand,
1326eff92135d32039c9874dc356f3e93143af6069c1John McCall                                  FunctionDecl *Fn) {
1327eff92135d32039c9874dc356f3e93143af6069c1John McCall  if (!isAddressOfOperand) return Fn->getType();
1328eff92135d32039c9874dc356f3e93143af6069c1John McCall  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
1329eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (Method->isInstance())
1330eff92135d32039c9874dc356f3e93143af6069c1John McCall      return Context.getMemberPointerType(Fn->getType(),
1331eff92135d32039c9874dc356f3e93143af6069c1John McCall               Context.getTypeDeclType(Method->getParent()).getTypePtr());
1332eff92135d32039c9874dc356f3e93143af6069c1John McCall  return Context.getPointerType(Fn->getType());
1333eff92135d32039c9874dc356f3e93143af6069c1John McCall}
1334eff92135d32039c9874dc356f3e93143af6069c1John McCall
1335eff92135d32039c9874dc356f3e93143af6069c1John McCall/// Apply the deduction rules for overload sets.
1336eff92135d32039c9874dc356f3e93143af6069c1John McCall///
1337eff92135d32039c9874dc356f3e93143af6069c1John McCall/// \return the null type if this argument should be treated as an
1338eff92135d32039c9874dc356f3e93143af6069c1John McCall/// undeduced context
1339eff92135d32039c9874dc356f3e93143af6069c1John McCallstatic QualType
1340eff92135d32039c9874dc356f3e93143af6069c1John McCallResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
1341eff92135d32039c9874dc356f3e93143af6069c1John McCall                            Expr *Arg, QualType ParamType) {
13427bb12da2b0749eeebb21854c77877736969e59f2John McCall  llvm::PointerIntPair<OverloadExpr*,1> R = OverloadExpr::find(Arg);
1343eff92135d32039c9874dc356f3e93143af6069c1John McCall
13447bb12da2b0749eeebb21854c77877736969e59f2John McCall  bool isAddressOfOperand = bool(R.getInt());
13457bb12da2b0749eeebb21854c77877736969e59f2John McCall  OverloadExpr *Ovl = R.getPointer();
1346eff92135d32039c9874dc356f3e93143af6069c1John McCall
1347eff92135d32039c9874dc356f3e93143af6069c1John McCall  // If there were explicit template arguments, we can only find
1348eff92135d32039c9874dc356f3e93143af6069c1John McCall  // something via C++ [temp.arg.explicit]p3, i.e. if the arguments
1349eff92135d32039c9874dc356f3e93143af6069c1John McCall  // unambiguously name a full specialization.
13507bb12da2b0749eeebb21854c77877736969e59f2John McCall  if (Ovl->hasExplicitTemplateArgs()) {
1351eff92135d32039c9874dc356f3e93143af6069c1John McCall    // But we can still look for an explicit specialization.
1352eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (FunctionDecl *ExplicitSpec
13537bb12da2b0749eeebb21854c77877736969e59f2John McCall          = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
13547bb12da2b0749eeebb21854c77877736969e59f2John McCall      return GetTypeOfFunction(S.Context, isAddressOfOperand, ExplicitSpec);
1355eff92135d32039c9874dc356f3e93143af6069c1John McCall    return QualType();
1356eff92135d32039c9874dc356f3e93143af6069c1John McCall  }
1357eff92135d32039c9874dc356f3e93143af6069c1John McCall
1358eff92135d32039c9874dc356f3e93143af6069c1John McCall  // C++0x [temp.deduct.call]p6:
1359eff92135d32039c9874dc356f3e93143af6069c1John McCall  //   When P is a function type, pointer to function type, or pointer
1360eff92135d32039c9874dc356f3e93143af6069c1John McCall  //   to member function type:
1361eff92135d32039c9874dc356f3e93143af6069c1John McCall
1362eff92135d32039c9874dc356f3e93143af6069c1John McCall  if (!ParamType->isFunctionType() &&
1363eff92135d32039c9874dc356f3e93143af6069c1John McCall      !ParamType->isFunctionPointerType() &&
1364eff92135d32039c9874dc356f3e93143af6069c1John McCall      !ParamType->isMemberFunctionPointerType())
1365eff92135d32039c9874dc356f3e93143af6069c1John McCall    return QualType();
1366eff92135d32039c9874dc356f3e93143af6069c1John McCall
1367eff92135d32039c9874dc356f3e93143af6069c1John McCall  QualType Match;
13687bb12da2b0749eeebb21854c77877736969e59f2John McCall  for (UnresolvedSetIterator I = Ovl->decls_begin(),
13697bb12da2b0749eeebb21854c77877736969e59f2John McCall         E = Ovl->decls_end(); I != E; ++I) {
1370eff92135d32039c9874dc356f3e93143af6069c1John McCall    NamedDecl *D = (*I)->getUnderlyingDecl();
1371eff92135d32039c9874dc356f3e93143af6069c1John McCall
1372eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   - If the argument is an overload set containing one or more
1373eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     function templates, the parameter is treated as a
1374eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     non-deduced context.
1375eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (isa<FunctionTemplateDecl>(D))
1376eff92135d32039c9874dc356f3e93143af6069c1John McCall      return QualType();
1377eff92135d32039c9874dc356f3e93143af6069c1John McCall
1378eff92135d32039c9874dc356f3e93143af6069c1John McCall    FunctionDecl *Fn = cast<FunctionDecl>(D);
1379eff92135d32039c9874dc356f3e93143af6069c1John McCall    QualType ArgType = GetTypeOfFunction(S.Context, isAddressOfOperand, Fn);
1380eff92135d32039c9874dc356f3e93143af6069c1John McCall
1381eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   - If the argument is an overload set (not containing function
1382eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     templates), trial argument deduction is attempted using each
1383eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     of the members of the set. If deduction succeeds for only one
1384eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     of the overload set members, that member is used as the
1385eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     argument value for the deduction. If deduction succeeds for
1386eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     more than one member of the overload set the parameter is
1387eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     treated as a non-deduced context.
1388eff92135d32039c9874dc356f3e93143af6069c1John McCall
1389eff92135d32039c9874dc356f3e93143af6069c1John McCall    // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
1390eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   Type deduction is done independently for each P/A pair, and
1391eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   the deduced template argument values are then combined.
1392eff92135d32039c9874dc356f3e93143af6069c1John McCall    // So we do not reject deductions which were made elsewhere.
1393eff92135d32039c9874dc356f3e93143af6069c1John McCall    llvm::SmallVector<TemplateArgument, 8> Deduced(TemplateParams->size());
13945769d6195087229770d7ac90449443e026c47103John McCall    Sema::TemplateDeductionInfo Info(S.Context, Ovl->getNameLoc());
1395eff92135d32039c9874dc356f3e93143af6069c1John McCall    unsigned TDF = 0;
1396eff92135d32039c9874dc356f3e93143af6069c1John McCall
1397eff92135d32039c9874dc356f3e93143af6069c1John McCall    Sema::TemplateDeductionResult Result
1398a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      = DeduceTemplateArguments(S, TemplateParams,
1399eff92135d32039c9874dc356f3e93143af6069c1John McCall                                ParamType, ArgType,
1400eff92135d32039c9874dc356f3e93143af6069c1John McCall                                Info, Deduced, TDF);
1401eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (Result) continue;
1402eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (!Match.isNull()) return QualType();
1403eff92135d32039c9874dc356f3e93143af6069c1John McCall    Match = ArgType;
1404eff92135d32039c9874dc356f3e93143af6069c1John McCall  }
1405eff92135d32039c9874dc356f3e93143af6069c1John McCall
1406eff92135d32039c9874dc356f3e93143af6069c1John McCall  return Match;
1407eff92135d32039c9874dc356f3e93143af6069c1John McCall}
1408eff92135d32039c9874dc356f3e93143af6069c1John McCall
1409e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \brief Perform template argument deduction from a function call
1410e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// (C++ [temp.deduct.call]).
1411e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1412e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param FunctionTemplate the function template for which we are performing
1413e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// template argument deduction.
1414e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
141548026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// \param ExplicitTemplateArguments the explicit template arguments provided
141648026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// for this call.
14176db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor///
1418e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Args the function call arguments
1419e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1420e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param NumArgs the number of arguments in Args
1421e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
142248026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// \param Name the name of the function being called. This is only significant
142348026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// when the function template is a conversion function template, in which
142448026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// case this routine will also perform template argument deduction based on
142548026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// the function to which
142648026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor///
1427e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Specialization if template argument deduction was successful,
14281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// this will be set to the function template specialization produced by
1429e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// template argument deduction.
1430e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1431e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Info the argument will be updated to provide additional information
1432e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// about template argument deduction.
1433e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1434e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns the result of template argument deduction.
1435e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorSema::TemplateDeductionResult
1436e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
143748026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor                          const TemplateArgumentListInfo *ExplicitTemplateArgs,
1438e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                              Expr **Args, unsigned NumArgs,
1439e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                              FunctionDecl *&Specialization,
1440e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                              TemplateDeductionInfo &Info) {
1441e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
14426db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor
1443e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // C++ [temp.deduct.call]p1:
1444e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  //   Template argument deduction is done by comparing each function template
1445e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  //   parameter type (call it P) with the type of the corresponding argument
1446e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  //   of the call (call it A) as described below.
1447e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  unsigned CheckArgs = NumArgs;
14486db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  if (NumArgs < Function->getMinRequiredArguments())
1449e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return TDK_TooFewArguments;
1450e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  else if (NumArgs > Function->getNumParams()) {
14511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const FunctionProtoType *Proto
1452183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall      = Function->getType()->getAs<FunctionProtoType>();
1453e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (!Proto->isVariadic())
1454e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return TDK_TooManyArguments;
14551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1456e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    CheckArgs = Function->getNumParams();
1457e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  }
14581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14596db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  // The types of the parameters from which we will perform template argument
14606db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  // deduction.
1461e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  TemplateParameterList *TemplateParams
1462e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    = FunctionTemplate->getTemplateParameters();
14636db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  llvm::SmallVector<TemplateArgument, 4> Deduced;
14646db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  llvm::SmallVector<QualType, 4> ParamTypes;
1465d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (ExplicitTemplateArgs) {
146683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    TemplateDeductionResult Result =
146783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      SubstituteExplicitTemplateArguments(FunctionTemplate,
1468d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                          *ExplicitTemplateArgs,
146983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          Deduced,
147083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          ParamTypes,
147183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          0,
147283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          Info);
147383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (Result)
147483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return Result;
14756db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  } else {
14766db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor    // Just fill in the parameter types from the function declaration.
14776db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor    for (unsigned I = 0; I != CheckArgs; ++I)
14786db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor      ParamTypes.push_back(Function->getParamDecl(I)->getType());
14796db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  }
14801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14816db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  // Deduce template arguments from the function parameters.
14821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Deduced.resize(TemplateParams->size());
1483e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  for (unsigned I = 0; I != CheckArgs; ++I) {
14846db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor    QualType ParamType = ParamTypes[I];
1485e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    QualType ArgType = Args[I]->getType();
14861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1487eff92135d32039c9874dc356f3e93143af6069c1John McCall    // Overload sets usually make this parameter an undeduced
1488eff92135d32039c9874dc356f3e93143af6069c1John McCall    // context, but there are sometimes special circumstances.
1489eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (ArgType == Context.OverloadTy) {
1490eff92135d32039c9874dc356f3e93143af6069c1John McCall      ArgType = ResolveOverloadForDeduction(*this, TemplateParams,
1491eff92135d32039c9874dc356f3e93143af6069c1John McCall                                            Args[I], ParamType);
1492eff92135d32039c9874dc356f3e93143af6069c1John McCall      if (ArgType.isNull())
1493eff92135d32039c9874dc356f3e93143af6069c1John McCall        continue;
1494eff92135d32039c9874dc356f3e93143af6069c1John McCall    }
1495eff92135d32039c9874dc356f3e93143af6069c1John McCall
1496e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    // C++ [temp.deduct.call]p2:
1497e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    //   If P is not a reference type:
1498e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    QualType CanonParamType = Context.getCanonicalType(ParamType);
1499500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor    bool ParamWasReference = isa<ReferenceType>(CanonParamType);
1500500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor    if (!ParamWasReference) {
15011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   - If A is an array type, the pointer type produced by the
15021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //     array-to-pointer standard conversion (4.2) is used in place of
1503e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      //     A for type deduction; otherwise,
1504e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      if (ArgType->isArrayType())
1505e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        ArgType = Context.getArrayDecayedType(ArgType);
15061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   - If A is a function type, the pointer type produced by the
15071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //     function-to-pointer standard conversion (4.3) is used in place
1508e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      //     of A for type deduction; otherwise,
1509e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      else if (ArgType->isFunctionType())
1510e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        ArgType = Context.getPointerType(ArgType);
1511e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      else {
1512e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
1513e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        //   type are ignored for type deduction.
1514e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        QualType CanonArgType = Context.getCanonicalType(ArgType);
1515a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor        if (CanonArgType.getLocalCVRQualifiers())
1516a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor          ArgType = CanonArgType.getLocalUnqualifiedType();
1517e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      }
1518e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    }
15191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1520e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    // C++0x [temp.deduct.call]p3:
1521e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    //   If P is a cv-qualified type, the top level cv-qualifiers of P’s type
15221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //   are ignored for type deduction.
1523a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor    if (CanonParamType.getLocalCVRQualifiers())
1524a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor      ParamType = CanonParamType.getLocalUnqualifiedType();
15256217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
15261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   [...] If P is a reference type, the type referred to by P is used
15271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   for type deduction.
1528e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      ParamType = ParamRefType->getPointeeType();
15291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   [...] If P is of the form T&&, where T is a template parameter, and
15311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   the argument is an lvalue, the type A& is used in place of A for
1532e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      //   type deduction.
1533e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      if (isa<RValueReferenceType>(ParamRefType) &&
1534183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall          ParamRefType->getAs<TemplateTypeParmType>() &&
1535e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor          Args[I]->isLvalue(Context) == Expr::LV_Valid)
1536e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        ArgType = Context.getLValueReferenceType(ArgType);
1537e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    }
15381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1539e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    // C++0x [temp.deduct.call]p4:
1540e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    //   In general, the deduction process attempts to find template argument
1541e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    //   values that will make the deduced A identical to A (after the type A
1542e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    //   is transformed as described above). [...]
15431282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor    unsigned TDF = TDF_SkipNonDependent;
15441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1545508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    //     - If the original P is a reference type, the deduced A (i.e., the
1546508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    //       type referred to by the reference) can be more cv-qualified than
1547508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    //       the transformed A.
1548508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    if (ParamWasReference)
1549508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor      TDF |= TDF_ParamWithReferenceType;
15501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     - The transformed A can be another pointer or pointer to member
15511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //       type that can be converted to the deduced A via a qualification
1552508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    //       conversion (4.4).
1553508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    if (ArgType->isPointerType() || ArgType->isMemberPointerType())
1554508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor      TDF |= TDF_IgnoreQualifiers;
15551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     - If P is a class and P has the form simple-template-id, then the
15564112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    //       transformed A can be a derived class of the deduced A. Likewise,
15574112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    //       if P is a pointer to a class of the form simple-template-id, the
15584112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    //       transformed A can be a pointer to a derived class pointed to by
15594112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    //       the deduced A.
15604112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    if (isSimpleTemplateIdType(ParamType) ||
15611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        (isa<PointerType>(ParamType) &&
15624112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor         isSimpleTemplateIdType(
15636217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek                              ParamType->getAs<PointerType>()->getPointeeType())))
15644112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor      TDF |= TDF_DerivedClass;
15651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1566e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (TemplateDeductionResult Result
1567a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = ::DeduceTemplateArguments(*this, TemplateParams,
1568500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor                                    ParamType, ArgType, Info, Deduced,
1569508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                    TDF))
1570e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return Result;
15711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
157265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    // FIXME: we need to check that the deduced A is the same as A,
157365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    // modulo the various allowed differences.
1574e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  }
157565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
15761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
157783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                         Specialization, Info);
157883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor}
1579127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor
158083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \brief Deduce template arguments when taking the address of a function
15814b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
15824b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// a template.
158383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
158483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionTemplate the function template for which we are performing
158583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template argument deduction.
158683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
15874b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param ExplicitTemplateArguments the explicitly-specified template
15884b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// arguments.
158983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
159083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param ArgFunctionType the function type that will be used as the
159183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// "argument" type (A) when performing template argument deduction from the
15924b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// function template's function type. This type may be NULL, if there is no
15934b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
159483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
159583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Specialization if template argument deduction was successful,
15961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// this will be set to the function template specialization produced by
159783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template argument deduction.
159883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
159983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Info the argument will be updated to provide additional information
160083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// about template argument deduction.
160183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
160283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \returns the result of template argument deduction.
160383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::TemplateDeductionResult
160483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1605d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                        const TemplateArgumentListInfo *ExplicitTemplateArgs,
160683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                              QualType ArgFunctionType,
160783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                              FunctionDecl *&Specialization,
160883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                              TemplateDeductionInfo &Info) {
160983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
161083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateParameterList *TemplateParams
161183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = FunctionTemplate->getTemplateParameters();
161283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  QualType FunctionType = Function->getType();
16131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
161483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Substitute any explicit template arguments.
161583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  llvm::SmallVector<TemplateArgument, 4> Deduced;
161683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  llvm::SmallVector<QualType, 4> ParamTypes;
1617d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (ExplicitTemplateArgs) {
16181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (TemplateDeductionResult Result
16191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          = SubstituteExplicitTemplateArguments(FunctionTemplate,
1620d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                *ExplicitTemplateArgs,
16211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                Deduced, ParamTypes,
162283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                                &FunctionType, Info))
162383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return Result;
1624127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
162583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
162683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Template argument deduction for function templates in a SFINAE context.
162783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Trap any errors that might occur.
16281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SFINAETrap Trap(*this);
16291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1630eff92135d32039c9874dc356f3e93143af6069c1John McCall  Deduced.resize(TemplateParams->size());
1631eff92135d32039c9874dc356f3e93143af6069c1John McCall
16324b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  if (!ArgFunctionType.isNull()) {
16334b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    // Deduce template arguments from the function type.
16344b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (TemplateDeductionResult Result
1635a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          = ::DeduceTemplateArguments(*this, TemplateParams,
16364b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                                      FunctionType, ArgFunctionType, Info,
16374b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                                      Deduced, 0))
16384b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      return Result;
16394b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  }
16404b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor
16411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
164283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                         Specialization, Info);
1643e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
1644e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
164565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \brief Deduce template arguments for a templated conversion
164665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// function (C++ [temp.deduct.conv]) and, if successful, produce a
164765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// conversion function template specialization.
164865ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::TemplateDeductionResult
164965ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
165065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                              QualType ToType,
165165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                              CXXConversionDecl *&Specialization,
165265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                              TemplateDeductionInfo &Info) {
16531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  CXXConversionDecl *Conv
165465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
165565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  QualType FromType = Conv->getConversionType();
165665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
165765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Canonicalize the types for deduction.
165865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  QualType P = Context.getCanonicalType(FromType);
165965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  QualType A = Context.getCanonicalType(ToType);
166065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
166165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++0x [temp.deduct.conv]p3:
166265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   If P is a reference type, the type referred to by P is used for
166365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   type deduction.
166465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (const ReferenceType *PRef = P->getAs<ReferenceType>())
166565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    P = PRef->getPointeeType();
166665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
166765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++0x [temp.deduct.conv]p3:
166865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   If A is a reference type, the type referred to by A is used
166965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   for type deduction.
167065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (const ReferenceType *ARef = A->getAs<ReferenceType>())
167165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    A = ARef->getPointeeType();
167265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++ [temp.deduct.conv]p2:
167365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //
16741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   If A is not a reference type:
167565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  else {
167665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    assert(!A->isReferenceType() && "Reference types were handled above");
167765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
167865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   - If P is an array type, the pointer type produced by the
16791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     array-to-pointer standard conversion (4.2) is used in place
168065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     of P for type deduction; otherwise,
168165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    if (P->isArrayType())
168265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      P = Context.getArrayDecayedType(P);
168365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   - If P is a function type, the pointer type produced by the
168465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     function-to-pointer standard conversion (4.3) is used in
168565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     place of P for type deduction; otherwise,
168665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    else if (P->isFunctionType())
168765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      P = Context.getPointerType(P);
168865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   - If P is a cv-qualified type, the top level cv-qualifiers of
168965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     P’s type are ignored for type deduction.
169065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    else
169165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      P = P.getUnqualifiedType();
169265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
169365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    // C++0x [temp.deduct.conv]p3:
169465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   If A is a cv-qualified type, the top level cv-qualifiers of A’s
169565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   type are ignored for type deduction.
169665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    A = A.getUnqualifiedType();
169765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  }
169865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
169965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Template argument deduction for function templates in a SFINAE context.
170065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Trap any errors that might occur.
17011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SFINAETrap Trap(*this);
170265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
170365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++ [temp.deduct.conv]p1:
170465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   Template argument deduction is done by comparing the return
170565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   type of the template conversion function (call it P) with the
170665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   type that is required as the result of the conversion (call it
170765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   A) as described in 14.8.2.4.
170865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  TemplateParameterList *TemplateParams
170965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    = FunctionTemplate->getTemplateParameters();
171065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  llvm::SmallVector<TemplateArgument, 4> Deduced;
17111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Deduced.resize(TemplateParams->size());
171265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
171365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++0x [temp.deduct.conv]p4:
171465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   In general, the deduction process attempts to find template
171565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   argument values that will make the deduced A identical to
171665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   A. However, there are two cases that allow a difference:
171765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  unsigned TDF = 0;
171865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //     - If the original A is a reference type, A can be more
171965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       cv-qualified than the deduced A (i.e., the type referred to
172065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       by the reference)
172165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (ToType->isReferenceType())
172265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    TDF |= TDF_ParamWithReferenceType;
172365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //     - The deduced A can be another pointer or pointer to member
172465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       type that can be converted to A via a qualification
172565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       conversion.
172665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //
172765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
172865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // both P and A are pointers or member pointers. In this case, we
172965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // just ignore cv-qualifiers completely).
173065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if ((P->isPointerType() && A->isPointerType()) ||
173165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      (P->isMemberPointerType() && P->isMemberPointerType()))
173265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    TDF |= TDF_IgnoreQualifiers;
173365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (TemplateDeductionResult Result
1734a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = ::DeduceTemplateArguments(*this, TemplateParams,
173565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                    P, A, Info, Deduced, TDF))
173665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    return Result;
173765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
173865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // FIXME: we need to check that the deduced A is the same as A,
173965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // modulo the various allowed differences.
17401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
174165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Finish template argument deduction.
174265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  FunctionDecl *Spec = 0;
174365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  TemplateDeductionResult Result
174465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, Spec, Info);
174565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  Specialization = cast_or_null<CXXConversionDecl>(Spec);
174665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return Result;
174765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor}
174865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
17494b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \brief Deduce template arguments for a function template when there is
17504b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
17514b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
17524b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param FunctionTemplate the function template for which we are performing
17534b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template argument deduction.
17544b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
17554b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param ExplicitTemplateArguments the explicitly-specified template
17564b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// arguments.
17574b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
17584b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param Specialization if template argument deduction was successful,
17594b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// this will be set to the function template specialization produced by
17604b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template argument deduction.
17614b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
17624b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param Info the argument will be updated to provide additional information
17634b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// about template argument deduction.
17644b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
17654b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \returns the result of template argument deduction.
17664b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas GregorSema::TemplateDeductionResult
17674b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
17684b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                           const TemplateArgumentListInfo *ExplicitTemplateArgs,
17694b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                              FunctionDecl *&Specialization,
17704b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                              TemplateDeductionInfo &Info) {
17714b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
17724b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                                 QualType(), Specialization, Info);
17734b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor}
17744b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor
17758a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Stores the result of comparing the qualifiers of two types.
17768a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorenum DeductionQualifierComparison {
17778a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  NeitherMoreQualified = 0,
17788a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  ParamMoreQualified,
17798a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  ArgMoreQualified
17808a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor};
17818a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
17828a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Deduce the template arguments during partial ordering by comparing
17838a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// the parameter type and the argument type (C++0x [temp.deduct.partial]).
17848a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
1785a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth/// \param S the semantic analysis object within which we are deducing
17868a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
17878a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param TemplateParams the template parameters that we are deducing
17888a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
17898a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param ParamIn the parameter type
17908a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
17918a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param ArgIn the argument type
17928a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
17938a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param Info information about the template argument deduction itself
17948a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
17958a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param Deduced the deduced template arguments
17968a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
17978a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \returns the result of template argument deduction so far. Note that a
17988a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// "success" result means that template argument deduction has not yet failed,
17998a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// but it may still fail, later, for other reasons.
18008a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic Sema::TemplateDeductionResult
1801a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArgumentsDuringPartialOrdering(Sema &S,
18028a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                          TemplateParameterList *TemplateParams,
18038a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                             QualType ParamIn, QualType ArgIn,
18048a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                             Sema::TemplateDeductionInfo &Info,
18058a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                             llvm::SmallVectorImpl<TemplateArgument> &Deduced,
18068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
1807a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  CanQualType Param = S.Context.getCanonicalType(ParamIn);
1808a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  CanQualType Arg = S.Context.getCanonicalType(ArgIn);
18098a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
18108a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p5:
18118a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   Before the partial ordering is done, certain transformations are
18128a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   performed on the types used for partial ordering:
18138a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //     - If P is a reference type, P is replaced by the type referred to.
18148a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>();
1815e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  if (!ParamRef.isNull())
18168a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    Param = ParamRef->getPointeeType();
18178a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
18188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //     - If A is a reference type, A is replaced by the type referred to.
18198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>();
1820e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  if (!ArgRef.isNull())
18218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    Arg = ArgRef->getPointeeType();
18228a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
1823e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) {
18248a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // C++0x [temp.deduct.partial]p6:
18258a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   If both P and A were reference types (before being replaced with the
18268a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   type referred to above), determine which of the two types (if any) is
18278a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   more cv-qualified than the other; otherwise the types are considered to
18288a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   be equally cv-qualified for partial ordering purposes. The result of this
18298a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   determination will be used below.
18308a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //
18318a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // We save this information for later, using it only when deduction
18328a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // succeeds in both directions.
18338a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    DeductionQualifierComparison QualifierResult = NeitherMoreQualified;
18348a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    if (Param.isMoreQualifiedThan(Arg))
18358a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      QualifierResult = ParamMoreQualified;
18368a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    else if (Arg.isMoreQualifiedThan(Param))
18378a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      QualifierResult = ArgMoreQualified;
18388a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    QualifierComparisons->push_back(QualifierResult);
18398a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
18408a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
18418a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p7:
18428a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   Remove any top-level cv-qualifiers:
18438a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //     - If P is a cv-qualified type, P is replaced by the cv-unqualified
18448a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //       version of P.
18458a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Param = Param.getUnqualifiedType();
18468a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //     - If A is a cv-qualified type, A is replaced by the cv-unqualified
18478a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //       version of A.
18488a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Arg = Arg.getUnqualifiedType();
18498a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
18508a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p8:
18518a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   Using the resulting types P and A the deduction is then done as
18528a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   described in 14.9.2.5. If deduction succeeds for a given type, the type
18538a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   from the argument template is considered to be at least as specialized
18548a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   as the type from the parameter template.
1855a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  return DeduceTemplateArguments(S, TemplateParams, Param, Arg, Info,
18568a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                 Deduced, TDF_None);
18578a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor}
18588a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
18598a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic void
1860e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, QualType T,
1861e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
1862ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Level,
1863e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Deduced);
18648a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
18658a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Determine whether the function template \p FT1 is at least as
18668a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// specialized as \p FT2.
18678a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic bool isAtLeastAsSpecializedAs(Sema &S,
18685769d6195087229770d7ac90449443e026c47103John McCall                                     SourceLocation Loc,
18698a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                     FunctionTemplateDecl *FT1,
18708a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                     FunctionTemplateDecl *FT2,
18718a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                     TemplatePartialOrderingContext TPOC,
18728a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
18738a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  FunctionDecl *FD1 = FT1->getTemplatedDecl();
18748a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  FunctionDecl *FD2 = FT2->getTemplatedDecl();
18758a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
18768a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
18778a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
18788a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  assert(Proto1 && Proto2 && "Function templates must have prototypes");
18798a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
18808a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  llvm::SmallVector<TemplateArgument, 4> Deduced;
18818a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Deduced.resize(TemplateParams->size());
18828a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
18838a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p3:
18848a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   The types used to determine the ordering depend on the context in which
18858a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   the partial ordering is done:
18865769d6195087229770d7ac90449443e026c47103John McCall  Sema::TemplateDeductionInfo Info(S.Context, Loc);
18878a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  switch (TPOC) {
18888a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Call: {
18898a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   - In the context of a function call, the function parameter types are
18908a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //     used.
18918a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
18928a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    for (unsigned I = 0; I != NumParams; ++I)
1893a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      if (DeduceTemplateArgumentsDuringPartialOrdering(S,
18948a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                       TemplateParams,
18958a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                       Proto2->getArgType(I),
18968a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                       Proto1->getArgType(I),
18978a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                       Info,
18988a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                       Deduced,
18998a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                       QualifierComparisons))
19008a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        return false;
19018a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19028a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
19038a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
19048a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19058a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Conversion:
19068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   - In the context of a call to a conversion operator, the return types
19078a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //     of the conversion function templates are used.
1908a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth    if (DeduceTemplateArgumentsDuringPartialOrdering(S,
19098a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     TemplateParams,
19108a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     Proto2->getResultType(),
19118a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     Proto1->getResultType(),
19128a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     Info,
19138a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     Deduced,
19148a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     QualifierComparisons))
19158a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      return false;
19168a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
19178a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Other:
19198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   - In other contexts (14.6.6.2) the function template’s function type
19208a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //     is used.
1921a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth    if (DeduceTemplateArgumentsDuringPartialOrdering(S,
19228a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     TemplateParams,
19238a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     FD2->getType(),
19248a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     FD1->getType(),
19258a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     Info,
19268a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     Deduced,
19278a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     QualifierComparisons))
19288a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      return false;
19298a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
19308a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
19318a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19328a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p11:
19338a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   In most cases, all template parameters must have values in order for
19348a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   deduction to succeed, but for partial ordering purposes a template
19358a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   parameter may remain without a value provided it is not used in the
19368a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   types being used for partial ordering. [ Note: a template parameter used
19378a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   in a non-deduced context is considered used. -end note]
19388a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  unsigned ArgIdx = 0, NumArgs = Deduced.size();
19398a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  for (; ArgIdx != NumArgs; ++ArgIdx)
19408a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    if (Deduced[ArgIdx].isNull())
19418a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      break;
19428a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19438a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  if (ArgIdx == NumArgs) {
19448a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // All template arguments were deduced. FT1 is at least as specialized
19458a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // as FT2.
19468a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return true;
19478a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
19488a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
1949e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // Figure out which template parameters were used.
19508a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  llvm::SmallVector<bool, 4> UsedParameters;
19518a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  UsedParameters.resize(TemplateParams->size());
19528a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  switch (TPOC) {
19538a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Call: {
19548a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
19558a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    for (unsigned I = 0; I != NumParams; ++I)
1956ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
1957ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                   TemplateParams->getDepth(),
1958e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                   UsedParameters);
19598a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
19608a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
19618a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19628a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Conversion:
1963ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
1964ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 TemplateParams->getDepth(),
1965e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                 UsedParameters);
19668a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
19678a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19688a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Other:
1969ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    ::MarkUsedTemplateParameters(S, FD2->getType(), false,
1970ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 TemplateParams->getDepth(),
1971ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 UsedParameters);
19728a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
19738a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
19748a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19758a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  for (; ArgIdx != NumArgs; ++ArgIdx)
19768a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // If this argument had no value deduced but was used in one of the types
19778a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // used for partial ordering, then deduction fails.
19788a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
19798a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      return false;
19808a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19818a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  return true;
19828a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor}
19838a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19848a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
1985bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \brief Returns the more specialized function template according
198665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// to the rules of function template partial ordering (C++ [temp.func.order]).
198765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor///
198865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \param FT1 the first function template
198965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor///
199065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \param FT2 the second function template
199165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor///
19928a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param TPOC the context in which we are performing partial ordering of
19938a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// function templates.
19941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
1995bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \returns the more specialized function template. If neither
199665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// template is more specialized, returns NULL.
199765ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorFunctionTemplateDecl *
199865ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
199965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                 FunctionTemplateDecl *FT2,
20005769d6195087229770d7ac90449443e026c47103John McCall                                 SourceLocation Loc,
20018a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                 TemplatePartialOrderingContext TPOC) {
20028a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons;
20035769d6195087229770d7ac90449443e026c47103John McCall  bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, 0);
20045769d6195087229770d7ac90449443e026c47103John McCall  bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC,
20058a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                          &QualifierComparisons);
20068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20078a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  if (Better1 != Better2) // We have a clear winner
20088a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return Better1? FT1 : FT2;
20098a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20108a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  if (!Better1 && !Better2) // Neither is better than the other
201165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    return 0;
20128a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20138a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20148a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p10:
20158a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   If for each type being considered a given template is at least as
20168a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   specialized for all types and more specialized for some set of types and
20178a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   the other template is not more specialized for any types or is not at
20188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   least as specialized for any types, then the given template is more
20198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   specialized than the other template. Otherwise, neither template is more
20208a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   specialized than the other.
20218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Better1 = false;
20228a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Better2 = false;
20238a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) {
20248a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // C++0x [temp.deduct.partial]p9:
20258a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   If, for a given type, deduction succeeds in both directions (i.e., the
20268a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   types are identical after the transformations above) and if the type
20278a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   from the argument template is more cv-qualified than the type from the
20288a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   parameter template (as described above) that type is considered to be
20298a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   more specialized than the other. If neither type is more cv-qualified
20308a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   than the other then neither type is more specialized than the other.
20318a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    switch (QualifierComparisons[I]) {
20328a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      case NeitherMoreQualified:
20338a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        break;
20348a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20358a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      case ParamMoreQualified:
20368a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        Better1 = true;
20378a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        if (Better2)
20388a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor          return 0;
20398a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        break;
20408a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20418a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      case ArgMoreQualified:
20428a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        Better2 = true;
20438a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        if (Better1)
20448a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor          return 0;
20458a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        break;
20468a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    }
20478a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
20488a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20498a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  assert(!(Better1 && Better2) && "Should have broken out in the loop above");
205065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (Better1)
205165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    return FT1;
20528a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  else if (Better2)
20538a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return FT2;
20548a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  else
20558a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return 0;
205665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor}
205783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
2058d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \brief Determine if the two templates are equivalent.
2059d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregorstatic bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
2060d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  if (T1 == T2)
2061d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    return true;
2062d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2063d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  if (!T1 || !T2)
2064d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    return false;
2065d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2066d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  return T1->getCanonicalDecl() == T2->getCanonicalDecl();
2067d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor}
2068d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2069d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \brief Retrieve the most specialized of the given function template
2070d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specializations.
2071d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2072c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// \param SpecBegin the start iterator of the function template
2073c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// specializations that we will be comparing.
2074d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2075c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// \param SpecEnd the end iterator of the function template
2076c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// specializations, paired with \p SpecBegin.
2077d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2078d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param TPOC the partial ordering context to use to compare the function
2079d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// template specializations.
2080d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2081d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param Loc the location where the ambiguity or no-specializations
2082d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// diagnostic should occur.
2083d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2084d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param NoneDiag partial diagnostic used to diagnose cases where there are
2085d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// no matching candidates.
2086d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2087d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
2088d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// occurs.
2089d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2090d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param CandidateDiag partial diagnostic used for each function template
2091d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specialization that is a candidate in the ambiguous ordering. One parameter
2092d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// in this diagnostic should be unbound, which will correspond to the string
2093d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// describing the template arguments for the function template specialization.
2094d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2095d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param Index if non-NULL and the result of this function is non-nULL,
2096d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// receives the index corresponding to the resulting function template
2097d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specialization.
2098d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2099d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \returns the most specialized function template specialization, if
2100c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// found. Otherwise, returns SpecEnd.
2101d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2102d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
2103d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// template argument deduction.
2104c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallUnresolvedSetIterator
2105c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallSema::getMostSpecialized(UnresolvedSetIterator SpecBegin,
2106c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         UnresolvedSetIterator SpecEnd,
2107c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         TemplatePartialOrderingContext TPOC,
2108c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         SourceLocation Loc,
2109c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         const PartialDiagnostic &NoneDiag,
2110c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         const PartialDiagnostic &AmbigDiag,
2111c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         const PartialDiagnostic &CandidateDiag) {
2112c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  if (SpecBegin == SpecEnd) {
2113d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    Diag(Loc, NoneDiag);
2114c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    return SpecEnd;
2115d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
2116d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2117c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  if (SpecBegin + 1 == SpecEnd)
2118c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    return SpecBegin;
2119d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2120d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // Find the function template that is better than all of the templates it
2121d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // has been compared to.
2122c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  UnresolvedSetIterator Best = SpecBegin;
2123d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  FunctionTemplateDecl *BestTemplate
2124c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
2125d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  assert(BestTemplate && "Not a function template specialization?");
2126c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
2127c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    FunctionTemplateDecl *Challenger
2128c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall      = cast<FunctionDecl>(*I)->getPrimaryTemplate();
2129d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    assert(Challenger && "Not a function template specialization?");
2130c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
21315769d6195087229770d7ac90449443e026c47103John McCall                                                  Loc, TPOC),
2132d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor                       Challenger)) {
2133d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      Best = I;
2134d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      BestTemplate = Challenger;
2135d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    }
2136d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
2137d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2138d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // Make sure that the "best" function template is more specialized than all
2139d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // of the others.
2140d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  bool Ambiguous = false;
2141c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
2142c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    FunctionTemplateDecl *Challenger
2143c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall      = cast<FunctionDecl>(*I)->getPrimaryTemplate();
2144d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    if (I != Best &&
2145d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor        !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
21465769d6195087229770d7ac90449443e026c47103John McCall                                                   Loc, TPOC),
2147d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor                        BestTemplate)) {
2148d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      Ambiguous = true;
2149d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      break;
2150d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    }
2151d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
2152d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2153d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  if (!Ambiguous) {
2154d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    // We found an answer. Return it.
2155c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    return Best;
2156d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
2157d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2158d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // Diagnose the ambiguity.
2159d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  Diag(Loc, AmbigDiag);
2160d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2161d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // FIXME: Can we order the candidates in some sane way?
2162c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I)
2163c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    Diag((*I)->getLocation(), CandidateDiag)
2164d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      << getTemplateArgumentBindingsText(
2165c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall        cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
2166c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                    *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
2167d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2168c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  return SpecEnd;
2169d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor}
2170d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2171bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \brief Returns the more specialized class template partial specialization
2172bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// according to the rules of partial ordering of class template partial
2173bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// specializations (C++ [temp.class.order]).
2174bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor///
2175bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \param PS1 the first class template partial specialization
2176bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor///
2177bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \param PS2 the second class template partial specialization
2178bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor///
2179bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \returns the more specialized class template partial specialization. If
2180bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// neither partial specialization is more specialized, returns NULL.
2181bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas GregorClassTemplatePartialSpecializationDecl *
2182bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas GregorSema::getMoreSpecializedPartialSpecialization(
2183bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                  ClassTemplatePartialSpecializationDecl *PS1,
21845769d6195087229770d7ac90449443e026c47103John McCall                                  ClassTemplatePartialSpecializationDecl *PS2,
21855769d6195087229770d7ac90449443e026c47103John McCall                                              SourceLocation Loc) {
2186bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // C++ [temp.class.order]p1:
2187bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   For two class template partial specializations, the first is at least as
2188bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   specialized as the second if, given the following rewrite to two
2189bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   function templates, the first function template is at least as
2190bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   specialized as the second according to the ordering rules for function
2191bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   templates (14.6.6.2):
2192bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //     - the first function template has the same template parameters as the
2193bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       first partial specialization and has a single function parameter
2194bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       whose type is a class template specialization with the template
2195bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       arguments of the first partial specialization, and
2196bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //     - the second function template has the same template parameters as the
2197bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       second partial specialization and has a single function parameter
2198bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       whose type is a class template specialization with the template
2199bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       arguments of the second partial specialization.
2200bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //
2201bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // Rather than synthesize function templates, we merely perform the
2202bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // equivalent partial ordering by performing deduction directly on the
2203bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // template arguments of the class template partial specializations. This
2204bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // computation is slightly simpler than the general problem of function
2205bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // template partial ordering, because class template partial specializations
2206bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // are more constrained. We know that every template parameter is deduc
2207bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  llvm::SmallVector<TemplateArgument, 4> Deduced;
22085769d6195087229770d7ac90449443e026c47103John McCall  Sema::TemplateDeductionInfo Info(Context, Loc);
2209bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor
2210bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // Determine whether PS1 is at least as specialized as PS2
2211bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  Deduced.resize(PS2->getTemplateParameters()->size());
2212a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
2213bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                  PS2->getTemplateParameters(),
2214bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                  Context.getTypeDeclType(PS2),
2215bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                  Context.getTypeDeclType(PS1),
2216bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                               Info,
2217bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                               Deduced,
2218bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                               0);
2219bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor
2220bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // Determine whether PS2 is at least as specialized as PS1
2221db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Deduced.clear();
2222bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  Deduced.resize(PS1->getTemplateParameters()->size());
2223a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
2224bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                  PS1->getTemplateParameters(),
2225bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                  Context.getTypeDeclType(PS1),
2226bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                  Context.getTypeDeclType(PS2),
2227bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                               Info,
2228bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                               Deduced,
2229bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                               0);
2230bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor
2231bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  if (Better1 == Better2)
2232bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    return 0;
2233bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor
2234bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  return Better1? PS1 : PS2;
2235bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor}
2236bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor
22371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
2238e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
2239e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           const TemplateArgument &TemplateArg,
2240e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2241ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
2242e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used);
2243031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2244e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
2245031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// expression.
22461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
2247e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
2248e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           const Expr *E,
2249e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2250ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
2251e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
2252e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
2253e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // find other occurrences of template parameters.
2254f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor  const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
2255c781f9cd854f3d5d1c826f4a13382c6abca4cff7Douglas Gregor  if (!DRE)
2256031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    return;
2257031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
22581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const NonTypeTemplateParmDecl *NTTP
2259031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
2260031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  if (!NTTP)
2261031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    return;
2262031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2263ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (NTTP->getDepth() == Depth)
2264ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Used[NTTP->getIndex()] = true;
2265031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
2266031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2267e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
2268e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// nested name specifier.
2269e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregorstatic void
2270e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
2271e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           NestedNameSpecifier *NNS,
2272e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2273ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
2274e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
2275e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (!NNS)
2276e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    return;
2277e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
2278ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
2279ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             Used);
2280e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
2281ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             OnlyDeduced, Depth, Used);
2282e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor}
2283e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
2284e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
2285e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// template name.
2286e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregorstatic void
2287e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
2288e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           TemplateName Name,
2289e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2290ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
2291e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
2292e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2293e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    if (TemplateTemplateParmDecl *TTP
2294ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor          = dyn_cast<TemplateTemplateParmDecl>(Template)) {
2295ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      if (TTP->getDepth() == Depth)
2296ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        Used[TTP->getIndex()] = true;
2297ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    }
2298e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    return;
2299e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  }
2300e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
2301788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
2302788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
2303788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                               Depth, Used);
2304e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
2305ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
2306ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2307e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor}
2308e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
2309e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
2310031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// type.
23111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
2312e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2313e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2314ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
2315e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
2316e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (T.isNull())
2317e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    return;
2318e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
2319031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  // Non-dependent types have nothing deducible
2320031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  if (!T->isDependentType())
2321031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    return;
2322031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2323031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  T = SemaRef.Context.getCanonicalType(T);
2324031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  switch (T->getTypeClass()) {
2325031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Pointer:
2326e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
2327e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<PointerType>(T)->getPointeeType(),
2328e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               OnlyDeduced,
2329ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth,
2330e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               Used);
2331031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2332031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2333031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::BlockPointer:
2334e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
2335e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<BlockPointerType>(T)->getPointeeType(),
2336e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               OnlyDeduced,
2337ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth,
2338e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               Used);
2339031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2340031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2341031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::LValueReference:
2342031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::RValueReference:
2343e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
2344e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<ReferenceType>(T)->getPointeeType(),
2345e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               OnlyDeduced,
2346ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth,
2347e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               Used);
2348031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2349031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2350031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::MemberPointer: {
2351031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
2352e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
2353ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2354e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
2355ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
2356031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2357031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
2358031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2359031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::DependentSizedArray:
2360e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
2361e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<DependentSizedArrayType>(T)->getSizeExpr(),
2362ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
2363031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    // Fall through to check the element type
2364031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2365031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::ConstantArray:
2366031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::IncompleteArray:
2367e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
2368e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<ArrayType>(T)->getElementType(),
2369ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
2370031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2371031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2372031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Vector:
2373031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::ExtVector:
2374e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
2375e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<VectorType>(T)->getElementType(),
2376ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
2377031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2378031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
23799cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  case Type::DependentSizedExtVector: {
23809cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    const DependentSizedExtVectorType *VecType
2381f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor      = cast<DependentSizedExtVectorType>(T);
2382e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
2383ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2384e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
2385ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
23869cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    break;
23879cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
23889cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
2389031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::FunctionProto: {
2390f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor    const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
2391e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
2392ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2393031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
2394e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor      MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
2395ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 Depth, Used);
2396031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2397031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
2398031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2399ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  case Type::TemplateTypeParm: {
2400ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
2401ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (TTP->getDepth() == Depth)
2402ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      Used[TTP->getIndex()] = true;
2403031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2404ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
2405031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2406031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::TemplateSpecialization: {
24071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const TemplateSpecializationType *Spec
2408f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor      = cast<TemplateSpecializationType>(T);
2409e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
2410ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2411e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
2412ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2413ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 Used);
2414e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    break;
2415e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  }
24161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2417e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  case Type::Complex:
2418e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    if (!OnlyDeduced)
2419e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor      MarkUsedTemplateParameters(SemaRef,
2420e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                 cast<ComplexType>(T)->getElementType(),
2421ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 OnlyDeduced, Depth, Used);
2422e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    break;
2423031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2424e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  case Type::Typename:
2425e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    if (!OnlyDeduced)
2426e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor      MarkUsedTemplateParameters(SemaRef,
2427e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                 cast<TypenameType>(T)->getQualifier(),
2428ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 OnlyDeduced, Depth, Used);
2429031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2430031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2431ad5e73887052193afda72db8efcb812bd083a4a8John McCall  case Type::TypeOf:
2432ad5e73887052193afda72db8efcb812bd083a4a8John McCall    if (!OnlyDeduced)
2433ad5e73887052193afda72db8efcb812bd083a4a8John McCall      MarkUsedTemplateParameters(SemaRef,
2434ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 cast<TypeOfType>(T)->getUnderlyingType(),
2435ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 OnlyDeduced, Depth, Used);
2436ad5e73887052193afda72db8efcb812bd083a4a8John McCall    break;
2437ad5e73887052193afda72db8efcb812bd083a4a8John McCall
2438ad5e73887052193afda72db8efcb812bd083a4a8John McCall  case Type::TypeOfExpr:
2439ad5e73887052193afda72db8efcb812bd083a4a8John McCall    if (!OnlyDeduced)
2440ad5e73887052193afda72db8efcb812bd083a4a8John McCall      MarkUsedTemplateParameters(SemaRef,
2441ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 cast<TypeOfExprType>(T)->getUnderlyingExpr(),
2442ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 OnlyDeduced, Depth, Used);
2443ad5e73887052193afda72db8efcb812bd083a4a8John McCall    break;
2444ad5e73887052193afda72db8efcb812bd083a4a8John McCall
2445ad5e73887052193afda72db8efcb812bd083a4a8John McCall  case Type::Decltype:
2446ad5e73887052193afda72db8efcb812bd083a4a8John McCall    if (!OnlyDeduced)
2447ad5e73887052193afda72db8efcb812bd083a4a8John McCall      MarkUsedTemplateParameters(SemaRef,
2448ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 cast<DecltypeType>(T)->getUnderlyingExpr(),
2449ad5e73887052193afda72db8efcb812bd083a4a8John McCall                                 OnlyDeduced, Depth, Used);
2450ad5e73887052193afda72db8efcb812bd083a4a8John McCall    break;
2451ad5e73887052193afda72db8efcb812bd083a4a8John McCall
2452e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // None of these types have any template parameters in them.
2453031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Builtin:
2454031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::VariableArray:
2455031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::FunctionNoProto:
2456031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Record:
2457031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Enum:
2458031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::ObjCInterface:
2459d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  case Type::ObjCObjectPointer:
2460ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  case Type::UnresolvedUsing:
2461031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define TYPE(Class, Base)
2462031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
2463031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define DEPENDENT_TYPE(Class, Base)
2464031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2465031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#include "clang/AST/TypeNodes.def"
2466031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2467031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
2468031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
2469031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2470e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by this
2471031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// template argument.
24721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
2473e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
2474e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           const TemplateArgument &TemplateArg,
2475e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2476ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
2477e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
2478031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  switch (TemplateArg.getKind()) {
2479031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Null:
2480031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Integral:
2481788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Declaration:
2482031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
24831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2484031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Type:
2485e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
2486ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2487031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2488031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2489788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  case TemplateArgument::Template:
2490788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsTemplate(),
2491788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                               OnlyDeduced, Depth, Used);
2492031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2493031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2494031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Expression:
2495e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
2496ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2497031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2498e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
2499d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson  case TemplateArgument::Pack:
2500e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
2501e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                      PEnd = TemplateArg.pack_end();
2502e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor         P != PEnd; ++P)
2503ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
2504d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson    break;
2505031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
2506031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
2507031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2508031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \brief Mark the template parameters can be deduced by the given
2509031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// template argument list.
2510031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor///
2511031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \param TemplateArgs the template argument list from which template
2512031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// parameters will be deduced.
2513031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor///
2514031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \param Deduced a bit vector whose elements will be set to \c true
2515031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// to indicate when the corresponding template parameter will be
2516031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// deduced.
25171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
2518e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorSema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
2519ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 bool OnlyDeduced, unsigned Depth,
2520e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                 llvm::SmallVectorImpl<bool> &Used) {
2521031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
2522ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
2523ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 Depth, Used);
2524031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
252563f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor
252663f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor/// \brief Marks all of the template parameters that will be deduced by a
252763f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor/// call to the given function template.
252863f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregorvoid Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
252963f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor                                         llvm::SmallVectorImpl<bool> &Deduced) {
253063f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  TemplateParameterList *TemplateParams
253163f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor    = FunctionTemplate->getTemplateParameters();
253263f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  Deduced.clear();
253363f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  Deduced.resize(TemplateParams->size());
253463f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor
253563f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
253663f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
253763f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor    ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
2538ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 true, TemplateParams->getDepth(), Deduced);
253963f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor}
2540