SemaTemplateDeduction.cpp revision a7ef13024e4cc3dfb75e3bc1695371b39d9a5240
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
628f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    //     template-name<T> (where template-name refers to a class template)
629d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor    //     template-name<i>
630db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    //     TT<T>
631db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    //     TT<i>
632db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    //     TT<>
633d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor    case Type::TemplateSpecialization: {
634d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor      const TemplateSpecializationType *SpecParam
635d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor        = cast<TemplateSpecializationType>(Param);
6361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
637de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      // Try to deduce template arguments from the template-id.
638de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      Sema::TemplateDeductionResult Result
639a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg,
640de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                  Info, Deduced);
6411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
6424a5c15f75f76b95e1c2ceb6fa2737dcadd5f4be1Douglas Gregor      if (Result && (TDF & TDF_DerivedClass)) {
643de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        // C++ [temp.deduct.call]p3b3:
644de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   If P is a class, and P has the form template-id, then A can be a
645de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   derived class of the deduced A. Likewise, if P is a pointer to a
6461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        //   class of the form template-id, A can be a pointer to a derived
647de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   class pointed to by the deduced A.
648de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //
649de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        // More importantly:
6501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        //   These alternatives are considered only if type deduction would
651de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        //   otherwise fail.
652a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        if (const RecordType *RecordT = Arg->getAs<RecordType>()) {
653a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          // We cannot inspect base classes as part of deduction when the type
654a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          // is incomplete, so either instantiate any templates necessary to
655a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          // complete the type, or skip over it if it cannot be completed.
656a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          // FIXME: The location given becomes the PoI, so we should thread
657a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          // a better location through the TemplateDeductionInfo.
658a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          if (S.RequireCompleteType(RecordT->getDecl()->getLocStart(), Arg, 0))
659a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            return Result;
660a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth
661de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          // Use data recursion to crawl through the list of base classes.
6621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          // Visited contains the set of nodes we have already visited, while
663de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          // ToVisit is our stack of records that we still need to visit.
664de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          llvm::SmallPtrSet<const RecordType *, 8> Visited;
665de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          llvm::SmallVector<const RecordType *, 8> ToVisit;
666de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          ToVisit.push_back(RecordT);
667de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          bool Successful = false;
668de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          while (!ToVisit.empty()) {
669de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // Retrieve the next class in the inheritance hierarchy.
670de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            const RecordType *NextT = ToVisit.back();
671de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            ToVisit.pop_back();
6721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
673de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // If we have already seen this type, skip it.
674de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            if (!Visited.insert(NextT))
675de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              continue;
6761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
677de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // If this is a base class, try to perform template argument
678de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // deduction from it.
679de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            if (NextT != RecordT) {
680de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              Sema::TemplateDeductionResult BaseResult
681a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth                = DeduceTemplateArguments(S, TemplateParams, SpecParam,
682de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                          QualType(NextT, 0), Info, Deduced);
6831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
684de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              // If template argument deduction for this base was successful,
685de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              // note that we had some success.
686de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor              if (BaseResult == Sema::TDK_Success)
687de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                Successful = true;
688de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            }
6891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
690de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            // Visit base classes
691de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl());
692de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(),
693de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                                                 BaseEnd = Next->bases_end();
6949994a34f6cf842721ba7723edc0b9036229fe387Sebastian Redl                 Base != BaseEnd; ++Base) {
6951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump              assert(Base->getType()->isRecordType() &&
696de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor                     "Base class that isn't a record?");
6976217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek              ToVisit.push_back(Base->getType()->getAs<RecordType>());
698de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            }
699de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          }
7001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
701de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor          if (Successful)
702de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor            return Sema::TDK_Success;
703de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor        }
7041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
705de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      }
7061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
707de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor      return Result;
708d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor    }
709d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor
710637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T type::*
711637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T T::*
712637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (type::*)()
713637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     type (T::*)()
714637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     type (type::*)(T)
715637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     type (T::*)(T)
716637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (type::*)(T)
717637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (T::*)()
718637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    //     T (T::*)(T)
719637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    case Type::MemberPointer: {
720637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param);
721637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg);
722637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      if (!MemPtrArg)
723f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
724f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
725f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (Sema::TemplateDeductionResult Result
726a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth            = DeduceTemplateArguments(S, TemplateParams,
727f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      MemPtrParam->getPointeeType(),
728f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                      MemPtrArg->getPointeeType(),
729508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                      Info, Deduced,
730508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                      TDF & TDF_IgnoreQualifiers))
731f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Result;
732f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
733a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
734f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                     QualType(MemPtrParam->getClass(), 0),
735f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                     QualType(MemPtrArg->getClass(), 0),
736508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Info, Deduced, 0);
737637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    }
738637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor
7399a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    //     (clang extension)
7409a917e4fac79aba20fbd25983c78396475078918Anders Carlsson    //
7411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     type(^)(T)
7421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(^)()
7431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     T(^)(T)
744859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson    case Type::BlockPointer: {
745859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson      const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param);
746859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson      const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg);
7471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
748859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson      if (!BlockPtrArg)
749f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_NonDeducedMismatch;
7501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
751a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
752859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson                                     BlockPtrParam->getPointeeType(),
753f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                     BlockPtrArg->getPointeeType(), Info,
754508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                     Deduced, 0);
755859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson    }
756859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson
757637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    case Type::TypeOfExpr:
758637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    case Type::TypeOf:
759637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor    case Type::Typename:
760637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor      // No template argument deduction for these types
761f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_Success;
762637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor
763d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor    default:
764d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor      break;
7650b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
7660b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
7670b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  // FIXME: Many more cases to go (to go).
768f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  return Sema::TDK_Success;
7690b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
7700b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
771f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult
772a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
773f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateParameterList *TemplateParams,
774f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        const TemplateArgument &Param,
7750b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor                        const TemplateArgument &Arg,
776f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        Sema::TemplateDeductionInfo &Info,
7770b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor                        llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
7780b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  switch (Param.getKind()) {
779199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Null:
780199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    assert(false && "Null template argument in parameter list");
781199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    break;
7821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
7831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  case TemplateArgument::Type:
784788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    if (Arg.getKind() == TemplateArgument::Type)
785a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams, Param.getAsType(),
786788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                                     Arg.getAsType(), Info, Deduced, 0);
787788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.FirstArg = Param;
788788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.SecondArg = Arg;
789788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
790788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor
791788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  case TemplateArgument::Template:
792db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor    if (Arg.getKind() == TemplateArgument::Template)
793a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      return DeduceTemplateArguments(S, TemplateParams,
794788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                                     Param.getAsTemplate(),
795db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor                                     Arg.getAsTemplate(), Info, Deduced);
796788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.FirstArg = Param;
797788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    Info.SecondArg = Arg;
798788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    return Sema::TDK_NonDeducedMismatch;
799788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor
800199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Declaration:
801788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    if (Arg.getKind() == TemplateArgument::Declaration &&
802788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor        Param.getAsDecl()->getCanonicalDecl() ==
803788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor          Arg.getAsDecl()->getCanonicalDecl())
804788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return Sema::TDK_Success;
805788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor
806f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.FirstArg = Param;
807f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.SecondArg = Arg;
808f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_NonDeducedMismatch;
8091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
810199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Integral:
811199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    if (Arg.getKind() == TemplateArgument::Integral) {
812199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      // FIXME: Zero extension + sign checking here?
813f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (*Param.getAsIntegral() == *Arg.getAsIntegral())
814f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        return Sema::TDK_Success;
815f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
816f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.FirstArg = Param;
817f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.SecondArg = Arg;
818f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
819f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    }
820f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor
821f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    if (Arg.getKind() == TemplateArgument::Expression) {
822f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.FirstArg = Param;
823f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.SecondArg = Arg;
824f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
825199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    }
826199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
827199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    assert(false && "Type/value mismatch");
828f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.FirstArg = Param;
829f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    Info.SecondArg = Arg;
830f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_NonDeducedMismatch;
8311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
832199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  case TemplateArgument::Expression: {
8331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (NonTypeTemplateParmDecl *NTTP
834199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor          = getDeducedParameterFromExpr(Param.getAsExpr())) {
835199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (Arg.getKind() == TemplateArgument::Integral)
836199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor        // FIXME: Sign problems here
837a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP,
8381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                             *Arg.getAsIntegral(),
839f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                             Info, Deduced);
840199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      if (Arg.getKind() == TemplateArgument::Expression)
841a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(),
842f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                             Info, Deduced);
84315755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor      if (Arg.getKind() == TemplateArgument::Declaration)
844a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(),
84515755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor                                             Info, Deduced);
84615755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor
847199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor      assert(false && "Type/value mismatch");
848f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.FirstArg = Param;
849f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      Info.SecondArg = Arg;
850f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Sema::TDK_NonDeducedMismatch;
851199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    }
8521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
853199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor    // Can't deduce anything, but that's okay.
854f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Sema::TDK_Success;
855199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor  }
856d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson  case TemplateArgument::Pack:
857d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson    assert(0 && "FIXME: Implement!");
858d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson    break;
8590b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
8601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
861f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  return Sema::TDK_Success;
8620b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
8630b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
8641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic Sema::TemplateDeductionResult
865a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S,
866f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        TemplateParameterList *TemplateParams,
8670b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor                        const TemplateArgumentList &ParamList,
8680b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor                        const TemplateArgumentList &ArgList,
869f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                        Sema::TemplateDeductionInfo &Info,
8700b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor                        llvm::SmallVectorImpl<TemplateArgument> &Deduced) {
8710b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  assert(ParamList.size() == ArgList.size());
8720b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  for (unsigned I = 0, N = ParamList.size(); I != N; ++I) {
873f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    if (Sema::TemplateDeductionResult Result
874a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          = DeduceTemplateArguments(S, TemplateParams,
8751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                    ParamList[I], ArgList[I],
876f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                    Info, Deduced))
877f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return Result;
8780b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  }
879f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  return Sema::TDK_Success;
8800b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
8810b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor
882f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// \brief Determine whether two template arguments are the same.
8831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool isSameTemplateArg(ASTContext &Context,
884f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor                              const TemplateArgument &X,
885f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor                              const TemplateArgument &Y) {
886f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  if (X.getKind() != Y.getKind())
887f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    return false;
8881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
889f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  switch (X.getKind()) {
890f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Null:
891f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      assert(false && "Comparing NULL template argument");
892f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      break;
8931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
894f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Type:
895f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      return Context.getCanonicalType(X.getAsType()) ==
896f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor             Context.getCanonicalType(Y.getAsType());
8971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
898f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Declaration:
89997fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis      return X.getAsDecl()->getCanonicalDecl() ==
90097fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis             Y.getAsDecl()->getCanonicalDecl();
9011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
902788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Template:
903788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return Context.getCanonicalTemplateName(X.getAsTemplate())
904788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor               .getAsVoidPointer() ==
905788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor             Context.getCanonicalTemplateName(Y.getAsTemplate())
906788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor               .getAsVoidPointer();
907788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor
908f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Integral:
909f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      return *X.getAsIntegral() == *Y.getAsIntegral();
9101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
911788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Expression: {
912788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      llvm::FoldingSetNodeID XID, YID;
913788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      X.getAsExpr()->Profile(XID, Context, true);
914788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      Y.getAsExpr()->Profile(YID, Context, true);
915788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor      return XID == YID;
916788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    }
9171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
918f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    case TemplateArgument::Pack:
919f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      if (X.pack_size() != Y.pack_size())
920f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor        return false;
9211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
9221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      for (TemplateArgument::pack_iterator XP = X.pack_begin(),
9231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                        XPEnd = X.pack_end(),
924f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor                                           YP = Y.pack_begin();
9251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump           XP != XPEnd; ++XP, ++YP)
926f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor        if (!isSameTemplateArg(Context, *XP, *YP))
927f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor          return false;
928f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
929f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      return true;
930f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  }
931f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
932f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  return false;
933f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor}
934f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
935f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// \brief Helper function to build a TemplateParameter when we don't
936f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// know its type statically.
937f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregorstatic TemplateParameter makeTemplateParameter(Decl *D) {
938f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D))
939f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    return TemplateParameter(TTP);
940f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D))
941f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    return TemplateParameter(NTTP);
9421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
943f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor  return TemplateParameter(cast<TemplateTemplateParmDecl>(D));
944f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor}
945f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor
946c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor/// \brief Perform template argument deduction to determine whether
947c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor/// the given template arguments match the given class template
948c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor/// partial specialization per C++ [temp.class.spec.match].
949f67875d5addf36b951ad37fb04509ab2b572c88aDouglas GregorSema::TemplateDeductionResult
9500b9247f129401b4849682b2e2bcdea8fc977068aDouglas GregorSema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial,
951f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                              const TemplateArgumentList &TemplateArgs,
952f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                              TemplateDeductionInfo &Info) {
953c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor  // C++ [temp.class.spec.match]p2:
954c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor  //   A partial specialization matches a given actual template
955c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor  //   argument list if the template arguments of the partial
956c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor  //   specialization can be deduced from the actual template argument
957c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor  //   list (14.8.2).
958bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor  SFINAETrap Trap(*this);
9590b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  llvm::SmallVector<TemplateArgument, 4> Deduced;
9600b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor  Deduced.resize(Partial->getTemplateParameters()->size());
961f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  if (TemplateDeductionResult Result
962a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = ::DeduceTemplateArguments(*this,
963f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                    Partial->getTemplateParameters(),
9641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                    Partial->getTemplateArgs(),
965f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                                    TemplateArgs, Info, Deduced))
966f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return Result;
967637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor
968637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor  InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial,
969637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor                             Deduced.data(), Deduced.size());
970637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor  if (Inst)
971f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    return TDK_InstantiationDepth;
972199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor
97302cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // C++ [temp.deduct.type]p2:
97402cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  //   [...] or if any template argument remains neither deduced nor
97502cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  //   explicitly specified, template argument deduction fails.
976fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson  TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(),
977fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson                                      Deduced.size());
97802cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
979f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    if (Deduced[I].isNull()) {
9801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      Decl *Param
981bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor        = const_cast<NamedDecl *>(
982bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                Partial->getTemplateParameters()->getParam(I));
983f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(Param))
984f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        Info.Param = TTP;
9851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      else if (NonTypeTemplateParmDecl *NTTP
986f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor                 = dyn_cast<NonTypeTemplateParmDecl>(Param))
987f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        Info.Param = NTTP;
988f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      else
989f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor        Info.Param = cast<TemplateTemplateParmDecl>(Param);
990f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      return TDK_Incomplete;
991f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor    }
99202cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor
993fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson    Builder.Append(Deduced[I]);
99402cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  }
99502cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor
99602cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // Form the template argument list from the deduced template arguments.
9971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentList *DeducedArgumentList
998fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson    = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
999f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  Info.reset(DeducedArgumentList);
100002cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor
100102cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // Substitute the deduced template arguments into the template
100202cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // arguments of the class template partial specialization, and
100302cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // verify that the instantiated template arguments are both valid
100402cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  // and are equivalent to the template arguments originally provided
10051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // to the class template.
100602cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate();
1007833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  const TemplateArgumentLoc *PartialTemplateArgs
1008833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    = Partial->getTemplateArgsAsWritten();
1009833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  unsigned N = Partial->getNumTemplateArgsAsWritten();
1010d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
1011d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  // Note that we don't provide the langle and rangle locations.
1012d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  TemplateArgumentListInfo InstArgs;
1013d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall
1014833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned I = 0; I != N; ++I) {
1015bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    Decl *Param = const_cast<NamedDecl *>(
1016c9e5d25ea33616c896a2ce5fbd6d68186eaddc5cDouglas Gregor                    ClassTemplate->getTemplateParameters()->getParam(I));
1017d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    TemplateArgumentLoc InstArg;
1018d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    if (Subst(PartialTemplateArgs[I], InstArg,
1019833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall              MultiLevelTemplateArgumentList(*DeducedArgumentList))) {
1020f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Info.Param = makeTemplateParameter(Param);
1021833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall      Info.FirstArg = PartialTemplateArgs[I].getArgument();
10221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      return TDK_SubstitutionFailure;
1023f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    }
1024d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    InstArgs.addArgument(InstArg);
1025833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  }
1026833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1027833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  TemplateArgumentListBuilder ConvertedInstArgs(
1028833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall                                  ClassTemplate->getTemplateParameters(), N);
1029833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1030833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  if (CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(),
1031d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                InstArgs, false, ConvertedInstArgs)) {
1032833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    // FIXME: fail with more useful information?
1033833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    return TDK_SubstitutionFailure;
1034833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  }
1035833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1036833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall  for (unsigned I = 0, E = ConvertedInstArgs.flatSize(); I != E; ++I) {
1037d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall    TemplateArgument InstArg = ConvertedInstArgs.getFlatArguments()[I];
1038833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall
1039833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall    Decl *Param = const_cast<NamedDecl *>(
1040833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall                    ClassTemplate->getTemplateParameters()->getParam(I));
10411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1042f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    if (InstArg.getKind() == TemplateArgument::Expression) {
10431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      // When the argument is an expression, check the expression result
1044f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      // against the actual template parameter to get down to the canonical
1045f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      // template argument.
1046f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Expr *InstExpr = InstArg.getAsExpr();
10471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      if (NonTypeTemplateParmDecl *NTTP
1048f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor            = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
1049f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor        if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) {
1050f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor          Info.Param = makeTemplateParameter(Param);
1051833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall          Info.FirstArg = Partial->getTemplateArgs()[I];
10521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          return TDK_SubstitutionFailure;
1053f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor        }
1054f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor      }
105502cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor    }
10561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1057f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) {
1058f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Info.Param = makeTemplateParameter(Param);
1059f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Info.FirstArg = TemplateArgs[I];
1060f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      Info.SecondArg = InstArg;
1061f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor      return TDK_NonDeducedMismatch;
1062f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor    }
106302cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor  }
106402cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor
1065bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor  if (Trap.hasErrorOccurred())
1066bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor    return TDK_SubstitutionFailure;
1067bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor
1068f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor  return TDK_Success;
10690b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor}
1070031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
10714112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor/// \brief Determine whether the given type T is a simple-template-id type.
10724112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregorstatic bool isSimpleTemplateIdType(QualType T) {
10731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  if (const TemplateSpecializationType *Spec
1074183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall        = T->getAs<TemplateSpecializationType>())
10754112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    return Spec->getTemplateName().getAsTemplateDecl() != 0;
10761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
10774112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor  return false;
10784112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor}
107983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
108083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \brief Substitute the explicitly-provided template arguments into the
108183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// given function template according to C++ [temp.arg.explicit].
108283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
108383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionTemplate the function template into which the explicit
108483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template arguments will be substituted.
108583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
10861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ExplicitTemplateArguments the explicitly-specified template
108783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// arguments.
108883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
10891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param Deduced the deduced template arguments, which will be populated
109083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// with the converted and checked explicit template arguments.
109183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
10921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ParamTypes will be populated with the instantiated function
109383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// parameters.
109483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
109583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionType if non-NULL, the result type of the function template
109683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// will also be instantiated and the pointed-to value will be updated with
109783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// the instantiated function type.
109883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
109983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Info if substitution fails for any reason, this object will be
110083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// populated with more information about the failure.
110183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
110283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \returns TDK_Success if substitution was successful, or some failure
110383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// condition.
110483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::TemplateDeductionResult
110583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::SubstituteExplicitTemplateArguments(
110683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      FunctionTemplateDecl *FunctionTemplate,
1107d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                        const TemplateArgumentListInfo &ExplicitTemplateArgs,
110883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                            llvm::SmallVectorImpl<TemplateArgument> &Deduced,
110983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                 llvm::SmallVectorImpl<QualType> &ParamTypes,
111083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          QualType *FunctionType,
111183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          TemplateDeductionInfo &Info) {
111283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
111383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateParameterList *TemplateParams
111483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = FunctionTemplate->getTemplateParameters();
111583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
1116d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (ExplicitTemplateArgs.size() == 0) {
111783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    // No arguments to substitute; just copy over the parameter types and
111883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    // fill in the function type.
111983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    for (FunctionDecl::param_iterator P = Function->param_begin(),
112083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                   PEnd = Function->param_end();
112183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor         P != PEnd;
112283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor         ++P)
112383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      ParamTypes.push_back((*P)->getType());
11241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
112583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (FunctionType)
112683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      *FunctionType = Function->getType();
112783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_Success;
112883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
11291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
113083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Substitution of the explicit template arguments into a function template
113183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  /// is a SFINAE context. Trap any errors that might occur.
11321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SFINAETrap Trap(*this);
11331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
113483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // C++ [temp.arg.explicit]p3:
11351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   Template arguments that are present shall be specified in the
11361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   declaration order of their corresponding template-parameters. The
113783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   template argument list shall not specify more template-arguments than
11381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   there are corresponding template-parameters.
11391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentListBuilder Builder(TemplateParams,
1140d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                      ExplicitTemplateArgs.size());
11411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Enter a new template instantiation context where we check the
114383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // explicitly-specified template arguments against this function template,
114483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // and then substitute them into the function parameter types.
11451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
114683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                             FunctionTemplate, Deduced.data(), Deduced.size(),
114783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor           ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution);
114883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (Inst)
114983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_InstantiationDepth;
11501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
115183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (CheckTemplateArgumentList(FunctionTemplate,
115283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                SourceLocation(),
1153d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                ExplicitTemplateArgs,
115483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                true,
115583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                Builder) || Trap.hasErrorOccurred())
115683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_InvalidExplicitArguments;
11571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
115883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Form the template argument list from the explicitly-specified
115983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // template arguments.
11601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentList *ExplicitArgumentList
116183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
116283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Info.reset(ExplicitArgumentList);
11631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
116483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Instantiate the types of each of the function parameters given the
116583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // explicitly-specified template arguments.
116683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  for (FunctionDecl::param_iterator P = Function->param_begin(),
116783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                PEnd = Function->param_end();
116883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor       P != PEnd;
116983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor       ++P) {
11701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    QualType ParamType
11711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      = SubstType((*P)->getType(),
1172357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1173357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  (*P)->getLocation(), (*P)->getDeclName());
117483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (ParamType.isNull() || Trap.hasErrorOccurred())
117583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_SubstitutionFailure;
11761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
117783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    ParamTypes.push_back(ParamType);
117883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
117983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
118083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // If the caller wants a full function type back, instantiate the return
118183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // type and form that function type.
118283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (FunctionType) {
118383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    // FIXME: exception-specifications?
11841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const FunctionProtoType *Proto
1185183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall      = Function->getType()->getAs<FunctionProtoType>();
118683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    assert(Proto && "Function template does not have a prototype?");
11871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    QualType ResultType
1189357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor      = SubstType(Proto->getResultType(),
1190357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  MultiLevelTemplateArgumentList(*ExplicitArgumentList),
1191357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  Function->getTypeSpecStartLoc(),
1192357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                  Function->getDeclName());
119383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (ResultType.isNull() || Trap.hasErrorOccurred())
119483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_SubstitutionFailure;
11951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
11961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    *FunctionType = BuildFunctionType(ResultType,
119783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      ParamTypes.data(), ParamTypes.size(),
119883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      Proto->isVariadic(),
119983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      Proto->getTypeQuals(),
120083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      Function->getLocation(),
120183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      Function->getDeclName());
120283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (FunctionType->isNull() || Trap.hasErrorOccurred())
120383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_SubstitutionFailure;
120483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
12051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
120683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // C++ [temp.arg.explicit]p2:
12071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   Trailing template arguments that can be deduced (14.8.2) may be
12081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   omitted from the list of explicit template-arguments. If all of the
120983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   template arguments can be deduced, they may all be omitted; in this
121083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   case, the empty template argument list <> itself may also be omitted.
121183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //
121283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Take all of the explicitly-specified arguments and put them into the
12131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // set of deduced template arguments.
121483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Deduced.reserve(TemplateParams->size());
121583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I)
12161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    Deduced.push_back(ExplicitArgumentList->get(I));
12171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
121883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  return TDK_Success;
121983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor}
122083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
12211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Finish template argument deduction for a function template,
122283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// checking the deduced template arguments for completeness and forming
122383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// the function template specialization.
12241eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSema::TemplateDeductionResult
122583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate,
122683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                            llvm::SmallVectorImpl<TemplateArgument> &Deduced,
122783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      FunctionDecl *&Specialization,
122883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                      TemplateDeductionInfo &Info) {
122983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateParameterList *TemplateParams
123083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = FunctionTemplate->getTemplateParameters();
12311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
123251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // Template argument deduction for function templates in a SFINAE context.
123351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // Trap any errors that might occur.
123451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  SFINAETrap Trap(*this);
123551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
123651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // Enter a new template instantiation context while we instantiate the
123751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  // actual function declaration.
123851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(),
123951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                             FunctionTemplate, Deduced.data(), Deduced.size(),
124051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor              ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution);
124151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor  if (Inst)
124251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    return TDK_InstantiationDepth;
124351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
124483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // C++ [temp.deduct.type]p2:
124583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   [...] or if any template argument remains neither deduced nor
124683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  //   explicitly specified, template argument deduction fails.
124783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size());
124883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  for (unsigned I = 0, N = Deduced.size(); I != N; ++I) {
124951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    if (!Deduced[I].isNull()) {
125051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      Builder.Append(Deduced[I]);
125151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      continue;
125251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    }
125351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
125451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // Substitute into the default template argument, if available.
125551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    NamedDecl *Param = FunctionTemplate->getTemplateParameters()->getParam(I);
125651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    TemplateArgumentLoc DefArg
125751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate,
125851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                              FunctionTemplate->getLocation(),
125951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                  FunctionTemplate->getSourceRange().getEnd(),
126051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                                Param,
126151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                                                Builder);
126251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
126351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // If there was no default argument, deduction is incomplete.
126451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    if (DefArg.getArgument().isNull()) {
126583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      Info.Param = makeTemplateParameter(
126651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                         const_cast<NamedDecl *>(TemplateParams->getParam(I)));
126783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return TDK_Incomplete;
126883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    }
126951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor
127051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // Check whether we can actually use the default argument.
127151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    if (CheckTemplateArgument(Param, DefArg,
127251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                              FunctionTemplate,
127351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                              FunctionTemplate->getLocation(),
127451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                              FunctionTemplate->getSourceRange().getEnd(),
127551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                              Builder)) {
127651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      Info.Param = makeTemplateParameter(
127751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor                         const_cast<NamedDecl *>(TemplateParams->getParam(I)));
127851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor      return TDK_SubstitutionFailure;
127951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    }
12801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
128151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor    // If we get here, we successfully used the default template argument.
128283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
12831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
128483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Form the template argument list from the deduced template arguments.
12851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  TemplateArgumentList *DeducedArgumentList
128683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true);
128783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Info.reset(DeducedArgumentList);
12881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
12891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Substitute the deduced template arguments into the function template
129083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // declaration to produce the function template specialization.
129183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  Specialization = cast_or_null<FunctionDecl>(
1292ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                      SubstDecl(FunctionTemplate->getTemplatedDecl(),
1293ce3ff2bd3a3386dbc209d3cba4b8769173b274c1John McCall                                FunctionTemplate->getDeclContext(),
1294357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor                         MultiLevelTemplateArgumentList(*DeducedArgumentList)));
129583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (!Specialization)
129683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_SubstitutionFailure;
12971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1298f882574cf640d5c8355965e1c486f9d8d8ffcf47Douglas Gregor  assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() ==
1299f882574cf640d5c8355965e1c486f9d8d8ffcf47Douglas Gregor         FunctionTemplate->getCanonicalDecl());
1300f882574cf640d5c8355965e1c486f9d8d8ffcf47Douglas Gregor
13011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // If the template argument list is owned by the function template
130283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // specialization, release it.
130383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList)
130483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    Info.take();
13051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
130683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // There may have been an error that did not prevent us from constructing a
130783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // declaration. Mark the declaration invalid and return with a substitution
130883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // failure.
130983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  if (Trap.hasErrorOccurred()) {
131083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    Specialization->setInvalidDecl(true);
131183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    return TDK_SubstitutionFailure;
131283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  }
13131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
13141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return TDK_Success;
131583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor}
131683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
1317eff92135d32039c9874dc356f3e93143af6069c1John McCallstatic QualType GetTypeOfFunction(ASTContext &Context,
1318eff92135d32039c9874dc356f3e93143af6069c1John McCall                                  bool isAddressOfOperand,
1319eff92135d32039c9874dc356f3e93143af6069c1John McCall                                  FunctionDecl *Fn) {
1320eff92135d32039c9874dc356f3e93143af6069c1John McCall  if (!isAddressOfOperand) return Fn->getType();
1321eff92135d32039c9874dc356f3e93143af6069c1John McCall  if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn))
1322eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (Method->isInstance())
1323eff92135d32039c9874dc356f3e93143af6069c1John McCall      return Context.getMemberPointerType(Fn->getType(),
1324eff92135d32039c9874dc356f3e93143af6069c1John McCall               Context.getTypeDeclType(Method->getParent()).getTypePtr());
1325eff92135d32039c9874dc356f3e93143af6069c1John McCall  return Context.getPointerType(Fn->getType());
1326eff92135d32039c9874dc356f3e93143af6069c1John McCall}
1327eff92135d32039c9874dc356f3e93143af6069c1John McCall
1328eff92135d32039c9874dc356f3e93143af6069c1John McCall/// Apply the deduction rules for overload sets.
1329eff92135d32039c9874dc356f3e93143af6069c1John McCall///
1330eff92135d32039c9874dc356f3e93143af6069c1John McCall/// \return the null type if this argument should be treated as an
1331eff92135d32039c9874dc356f3e93143af6069c1John McCall/// undeduced context
1332eff92135d32039c9874dc356f3e93143af6069c1John McCallstatic QualType
1333eff92135d32039c9874dc356f3e93143af6069c1John McCallResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams,
1334eff92135d32039c9874dc356f3e93143af6069c1John McCall                            Expr *Arg, QualType ParamType) {
13357bb12da2b0749eeebb21854c77877736969e59f2John McCall  llvm::PointerIntPair<OverloadExpr*,1> R = OverloadExpr::find(Arg);
1336eff92135d32039c9874dc356f3e93143af6069c1John McCall
13377bb12da2b0749eeebb21854c77877736969e59f2John McCall  bool isAddressOfOperand = bool(R.getInt());
13387bb12da2b0749eeebb21854c77877736969e59f2John McCall  OverloadExpr *Ovl = R.getPointer();
1339eff92135d32039c9874dc356f3e93143af6069c1John McCall
1340eff92135d32039c9874dc356f3e93143af6069c1John McCall  // If there were explicit template arguments, we can only find
1341eff92135d32039c9874dc356f3e93143af6069c1John McCall  // something via C++ [temp.arg.explicit]p3, i.e. if the arguments
1342eff92135d32039c9874dc356f3e93143af6069c1John McCall  // unambiguously name a full specialization.
13437bb12da2b0749eeebb21854c77877736969e59f2John McCall  if (Ovl->hasExplicitTemplateArgs()) {
1344eff92135d32039c9874dc356f3e93143af6069c1John McCall    // But we can still look for an explicit specialization.
1345eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (FunctionDecl *ExplicitSpec
13467bb12da2b0749eeebb21854c77877736969e59f2John McCall          = S.ResolveSingleFunctionTemplateSpecialization(Ovl))
13477bb12da2b0749eeebb21854c77877736969e59f2John McCall      return GetTypeOfFunction(S.Context, isAddressOfOperand, ExplicitSpec);
1348eff92135d32039c9874dc356f3e93143af6069c1John McCall    return QualType();
1349eff92135d32039c9874dc356f3e93143af6069c1John McCall  }
1350eff92135d32039c9874dc356f3e93143af6069c1John McCall
1351eff92135d32039c9874dc356f3e93143af6069c1John McCall  // C++0x [temp.deduct.call]p6:
1352eff92135d32039c9874dc356f3e93143af6069c1John McCall  //   When P is a function type, pointer to function type, or pointer
1353eff92135d32039c9874dc356f3e93143af6069c1John McCall  //   to member function type:
1354eff92135d32039c9874dc356f3e93143af6069c1John McCall
1355eff92135d32039c9874dc356f3e93143af6069c1John McCall  if (!ParamType->isFunctionType() &&
1356eff92135d32039c9874dc356f3e93143af6069c1John McCall      !ParamType->isFunctionPointerType() &&
1357eff92135d32039c9874dc356f3e93143af6069c1John McCall      !ParamType->isMemberFunctionPointerType())
1358eff92135d32039c9874dc356f3e93143af6069c1John McCall    return QualType();
1359eff92135d32039c9874dc356f3e93143af6069c1John McCall
1360eff92135d32039c9874dc356f3e93143af6069c1John McCall  QualType Match;
13617bb12da2b0749eeebb21854c77877736969e59f2John McCall  for (UnresolvedSetIterator I = Ovl->decls_begin(),
13627bb12da2b0749eeebb21854c77877736969e59f2John McCall         E = Ovl->decls_end(); I != E; ++I) {
1363eff92135d32039c9874dc356f3e93143af6069c1John McCall    NamedDecl *D = (*I)->getUnderlyingDecl();
1364eff92135d32039c9874dc356f3e93143af6069c1John McCall
1365eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   - If the argument is an overload set containing one or more
1366eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     function templates, the parameter is treated as a
1367eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     non-deduced context.
1368eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (isa<FunctionTemplateDecl>(D))
1369eff92135d32039c9874dc356f3e93143af6069c1John McCall      return QualType();
1370eff92135d32039c9874dc356f3e93143af6069c1John McCall
1371eff92135d32039c9874dc356f3e93143af6069c1John McCall    FunctionDecl *Fn = cast<FunctionDecl>(D);
1372eff92135d32039c9874dc356f3e93143af6069c1John McCall    QualType ArgType = GetTypeOfFunction(S.Context, isAddressOfOperand, Fn);
1373eff92135d32039c9874dc356f3e93143af6069c1John McCall
1374eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   - If the argument is an overload set (not containing function
1375eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     templates), trial argument deduction is attempted using each
1376eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     of the members of the set. If deduction succeeds for only one
1377eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     of the overload set members, that member is used as the
1378eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     argument value for the deduction. If deduction succeeds for
1379eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     more than one member of the overload set the parameter is
1380eff92135d32039c9874dc356f3e93143af6069c1John McCall    //     treated as a non-deduced context.
1381eff92135d32039c9874dc356f3e93143af6069c1John McCall
1382eff92135d32039c9874dc356f3e93143af6069c1John McCall    // We do all of this in a fresh context per C++0x [temp.deduct.type]p2:
1383eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   Type deduction is done independently for each P/A pair, and
1384eff92135d32039c9874dc356f3e93143af6069c1John McCall    //   the deduced template argument values are then combined.
1385eff92135d32039c9874dc356f3e93143af6069c1John McCall    // So we do not reject deductions which were made elsewhere.
1386eff92135d32039c9874dc356f3e93143af6069c1John McCall    llvm::SmallVector<TemplateArgument, 8> Deduced(TemplateParams->size());
1387eff92135d32039c9874dc356f3e93143af6069c1John McCall    Sema::TemplateDeductionInfo Info(S.Context);
1388eff92135d32039c9874dc356f3e93143af6069c1John McCall    unsigned TDF = 0;
1389eff92135d32039c9874dc356f3e93143af6069c1John McCall
1390eff92135d32039c9874dc356f3e93143af6069c1John McCall    Sema::TemplateDeductionResult Result
1391a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      = DeduceTemplateArguments(S, TemplateParams,
1392eff92135d32039c9874dc356f3e93143af6069c1John McCall                                ParamType, ArgType,
1393eff92135d32039c9874dc356f3e93143af6069c1John McCall                                Info, Deduced, TDF);
1394eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (Result) continue;
1395eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (!Match.isNull()) return QualType();
1396eff92135d32039c9874dc356f3e93143af6069c1John McCall    Match = ArgType;
1397eff92135d32039c9874dc356f3e93143af6069c1John McCall  }
1398eff92135d32039c9874dc356f3e93143af6069c1John McCall
1399eff92135d32039c9874dc356f3e93143af6069c1John McCall  return Match;
1400eff92135d32039c9874dc356f3e93143af6069c1John McCall}
1401eff92135d32039c9874dc356f3e93143af6069c1John McCall
1402e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \brief Perform template argument deduction from a function call
1403e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// (C++ [temp.deduct.call]).
1404e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1405e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param FunctionTemplate the function template for which we are performing
1406e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// template argument deduction.
1407e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
140848026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// \param ExplicitTemplateArguments the explicit template arguments provided
140948026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// for this call.
14106db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor///
1411e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Args the function call arguments
1412e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1413e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param NumArgs the number of arguments in Args
1414e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
141548026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// \param Name the name of the function being called. This is only significant
141648026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// when the function template is a conversion function template, in which
141748026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// case this routine will also perform template argument deduction based on
141848026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// the function to which
141948026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor///
1420e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Specialization if template argument deduction was successful,
14211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// this will be set to the function template specialization produced by
1422e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// template argument deduction.
1423e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1424e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Info the argument will be updated to provide additional information
1425e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// about template argument deduction.
1426e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor///
1427e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns the result of template argument deduction.
1428e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorSema::TemplateDeductionResult
1429e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
143048026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor                          const TemplateArgumentListInfo *ExplicitTemplateArgs,
1431e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                              Expr **Args, unsigned NumArgs,
1432e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                              FunctionDecl *&Specialization,
1433e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor                              TemplateDeductionInfo &Info) {
1434e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
14356db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor
1436e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  // C++ [temp.deduct.call]p1:
1437e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  //   Template argument deduction is done by comparing each function template
1438e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  //   parameter type (call it P) with the type of the corresponding argument
1439e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  //   of the call (call it A) as described below.
1440e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  unsigned CheckArgs = NumArgs;
14416db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  if (NumArgs < Function->getMinRequiredArguments())
1442e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    return TDK_TooFewArguments;
1443e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  else if (NumArgs > Function->getNumParams()) {
14441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const FunctionProtoType *Proto
1445183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall      = Function->getType()->getAs<FunctionProtoType>();
1446e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (!Proto->isVariadic())
1447e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return TDK_TooManyArguments;
14481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1449e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    CheckArgs = Function->getNumParams();
1450e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  }
14511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14526db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  // The types of the parameters from which we will perform template argument
14536db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  // deduction.
1454e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  TemplateParameterList *TemplateParams
1455e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    = FunctionTemplate->getTemplateParameters();
14566db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  llvm::SmallVector<TemplateArgument, 4> Deduced;
14576db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  llvm::SmallVector<QualType, 4> ParamTypes;
1458d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (ExplicitTemplateArgs) {
145983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    TemplateDeductionResult Result =
146083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      SubstituteExplicitTemplateArguments(FunctionTemplate,
1461d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                          *ExplicitTemplateArgs,
146283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          Deduced,
146383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          ParamTypes,
146483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          0,
146583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                          Info);
146683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    if (Result)
146783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return Result;
14686db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  } else {
14696db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor    // Just fill in the parameter types from the function declaration.
14706db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor    for (unsigned I = 0; I != CheckArgs; ++I)
14716db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor      ParamTypes.push_back(Function->getParamDecl(I)->getType());
14726db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  }
14731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
14746db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor  // Deduce template arguments from the function parameters.
14751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Deduced.resize(TemplateParams->size());
1476e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  for (unsigned I = 0; I != CheckArgs; ++I) {
14776db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor    QualType ParamType = ParamTypes[I];
1478e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    QualType ArgType = Args[I]->getType();
14791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1480eff92135d32039c9874dc356f3e93143af6069c1John McCall    // Overload sets usually make this parameter an undeduced
1481eff92135d32039c9874dc356f3e93143af6069c1John McCall    // context, but there are sometimes special circumstances.
1482eff92135d32039c9874dc356f3e93143af6069c1John McCall    if (ArgType == Context.OverloadTy) {
1483eff92135d32039c9874dc356f3e93143af6069c1John McCall      ArgType = ResolveOverloadForDeduction(*this, TemplateParams,
1484eff92135d32039c9874dc356f3e93143af6069c1John McCall                                            Args[I], ParamType);
1485eff92135d32039c9874dc356f3e93143af6069c1John McCall      if (ArgType.isNull())
1486eff92135d32039c9874dc356f3e93143af6069c1John McCall        continue;
1487eff92135d32039c9874dc356f3e93143af6069c1John McCall    }
1488eff92135d32039c9874dc356f3e93143af6069c1John McCall
1489e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    // C++ [temp.deduct.call]p2:
1490e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    //   If P is not a reference type:
1491e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    QualType CanonParamType = Context.getCanonicalType(ParamType);
1492500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor    bool ParamWasReference = isa<ReferenceType>(CanonParamType);
1493500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor    if (!ParamWasReference) {
14941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   - If A is an array type, the pointer type produced by the
14951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //     array-to-pointer standard conversion (4.2) is used in place of
1496e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      //     A for type deduction; otherwise,
1497e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      if (ArgType->isArrayType())
1498e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        ArgType = Context.getArrayDecayedType(ArgType);
14991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   - If A is a function type, the pointer type produced by the
15001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //     function-to-pointer standard conversion (4.3) is used in place
1501e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      //     of A for type deduction; otherwise,
1502e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      else if (ArgType->isFunctionType())
1503e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        ArgType = Context.getPointerType(ArgType);
1504e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      else {
1505e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        // - If A is a cv-qualified type, the top level cv-qualifiers of A’s
1506e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        //   type are ignored for type deduction.
1507e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        QualType CanonArgType = Context.getCanonicalType(ArgType);
1508a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor        if (CanonArgType.getLocalCVRQualifiers())
1509a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor          ArgType = CanonArgType.getLocalUnqualifiedType();
1510e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      }
1511e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    }
15121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1513e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    // C++0x [temp.deduct.call]p3:
1514e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    //   If P is a cv-qualified type, the top level cv-qualifiers of P’s type
15151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //   are ignored for type deduction.
1516a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor    if (CanonParamType.getLocalCVRQualifiers())
1517a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor      ParamType = CanonParamType.getLocalUnqualifiedType();
15186217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek    if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) {
15191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   [...] If P is a reference type, the type referred to by P is used
15201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   for type deduction.
1521e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      ParamType = ParamRefType->getPointeeType();
15221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
15231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   [...] If P is of the form T&&, where T is a template parameter, and
15241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump      //   the argument is an lvalue, the type A& is used in place of A for
1525e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      //   type deduction.
1526e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      if (isa<RValueReferenceType>(ParamRefType) &&
1527183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall          ParamRefType->getAs<TemplateTypeParmType>() &&
1528e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor          Args[I]->isLvalue(Context) == Expr::LV_Valid)
1529e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor        ArgType = Context.getLValueReferenceType(ArgType);
1530e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    }
15311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1532e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    // C++0x [temp.deduct.call]p4:
1533e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    //   In general, the deduction process attempts to find template argument
1534e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    //   values that will make the deduced A identical to A (after the type A
1535e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    //   is transformed as described above). [...]
15361282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor    unsigned TDF = TDF_SkipNonDependent;
15371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1538508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    //     - If the original P is a reference type, the deduced A (i.e., the
1539508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    //       type referred to by the reference) can be more cv-qualified than
1540508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    //       the transformed A.
1541508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    if (ParamWasReference)
1542508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor      TDF |= TDF_ParamWithReferenceType;
15431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     - The transformed A can be another pointer or pointer to member
15441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //       type that can be converted to the deduced A via a qualification
1545508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    //       conversion (4.4).
1546508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor    if (ArgType->isPointerType() || ArgType->isMemberPointerType())
1547508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor      TDF |= TDF_IgnoreQualifiers;
15481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     - If P is a class and P has the form simple-template-id, then the
15494112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    //       transformed A can be a derived class of the deduced A. Likewise,
15504112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    //       if P is a pointer to a class of the form simple-template-id, the
15514112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    //       transformed A can be a pointer to a derived class pointed to by
15524112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    //       the deduced A.
15534112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor    if (isSimpleTemplateIdType(ParamType) ||
15541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump        (isa<PointerType>(ParamType) &&
15554112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor         isSimpleTemplateIdType(
15566217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek                              ParamType->getAs<PointerType>()->getPointeeType())))
15574112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor      TDF |= TDF_DerivedClass;
15581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1559e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor    if (TemplateDeductionResult Result
1560a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = ::DeduceTemplateArguments(*this, TemplateParams,
1561500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor                                    ParamType, ArgType, Info, Deduced,
1562508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor                                    TDF))
1563e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor      return Result;
15641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
156565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    // FIXME: we need to check that the deduced A is the same as A,
156665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    // modulo the various allowed differences.
1567e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor  }
156865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
15691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
157083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                         Specialization, Info);
157183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor}
1572127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor
157383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \brief Deduce template arguments when taking the address of a function
15744b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to
15754b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// a template.
157683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
157783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionTemplate the function template for which we are performing
157883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template argument deduction.
157983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
15804b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param ExplicitTemplateArguments the explicitly-specified template
15814b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// arguments.
158283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
158383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param ArgFunctionType the function type that will be used as the
158483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// "argument" type (A) when performing template argument deduction from the
15854b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// function template's function type. This type may be NULL, if there is no
15864b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// argument type to compare against, in C++0x [temp.arg.explicit]p3.
158783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
158883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Specialization if template argument deduction was successful,
15891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// this will be set to the function template specialization produced by
159083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template argument deduction.
159183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
159283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Info the argument will be updated to provide additional information
159383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// about template argument deduction.
159483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor///
159583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \returns the result of template argument deduction.
159683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::TemplateDeductionResult
159783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
1598d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                        const TemplateArgumentListInfo *ExplicitTemplateArgs,
159983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                              QualType ArgFunctionType,
160083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                              FunctionDecl *&Specialization,
160183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                              TemplateDeductionInfo &Info) {
160283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
160383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  TemplateParameterList *TemplateParams
160483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor    = FunctionTemplate->getTemplateParameters();
160583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  QualType FunctionType = Function->getType();
16061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
160783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Substitute any explicit template arguments.
160883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  llvm::SmallVector<TemplateArgument, 4> Deduced;
160983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  llvm::SmallVector<QualType, 4> ParamTypes;
1610d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall  if (ExplicitTemplateArgs) {
16111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    if (TemplateDeductionResult Result
16121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump          = SubstituteExplicitTemplateArguments(FunctionTemplate,
1613d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall                                                *ExplicitTemplateArgs,
16141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump                                                Deduced, ParamTypes,
161583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                                &FunctionType, Info))
161683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor      return Result;
1617127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor  }
161883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
161983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Template argument deduction for function templates in a SFINAE context.
162083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor  // Trap any errors that might occur.
16211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SFINAETrap Trap(*this);
16221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
1623eff92135d32039c9874dc356f3e93143af6069c1John McCall  Deduced.resize(TemplateParams->size());
1624eff92135d32039c9874dc356f3e93143af6069c1John McCall
16254b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  if (!ArgFunctionType.isNull()) {
16264b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    // Deduce template arguments from the function type.
16274b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor    if (TemplateDeductionResult Result
1628a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth          = ::DeduceTemplateArguments(*this, TemplateParams,
16294b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                                      FunctionType, ArgFunctionType, Info,
16304b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                                      Deduced, 0))
16314b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor      return Result;
16324b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  }
16334b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor
16341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced,
163583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor                                         Specialization, Info);
1636e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor}
1637e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor
163865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \brief Deduce template arguments for a templated conversion
163965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// function (C++ [temp.deduct.conv]) and, if successful, produce a
164065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// conversion function template specialization.
164165ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::TemplateDeductionResult
164265ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
164365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                              QualType ToType,
164465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                              CXXConversionDecl *&Specialization,
164565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                              TemplateDeductionInfo &Info) {
16461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  CXXConversionDecl *Conv
164765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl());
164865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  QualType FromType = Conv->getConversionType();
164965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
165065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Canonicalize the types for deduction.
165165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  QualType P = Context.getCanonicalType(FromType);
165265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  QualType A = Context.getCanonicalType(ToType);
165365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
165465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++0x [temp.deduct.conv]p3:
165565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   If P is a reference type, the type referred to by P is used for
165665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   type deduction.
165765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (const ReferenceType *PRef = P->getAs<ReferenceType>())
165865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    P = PRef->getPointeeType();
165965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
166065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++0x [temp.deduct.conv]p3:
166165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   If A is a reference type, the type referred to by A is used
166265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   for type deduction.
166365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (const ReferenceType *ARef = A->getAs<ReferenceType>())
166465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    A = ARef->getPointeeType();
166565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++ [temp.deduct.conv]p2:
166665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //
16671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  //   If A is not a reference type:
166865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  else {
166965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    assert(!A->isReferenceType() && "Reference types were handled above");
167065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
167165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   - If P is an array type, the pointer type produced by the
16721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    //     array-to-pointer standard conversion (4.2) is used in place
167365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     of P for type deduction; otherwise,
167465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    if (P->isArrayType())
167565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      P = Context.getArrayDecayedType(P);
167665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   - If P is a function type, the pointer type produced by the
167765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     function-to-pointer standard conversion (4.3) is used in
167865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     place of P for type deduction; otherwise,
167965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    else if (P->isFunctionType())
168065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      P = Context.getPointerType(P);
168165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   - If P is a cv-qualified type, the top level cv-qualifiers of
168265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //     P’s type are ignored for type deduction.
168365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    else
168465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      P = P.getUnqualifiedType();
168565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
168665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    // C++0x [temp.deduct.conv]p3:
168765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   If A is a cv-qualified type, the top level cv-qualifiers of A’s
168865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    //   type are ignored for type deduction.
168965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    A = A.getUnqualifiedType();
169065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  }
169165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
169265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Template argument deduction for function templates in a SFINAE context.
169365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Trap any errors that might occur.
16941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  SFINAETrap Trap(*this);
169565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
169665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++ [temp.deduct.conv]p1:
169765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   Template argument deduction is done by comparing the return
169865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   type of the template conversion function (call it P) with the
169965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   type that is required as the result of the conversion (call it
170065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   A) as described in 14.8.2.4.
170165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  TemplateParameterList *TemplateParams
170265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    = FunctionTemplate->getTemplateParameters();
170365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  llvm::SmallVector<TemplateArgument, 4> Deduced;
17041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  Deduced.resize(TemplateParams->size());
170565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
170665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // C++0x [temp.deduct.conv]p4:
170765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   In general, the deduction process attempts to find template
170865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   argument values that will make the deduced A identical to
170965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //   A. However, there are two cases that allow a difference:
171065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  unsigned TDF = 0;
171165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //     - If the original A is a reference type, A can be more
171265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       cv-qualified than the deduced A (i.e., the type referred to
171365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       by the reference)
171465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (ToType->isReferenceType())
171565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    TDF |= TDF_ParamWithReferenceType;
171665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //     - The deduced A can be another pointer or pointer to member
171765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       type that can be converted to A via a qualification
171865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //       conversion.
171965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  //
172065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when
172165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // both P and A are pointers or member pointers. In this case, we
172265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // just ignore cv-qualifiers completely).
172365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if ((P->isPointerType() && A->isPointerType()) ||
172465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor      (P->isMemberPointerType() && P->isMemberPointerType()))
172565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    TDF |= TDF_IgnoreQualifiers;
172665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (TemplateDeductionResult Result
1727a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth        = ::DeduceTemplateArguments(*this, TemplateParams,
172865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                    P, A, Info, Deduced, TDF))
172965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    return Result;
173065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
173165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // FIXME: we need to check that the deduced A is the same as A,
173265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // modulo the various allowed differences.
17331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
173465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  // Finish template argument deduction.
173565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  FunctionDecl *Spec = 0;
173665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  TemplateDeductionResult Result
173765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, Spec, Info);
173865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  Specialization = cast_or_null<CXXConversionDecl>(Spec);
173965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  return Result;
174065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor}
174165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor
17424b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \brief Deduce template arguments for a function template when there is
17434b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// nothing to deduce against (C++0x [temp.arg.explicit]p3).
17444b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
17454b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param FunctionTemplate the function template for which we are performing
17464b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template argument deduction.
17474b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
17484b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param ExplicitTemplateArguments the explicitly-specified template
17494b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// arguments.
17504b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
17514b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param Specialization if template argument deduction was successful,
17524b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// this will be set to the function template specialization produced by
17534b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template argument deduction.
17544b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
17554b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param Info the argument will be updated to provide additional information
17564b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// about template argument deduction.
17574b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor///
17584b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \returns the result of template argument deduction.
17594b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas GregorSema::TemplateDeductionResult
17604b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate,
17614b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                           const TemplateArgumentListInfo *ExplicitTemplateArgs,
17624b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                              FunctionDecl *&Specialization,
17634b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                              TemplateDeductionInfo &Info) {
17644b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor  return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs,
17654b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor                                 QualType(), Specialization, Info);
17664b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor}
17674b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor
17688a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Stores the result of comparing the qualifiers of two types.
17698a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorenum DeductionQualifierComparison {
17708a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  NeitherMoreQualified = 0,
17718a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  ParamMoreQualified,
17728a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  ArgMoreQualified
17738a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor};
17748a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
17758a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Deduce the template arguments during partial ordering by comparing
17768a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// the parameter type and the argument type (C++0x [temp.deduct.partial]).
17778a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
1778a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth/// \param S the semantic analysis object within which we are deducing
17798a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
17808a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param TemplateParams the template parameters that we are deducing
17818a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
17828a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param ParamIn the parameter type
17838a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
17848a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param ArgIn the argument type
17858a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
17868a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param Info information about the template argument deduction itself
17878a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
17888a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param Deduced the deduced template arguments
17898a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor///
17908a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \returns the result of template argument deduction so far. Note that a
17918a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// "success" result means that template argument deduction has not yet failed,
17928a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// but it may still fail, later, for other reasons.
17938a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic Sema::TemplateDeductionResult
1794a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArgumentsDuringPartialOrdering(Sema &S,
17958a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                          TemplateParameterList *TemplateParams,
17968a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                             QualType ParamIn, QualType ArgIn,
17978a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                             Sema::TemplateDeductionInfo &Info,
17988a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                             llvm::SmallVectorImpl<TemplateArgument> &Deduced,
17998a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
1800a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  CanQualType Param = S.Context.getCanonicalType(ParamIn);
1801a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  CanQualType Arg = S.Context.getCanonicalType(ArgIn);
18028a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
18038a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p5:
18048a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   Before the partial ordering is done, certain transformations are
18058a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   performed on the types used for partial ordering:
18068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //     - If P is a reference type, P is replaced by the type referred to.
18078a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>();
1808e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  if (!ParamRef.isNull())
18098a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    Param = ParamRef->getPointeeType();
18108a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
18118a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //     - If A is a reference type, A is replaced by the type referred to.
18128a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>();
1813e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  if (!ArgRef.isNull())
18148a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    Arg = ArgRef->getPointeeType();
18158a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
1816e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall  if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) {
18178a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // C++0x [temp.deduct.partial]p6:
18188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   If both P and A were reference types (before being replaced with the
18198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   type referred to above), determine which of the two types (if any) is
18208a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   more cv-qualified than the other; otherwise the types are considered to
18218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   be equally cv-qualified for partial ordering purposes. The result of this
18228a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   determination will be used below.
18238a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //
18248a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // We save this information for later, using it only when deduction
18258a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // succeeds in both directions.
18268a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    DeductionQualifierComparison QualifierResult = NeitherMoreQualified;
18278a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    if (Param.isMoreQualifiedThan(Arg))
18288a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      QualifierResult = ParamMoreQualified;
18298a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    else if (Arg.isMoreQualifiedThan(Param))
18308a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      QualifierResult = ArgMoreQualified;
18318a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    QualifierComparisons->push_back(QualifierResult);
18328a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
18338a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
18348a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p7:
18358a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   Remove any top-level cv-qualifiers:
18368a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //     - If P is a cv-qualified type, P is replaced by the cv-unqualified
18378a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //       version of P.
18388a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Param = Param.getUnqualifiedType();
18398a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //     - If A is a cv-qualified type, A is replaced by the cv-unqualified
18408a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //       version of A.
18418a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Arg = Arg.getUnqualifiedType();
18428a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
18438a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p8:
18448a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   Using the resulting types P and A the deduction is then done as
18458a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   described in 14.9.2.5. If deduction succeeds for a given type, the type
18468a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   from the argument template is considered to be at least as specialized
18478a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   as the type from the parameter template.
1848a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  return DeduceTemplateArguments(S, TemplateParams, Param, Arg, Info,
18498a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                 Deduced, TDF_None);
18508a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor}
18518a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
18528a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic void
1853e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, QualType T,
1854e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
1855ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Level,
1856e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Deduced);
18578a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
18588a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Determine whether the function template \p FT1 is at least as
18598a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// specialized as \p FT2.
18608a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic bool isAtLeastAsSpecializedAs(Sema &S,
18618a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                     FunctionTemplateDecl *FT1,
18628a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                     FunctionTemplateDecl *FT2,
18638a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                     TemplatePartialOrderingContext TPOC,
18648a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) {
18658a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  FunctionDecl *FD1 = FT1->getTemplatedDecl();
18668a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  FunctionDecl *FD2 = FT2->getTemplatedDecl();
18678a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>();
18688a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>();
18698a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
18708a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  assert(Proto1 && Proto2 && "Function templates must have prototypes");
18718a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  TemplateParameterList *TemplateParams = FT2->getTemplateParameters();
18728a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  llvm::SmallVector<TemplateArgument, 4> Deduced;
18738a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Deduced.resize(TemplateParams->size());
18748a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
18758a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p3:
18768a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   The types used to determine the ordering depend on the context in which
18778a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   the partial ordering is done:
18788a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Sema::TemplateDeductionInfo Info(S.Context);
18798a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  switch (TPOC) {
18808a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Call: {
18818a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   - In the context of a function call, the function parameter types are
18828a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //     used.
18838a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
18848a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    for (unsigned I = 0; I != NumParams; ++I)
1885a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth      if (DeduceTemplateArgumentsDuringPartialOrdering(S,
18868a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                       TemplateParams,
18878a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                       Proto2->getArgType(I),
18888a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                       Proto1->getArgType(I),
18898a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                       Info,
18908a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                       Deduced,
18918a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                       QualifierComparisons))
18928a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        return false;
18938a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
18948a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
18958a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
18968a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
18978a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Conversion:
18988a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   - In the context of a call to a conversion operator, the return types
18998a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //     of the conversion function templates are used.
1900a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth    if (DeduceTemplateArgumentsDuringPartialOrdering(S,
19018a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     TemplateParams,
19028a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     Proto2->getResultType(),
19038a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     Proto1->getResultType(),
19048a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     Info,
19058a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     Deduced,
19068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     QualifierComparisons))
19078a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      return false;
19088a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
19098a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19108a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Other:
19118a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   - In other contexts (14.6.6.2) the function template’s function type
19128a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //     is used.
1913a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth    if (DeduceTemplateArgumentsDuringPartialOrdering(S,
19148a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     TemplateParams,
19158a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     FD2->getType(),
19168a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     FD1->getType(),
19178a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     Info,
19188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     Deduced,
19198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                                     QualifierComparisons))
19208a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      return false;
19218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
19228a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
19238a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19248a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p11:
19258a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   In most cases, all template parameters must have values in order for
19268a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   deduction to succeed, but for partial ordering purposes a template
19278a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   parameter may remain without a value provided it is not used in the
19288a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   types being used for partial ordering. [ Note: a template parameter used
19298a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   in a non-deduced context is considered used. -end note]
19308a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  unsigned ArgIdx = 0, NumArgs = Deduced.size();
19318a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  for (; ArgIdx != NumArgs; ++ArgIdx)
19328a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    if (Deduced[ArgIdx].isNull())
19338a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      break;
19348a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19358a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  if (ArgIdx == NumArgs) {
19368a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // All template arguments were deduced. FT1 is at least as specialized
19378a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // as FT2.
19388a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return true;
19398a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
19408a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
1941e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // Figure out which template parameters were used.
19428a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  llvm::SmallVector<bool, 4> UsedParameters;
19438a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  UsedParameters.resize(TemplateParams->size());
19448a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  switch (TPOC) {
19458a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Call: {
19468a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs());
19478a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    for (unsigned I = 0; I != NumParams; ++I)
1948ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false,
1949ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                   TemplateParams->getDepth(),
1950e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                   UsedParameters);
19518a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
19528a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
19538a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19548a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Conversion:
1955ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false,
1956ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 TemplateParams->getDepth(),
1957e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                 UsedParameters);
19588a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
19598a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19608a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  case TPOC_Other:
1961ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    ::MarkUsedTemplateParameters(S, FD2->getType(), false,
1962ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 TemplateParams->getDepth(),
1963ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 UsedParameters);
19648a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    break;
19658a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
19668a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19678a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  for (; ArgIdx != NumArgs; ++ArgIdx)
19688a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // If this argument had no value deduced but was used in one of the types
19698a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // used for partial ordering, then deduction fails.
19708a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx])
19718a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      return false;
19728a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19738a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  return true;
19748a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor}
19758a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19768a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
1977bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \brief Returns the more specialized function template according
197865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// to the rules of function template partial ordering (C++ [temp.func.order]).
197965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor///
198065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \param FT1 the first function template
198165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor///
198265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \param FT2 the second function template
198365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor///
19848a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param TPOC the context in which we are performing partial ordering of
19858a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// function templates.
19861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump///
1987bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \returns the more specialized function template. If neither
198865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// template is more specialized, returns NULL.
198965ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorFunctionTemplateDecl *
199065ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1,
199165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor                                 FunctionTemplateDecl *FT2,
19928a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                 TemplatePartialOrderingContext TPOC) {
19938a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons;
19948a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  bool Better1 = isAtLeastAsSpecializedAs(*this, FT1, FT2, TPOC, 0);
19958a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  bool Better2 = isAtLeastAsSpecializedAs(*this, FT2, FT1, TPOC,
19968a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor                                          &QualifierComparisons);
19978a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
19988a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  if (Better1 != Better2) // We have a clear winner
19998a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return Better1? FT1 : FT2;
20008a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20018a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  if (!Better1 && !Better2) // Neither is better than the other
200265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    return 0;
20038a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20048a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20058a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  // C++0x [temp.deduct.partial]p10:
20068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   If for each type being considered a given template is at least as
20078a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   specialized for all types and more specialized for some set of types and
20088a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   the other template is not more specialized for any types or is not at
20098a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   least as specialized for any types, then the given template is more
20108a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   specialized than the other template. Otherwise, neither template is more
20118a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  //   specialized than the other.
20128a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Better1 = false;
20138a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  Better2 = false;
20148a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) {
20158a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    // C++0x [temp.deduct.partial]p9:
20168a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   If, for a given type, deduction succeeds in both directions (i.e., the
20178a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   types are identical after the transformations above) and if the type
20188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   from the argument template is more cv-qualified than the type from the
20198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   parameter template (as described above) that type is considered to be
20208a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   more specialized than the other. If neither type is more cv-qualified
20218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    //   than the other then neither type is more specialized than the other.
20228a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    switch (QualifierComparisons[I]) {
20238a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      case NeitherMoreQualified:
20248a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        break;
20258a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20268a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      case ParamMoreQualified:
20278a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        Better1 = true;
20288a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        if (Better2)
20298a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor          return 0;
20308a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        break;
20318a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20328a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor      case ArgMoreQualified:
20338a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        Better2 = true;
20348a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        if (Better1)
20358a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor          return 0;
20368a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor        break;
20378a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    }
20388a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  }
20398a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor
20408a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  assert(!(Better1 && Better2) && "Should have broken out in the loop above");
204165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor  if (Better1)
204265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor    return FT1;
20438a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  else if (Better2)
20448a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return FT2;
20458a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor  else
20468a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor    return 0;
204765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor}
204883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor
2049d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \brief Determine if the two templates are equivalent.
2050d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregorstatic bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) {
2051d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  if (T1 == T2)
2052d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    return true;
2053d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2054d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  if (!T1 || !T2)
2055d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    return false;
2056d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2057d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  return T1->getCanonicalDecl() == T2->getCanonicalDecl();
2058d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor}
2059d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2060d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \brief Retrieve the most specialized of the given function template
2061d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specializations.
2062d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2063c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// \param SpecBegin the start iterator of the function template
2064c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// specializations that we will be comparing.
2065d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2066c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// \param SpecEnd the end iterator of the function template
2067c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// specializations, paired with \p SpecBegin.
2068d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2069d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param TPOC the partial ordering context to use to compare the function
2070d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// template specializations.
2071d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2072d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param Loc the location where the ambiguity or no-specializations
2073d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// diagnostic should occur.
2074d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2075d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param NoneDiag partial diagnostic used to diagnose cases where there are
2076d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// no matching candidates.
2077d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2078d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one
2079d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// occurs.
2080d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2081d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param CandidateDiag partial diagnostic used for each function template
2082d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specialization that is a candidate in the ambiguous ordering. One parameter
2083d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// in this diagnostic should be unbound, which will correspond to the string
2084d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// describing the template arguments for the function template specialization.
2085d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2086d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param Index if non-NULL and the result of this function is non-nULL,
2087d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// receives the index corresponding to the resulting function template
2088d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specialization.
2089d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2090d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \returns the most specialized function template specialization, if
2091c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// found. Otherwise, returns SpecEnd.
2092d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor///
2093d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \todo FIXME: Consider passing in the "also-ran" candidates that failed
2094d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// template argument deduction.
2095c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallUnresolvedSetIterator
2096c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallSema::getMostSpecialized(UnresolvedSetIterator SpecBegin,
2097c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         UnresolvedSetIterator SpecEnd,
2098c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         TemplatePartialOrderingContext TPOC,
2099c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         SourceLocation Loc,
2100c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         const PartialDiagnostic &NoneDiag,
2101c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         const PartialDiagnostic &AmbigDiag,
2102c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                         const PartialDiagnostic &CandidateDiag) {
2103c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  if (SpecBegin == SpecEnd) {
2104d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    Diag(Loc, NoneDiag);
2105c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    return SpecEnd;
2106d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
2107d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2108c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  if (SpecBegin + 1 == SpecEnd)
2109c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    return SpecBegin;
2110d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2111d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // Find the function template that is better than all of the templates it
2112d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // has been compared to.
2113c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  UnresolvedSetIterator Best = SpecBegin;
2114d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  FunctionTemplateDecl *BestTemplate
2115c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    = cast<FunctionDecl>(*Best)->getPrimaryTemplate();
2116d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  assert(BestTemplate && "Not a function template specialization?");
2117c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) {
2118c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    FunctionTemplateDecl *Challenger
2119c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall      = cast<FunctionDecl>(*I)->getPrimaryTemplate();
2120d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    assert(Challenger && "Not a function template specialization?");
2121c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
2122d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor                                                  TPOC),
2123d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor                       Challenger)) {
2124d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      Best = I;
2125d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      BestTemplate = Challenger;
2126d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    }
2127d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
2128d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2129d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // Make sure that the "best" function template is more specialized than all
2130d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // of the others.
2131d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  bool Ambiguous = false;
2132c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) {
2133c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    FunctionTemplateDecl *Challenger
2134c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall      = cast<FunctionDecl>(*I)->getPrimaryTemplate();
2135d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    if (I != Best &&
2136d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor        !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger,
2137d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor                                                  TPOC),
2138d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor                        BestTemplate)) {
2139d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      Ambiguous = true;
2140d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      break;
2141d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    }
2142d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
2143d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2144d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  if (!Ambiguous) {
2145d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor    // We found an answer. Return it.
2146c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    return Best;
2147d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  }
2148d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2149d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // Diagnose the ambiguity.
2150d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  Diag(Loc, AmbigDiag);
2151d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2152d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor  // FIXME: Can we order the candidates in some sane way?
2153c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I)
2154c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall    Diag((*I)->getLocation(), CandidateDiag)
2155d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor      << getTemplateArgumentBindingsText(
2156c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall        cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(),
2157c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall                    *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs());
2158d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2159c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall  return SpecEnd;
2160d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor}
2161d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor
2162bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \brief Returns the more specialized class template partial specialization
2163bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// according to the rules of partial ordering of class template partial
2164bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// specializations (C++ [temp.class.order]).
2165bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor///
2166bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \param PS1 the first class template partial specialization
2167bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor///
2168bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \param PS2 the second class template partial specialization
2169bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor///
2170bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \returns the more specialized class template partial specialization. If
2171bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// neither partial specialization is more specialized, returns NULL.
2172bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas GregorClassTemplatePartialSpecializationDecl *
2173bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas GregorSema::getMoreSpecializedPartialSpecialization(
2174bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                  ClassTemplatePartialSpecializationDecl *PS1,
2175bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                  ClassTemplatePartialSpecializationDecl *PS2) {
2176bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // C++ [temp.class.order]p1:
2177bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   For two class template partial specializations, the first is at least as
2178bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   specialized as the second if, given the following rewrite to two
2179bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   function templates, the first function template is at least as
2180bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   specialized as the second according to the ordering rules for function
2181bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //   templates (14.6.6.2):
2182bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //     - the first function template has the same template parameters as the
2183bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       first partial specialization and has a single function parameter
2184bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       whose type is a class template specialization with the template
2185bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       arguments of the first partial specialization, and
2186bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //     - the second function template has the same template parameters as the
2187bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       second partial specialization and has a single function parameter
2188bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       whose type is a class template specialization with the template
2189bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //       arguments of the second partial specialization.
2190bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  //
2191bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // Rather than synthesize function templates, we merely perform the
2192bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // equivalent partial ordering by performing deduction directly on the
2193bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // template arguments of the class template partial specializations. This
2194bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // computation is slightly simpler than the general problem of function
2195bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // template partial ordering, because class template partial specializations
2196bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // are more constrained. We know that every template parameter is deduc
2197bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  llvm::SmallVector<TemplateArgument, 4> Deduced;
2198bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  Sema::TemplateDeductionInfo Info(Context);
2199bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor
2200bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // Determine whether PS1 is at least as specialized as PS2
2201bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  Deduced.resize(PS2->getTemplateParameters()->size());
2202a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
2203bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                  PS2->getTemplateParameters(),
2204bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                  Context.getTypeDeclType(PS2),
2205bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                  Context.getTypeDeclType(PS1),
2206bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                               Info,
2207bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                               Deduced,
2208bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                               0);
2209bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor
2210bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  // Determine whether PS2 is at least as specialized as PS1
2211db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor  Deduced.clear();
2212bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  Deduced.resize(PS1->getTemplateParameters()->size());
2213a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth  bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(*this,
2214bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                  PS1->getTemplateParameters(),
2215bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                  Context.getTypeDeclType(PS1),
2216bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                  Context.getTypeDeclType(PS2),
2217bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                               Info,
2218bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                               Deduced,
2219bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor                                                               0);
2220bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor
2221bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  if (Better1 == Better2)
2222bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor    return 0;
2223bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor
2224bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor  return Better1? PS1 : PS2;
2225bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor}
2226bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor
22271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
2228e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
2229e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           const TemplateArgument &TemplateArg,
2230e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2231ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
2232e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used);
2233031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2234e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
2235031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// expression.
22361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
2237e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
2238e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           const Expr *E,
2239e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2240ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
2241e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
2242e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to
2243e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // find other occurrences of template parameters.
2244f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor  const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E);
2245c781f9cd854f3d5d1c826f4a13382c6abca4cff7Douglas Gregor  if (!DRE)
2246031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    return;
2247031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
22481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  const NonTypeTemplateParmDecl *NTTP
2249031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl());
2250031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  if (!NTTP)
2251031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    return;
2252031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2253ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  if (NTTP->getDepth() == Depth)
2254ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    Used[NTTP->getIndex()] = true;
2255031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
2256031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2257e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
2258e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// nested name specifier.
2259e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregorstatic void
2260e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
2261e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           NestedNameSpecifier *NNS,
2262e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2263ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
2264e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
2265e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (!NNS)
2266e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    return;
2267e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
2268ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth,
2269ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             Used);
2270e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0),
2271ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                             OnlyDeduced, Depth, Used);
2272e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor}
2273e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
2274e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
2275e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// template name.
2276e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregorstatic void
2277e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
2278e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           TemplateName Name,
2279e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2280ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
2281e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
2282e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (TemplateDecl *Template = Name.getAsTemplateDecl()) {
2283e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    if (TemplateTemplateParmDecl *TTP
2284ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor          = dyn_cast<TemplateTemplateParmDecl>(Template)) {
2285ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      if (TTP->getDepth() == Depth)
2286ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor        Used[TTP->getIndex()] = true;
2287ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    }
2288e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    return;
2289e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  }
2290e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
2291788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName())
2292788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced,
2293788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                               Depth, Used);
2294e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (DependentTemplateName *DTN = Name.getAsDependentTemplateName())
2295ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced,
2296ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2297e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor}
2298e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
2299e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given
2300031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// type.
23011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
2302e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, QualType T,
2303e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2304ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
2305e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
2306e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  if (T.isNull())
2307e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    return;
2308e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
2309031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  // Non-dependent types have nothing deducible
2310031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  if (!T->isDependentType())
2311031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    return;
2312031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2313031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  T = SemaRef.Context.getCanonicalType(T);
2314031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  switch (T->getTypeClass()) {
2315031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Pointer:
2316e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
2317e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<PointerType>(T)->getPointeeType(),
2318e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               OnlyDeduced,
2319ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth,
2320e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               Used);
2321031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2322031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2323031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::BlockPointer:
2324e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
2325e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<BlockPointerType>(T)->getPointeeType(),
2326e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               OnlyDeduced,
2327ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth,
2328e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               Used);
2329031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2330031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2331031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::LValueReference:
2332031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::RValueReference:
2333e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
2334e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<ReferenceType>(T)->getPointeeType(),
2335e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               OnlyDeduced,
2336ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth,
2337e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               Used);
2338031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2339031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2340031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::MemberPointer: {
2341031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr());
2342e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced,
2343ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2344e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0),
2345ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
2346031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2347031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
2348031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2349031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::DependentSizedArray:
2350e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
2351e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<DependentSizedArrayType>(T)->getSizeExpr(),
2352ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
2353031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    // Fall through to check the element type
2354031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2355031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::ConstantArray:
2356031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::IncompleteArray:
2357e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
2358e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<ArrayType>(T)->getElementType(),
2359ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
2360031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2361031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2362031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Vector:
2363031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::ExtVector:
2364e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef,
2365e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                               cast<VectorType>(T)->getElementType(),
2366ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               OnlyDeduced, Depth, Used);
2367031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2368031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
23699cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  case Type::DependentSizedExtVector: {
23709cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    const DependentSizedExtVectorType *VecType
2371f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor      = cast<DependentSizedExtVectorType>(T);
2372e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced,
2373ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2374e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced,
2375ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
23769cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor    break;
23779cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor  }
23789cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor
2379031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::FunctionProto: {
2380f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor    const FunctionProtoType *Proto = cast<FunctionProtoType>(T);
2381e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced,
2382ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2383031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I)
2384e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor      MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced,
2385ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 Depth, Used);
2386031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2387031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
2388031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2389ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  case Type::TemplateTypeParm: {
2390ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T);
2391ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    if (TTP->getDepth() == Depth)
2392ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      Used[TTP->getIndex()] = true;
2393031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2394ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor  }
2395031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2396031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::TemplateSpecialization: {
23971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump    const TemplateSpecializationType *Spec
2398f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor      = cast<TemplateSpecializationType>(T);
2399e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced,
2400ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2401e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I)
2402ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth,
2403ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 Used);
2404e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    break;
2405e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  }
24061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2407e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  case Type::Complex:
2408e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    if (!OnlyDeduced)
2409e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor      MarkUsedTemplateParameters(SemaRef,
2410e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                 cast<ComplexType>(T)->getElementType(),
2411ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 OnlyDeduced, Depth, Used);
2412e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    break;
2413031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2414e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  case Type::Typename:
2415e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    if (!OnlyDeduced)
2416e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor      MarkUsedTemplateParameters(SemaRef,
2417e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                 cast<TypenameType>(T)->getQualifier(),
2418ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 OnlyDeduced, Depth, Used);
2419031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2420031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2421e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor  // None of these types have any template parameters in them.
2422031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Builtin:
2423031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::VariableArray:
2424031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::FunctionNoProto:
2425031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Record:
2426031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::Enum:
2427031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case Type::ObjCInterface:
2428d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff  case Type::ObjCObjectPointer:
2429ed97649e9574b9d854fa4d6109c9333ae0993554John McCall  case Type::UnresolvedUsing:
2430031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define TYPE(Class, Base)
2431031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define ABSTRACT_TYPE(Class, Base)
2432031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define DEPENDENT_TYPE(Class, Base)
2433031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class:
2434031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#include "clang/AST/TypeNodes.def"
2435031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2436031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
2437031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
2438031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2439e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by this
2440031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// template argument.
24411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void
2442e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef,
2443e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           const TemplateArgument &TemplateArg,
2444e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           bool OnlyDeduced,
2445ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                           unsigned Depth,
2446e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                           llvm::SmallVectorImpl<bool> &Used) {
2447031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  switch (TemplateArg.getKind()) {
2448031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Null:
2449031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Integral:
2450788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    case TemplateArgument::Declaration:
2451031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
24521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2453031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Type:
2454e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced,
2455ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2456031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2457031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2458788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor  case TemplateArgument::Template:
2459788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor    MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsTemplate(),
2460788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor                               OnlyDeduced, Depth, Used);
2461031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2462031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2463031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  case TemplateArgument::Expression:
2464e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced,
2465ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                               Depth, Used);
2466031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor    break;
2467e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor
2468d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson  case TemplateArgument::Pack:
2469e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor    for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(),
2470e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                      PEnd = TemplateArg.pack_end();
2471e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor         P != PEnd; ++P)
2472ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor      MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used);
2473d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson    break;
2474031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  }
2475031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
2476031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor
2477031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \brief Mark the template parameters can be deduced by the given
2478031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// template argument list.
2479031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor///
2480031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \param TemplateArgs the template argument list from which template
2481031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// parameters will be deduced.
2482031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor///
2483031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \param Deduced a bit vector whose elements will be set to \c true
2484031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// to indicate when the corresponding template parameter will be
2485031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// deduced.
24861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid
2487e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorSema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs,
2488ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 bool OnlyDeduced, unsigned Depth,
2489e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor                                 llvm::SmallVectorImpl<bool> &Used) {
2490031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor  for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I)
2491ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor    ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced,
2492ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 Depth, Used);
2493031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor}
249463f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor
249563f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor/// \brief Marks all of the template parameters that will be deduced by a
249663f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor/// call to the given function template.
249763f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregorvoid Sema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate,
249863f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor                                         llvm::SmallVectorImpl<bool> &Deduced) {
249963f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  TemplateParameterList *TemplateParams
250063f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor    = FunctionTemplate->getTemplateParameters();
250163f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  Deduced.clear();
250263f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  Deduced.resize(TemplateParams->size());
250363f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor
250463f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  FunctionDecl *Function = FunctionTemplate->getTemplatedDecl();
250563f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor  for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I)
250663f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor    ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(),
2507ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor                                 true, TemplateParams->getDepth(), Deduced);
250863f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor}
2509