SemaTemplateDeduction.cpp revision d4598a2cc7576c06f69d3cf64d0e2c9783ddf529
10b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor//===------- SemaTemplateDeduction.cpp - Template Argument Deduction ------===/ 20b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor// 30b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor// The LLVM Compiler Infrastructure 40b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor// 50b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor// This file is distributed under the University of Illinois Open Source 60b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor// License. See LICENSE.TXT for details. 70b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor//===----------------------------------------------------------------------===/ 80b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor// 90b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor// This file implements C++ template argument deduction. 100b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor// 110b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor//===----------------------------------------------------------------------===/ 120b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 130b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "Sema.h" 140b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/ASTContext.h" 150b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/DeclTemplate.h" 160b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/StmtVisitor.h" 170b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/Expr.h" 180b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/ExprCXX.h" 190b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/Parse/DeclSpec.h" 208a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor#include <algorithm> 21508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor 22508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregornamespace clang { 23508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// \brief Various flags that control template argument deduction. 24508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// 25508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// These flags can be bitwise-OR'd together. 26508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor enum TemplateDeductionFlags { 27508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// \brief No template argument deduction flags, which indicates the 28508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// strictest results for template argument deduction (as used for, e.g., 29508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// matching class template partial specializations). 30508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor TDF_None = 0, 31508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// \brief Within template argument deduction from a function call, we are 32508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// matching with a parameter type for which the original parameter was 33508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// a reference. 34508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor TDF_ParamWithReferenceType = 0x1, 35508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// \brief Within template argument deduction from a function call, we 36508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// are matching in a case where we ignore cv-qualifiers. 37508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor TDF_IgnoreQualifiers = 0x02, 38508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// \brief Within template argument deduction from a function call, 39508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// we are matching in a case where we can perform template argument 404112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor /// deduction from a template-id of a derived class of the argument type. 411282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor TDF_DerivedClass = 0x04, 421282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor /// \brief Allow non-dependent types to differ, e.g., when performing 431282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor /// template argument deduction from a function call where conversions 441282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor /// may apply. 451282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor TDF_SkipNonDependent = 0x08 46508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor }; 47508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor} 48508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor 490b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregorusing namespace clang; 500b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 519d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor/// \brief Compare two APSInts, extending and switching the sign as 529d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor/// necessary to compare their values regardless of underlying type. 539d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregorstatic bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) { 549d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor if (Y.getBitWidth() > X.getBitWidth()) 559d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor X.extend(Y.getBitWidth()); 569d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor else if (Y.getBitWidth() < X.getBitWidth()) 579d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor Y.extend(X.getBitWidth()); 589d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor 599d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor // If there is a signedness mismatch, correct it. 609d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor if (X.isSigned() != Y.isSigned()) { 619d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor // If the signed value is negative, then the values cannot be the same. 629d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative())) 639d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor return false; 649d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor 659d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor Y.setIsSigned(true); 669d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor X.setIsSigned(true); 679d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor } 689d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor 699d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor return X == Y; 709d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor} 719d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor 72f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult 73a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S, 74f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateParameterList *TemplateParams, 75f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor const TemplateArgument &Param, 76d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor const TemplateArgument &Arg, 77f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Sema::TemplateDeductionInfo &Info, 7802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced); 79d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor 80199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// \brief If the given expression is of a form that permits the deduction 81199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// of a non-type template parameter, return the declaration of that 82199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// non-type template parameter. 83199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregorstatic NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) { 84199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E)) 85199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor E = IC->getSubExpr(); 861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 87199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) 88199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); 891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 90199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor return 0; 91199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor} 92199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor 931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Deduce the value of the given non-type template parameter 94199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// from the given constant. 95f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult 96a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceNonTypeTemplateArgument(Sema &S, 971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump NonTypeTemplateParmDecl *NTTP, 989d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor llvm::APSInt Value, QualType ValueType, 9902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor bool DeducedFromArrayBound, 100f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Sema::TemplateDeductionInfo &Info, 10102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 1021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(NTTP->getDepth() == 0 && 103199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor "Cannot deduce non-type template argument with depth > 0"); 1041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 105199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (Deduced[NTTP->getIndex()].isNull()) { 10602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor Deduced[NTTP->getIndex()] = DeducedTemplateArgument(Value, ValueType, 10702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor DeducedFromArrayBound); 108f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 109199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor } 1101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1119d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor if (Deduced[NTTP->getIndex()].getKind() != TemplateArgument::Integral) { 112f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.Param = NTTP; 113f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Deduced[NTTP->getIndex()]; 1149d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor Info.SecondArg = TemplateArgument(Value, ValueType); 1159d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor return Sema::TDK_Inconsistent; 116f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 117f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 1189d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor // Extent the smaller of the two values. 1199d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor llvm::APSInt PrevValue = *Deduced[NTTP->getIndex()].getAsIntegral(); 1209d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor if (!hasSameExtendedValue(PrevValue, Value)) { 121f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.Param = NTTP; 122f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Deduced[NTTP->getIndex()]; 1239d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor Info.SecondArg = TemplateArgument(Value, ValueType); 124f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Inconsistent; 125f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 126f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 12702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor if (!DeducedFromArrayBound) 12802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor Deduced[NTTP->getIndex()].setDeducedFromArrayBound(false); 12902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor 130f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 131199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor} 132199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor 1331eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Deduce the value of the given non-type template parameter 134199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// from the given type- or value-dependent expression. 135199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// 136199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// \returns true if deduction succeeded, false otherwise. 137f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult 138a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceNonTypeTemplateArgument(Sema &S, 139f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor NonTypeTemplateParmDecl *NTTP, 140f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Expr *Value, 141f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Sema::TemplateDeductionInfo &Info, 14202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 1431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(NTTP->getDepth() == 0 && 144199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor "Cannot deduce non-type template argument with depth > 0"); 145199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor assert((Value->isTypeDependent() || Value->isValueDependent()) && 146199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor "Expression template argument must be type- or value-dependent."); 1471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 148199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (Deduced[NTTP->getIndex()].isNull()) { 1499d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor Deduced[NTTP->getIndex()] = TemplateArgument(Value->Retain()); 150f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 151199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor } 1521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 153199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Integral) { 1541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Okay, we deduced a constant in one case and a dependent expression 1551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // in another case. FIXME: Later, we will check that instantiating the 156199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // dependent expression gives us the constant value. 157f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 158199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor } 1591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1609eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) { 1619eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor // Compare the expressions for equality 1629eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor llvm::FoldingSetNodeID ID1, ID2; 163a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth Deduced[NTTP->getIndex()].getAsExpr()->Profile(ID1, S.Context, true); 164a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth Value->Profile(ID2, S.Context, true); 1659eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor if (ID1 == ID2) 1669eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor return Sema::TDK_Success; 1679eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor 1689eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor // FIXME: Fill in argument mismatch information 1699eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor return Sema::TDK_NonDeducedMismatch; 1709eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor } 1719eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor 172f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 173199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor} 174199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor 17515755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor/// \brief Deduce the value of the given non-type template parameter 17615755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor/// from the given declaration. 17715755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor/// 17815755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor/// \returns true if deduction succeeded, false otherwise. 17915755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregorstatic Sema::TemplateDeductionResult 180a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceNonTypeTemplateArgument(Sema &S, 18115755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor NonTypeTemplateParmDecl *NTTP, 18215755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor Decl *D, 18315755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor Sema::TemplateDeductionInfo &Info, 18402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 18515755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor assert(NTTP->getDepth() == 0 && 18615755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor "Cannot deduce non-type template argument with depth > 0"); 18715755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor 18815755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor if (Deduced[NTTP->getIndex()].isNull()) { 18915755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor Deduced[NTTP->getIndex()] = TemplateArgument(D->getCanonicalDecl()); 19015755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor return Sema::TDK_Success; 19115755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor } 19215755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor 19315755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Expression) { 19415755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor // Okay, we deduced a declaration in one case and a dependent expression 19515755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor // in another case. 19615755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor return Sema::TDK_Success; 19715755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor } 19815755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor 19915755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor if (Deduced[NTTP->getIndex()].getKind() == TemplateArgument::Declaration) { 20015755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor // Compare the declarations for equality 20115755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor if (Deduced[NTTP->getIndex()].getAsDecl()->getCanonicalDecl() == 20215755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor D->getCanonicalDecl()) 20315755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor return Sema::TDK_Success; 20415755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor 20515755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor // FIXME: Fill in argument mismatch information 20615755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor return Sema::TDK_NonDeducedMismatch; 20715755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor } 20815755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor 20915755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor return Sema::TDK_Success; 21015755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor} 21115755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor 212f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult 213a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S, 214db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor TemplateParameterList *TemplateParams, 215f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateName Param, 216f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateName Arg, 217f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Sema::TemplateDeductionInfo &Info, 21802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 219d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); 220db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor if (!ParamDecl) { 221db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor // The parameter type is dependent and is not a template template parameter, 222db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor // so there is nothing that we can deduce. 223db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor return Sema::TDK_Success; 224f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 225db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor 226db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor if (TemplateTemplateParmDecl *TempParam 227db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) { 228db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor // Bind the template template parameter to the given template name. 229db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor TemplateArgument &ExistingArg = Deduced[TempParam->getIndex()]; 230db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor if (ExistingArg.isNull()) { 231db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor // This is the first deduction for this template template parameter. 232a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth ExistingArg = TemplateArgument(S.Context.getCanonicalTemplateName(Arg)); 233db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor return Sema::TDK_Success; 234db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor } 235db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor 236db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor // Verify that the previous binding matches this deduction. 237db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor assert(ExistingArg.getKind() == TemplateArgument::Template); 238a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth if (S.Context.hasSameTemplateName(ExistingArg.getAsTemplate(), Arg)) 239db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor return Sema::TDK_Success; 240db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor 241db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor // Inconsistent deduction. 242db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor Info.Param = TempParam; 243db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor Info.FirstArg = ExistingArg; 244db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor Info.SecondArg = TemplateArgument(Arg); 245f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Inconsistent; 246f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 247db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor 248db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor // Verify that the two template names are equivalent. 249a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth if (S.Context.hasSameTemplateName(Param, Arg)) 250db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor return Sema::TDK_Success; 251db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor 252db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor // Mismatch of non-dependent template parameter to argument. 253db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor Info.FirstArg = TemplateArgument(Param); 254db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor Info.SecondArg = TemplateArgument(Arg); 255db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor return Sema::TDK_NonDeducedMismatch; 256d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor} 257d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor 2581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Deduce the template arguments by comparing the template parameter 259de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// type (which is a template-id) with the template argument type. 260de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 261a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth/// \param S the Sema 262de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 263de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param TemplateParams the template parameters that we are deducing 264de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 265de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Param the parameter type 266de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 267de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Arg the argument type 268de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 269de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Info information about the template argument deduction itself 270de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 271de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Deduced the deduced template arguments 272de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 273de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \returns the result of template argument deduction so far. Note that a 274de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// "success" result means that template argument deduction has not yet failed, 275de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// but it may still fail, later, for other reasons. 276de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregorstatic Sema::TemplateDeductionResult 277a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S, 278de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor TemplateParameterList *TemplateParams, 279de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor const TemplateSpecializationType *Param, 280de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor QualType Arg, 281de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Sema::TemplateDeductionInfo &Info, 28202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 283467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall assert(Arg.isCanonical() && "Argument type must be canonical"); 2841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 285de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Check whether the template argument is a dependent template-id. 2861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (const TemplateSpecializationType *SpecArg 287de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor = dyn_cast<TemplateSpecializationType>(Arg)) { 288de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Perform template argument deduction for the template name. 289de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (Sema::TemplateDeductionResult Result 290a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = DeduceTemplateArguments(S, TemplateParams, 291de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Param->getTemplateName(), 292de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor SpecArg->getTemplateName(), 293de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Info, Deduced)) 294de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Result; 2951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 297de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Perform template argument deduction on each template 298de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // argument. 299db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor unsigned NumArgs = std::min(SpecArg->getNumArgs(), Param->getNumArgs()); 300de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor for (unsigned I = 0; I != NumArgs; ++I) 301de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (Sema::TemplateDeductionResult Result 302a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = DeduceTemplateArguments(S, TemplateParams, 303de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Param->getArg(I), 304de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor SpecArg->getArg(I), 305de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Info, Deduced)) 306de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Result; 3071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 308de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Sema::TDK_Success; 309de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor } 3101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 311de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // If the argument type is a class template specialization, we 312de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // perform template argument deduction using its template 313de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // arguments. 314de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor const RecordType *RecordArg = dyn_cast<RecordType>(Arg); 315de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (!RecordArg) 316de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Sema::TDK_NonDeducedMismatch; 3171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump ClassTemplateSpecializationDecl *SpecArg 319de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); 320de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (!SpecArg) 321de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Sema::TDK_NonDeducedMismatch; 3221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 323de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Perform template argument deduction for the template name. 324de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (Sema::TemplateDeductionResult Result 325a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = DeduceTemplateArguments(S, 326db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor TemplateParams, 327de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Param->getTemplateName(), 328de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor TemplateName(SpecArg->getSpecializedTemplate()), 329de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Info, Deduced)) 330de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Result; 3311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 332de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor unsigned NumArgs = Param->getNumArgs(); 333de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor const TemplateArgumentList &ArgArgs = SpecArg->getTemplateArgs(); 334de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (NumArgs != ArgArgs.size()) 335de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Sema::TDK_NonDeducedMismatch; 3361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 337de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor for (unsigned I = 0; I != NumArgs; ++I) 3381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (Sema::TemplateDeductionResult Result 339a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = DeduceTemplateArguments(S, TemplateParams, 340de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Param->getArg(I), 341de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor ArgArgs.get(I), 342de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Info, Deduced)) 343de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Result; 3441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 345de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Sema::TDK_Success; 346de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor} 347de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor 348500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \brief Deduce the template arguments by comparing the parameter type and 349500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// the argument type (C++ [temp.deduct.type]). 350500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 351a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth/// \param S the semantic analysis object within which we are deducing 352500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 353500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param TemplateParams the template parameters that we are deducing 354500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 355500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param ParamIn the parameter type 356500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 357500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param ArgIn the argument type 358500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 359500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param Info information about the template argument deduction itself 360500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 361500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param Deduced the deduced template arguments 362500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 363508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe 3641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// how template argument deduction is performed. 365500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 366500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \returns the result of template argument deduction so far. Note that a 367500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// "success" result means that template argument deduction has not yet failed, 368500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// but it may still fail, later, for other reasons. 369f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult 370a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S, 371f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateParameterList *TemplateParams, 372f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor QualType ParamIn, QualType ArgIn, 373f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Sema::TemplateDeductionInfo &Info, 37402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced, 375508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor unsigned TDF) { 3760b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // We only want to look at the canonical types, since typedefs and 3770b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // sugar are not part of template argument deduction. 378a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth QualType Param = S.Context.getCanonicalType(ParamIn); 379a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth QualType Arg = S.Context.getCanonicalType(ArgIn); 3800b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 381500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor // C++0x [temp.deduct.call]p4 bullet 1: 382500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor // - If the original P is a reference type, the deduced A (i.e., the type 3831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // referred to by the reference) can be more cv-qualified than the 384500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor // transformed A. 385508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (TDF & TDF_ParamWithReferenceType) { 386e724246b9f655801bd96b727daf9dddc44beef4dChandler Carruth Qualifiers Quals; 387a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals); 388e724246b9f655801bd96b727daf9dddc44beef4dChandler Carruth Quals.setCVRQualifiers(Quals.getCVRQualifiers() & 389e724246b9f655801bd96b727daf9dddc44beef4dChandler Carruth Arg.getCVRQualifiersThroughArrayTypes()); 390a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth Param = S.Context.getQualifiedType(UnqualParam, Quals); 391500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor } 3921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 393f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor // If the parameter type is not dependent, there is nothing to deduce. 3941282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor if (!Param->isDependentType()) { 3951282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor if (!(TDF & TDF_SkipNonDependent) && Param != Arg) { 3961282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor 3971282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor return Sema::TDK_NonDeducedMismatch; 3981282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor } 3991282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor 400f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return Sema::TDK_Success; 4011282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor } 4020b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 403199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // C++ [temp.deduct.type]p9: 4041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // A template type argument T, a template template argument TT or a 4051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // template non-type argument i can be deduced if P and A have one of 406199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // the following forms: 407199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // 408199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // T 409199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // cv-list T 4101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (const TemplateTypeParmType *TemplateTypeParm 411183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall = Param->getAs<TemplateTypeParmType>()) { 412f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor unsigned Index = TemplateTypeParm->getIndex(); 413f290e0db835a619014538c41ed552696efc0e977Douglas Gregor bool RecanonicalizeArg = false; 4141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4159e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor // If the argument type is an array type, move the qualifiers up to the 4169e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor // top level, so they can be matched with the qualifiers on the parameter. 4179e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor // FIXME: address spaces, ObjC GC qualifiers 418f290e0db835a619014538c41ed552696efc0e977Douglas Gregor if (isa<ArrayType>(Arg)) { 4190953e767ff7817f97b3ab20896b229891eeff45bJohn McCall Qualifiers Quals; 420a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth Arg = S.Context.getUnqualifiedArrayType(Arg, Quals); 4210953e767ff7817f97b3ab20896b229891eeff45bJohn McCall if (Quals) { 422a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth Arg = S.Context.getQualifiedType(Arg, Quals); 423f290e0db835a619014538c41ed552696efc0e977Douglas Gregor RecanonicalizeArg = true; 424f290e0db835a619014538c41ed552696efc0e977Douglas Gregor } 425f290e0db835a619014538c41ed552696efc0e977Douglas Gregor } 4261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4270b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // The argument type can not be less qualified than the parameter 4280b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // type. 429508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) { 430f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); 431f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Deduced[Index]; 432833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall Info.SecondArg = TemplateArgument(Arg); 433f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_InconsistentQuals; 434f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 4350b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 4360b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0"); 437a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function"); 4380953e767ff7817f97b3ab20896b229891eeff45bJohn McCall QualType DeducedType = Arg; 4390953e767ff7817f97b3ab20896b229891eeff45bJohn McCall DeducedType.removeCVRQualifiers(Param.getCVRQualifiers()); 440f290e0db835a619014538c41ed552696efc0e977Douglas Gregor if (RecanonicalizeArg) 441a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth DeducedType = S.Context.getCanonicalType(DeducedType); 4421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4430b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor if (Deduced[Index].isNull()) 444833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall Deduced[Index] = TemplateArgument(DeducedType); 4450b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor else { 4461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // C++ [temp.deduct.type]p2: 4470b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // [...] If type deduction cannot be done for any P/A pair, or if for 4481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // any pair the deduction leads to more than one possible set of 4491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // deduced values, or if different pairs yield different deduced 4501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // values, or if any template argument remains neither deduced nor 4510b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // explicitly specified, template argument deduction fails. 452f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Deduced[Index].getAsType() != DeducedType) { 4531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Info.Param 454f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); 455f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Deduced[Index]; 456833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall Info.SecondArg = TemplateArgument(Arg); 457f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Inconsistent; 458f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 4590b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor } 460f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 4610b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor } 4620b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 463f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor // Set up the template argument deduction information for a failure. 464833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall Info.FirstArg = TemplateArgument(ParamIn); 465833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall Info.SecondArg = TemplateArgument(ArgIn); 466f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 467508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor // Check the cv-qualifiers on the parameter and argument types. 468508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (!(TDF & TDF_IgnoreQualifiers)) { 469508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (TDF & TDF_ParamWithReferenceType) { 470508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (Param.isMoreQualifiedThan(Arg)) 471508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor return Sema::TDK_NonDeducedMismatch; 472508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor } else { 473508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) 4741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return Sema::TDK_NonDeducedMismatch; 475508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor } 476508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor } 4770b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 478d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor switch (Param->getTypeClass()) { 479199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // No deduction possible for these types 480199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor case Type::Builtin: 481f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 4821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 483199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // T * 484d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor case Type::Pointer: { 4856217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const PointerType *PointerArg = Arg->getAs<PointerType>(); 486d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor if (!PointerArg) 487f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 4881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4894112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); 490a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceTemplateArguments(S, TemplateParams, 491d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor cast<PointerType>(Param)->getPointeeType(), 492d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor PointerArg->getPointeeType(), 4934112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor Info, Deduced, SubTDF); 494d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor } 4951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 496199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // T & 497d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor case Type::LValueReference: { 4986217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>(); 499d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor if (!ReferenceArg) 500f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 5011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 502a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceTemplateArguments(S, TemplateParams, 503d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor cast<LValueReferenceType>(Param)->getPointeeType(), 504d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor ReferenceArg->getPointeeType(), 505508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0); 506d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor } 5070b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 508199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // T && [C++0x] 509d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor case Type::RValueReference: { 5106217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>(); 511d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor if (!ReferenceArg) 512f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 5131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 514a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceTemplateArguments(S, TemplateParams, 515d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor cast<RValueReferenceType>(Param)->getPointeeType(), 516d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor ReferenceArg->getPointeeType(), 517508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0); 518d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor } 5191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 520199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // T [] (implied, but not stated explicitly) 5214d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson case Type::IncompleteArray: { 5221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const IncompleteArrayType *IncompleteArrayArg = 523a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth S.Context.getAsIncompleteArrayType(Arg); 5244d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson if (!IncompleteArrayArg) 525f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 5261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 527a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceTemplateArguments(S, TemplateParams, 528a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth S.Context.getAsIncompleteArrayType(Param)->getElementType(), 5294d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson IncompleteArrayArg->getElementType(), 530508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0); 5314d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson } 532199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor 533199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // T [integer-constant] 5344d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson case Type::ConstantArray: { 5351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const ConstantArrayType *ConstantArrayArg = 536a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth S.Context.getAsConstantArrayType(Arg); 5374d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson if (!ConstantArrayArg) 538f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 5391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const ConstantArrayType *ConstantArrayParm = 541a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth S.Context.getAsConstantArrayType(Param); 5424d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) 543f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 5441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 545a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceTemplateArguments(S, TemplateParams, 5464d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson ConstantArrayParm->getElementType(), 5474d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson ConstantArrayArg->getElementType(), 548508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0); 5494d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson } 5504d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson 551199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // type [i] 552199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor case Type::DependentSizedArray: { 553a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg); 554199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (!ArrayArg) 555f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 5561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 557199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // Check the element type of the arrays 558199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor const DependentSizedArrayType *DependentArrayParm 559a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = S.Context.getAsDependentSizedArrayType(Param); 560f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Sema::TemplateDeductionResult Result 561a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = DeduceTemplateArguments(S, TemplateParams, 562f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor DependentArrayParm->getElementType(), 563f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor ArrayArg->getElementType(), 564508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0)) 565f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Result; 5661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 567199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // Determine the array bound is something we can deduce. 5681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump NonTypeTemplateParmDecl *NTTP 569199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr()); 570199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (!NTTP) 571f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 5721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // We can perform template argument deduction for the given non-type 574199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // template parameter. 5751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(NTTP->getDepth() == 0 && 576199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor "Cannot deduce non-type template argument at depth > 0"); 5771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (const ConstantArrayType *ConstantArrayArg 578335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson = dyn_cast<ConstantArrayType>(ArrayArg)) { 579335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson llvm::APSInt Size(ConstantArrayArg->getSize()); 5809d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor return DeduceNonTypeTemplateArgument(S, NTTP, Size, 5819d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor S.Context.getSizeType(), 58202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor /*ArrayBound=*/true, 583f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info, Deduced); 584335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson } 585199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (const DependentSizedArrayType *DependentArrayArg 586199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor = dyn_cast<DependentSizedArrayType>(ArrayArg)) 587a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceNonTypeTemplateArgument(S, NTTP, 588199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor DependentArrayArg->getSizeExpr(), 589f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info, Deduced); 5901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 591199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // Incomplete type does not match a dependently-sized array type 592f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 593199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor } 5941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 5951eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // type(*)(T) 5961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // T(*)() 5971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // T(*)(T) 598a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson case Type::FunctionProto: { 5991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const FunctionProtoType *FunctionProtoArg = 600a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson dyn_cast<FunctionProtoType>(Arg); 601a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson if (!FunctionProtoArg) 602f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 6031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const FunctionProtoType *FunctionProtoParam = 605a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson cast<FunctionProtoType>(Param); 606994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson 6071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (FunctionProtoParam->getTypeQuals() != 608994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson FunctionProtoArg->getTypeQuals()) 609f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 6101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 611994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson if (FunctionProtoParam->getNumArgs() != FunctionProtoArg->getNumArgs()) 612f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 6131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 614994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) 615f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 616994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson 617a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson // Check return types. 618f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Sema::TemplateDeductionResult Result 619a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = DeduceTemplateArguments(S, TemplateParams, 620f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor FunctionProtoParam->getResultType(), 621f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor FunctionProtoArg->getResultType(), 622508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0)) 623f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Result; 6241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 625a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson for (unsigned I = 0, N = FunctionProtoParam->getNumArgs(); I != N; ++I) { 626a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson // Check argument types. 627f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Sema::TemplateDeductionResult Result 628a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = DeduceTemplateArguments(S, TemplateParams, 629f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor FunctionProtoParam->getArgType(I), 630f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor FunctionProtoArg->getArgType(I), 631508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0)) 632f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Result; 633a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson } 6341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 635f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 636a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson } 6371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6383cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall case Type::InjectedClassName: { 6393cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall // Treat a template's injected-class-name as if the template 6403cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall // specialization type had been used. 64131f17ecbef57b5679c017c375db330546b7b5145John McCall Param = cast<InjectedClassNameType>(Param) 64231f17ecbef57b5679c017c375db330546b7b5145John McCall ->getInjectedSpecializationType(); 6433cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall assert(isa<TemplateSpecializationType>(Param) && 6443cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall "injected class name is not a template specialization type"); 6453cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall // fall through 6463cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall } 6473cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall 648f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor // template-name<T> (where template-name refers to a class template) 649d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor // template-name<i> 650db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor // TT<T> 651db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor // TT<i> 652db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor // TT<> 653d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor case Type::TemplateSpecialization: { 654d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor const TemplateSpecializationType *SpecParam 655d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor = cast<TemplateSpecializationType>(Param); 6561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 657de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Try to deduce template arguments from the template-id. 658de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Sema::TemplateDeductionResult Result 659a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg, 660de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Info, Deduced); 6611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 6624a5c15f75f76b95e1c2ceb6fa2737dcadd5f4be1Douglas Gregor if (Result && (TDF & TDF_DerivedClass)) { 663de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // C++ [temp.deduct.call]p3b3: 664de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // If P is a class, and P has the form template-id, then A can be a 665de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // derived class of the deduced A. Likewise, if P is a pointer to a 6661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // class of the form template-id, A can be a pointer to a derived 667de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // class pointed to by the deduced A. 668de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // 669de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // More importantly: 6701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // These alternatives are considered only if type deduction would 671de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // otherwise fail. 672a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth if (const RecordType *RecordT = Arg->getAs<RecordType>()) { 673a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth // We cannot inspect base classes as part of deduction when the type 674a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth // is incomplete, so either instantiate any templates necessary to 675a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth // complete the type, or skip over it if it cannot be completed. 6765769d6195087229770d7ac90449443e026c47103John McCall if (S.RequireCompleteType(Info.getLocation(), Arg, 0)) 677a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return Result; 678a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth 679de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Use data recursion to crawl through the list of base classes. 6801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Visited contains the set of nodes we have already visited, while 681de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // ToVisit is our stack of records that we still need to visit. 682de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor llvm::SmallPtrSet<const RecordType *, 8> Visited; 683de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor llvm::SmallVector<const RecordType *, 8> ToVisit; 684de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor ToVisit.push_back(RecordT); 685de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor bool Successful = false; 686de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor while (!ToVisit.empty()) { 687de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Retrieve the next class in the inheritance hierarchy. 688de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor const RecordType *NextT = ToVisit.back(); 689de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor ToVisit.pop_back(); 6901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 691de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // If we have already seen this type, skip it. 692de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (!Visited.insert(NextT)) 693de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor continue; 6941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 695de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // If this is a base class, try to perform template argument 696de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // deduction from it. 697de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (NextT != RecordT) { 698de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Sema::TemplateDeductionResult BaseResult 699a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = DeduceTemplateArguments(S, TemplateParams, SpecParam, 700de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor QualType(NextT, 0), Info, Deduced); 7011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 702de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // If template argument deduction for this base was successful, 703de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // note that we had some success. 704de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (BaseResult == Sema::TDK_Success) 705de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Successful = true; 706de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor } 7071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 708de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Visit base classes 709de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl()); 710de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(), 711de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor BaseEnd = Next->bases_end(); 7129994a34f6cf842721ba7723edc0b9036229fe387Sebastian Redl Base != BaseEnd; ++Base) { 7131eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(Base->getType()->isRecordType() && 714de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor "Base class that isn't a record?"); 7156217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek ToVisit.push_back(Base->getType()->getAs<RecordType>()); 716de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor } 717de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor } 7181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 719de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (Successful) 720de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Sema::TDK_Success; 721de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor } 7221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 723de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor } 7241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 725de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Result; 726d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor } 727d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor 728637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // T type::* 729637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // T T::* 730637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // T (type::*)() 731637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // type (T::*)() 732637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // type (type::*)(T) 733637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // type (T::*)(T) 734637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // T (type::*)(T) 735637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // T (T::*)() 736637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // T (T::*)(T) 737637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor case Type::MemberPointer: { 738637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); 739637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); 740637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor if (!MemPtrArg) 741f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 742f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 743f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Sema::TemplateDeductionResult Result 744a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = DeduceTemplateArguments(S, TemplateParams, 745f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor MemPtrParam->getPointeeType(), 746f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor MemPtrArg->getPointeeType(), 747508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 748508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor TDF & TDF_IgnoreQualifiers)) 749f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Result; 750f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 751a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceTemplateArguments(S, TemplateParams, 752f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor QualType(MemPtrParam->getClass(), 0), 753f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor QualType(MemPtrArg->getClass(), 0), 754508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0); 755637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor } 756637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor 7579a917e4fac79aba20fbd25983c78396475078918Anders Carlsson // (clang extension) 7589a917e4fac79aba20fbd25983c78396475078918Anders Carlsson // 7591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // type(^)(T) 7601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // T(^)() 7611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // T(^)(T) 762859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson case Type::BlockPointer: { 763859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); 764859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); 7651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 766859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson if (!BlockPtrArg) 767f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 7681eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 769a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceTemplateArguments(S, TemplateParams, 770859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson BlockPtrParam->getPointeeType(), 771f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor BlockPtrArg->getPointeeType(), Info, 772508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Deduced, 0); 773859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson } 774859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson 775637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor case Type::TypeOfExpr: 776637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor case Type::TypeOf: 7774714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor case Type::DependentName: 778637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // No template argument deduction for these types 779f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 780637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor 781d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor default: 782d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor break; 7830b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor } 7840b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 7850b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // FIXME: Many more cases to go (to go). 786f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return Sema::TDK_Success; 7870b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor} 7880b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 789f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult 790a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S, 791f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateParameterList *TemplateParams, 792f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor const TemplateArgument &Param, 7930b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor const TemplateArgument &Arg, 794f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Sema::TemplateDeductionInfo &Info, 79502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 7960b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor switch (Param.getKind()) { 797199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor case TemplateArgument::Null: 798199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor assert(false && "Null template argument in parameter list"); 799199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor break; 8001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 8011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case TemplateArgument::Type: 802788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor if (Arg.getKind() == TemplateArgument::Type) 803a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceTemplateArguments(S, TemplateParams, Param.getAsType(), 804788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor Arg.getAsType(), Info, Deduced, 0); 805788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor Info.FirstArg = Param; 806788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor Info.SecondArg = Arg; 807788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor return Sema::TDK_NonDeducedMismatch; 808788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor 809788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor case TemplateArgument::Template: 810db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor if (Arg.getKind() == TemplateArgument::Template) 811a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceTemplateArguments(S, TemplateParams, 812788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor Param.getAsTemplate(), 813db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor Arg.getAsTemplate(), Info, Deduced); 814788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor Info.FirstArg = Param; 815788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor Info.SecondArg = Arg; 816788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor return Sema::TDK_NonDeducedMismatch; 817788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor 818199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor case TemplateArgument::Declaration: 819788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor if (Arg.getKind() == TemplateArgument::Declaration && 820788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor Param.getAsDecl()->getCanonicalDecl() == 821788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor Arg.getAsDecl()->getCanonicalDecl()) 822788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor return Sema::TDK_Success; 823788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor 824f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Param; 825f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = Arg; 826f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 8271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 828199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor case TemplateArgument::Integral: 829199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (Arg.getKind() == TemplateArgument::Integral) { 8309d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor if (hasSameExtendedValue(*Param.getAsIntegral(), *Arg.getAsIntegral())) 831f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 832f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 833f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Param; 834f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = Arg; 835f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 836f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 837f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 838f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Arg.getKind() == TemplateArgument::Expression) { 839f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Param; 840f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = Arg; 841f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 842199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor } 843199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor 844199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor assert(false && "Type/value mismatch"); 845f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Param; 846f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = Arg; 847f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 8481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 849199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor case TemplateArgument::Expression: { 8501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (NonTypeTemplateParmDecl *NTTP 851199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor = getDeducedParameterFromExpr(Param.getAsExpr())) { 852199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (Arg.getKind() == TemplateArgument::Integral) 853a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceNonTypeTemplateArgument(S, NTTP, 8541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump *Arg.getAsIntegral(), 8559d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor Arg.getIntegralType(), 85602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor /*ArrayBound=*/false, 857f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info, Deduced); 858199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (Arg.getKind() == TemplateArgument::Expression) 859a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(), 860f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info, Deduced); 86115755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor if (Arg.getKind() == TemplateArgument::Declaration) 862a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(), 86315755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor Info, Deduced); 86415755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor 865199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor assert(false && "Type/value mismatch"); 866f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Param; 867f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = Arg; 868f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 869199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor } 8701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 871199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // Can't deduce anything, but that's okay. 872f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 873199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor } 874d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson case TemplateArgument::Pack: 875d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson assert(0 && "FIXME: Implement!"); 876d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson break; 8770b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor } 8781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 879f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 8800b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor} 8810b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 8821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic Sema::TemplateDeductionResult 883a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S, 884f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateParameterList *TemplateParams, 8850b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor const TemplateArgumentList &ParamList, 8860b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor const TemplateArgumentList &ArgList, 887f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Sema::TemplateDeductionInfo &Info, 88802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 8890b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor assert(ParamList.size() == ArgList.size()); 8900b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor for (unsigned I = 0, N = ParamList.size(); I != N; ++I) { 891f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Sema::TemplateDeductionResult Result 892a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = DeduceTemplateArguments(S, TemplateParams, 8931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump ParamList[I], ArgList[I], 894f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info, Deduced)) 895f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Result; 8960b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor } 897f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 8980b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor} 8990b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 900f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// \brief Determine whether two template arguments are the same. 9011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool isSameTemplateArg(ASTContext &Context, 902f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor const TemplateArgument &X, 903f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor const TemplateArgument &Y) { 904f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (X.getKind() != Y.getKind()) 905f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return false; 9061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 907f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor switch (X.getKind()) { 908f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor case TemplateArgument::Null: 909f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor assert(false && "Comparing NULL template argument"); 910f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor break; 9111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 912f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor case TemplateArgument::Type: 913f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return Context.getCanonicalType(X.getAsType()) == 914f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Context.getCanonicalType(Y.getAsType()); 9151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 916f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor case TemplateArgument::Declaration: 91797fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis return X.getAsDecl()->getCanonicalDecl() == 91897fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis Y.getAsDecl()->getCanonicalDecl(); 9191eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 920788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor case TemplateArgument::Template: 921788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor return Context.getCanonicalTemplateName(X.getAsTemplate()) 922788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor .getAsVoidPointer() == 923788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor Context.getCanonicalTemplateName(Y.getAsTemplate()) 924788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor .getAsVoidPointer(); 925788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor 926f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor case TemplateArgument::Integral: 927f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return *X.getAsIntegral() == *Y.getAsIntegral(); 9281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 929788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor case TemplateArgument::Expression: { 930788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor llvm::FoldingSetNodeID XID, YID; 931788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor X.getAsExpr()->Profile(XID, Context, true); 932788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor Y.getAsExpr()->Profile(YID, Context, true); 933788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor return XID == YID; 934788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor } 9351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 936f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor case TemplateArgument::Pack: 937f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (X.pack_size() != Y.pack_size()) 938f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return false; 9391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (TemplateArgument::pack_iterator XP = X.pack_begin(), 9411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump XPEnd = X.pack_end(), 942f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor YP = Y.pack_begin(); 9431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump XP != XPEnd; ++XP, ++YP) 944f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (!isSameTemplateArg(Context, *XP, *YP)) 945f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return false; 946f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor 947f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return true; 948f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor } 949f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor 950f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return false; 951f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor} 952f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor 953f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// \brief Helper function to build a TemplateParameter when we don't 954f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// know its type statically. 955f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregorstatic TemplateParameter makeTemplateParameter(Decl *D) { 956f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) 957f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return TemplateParameter(TTP); 958f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) 959f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return TemplateParameter(NTTP); 9601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 961f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); 962f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor} 963f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor 964c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor/// \brief Perform template argument deduction to determine whether 965c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor/// the given template arguments match the given class template 966c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor/// partial specialization per C++ [temp.class.spec.match]. 967f67875d5addf36b951ad37fb04509ab2b572c88aDouglas GregorSema::TemplateDeductionResult 9680b9247f129401b4849682b2e2bcdea8fc977068aDouglas GregorSema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, 969f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor const TemplateArgumentList &TemplateArgs, 970f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateDeductionInfo &Info) { 971c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor // C++ [temp.class.spec.match]p2: 972c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor // A partial specialization matches a given actual template 973c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor // argument list if the template arguments of the partial 974c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor // specialization can be deduced from the actual template argument 975c1efb3faefa7d42f974fe384dfd45e5127f8afa6Douglas Gregor // list (14.8.2). 976bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor SFINAETrap Trap(*this); 97702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVector<DeducedTemplateArgument, 4> Deduced; 9780b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor Deduced.resize(Partial->getTemplateParameters()->size()); 979f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (TemplateDeductionResult Result 980a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = ::DeduceTemplateArguments(*this, 981f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Partial->getTemplateParameters(), 9821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Partial->getTemplateArgs(), 983f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateArgs, Info, Deduced)) 984f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Result; 985637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor 986637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial, 987637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor Deduced.data(), Deduced.size()); 988637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor if (Inst) 989f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return TDK_InstantiationDepth; 990199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor 99102cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // C++ [temp.deduct.type]p2: 99202cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // [...] or if any template argument remains neither deduced nor 99302cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // explicitly specified, template argument deduction fails. 994fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson TemplateArgumentListBuilder Builder(Partial->getTemplateParameters(), 995fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson Deduced.size()); 99602cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor for (unsigned I = 0, N = Deduced.size(); I != N; ++I) { 997f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Deduced[I].isNull()) { 9981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Decl *Param 999bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor = const_cast<NamedDecl *>( 1000bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor Partial->getTemplateParameters()->getParam(I)); 10012b0749a4f8965d0205bf77322db150c13c39e3beDouglas Gregor Info.Param = makeTemplateParameter(Param); 1002f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return TDK_Incomplete; 1003f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 100402cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor 1005fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson Builder.Append(Deduced[I]); 100602cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor } 100702cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor 100802cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // Form the template argument list from the deduced template arguments. 10091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateArgumentList *DeducedArgumentList 1010fb25052736439d72a557cddd41dfb927bcb3d3e5Anders Carlsson = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); 1011f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.reset(DeducedArgumentList); 101202cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor 101302cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // Substitute the deduced template arguments into the template 101402cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // arguments of the class template partial specialization, and 101502cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // verify that the instantiated template arguments are both valid 101602cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // and are equivalent to the template arguments originally provided 10171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // to the class template. 101802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor // FIXME: Do we have to correct the types of deduced non-type template 101902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor // arguments (in particular, integral non-type template arguments?). 10202b0749a4f8965d0205bf77322db150c13c39e3beDouglas Gregor Sema::LocalInstantiationScope InstScope(*this); 102102cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate(); 1022833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall const TemplateArgumentLoc *PartialTemplateArgs 1023833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall = Partial->getTemplateArgsAsWritten(); 1024833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall unsigned N = Partial->getNumTemplateArgsAsWritten(); 1025d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall 1026d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall // Note that we don't provide the langle and rangle locations. 1027d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall TemplateArgumentListInfo InstArgs; 1028d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall 1029833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall for (unsigned I = 0; I != N; ++I) { 1030bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor Decl *Param = const_cast<NamedDecl *>( 1031c9e5d25ea33616c896a2ce5fbd6d68186eaddc5cDouglas Gregor ClassTemplate->getTemplateParameters()->getParam(I)); 1032d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall TemplateArgumentLoc InstArg; 1033d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall if (Subst(PartialTemplateArgs[I], InstArg, 1034833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall MultiLevelTemplateArgumentList(*DeducedArgumentList))) { 1035f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.Param = makeTemplateParameter(Param); 1036833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall Info.FirstArg = PartialTemplateArgs[I].getArgument(); 10371eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return TDK_SubstitutionFailure; 1038f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor } 1039d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall InstArgs.addArgument(InstArg); 1040833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall } 1041833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall 1042833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall TemplateArgumentListBuilder ConvertedInstArgs( 1043833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall ClassTemplate->getTemplateParameters(), N); 1044833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall 1045833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall if (CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(), 1046d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall InstArgs, false, ConvertedInstArgs)) { 1047833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall // FIXME: fail with more useful information? 1048833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall return TDK_SubstitutionFailure; 1049833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall } 1050833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall 1051833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall for (unsigned I = 0, E = ConvertedInstArgs.flatSize(); I != E; ++I) { 1052d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall TemplateArgument InstArg = ConvertedInstArgs.getFlatArguments()[I]; 1053833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall 1054833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall Decl *Param = const_cast<NamedDecl *>( 1055833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall ClassTemplate->getTemplateParameters()->getParam(I)); 10561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1057f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (InstArg.getKind() == TemplateArgument::Expression) { 10581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // When the argument is an expression, check the expression result 1059f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor // against the actual template parameter to get down to the canonical 1060f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor // template argument. 1061f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Expr *InstExpr = InstArg.getAsExpr(); 10621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (NonTypeTemplateParmDecl *NTTP 1063f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor = dyn_cast<NonTypeTemplateParmDecl>(Param)) { 1064f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (CheckTemplateArgument(NTTP, NTTP->getType(), InstExpr, InstArg)) { 1065f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.Param = makeTemplateParameter(Param); 1066833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall Info.FirstArg = Partial->getTemplateArgs()[I]; 10671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return TDK_SubstitutionFailure; 1068f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor } 1069f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 107002cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor } 10711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1072f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (!isSameTemplateArg(Context, TemplateArgs[I], InstArg)) { 1073f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.Param = makeTemplateParameter(Param); 1074f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.FirstArg = TemplateArgs[I]; 1075f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.SecondArg = InstArg; 1076f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return TDK_NonDeducedMismatch; 1077f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor } 107802cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor } 107902cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor 1080bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor if (Trap.hasErrorOccurred()) 1081bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor return TDK_SubstitutionFailure; 1082bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor 1083f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return TDK_Success; 10840b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor} 1085031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 10864112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor/// \brief Determine whether the given type T is a simple-template-id type. 10874112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregorstatic bool isSimpleTemplateIdType(QualType T) { 10881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (const TemplateSpecializationType *Spec 1089183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall = T->getAs<TemplateSpecializationType>()) 10904112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor return Spec->getTemplateName().getAsTemplateDecl() != 0; 10911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10924112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor return false; 10934112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor} 109483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 109583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \brief Substitute the explicitly-provided template arguments into the 109683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// given function template according to C++ [temp.arg.explicit]. 109783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 109883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionTemplate the function template into which the explicit 109983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template arguments will be substituted. 110083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 11011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ExplicitTemplateArguments the explicitly-specified template 110283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// arguments. 110383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 11041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param Deduced the deduced template arguments, which will be populated 110583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// with the converted and checked explicit template arguments. 110683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 11071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ParamTypes will be populated with the instantiated function 110883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// parameters. 110983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 111083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionType if non-NULL, the result type of the function template 111183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// will also be instantiated and the pointed-to value will be updated with 111283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// the instantiated function type. 111383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 111483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Info if substitution fails for any reason, this object will be 111583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// populated with more information about the failure. 111683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 111783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \returns TDK_Success if substitution was successful, or some failure 111883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// condition. 111983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::TemplateDeductionResult 112083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::SubstituteExplicitTemplateArguments( 112183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionTemplateDecl *FunctionTemplate, 1122d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall const TemplateArgumentListInfo &ExplicitTemplateArgs, 112302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced, 112483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor llvm::SmallVectorImpl<QualType> &ParamTypes, 112583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor QualType *FunctionType, 112683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateDeductionInfo &Info) { 112783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 112883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateParameterList *TemplateParams 112983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor = FunctionTemplate->getTemplateParameters(); 113083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 1131d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall if (ExplicitTemplateArgs.size() == 0) { 113283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // No arguments to substitute; just copy over the parameter types and 113383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // fill in the function type. 113483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor for (FunctionDecl::param_iterator P = Function->param_begin(), 113583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor PEnd = Function->param_end(); 113683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor P != PEnd; 113783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ++P) 113883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ParamTypes.push_back((*P)->getType()); 11391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 114083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (FunctionType) 114183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor *FunctionType = Function->getType(); 114283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_Success; 114383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor } 11441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 114583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Substitution of the explicit template arguments into a function template 114683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor /// is a SFINAE context. Trap any errors that might occur. 11471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump SFINAETrap Trap(*this); 11481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 114983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // C++ [temp.arg.explicit]p3: 11501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Template arguments that are present shall be specified in the 11511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // declaration order of their corresponding template-parameters. The 115283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // template argument list shall not specify more template-arguments than 11531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // there are corresponding template-parameters. 11541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateArgumentListBuilder Builder(TemplateParams, 1155d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall ExplicitTemplateArgs.size()); 11561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Enter a new template instantiation context where we check the 115883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // explicitly-specified template arguments against this function template, 115983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // and then substitute them into the function parameter types. 11601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(), 116183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionTemplate, Deduced.data(), Deduced.size(), 116283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution); 116383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (Inst) 116483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_InstantiationDepth; 11651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 116683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (CheckTemplateArgumentList(FunctionTemplate, 116783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor SourceLocation(), 1168d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall ExplicitTemplateArgs, 116983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor true, 117083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Builder) || Trap.hasErrorOccurred()) 117183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_InvalidExplicitArguments; 11721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 117383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Form the template argument list from the explicitly-specified 117483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // template arguments. 11751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateArgumentList *ExplicitArgumentList 117683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); 117783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Info.reset(ExplicitArgumentList); 11781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 117983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Instantiate the types of each of the function parameters given the 118083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // explicitly-specified template arguments. 118183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor for (FunctionDecl::param_iterator P = Function->param_begin(), 118283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor PEnd = Function->param_end(); 118383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor P != PEnd; 118483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ++P) { 11851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump QualType ParamType 11861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump = SubstType((*P)->getType(), 1187357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor MultiLevelTemplateArgumentList(*ExplicitArgumentList), 1188357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor (*P)->getLocation(), (*P)->getDeclName()); 118983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (ParamType.isNull() || Trap.hasErrorOccurred()) 119083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_SubstitutionFailure; 11911eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 119283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ParamTypes.push_back(ParamType); 119383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor } 119483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 119583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // If the caller wants a full function type back, instantiate the return 119683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // type and form that function type. 119783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (FunctionType) { 119883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // FIXME: exception-specifications? 11991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const FunctionProtoType *Proto 1200183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall = Function->getType()->getAs<FunctionProtoType>(); 120183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor assert(Proto && "Function template does not have a prototype?"); 12021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump QualType ResultType 1204357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor = SubstType(Proto->getResultType(), 1205357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor MultiLevelTemplateArgumentList(*ExplicitArgumentList), 1206357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor Function->getTypeSpecStartLoc(), 1207357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor Function->getDeclName()); 120883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (ResultType.isNull() || Trap.hasErrorOccurred()) 120983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_SubstitutionFailure; 12101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump *FunctionType = BuildFunctionType(ResultType, 121283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ParamTypes.data(), ParamTypes.size(), 121383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Proto->isVariadic(), 121483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Proto->getTypeQuals(), 121583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Function->getLocation(), 121683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Function->getDeclName()); 121783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (FunctionType->isNull() || Trap.hasErrorOccurred()) 121883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_SubstitutionFailure; 121983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor } 12201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 122183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // C++ [temp.arg.explicit]p2: 12221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Trailing template arguments that can be deduced (14.8.2) may be 12231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // omitted from the list of explicit template-arguments. If all of the 122483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // template arguments can be deduced, they may all be omitted; in this 122583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // case, the empty template argument list <> itself may also be omitted. 122683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // 122783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Take all of the explicitly-specified arguments and put them into the 12281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // set of deduced template arguments. 122983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Deduced.reserve(TemplateParams->size()); 123083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) 12311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Deduced.push_back(ExplicitArgumentList->get(I)); 12321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 123383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_Success; 123483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor} 123583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 123602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// \brief Allocate a TemplateArgumentLoc where all locations have 123702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// been initialized to the given location. 123802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// 123902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// \param S The semantic analysis object. 124002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// 124102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// \param The template argument we are producing template argument 124202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// location information for. 124302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// 124402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// \param NTTPType For a declaration template argument, the type of 124502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// the non-type template parameter that corresponds to this template 124602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// argument. 124702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// 124802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// \param Loc The source location to use for the resulting template 124902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor/// argument. 125002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregorstatic TemplateArgumentLoc 125102024a9f0d8e6c898de276193af604c42ee41269Douglas GregorgetTrivialTemplateArgumentLoc(Sema &S, 125202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor const TemplateArgument &Arg, 125302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor QualType NTTPType, 125402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor SourceLocation Loc) { 125502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor switch (Arg.getKind()) { 125602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor case TemplateArgument::Null: 125702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm_unreachable("Can't get a NULL template argument here"); 125802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor break; 125902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor 126002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor case TemplateArgument::Type: 126102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor return TemplateArgumentLoc(Arg, 126202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc)); 126302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor 126402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor case TemplateArgument::Declaration: { 126502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor Expr *E 126602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) 126702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor .takeAs<Expr>(); 126802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor return TemplateArgumentLoc(TemplateArgument(E), E); 126902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor } 127002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor 127102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor case TemplateArgument::Integral: { 127202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor Expr *E 127302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).takeAs<Expr>(); 127402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor return TemplateArgumentLoc(TemplateArgument(E), E); 127502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor } 127602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor 127702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor case TemplateArgument::Template: 127802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor return TemplateArgumentLoc(Arg, SourceRange(), Loc); 127902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor 128002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor case TemplateArgument::Expression: 128102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor return TemplateArgumentLoc(Arg, Arg.getAsExpr()); 128202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor 128302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor case TemplateArgument::Pack: 128402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm_unreachable("Template parameter packs are not yet supported"); 128502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor } 128602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor 128702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor return TemplateArgumentLoc(); 128802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor} 128902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor 12901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Finish template argument deduction for a function template, 129183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// checking the deduced template arguments for completeness and forming 129283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// the function template specialization. 12931eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSema::TemplateDeductionResult 129483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate, 129502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced, 129602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor unsigned NumExplicitlySpecified, 129783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionDecl *&Specialization, 129883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateDeductionInfo &Info) { 129983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateParameterList *TemplateParams 130083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor = FunctionTemplate->getTemplateParameters(); 13011eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 130251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor // Template argument deduction for function templates in a SFINAE context. 130351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor // Trap any errors that might occur. 130451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor SFINAETrap Trap(*this); 130551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor 130651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor // Enter a new template instantiation context while we instantiate the 130751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor // actual function declaration. 130851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(), 130951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor FunctionTemplate, Deduced.data(), Deduced.size(), 131051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution); 131151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor if (Inst) 131251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor return TDK_InstantiationDepth; 131351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor 131483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // C++ [temp.deduct.type]p2: 131583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // [...] or if any template argument remains neither deduced nor 131683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // explicitly specified, template argument deduction fails. 131783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateArgumentListBuilder Builder(TemplateParams, Deduced.size()); 131883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor for (unsigned I = 0, N = Deduced.size(); I != N; ++I) { 131902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor NamedDecl *Param = FunctionTemplate->getTemplateParameters()->getParam(I); 132051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor if (!Deduced[I].isNull()) { 132102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor if (I < NumExplicitlySpecified || 132202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor Deduced[I].getKind() == TemplateArgument::Type) { 132302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor // We have already fully type-checked and converted this 132402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor // argument (because it was explicitly-specified) or no 132502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor // additional checking is necessary (because it's a template 132602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor // type parameter). Just record the presence of this 132702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor // parameter. 132802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor Builder.Append(Deduced[I]); 132902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor continue; 133002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor } 133102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor 133202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor // We have deduced this argument, so it still needs to be 133302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor // checked and converted. 133402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor 133502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor // First, for a non-type template parameter type that is 133602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor // initialized by a declaration, we need the type of the 133702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor // corresponding non-type template parameter. 133802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor QualType NTTPType; 133902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor if (NonTypeTemplateParmDecl *NTTP 134002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor = dyn_cast<NonTypeTemplateParmDecl>(Param)) { 134102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor if (Deduced[I].getKind() == TemplateArgument::Declaration) { 134202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor NTTPType = NTTP->getType(); 134302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor if (NTTPType->isDependentType()) { 134402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor TemplateArgumentList TemplateArgs(Context, Builder, 134502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor /*TakeArgs=*/false); 134602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor NTTPType = SubstType(NTTPType, 134702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor MultiLevelTemplateArgumentList(TemplateArgs), 134802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor NTTP->getLocation(), 134902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor NTTP->getDeclName()); 135002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor if (NTTPType.isNull()) { 135102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor Info.Param = makeTemplateParameter(Param); 135202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor return TDK_SubstitutionFailure; 135302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor } 135402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor } 135502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor } 135602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor } 135702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor 135802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor // Convert the deduced template argument into a template 135902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor // argument that we can check, almost as if the user had written 136002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor // the template argument explicitly. 136102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor TemplateArgumentLoc Arg = getTrivialTemplateArgumentLoc(*this, 136202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor Deduced[I], 136302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor NTTPType, 136402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor SourceLocation()); 136502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor 136602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor // Check the template argument, converting it as necessary. 136702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor if (CheckTemplateArgument(Param, Arg, 136802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor FunctionTemplate, 136902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor FunctionTemplate->getLocation(), 137002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor FunctionTemplate->getSourceRange().getEnd(), 137102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor Builder, 137202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor Deduced[I].wasDeducedFromArrayBound() 137302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor ? CTAK_DeducedFromArrayBound 137402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor : CTAK_Deduced)) { 137502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor Info.Param = makeTemplateParameter( 137602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor const_cast<NamedDecl *>(TemplateParams->getParam(I))); 137702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor return TDK_SubstitutionFailure; 137802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor } 137902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor 138051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor continue; 138151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor } 138251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor 138351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor // Substitute into the default template argument, if available. 138451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor TemplateArgumentLoc DefArg 138551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate, 138651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor FunctionTemplate->getLocation(), 138751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor FunctionTemplate->getSourceRange().getEnd(), 138851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor Param, 138951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor Builder); 139051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor 139151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor // If there was no default argument, deduction is incomplete. 139251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor if (DefArg.getArgument().isNull()) { 139383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Info.Param = makeTemplateParameter( 139451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor const_cast<NamedDecl *>(TemplateParams->getParam(I))); 139583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_Incomplete; 139683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor } 139751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor 139851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor // Check whether we can actually use the default argument. 139951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor if (CheckTemplateArgument(Param, DefArg, 140051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor FunctionTemplate, 140151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor FunctionTemplate->getLocation(), 140251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor FunctionTemplate->getSourceRange().getEnd(), 140302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor Builder, 140402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor CTAK_Deduced)) { 140551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor Info.Param = makeTemplateParameter( 140651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor const_cast<NamedDecl *>(TemplateParams->getParam(I))); 140751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor return TDK_SubstitutionFailure; 140851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor } 14091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 141051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor // If we get here, we successfully used the default template argument. 141183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor } 14121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 141383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Form the template argument list from the deduced template arguments. 14141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateArgumentList *DeducedArgumentList 141583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor = new (Context) TemplateArgumentList(Context, Builder, /*TakeArgs=*/true); 141683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Info.reset(DeducedArgumentList); 14171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Substitute the deduced template arguments into the function template 141983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // declaration to produce the function template specialization. 1420d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor DeclContext *Owner = FunctionTemplate->getDeclContext(); 1421d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor if (FunctionTemplate->getFriendObjectKind()) 1422d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor Owner = FunctionTemplate->getLexicalDeclContext(); 142383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Specialization = cast_or_null<FunctionDecl>( 1424d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner, 1425357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor MultiLevelTemplateArgumentList(*DeducedArgumentList))); 142683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (!Specialization) 142783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_SubstitutionFailure; 14281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1429f882574cf640d5c8355965e1c486f9d8d8ffcf47Douglas Gregor assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == 1430f882574cf640d5c8355965e1c486f9d8d8ffcf47Douglas Gregor FunctionTemplate->getCanonicalDecl()); 1431f882574cf640d5c8355965e1c486f9d8d8ffcf47Douglas Gregor 14321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // If the template argument list is owned by the function template 143383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // specialization, release it. 143483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList) 143583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Info.take(); 14361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 143783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // There may have been an error that did not prevent us from constructing a 143883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // declaration. Mark the declaration invalid and return with a substitution 143983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // failure. 144083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (Trap.hasErrorOccurred()) { 144183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Specialization->setInvalidDecl(true); 144283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_SubstitutionFailure; 144383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor } 14441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 14451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return TDK_Success; 144683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor} 144783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 1448eff92135d32039c9874dc356f3e93143af6069c1John McCallstatic QualType GetTypeOfFunction(ASTContext &Context, 1449eff92135d32039c9874dc356f3e93143af6069c1John McCall bool isAddressOfOperand, 1450eff92135d32039c9874dc356f3e93143af6069c1John McCall FunctionDecl *Fn) { 1451eff92135d32039c9874dc356f3e93143af6069c1John McCall if (!isAddressOfOperand) return Fn->getType(); 1452eff92135d32039c9874dc356f3e93143af6069c1John McCall if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) 1453eff92135d32039c9874dc356f3e93143af6069c1John McCall if (Method->isInstance()) 1454eff92135d32039c9874dc356f3e93143af6069c1John McCall return Context.getMemberPointerType(Fn->getType(), 1455eff92135d32039c9874dc356f3e93143af6069c1John McCall Context.getTypeDeclType(Method->getParent()).getTypePtr()); 1456eff92135d32039c9874dc356f3e93143af6069c1John McCall return Context.getPointerType(Fn->getType()); 1457eff92135d32039c9874dc356f3e93143af6069c1John McCall} 1458eff92135d32039c9874dc356f3e93143af6069c1John McCall 1459eff92135d32039c9874dc356f3e93143af6069c1John McCall/// Apply the deduction rules for overload sets. 1460eff92135d32039c9874dc356f3e93143af6069c1John McCall/// 1461eff92135d32039c9874dc356f3e93143af6069c1John McCall/// \return the null type if this argument should be treated as an 1462eff92135d32039c9874dc356f3e93143af6069c1John McCall/// undeduced context 1463eff92135d32039c9874dc356f3e93143af6069c1John McCallstatic QualType 1464eff92135d32039c9874dc356f3e93143af6069c1John McCallResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams, 1465eff92135d32039c9874dc356f3e93143af6069c1John McCall Expr *Arg, QualType ParamType) { 14667bb12da2b0749eeebb21854c77877736969e59f2John McCall llvm::PointerIntPair<OverloadExpr*,1> R = OverloadExpr::find(Arg); 1467eff92135d32039c9874dc356f3e93143af6069c1John McCall 14687bb12da2b0749eeebb21854c77877736969e59f2John McCall bool isAddressOfOperand = bool(R.getInt()); 14697bb12da2b0749eeebb21854c77877736969e59f2John McCall OverloadExpr *Ovl = R.getPointer(); 1470eff92135d32039c9874dc356f3e93143af6069c1John McCall 1471eff92135d32039c9874dc356f3e93143af6069c1John McCall // If there were explicit template arguments, we can only find 1472eff92135d32039c9874dc356f3e93143af6069c1John McCall // something via C++ [temp.arg.explicit]p3, i.e. if the arguments 1473eff92135d32039c9874dc356f3e93143af6069c1John McCall // unambiguously name a full specialization. 14747bb12da2b0749eeebb21854c77877736969e59f2John McCall if (Ovl->hasExplicitTemplateArgs()) { 1475eff92135d32039c9874dc356f3e93143af6069c1John McCall // But we can still look for an explicit specialization. 1476eff92135d32039c9874dc356f3e93143af6069c1John McCall if (FunctionDecl *ExplicitSpec 14777bb12da2b0749eeebb21854c77877736969e59f2John McCall = S.ResolveSingleFunctionTemplateSpecialization(Ovl)) 14787bb12da2b0749eeebb21854c77877736969e59f2John McCall return GetTypeOfFunction(S.Context, isAddressOfOperand, ExplicitSpec); 1479eff92135d32039c9874dc356f3e93143af6069c1John McCall return QualType(); 1480eff92135d32039c9874dc356f3e93143af6069c1John McCall } 1481eff92135d32039c9874dc356f3e93143af6069c1John McCall 1482eff92135d32039c9874dc356f3e93143af6069c1John McCall // C++0x [temp.deduct.call]p6: 1483eff92135d32039c9874dc356f3e93143af6069c1John McCall // When P is a function type, pointer to function type, or pointer 1484eff92135d32039c9874dc356f3e93143af6069c1John McCall // to member function type: 1485eff92135d32039c9874dc356f3e93143af6069c1John McCall 1486eff92135d32039c9874dc356f3e93143af6069c1John McCall if (!ParamType->isFunctionType() && 1487eff92135d32039c9874dc356f3e93143af6069c1John McCall !ParamType->isFunctionPointerType() && 1488eff92135d32039c9874dc356f3e93143af6069c1John McCall !ParamType->isMemberFunctionPointerType()) 1489eff92135d32039c9874dc356f3e93143af6069c1John McCall return QualType(); 1490eff92135d32039c9874dc356f3e93143af6069c1John McCall 1491eff92135d32039c9874dc356f3e93143af6069c1John McCall QualType Match; 14927bb12da2b0749eeebb21854c77877736969e59f2John McCall for (UnresolvedSetIterator I = Ovl->decls_begin(), 14937bb12da2b0749eeebb21854c77877736969e59f2John McCall E = Ovl->decls_end(); I != E; ++I) { 1494eff92135d32039c9874dc356f3e93143af6069c1John McCall NamedDecl *D = (*I)->getUnderlyingDecl(); 1495eff92135d32039c9874dc356f3e93143af6069c1John McCall 1496eff92135d32039c9874dc356f3e93143af6069c1John McCall // - If the argument is an overload set containing one or more 1497eff92135d32039c9874dc356f3e93143af6069c1John McCall // function templates, the parameter is treated as a 1498eff92135d32039c9874dc356f3e93143af6069c1John McCall // non-deduced context. 1499eff92135d32039c9874dc356f3e93143af6069c1John McCall if (isa<FunctionTemplateDecl>(D)) 1500eff92135d32039c9874dc356f3e93143af6069c1John McCall return QualType(); 1501eff92135d32039c9874dc356f3e93143af6069c1John McCall 1502eff92135d32039c9874dc356f3e93143af6069c1John McCall FunctionDecl *Fn = cast<FunctionDecl>(D); 1503eff92135d32039c9874dc356f3e93143af6069c1John McCall QualType ArgType = GetTypeOfFunction(S.Context, isAddressOfOperand, Fn); 1504eff92135d32039c9874dc356f3e93143af6069c1John McCall 1505eff92135d32039c9874dc356f3e93143af6069c1John McCall // - If the argument is an overload set (not containing function 1506eff92135d32039c9874dc356f3e93143af6069c1John McCall // templates), trial argument deduction is attempted using each 1507eff92135d32039c9874dc356f3e93143af6069c1John McCall // of the members of the set. If deduction succeeds for only one 1508eff92135d32039c9874dc356f3e93143af6069c1John McCall // of the overload set members, that member is used as the 1509eff92135d32039c9874dc356f3e93143af6069c1John McCall // argument value for the deduction. If deduction succeeds for 1510eff92135d32039c9874dc356f3e93143af6069c1John McCall // more than one member of the overload set the parameter is 1511eff92135d32039c9874dc356f3e93143af6069c1John McCall // treated as a non-deduced context. 1512eff92135d32039c9874dc356f3e93143af6069c1John McCall 1513eff92135d32039c9874dc356f3e93143af6069c1John McCall // We do all of this in a fresh context per C++0x [temp.deduct.type]p2: 1514eff92135d32039c9874dc356f3e93143af6069c1John McCall // Type deduction is done independently for each P/A pair, and 1515eff92135d32039c9874dc356f3e93143af6069c1John McCall // the deduced template argument values are then combined. 1516eff92135d32039c9874dc356f3e93143af6069c1John McCall // So we do not reject deductions which were made elsewhere. 151702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVector<DeducedTemplateArgument, 8> 151802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor Deduced(TemplateParams->size()); 15195769d6195087229770d7ac90449443e026c47103John McCall Sema::TemplateDeductionInfo Info(S.Context, Ovl->getNameLoc()); 1520eff92135d32039c9874dc356f3e93143af6069c1John McCall unsigned TDF = 0; 1521eff92135d32039c9874dc356f3e93143af6069c1John McCall 1522eff92135d32039c9874dc356f3e93143af6069c1John McCall Sema::TemplateDeductionResult Result 1523a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = DeduceTemplateArguments(S, TemplateParams, 1524eff92135d32039c9874dc356f3e93143af6069c1John McCall ParamType, ArgType, 1525eff92135d32039c9874dc356f3e93143af6069c1John McCall Info, Deduced, TDF); 1526eff92135d32039c9874dc356f3e93143af6069c1John McCall if (Result) continue; 1527eff92135d32039c9874dc356f3e93143af6069c1John McCall if (!Match.isNull()) return QualType(); 1528eff92135d32039c9874dc356f3e93143af6069c1John McCall Match = ArgType; 1529eff92135d32039c9874dc356f3e93143af6069c1John McCall } 1530eff92135d32039c9874dc356f3e93143af6069c1John McCall 1531eff92135d32039c9874dc356f3e93143af6069c1John McCall return Match; 1532eff92135d32039c9874dc356f3e93143af6069c1John McCall} 1533eff92135d32039c9874dc356f3e93143af6069c1John McCall 1534e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \brief Perform template argument deduction from a function call 1535e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// (C++ [temp.deduct.call]). 1536e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 1537e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param FunctionTemplate the function template for which we are performing 1538e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// template argument deduction. 1539e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 154048026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// \param ExplicitTemplateArguments the explicit template arguments provided 154148026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// for this call. 15426db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor/// 1543e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Args the function call arguments 1544e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 1545e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param NumArgs the number of arguments in Args 1546e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 154748026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// \param Name the name of the function being called. This is only significant 154848026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// when the function template is a conversion function template, in which 154948026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// case this routine will also perform template argument deduction based on 155048026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// the function to which 155148026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// 1552e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Specialization if template argument deduction was successful, 15531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// this will be set to the function template specialization produced by 1554e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// template argument deduction. 1555e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 1556e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Info the argument will be updated to provide additional information 1557e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// about template argument deduction. 1558e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 1559e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns the result of template argument deduction. 1560e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorSema::TemplateDeductionResult 1561e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, 156248026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor const TemplateArgumentListInfo *ExplicitTemplateArgs, 1563e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor Expr **Args, unsigned NumArgs, 1564e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor FunctionDecl *&Specialization, 1565e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor TemplateDeductionInfo &Info) { 1566e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 15676db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor 1568e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // C++ [temp.deduct.call]p1: 1569e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // Template argument deduction is done by comparing each function template 1570e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // parameter type (call it P) with the type of the corresponding argument 1571e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // of the call (call it A) as described below. 1572e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor unsigned CheckArgs = NumArgs; 15736db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor if (NumArgs < Function->getMinRequiredArguments()) 1574e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor return TDK_TooFewArguments; 1575e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor else if (NumArgs > Function->getNumParams()) { 15761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const FunctionProtoType *Proto 1577183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall = Function->getType()->getAs<FunctionProtoType>(); 1578e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (!Proto->isVariadic()) 1579e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor return TDK_TooManyArguments; 15801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1581e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor CheckArgs = Function->getNumParams(); 1582e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor } 15831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 15846db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor // The types of the parameters from which we will perform template argument 15856db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor // deduction. 15862b0749a4f8965d0205bf77322db150c13c39e3beDouglas Gregor Sema::LocalInstantiationScope InstScope(*this); 1587e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor TemplateParameterList *TemplateParams 1588e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor = FunctionTemplate->getTemplateParameters(); 158902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVector<DeducedTemplateArgument, 4> Deduced; 15906db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor llvm::SmallVector<QualType, 4> ParamTypes; 159102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor unsigned NumExplicitlySpecified = 0; 1592d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall if (ExplicitTemplateArgs) { 159383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateDeductionResult Result = 159483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor SubstituteExplicitTemplateArguments(FunctionTemplate, 1595d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall *ExplicitTemplateArgs, 159683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Deduced, 159783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ParamTypes, 159883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 0, 159983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Info); 160083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (Result) 160183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return Result; 160202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor 160302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor NumExplicitlySpecified = Deduced.size(); 16046db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor } else { 16056db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor // Just fill in the parameter types from the function declaration. 16066db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor for (unsigned I = 0; I != CheckArgs; ++I) 16076db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor ParamTypes.push_back(Function->getParamDecl(I)->getType()); 16086db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor } 16091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16106db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor // Deduce template arguments from the function parameters. 16111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Deduced.resize(TemplateParams->size()); 1612e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor for (unsigned I = 0; I != CheckArgs; ++I) { 16136db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor QualType ParamType = ParamTypes[I]; 1614e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor QualType ArgType = Args[I]->getType(); 16151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1616eff92135d32039c9874dc356f3e93143af6069c1John McCall // Overload sets usually make this parameter an undeduced 1617eff92135d32039c9874dc356f3e93143af6069c1John McCall // context, but there are sometimes special circumstances. 1618eff92135d32039c9874dc356f3e93143af6069c1John McCall if (ArgType == Context.OverloadTy) { 1619eff92135d32039c9874dc356f3e93143af6069c1John McCall ArgType = ResolveOverloadForDeduction(*this, TemplateParams, 1620eff92135d32039c9874dc356f3e93143af6069c1John McCall Args[I], ParamType); 1621eff92135d32039c9874dc356f3e93143af6069c1John McCall if (ArgType.isNull()) 1622eff92135d32039c9874dc356f3e93143af6069c1John McCall continue; 1623eff92135d32039c9874dc356f3e93143af6069c1John McCall } 1624eff92135d32039c9874dc356f3e93143af6069c1John McCall 1625e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // C++ [temp.deduct.call]p2: 1626e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // If P is not a reference type: 1627e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor QualType CanonParamType = Context.getCanonicalType(ParamType); 1628500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor bool ParamWasReference = isa<ReferenceType>(CanonParamType); 1629500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor if (!ParamWasReference) { 16301eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // - If A is an array type, the pointer type produced by the 16311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // array-to-pointer standard conversion (4.2) is used in place of 1632e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // A for type deduction; otherwise, 1633e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (ArgType->isArrayType()) 1634e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor ArgType = Context.getArrayDecayedType(ArgType); 16351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // - If A is a function type, the pointer type produced by the 16361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // function-to-pointer standard conversion (4.3) is used in place 1637e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // of A for type deduction; otherwise, 1638e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor else if (ArgType->isFunctionType()) 1639e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor ArgType = Context.getPointerType(ArgType); 1640e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor else { 1641e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // - If A is a cv-qualified type, the top level cv-qualifiers of A’s 1642e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // type are ignored for type deduction. 1643e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor QualType CanonArgType = Context.getCanonicalType(ArgType); 1644a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor if (CanonArgType.getLocalCVRQualifiers()) 1645a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor ArgType = CanonArgType.getLocalUnqualifiedType(); 1646e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor } 1647e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor } 16481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1649e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // C++0x [temp.deduct.call]p3: 1650e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // If P is a cv-qualified type, the top level cv-qualifiers of P’s type 16511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // are ignored for type deduction. 1652a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor if (CanonParamType.getLocalCVRQualifiers()) 1653a4923eb7c4b04d360cb2747641a5e92818edf804Douglas Gregor ParamType = CanonParamType.getLocalUnqualifiedType(); 16546217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek if (const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>()) { 16551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // [...] If P is a reference type, the type referred to by P is used 16561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // for type deduction. 1657e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor ParamType = ParamRefType->getPointeeType(); 16581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // [...] If P is of the form T&&, where T is a template parameter, and 16601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // the argument is an lvalue, the type A& is used in place of A for 1661e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // type deduction. 1662e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (isa<RValueReferenceType>(ParamRefType) && 1663183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall ParamRefType->getAs<TemplateTypeParmType>() && 1664e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor Args[I]->isLvalue(Context) == Expr::LV_Valid) 1665e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor ArgType = Context.getLValueReferenceType(ArgType); 1666e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor } 16671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1668e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // C++0x [temp.deduct.call]p4: 1669e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // In general, the deduction process attempts to find template argument 1670e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // values that will make the deduced A identical to A (after the type A 1671e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // is transformed as described above). [...] 16721282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor unsigned TDF = TDF_SkipNonDependent; 16731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1674508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor // - If the original P is a reference type, the deduced A (i.e., the 1675508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor // type referred to by the reference) can be more cv-qualified than 1676508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor // the transformed A. 1677508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (ParamWasReference) 1678508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor TDF |= TDF_ParamWithReferenceType; 16791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // - The transformed A can be another pointer or pointer to member 16801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // type that can be converted to the deduced A via a qualification 1681508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor // conversion (4.4). 1682508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (ArgType->isPointerType() || ArgType->isMemberPointerType()) 1683508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor TDF |= TDF_IgnoreQualifiers; 16841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // - If P is a class and P has the form simple-template-id, then the 16854112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor // transformed A can be a derived class of the deduced A. Likewise, 16864112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor // if P is a pointer to a class of the form simple-template-id, the 16874112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor // transformed A can be a pointer to a derived class pointed to by 16884112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor // the deduced A. 16894112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor if (isSimpleTemplateIdType(ParamType) || 16901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump (isa<PointerType>(ParamType) && 16914112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor isSimpleTemplateIdType( 16926217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek ParamType->getAs<PointerType>()->getPointeeType()))) 16934112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor TDF |= TDF_DerivedClass; 16941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1695e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor if (TemplateDeductionResult Result 1696a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = ::DeduceTemplateArguments(*this, TemplateParams, 1697500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor ParamType, ArgType, Info, Deduced, 1698508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor TDF)) 1699e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor return Result; 17001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 170165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // FIXME: we need to check that the deduced A is the same as A, 170265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // modulo the various allowed differences. 1703e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor } 170465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 17051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 170602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor NumExplicitlySpecified, 170783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Specialization, Info); 170883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor} 1709127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor 171083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \brief Deduce template arguments when taking the address of a function 17114b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to 17124b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// a template. 171383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 171483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionTemplate the function template for which we are performing 171583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template argument deduction. 171683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 17174b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param ExplicitTemplateArguments the explicitly-specified template 17184b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// arguments. 171983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 172083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param ArgFunctionType the function type that will be used as the 172183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// "argument" type (A) when performing template argument deduction from the 17224b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// function template's function type. This type may be NULL, if there is no 17234b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// argument type to compare against, in C++0x [temp.arg.explicit]p3. 172483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 172583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Specialization if template argument deduction was successful, 17261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// this will be set to the function template specialization produced by 172783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template argument deduction. 172883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 172983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Info the argument will be updated to provide additional information 173083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// about template argument deduction. 173183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 173283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \returns the result of template argument deduction. 173383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::TemplateDeductionResult 173483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, 1735d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall const TemplateArgumentListInfo *ExplicitTemplateArgs, 173683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor QualType ArgFunctionType, 173783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionDecl *&Specialization, 173883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateDeductionInfo &Info) { 173983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 174083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateParameterList *TemplateParams 174183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor = FunctionTemplate->getTemplateParameters(); 174283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor QualType FunctionType = Function->getType(); 17431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 174483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Substitute any explicit template arguments. 17452b0749a4f8965d0205bf77322db150c13c39e3beDouglas Gregor Sema::LocalInstantiationScope InstScope(*this); 174602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVector<DeducedTemplateArgument, 4> Deduced; 174702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor unsigned NumExplicitlySpecified = 0; 174883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor llvm::SmallVector<QualType, 4> ParamTypes; 1749d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall if (ExplicitTemplateArgs) { 17501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (TemplateDeductionResult Result 17511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump = SubstituteExplicitTemplateArguments(FunctionTemplate, 1752d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall *ExplicitTemplateArgs, 17531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Deduced, ParamTypes, 175483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor &FunctionType, Info)) 175583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return Result; 175602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor 175702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor NumExplicitlySpecified = Deduced.size(); 1758127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor } 175983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 176083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Template argument deduction for function templates in a SFINAE context. 176183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Trap any errors that might occur. 17621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump SFINAETrap Trap(*this); 17631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1764eff92135d32039c9874dc356f3e93143af6069c1John McCall Deduced.resize(TemplateParams->size()); 1765eff92135d32039c9874dc356f3e93143af6069c1John McCall 17664b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor if (!ArgFunctionType.isNull()) { 17674b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor // Deduce template arguments from the function type. 17684b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor if (TemplateDeductionResult Result 1769a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = ::DeduceTemplateArguments(*this, TemplateParams, 17704b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor FunctionType, ArgFunctionType, Info, 17714b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor Deduced, 0)) 17724b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor return Result; 17734b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor } 17744b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor 17751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 177602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor NumExplicitlySpecified, 177783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Specialization, Info); 1778e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor} 1779e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor 178065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \brief Deduce template arguments for a templated conversion 178165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// function (C++ [temp.deduct.conv]) and, if successful, produce a 178265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// conversion function template specialization. 178365ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::TemplateDeductionResult 178465ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, 178565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor QualType ToType, 178665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor CXXConversionDecl *&Specialization, 178765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor TemplateDeductionInfo &Info) { 17881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump CXXConversionDecl *Conv 178965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()); 179065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor QualType FromType = Conv->getConversionType(); 179165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 179265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // Canonicalize the types for deduction. 179365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor QualType P = Context.getCanonicalType(FromType); 179465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor QualType A = Context.getCanonicalType(ToType); 179565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 179665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // C++0x [temp.deduct.conv]p3: 179765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // If P is a reference type, the type referred to by P is used for 179865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // type deduction. 179965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (const ReferenceType *PRef = P->getAs<ReferenceType>()) 180065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor P = PRef->getPointeeType(); 180165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 180265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // C++0x [temp.deduct.conv]p3: 180365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // If A is a reference type, the type referred to by A is used 180465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // for type deduction. 180565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (const ReferenceType *ARef = A->getAs<ReferenceType>()) 180665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor A = ARef->getPointeeType(); 180765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // C++ [temp.deduct.conv]p2: 180865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // 18091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // If A is not a reference type: 181065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor else { 181165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor assert(!A->isReferenceType() && "Reference types were handled above"); 181265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 181365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // - If P is an array type, the pointer type produced by the 18141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // array-to-pointer standard conversion (4.2) is used in place 181565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // of P for type deduction; otherwise, 181665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (P->isArrayType()) 181765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor P = Context.getArrayDecayedType(P); 181865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // - If P is a function type, the pointer type produced by the 181965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // function-to-pointer standard conversion (4.3) is used in 182065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // place of P for type deduction; otherwise, 182165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor else if (P->isFunctionType()) 182265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor P = Context.getPointerType(P); 182365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // - If P is a cv-qualified type, the top level cv-qualifiers of 182465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // P’s type are ignored for type deduction. 182565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor else 182665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor P = P.getUnqualifiedType(); 182765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 182865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // C++0x [temp.deduct.conv]p3: 182965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // If A is a cv-qualified type, the top level cv-qualifiers of A’s 183065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // type are ignored for type deduction. 183165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor A = A.getUnqualifiedType(); 183265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor } 183365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 183465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // Template argument deduction for function templates in a SFINAE context. 183565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // Trap any errors that might occur. 18361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump SFINAETrap Trap(*this); 183765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 183865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // C++ [temp.deduct.conv]p1: 183965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // Template argument deduction is done by comparing the return 184065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // type of the template conversion function (call it P) with the 184165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // type that is required as the result of the conversion (call it 184265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // A) as described in 14.8.2.4. 184365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor TemplateParameterList *TemplateParams 184465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor = FunctionTemplate->getTemplateParameters(); 184502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVector<DeducedTemplateArgument, 4> Deduced; 18461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Deduced.resize(TemplateParams->size()); 184765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 184865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // C++0x [temp.deduct.conv]p4: 184965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // In general, the deduction process attempts to find template 185065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // argument values that will make the deduced A identical to 185165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // A. However, there are two cases that allow a difference: 185265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor unsigned TDF = 0; 185365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // - If the original A is a reference type, A can be more 185465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // cv-qualified than the deduced A (i.e., the type referred to 185565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // by the reference) 185665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (ToType->isReferenceType()) 185765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor TDF |= TDF_ParamWithReferenceType; 185865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // - The deduced A can be another pointer or pointer to member 185965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // type that can be converted to A via a qualification 186065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // conversion. 186165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // 186265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when 186365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // both P and A are pointers or member pointers. In this case, we 186465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // just ignore cv-qualifiers completely). 186565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if ((P->isPointerType() && A->isPointerType()) || 186665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor (P->isMemberPointerType() && P->isMemberPointerType())) 186765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor TDF |= TDF_IgnoreQualifiers; 186865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (TemplateDeductionResult Result 1869a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = ::DeduceTemplateArguments(*this, TemplateParams, 187065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor P, A, Info, Deduced, TDF)) 187165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor return Result; 187265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 187365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // FIXME: we need to check that the deduced A is the same as A, 187465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // modulo the various allowed differences. 18751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 187665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // Finish template argument deduction. 18772b0749a4f8965d0205bf77322db150c13c39e3beDouglas Gregor Sema::LocalInstantiationScope InstScope(*this); 187865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor FunctionDecl *Spec = 0; 187965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor TemplateDeductionResult Result 188002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 0, Spec, 188102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor Info); 188265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor Specialization = cast_or_null<CXXConversionDecl>(Spec); 188365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor return Result; 188465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor} 188565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 18864b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \brief Deduce template arguments for a function template when there is 18874b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// nothing to deduce against (C++0x [temp.arg.explicit]p3). 18884b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// 18894b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param FunctionTemplate the function template for which we are performing 18904b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template argument deduction. 18914b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// 18924b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param ExplicitTemplateArguments the explicitly-specified template 18934b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// arguments. 18944b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// 18954b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param Specialization if template argument deduction was successful, 18964b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// this will be set to the function template specialization produced by 18974b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template argument deduction. 18984b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// 18994b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param Info the argument will be updated to provide additional information 19004b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// about template argument deduction. 19014b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// 19024b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \returns the result of template argument deduction. 19034b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas GregorSema::TemplateDeductionResult 19044b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, 19054b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor const TemplateArgumentListInfo *ExplicitTemplateArgs, 19064b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor FunctionDecl *&Specialization, 19074b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor TemplateDeductionInfo &Info) { 19084b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, 19094b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor QualType(), Specialization, Info); 19104b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor} 19114b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor 19128a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Stores the result of comparing the qualifiers of two types. 19138a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorenum DeductionQualifierComparison { 19148a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor NeitherMoreQualified = 0, 19158a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor ParamMoreQualified, 19168a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor ArgMoreQualified 19178a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor}; 19188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 19198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Deduce the template arguments during partial ordering by comparing 19208a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// the parameter type and the argument type (C++0x [temp.deduct.partial]). 19218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// 1922a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth/// \param S the semantic analysis object within which we are deducing 19238a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// 19248a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param TemplateParams the template parameters that we are deducing 19258a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// 19268a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param ParamIn the parameter type 19278a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// 19288a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param ArgIn the argument type 19298a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// 19308a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param Info information about the template argument deduction itself 19318a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// 19328a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param Deduced the deduced template arguments 19338a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// 19348a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \returns the result of template argument deduction so far. Note that a 19358a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// "success" result means that template argument deduction has not yet failed, 19368a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// but it may still fail, later, for other reasons. 19378a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic Sema::TemplateDeductionResult 1938a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArgumentsDuringPartialOrdering(Sema &S, 193902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor TemplateParameterList *TemplateParams, 19408a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor QualType ParamIn, QualType ArgIn, 19418a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Sema::TemplateDeductionInfo &Info, 194202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced, 194302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) { 1944a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth CanQualType Param = S.Context.getCanonicalType(ParamIn); 1945a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth CanQualType Arg = S.Context.getCanonicalType(ArgIn); 19468a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 19478a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p5: 19488a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // Before the partial ordering is done, certain transformations are 19498a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // performed on the types used for partial ordering: 19508a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // - If P is a reference type, P is replaced by the type referred to. 19518a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor CanQual<ReferenceType> ParamRef = Param->getAs<ReferenceType>(); 1952e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall if (!ParamRef.isNull()) 19538a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Param = ParamRef->getPointeeType(); 19548a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 19558a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // - If A is a reference type, A is replaced by the type referred to. 19568a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor CanQual<ReferenceType> ArgRef = Arg->getAs<ReferenceType>(); 1957e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall if (!ArgRef.isNull()) 19588a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Arg = ArgRef->getPointeeType(); 19598a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 1960e27ec8ad56dbf1efb2de004b90fbbb86f740e3f1John McCall if (QualifierComparisons && !ParamRef.isNull() && !ArgRef.isNull()) { 19618a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p6: 19628a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // If both P and A were reference types (before being replaced with the 19638a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // type referred to above), determine which of the two types (if any) is 19648a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // more cv-qualified than the other; otherwise the types are considered to 19658a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // be equally cv-qualified for partial ordering purposes. The result of this 19668a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // determination will be used below. 19678a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // 19688a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // We save this information for later, using it only when deduction 19698a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // succeeds in both directions. 19708a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor DeductionQualifierComparison QualifierResult = NeitherMoreQualified; 19718a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (Param.isMoreQualifiedThan(Arg)) 19728a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor QualifierResult = ParamMoreQualified; 19738a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor else if (Arg.isMoreQualifiedThan(Param)) 19748a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor QualifierResult = ArgMoreQualified; 19758a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor QualifierComparisons->push_back(QualifierResult); 19768a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 19778a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 19788a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p7: 19798a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // Remove any top-level cv-qualifiers: 19808a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // - If P is a cv-qualified type, P is replaced by the cv-unqualified 19818a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // version of P. 19828a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Param = Param.getUnqualifiedType(); 19838a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // - If A is a cv-qualified type, A is replaced by the cv-unqualified 19848a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // version of A. 19858a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Arg = Arg.getUnqualifiedType(); 19868a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 19878a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p8: 19888a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // Using the resulting types P and A the deduction is then done as 19898a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // described in 14.9.2.5. If deduction succeeds for a given type, the type 19908a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // from the argument template is considered to be at least as specialized 19918a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // as the type from the parameter template. 1992a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceTemplateArguments(S, TemplateParams, Param, Arg, Info, 19938a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Deduced, TDF_None); 19948a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor} 19958a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 19968a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic void 1997e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, QualType T, 1998e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor bool OnlyDeduced, 1999ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor unsigned Level, 2000e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor llvm::SmallVectorImpl<bool> &Deduced); 20018a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 20028a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Determine whether the function template \p FT1 is at least as 20038a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// specialized as \p FT2. 20048a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic bool isAtLeastAsSpecializedAs(Sema &S, 20055769d6195087229770d7ac90449443e026c47103John McCall SourceLocation Loc, 20068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor FunctionTemplateDecl *FT1, 20078a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor FunctionTemplateDecl *FT2, 20088a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor TemplatePartialOrderingContext TPOC, 20098a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor llvm::SmallVectorImpl<DeductionQualifierComparison> *QualifierComparisons) { 20108a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor FunctionDecl *FD1 = FT1->getTemplatedDecl(); 20118a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor FunctionDecl *FD2 = FT2->getTemplatedDecl(); 20128a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); 20138a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); 20148a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 20158a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor assert(Proto1 && Proto2 && "Function templates must have prototypes"); 20168a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); 201702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVector<DeducedTemplateArgument, 4> Deduced; 20188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Deduced.resize(TemplateParams->size()); 20198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 20208a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p3: 20218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // The types used to determine the ordering depend on the context in which 20228a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // the partial ordering is done: 20235769d6195087229770d7ac90449443e026c47103John McCall Sema::TemplateDeductionInfo Info(S.Context, Loc); 20248a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor switch (TPOC) { 20258a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case TPOC_Call: { 20268a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // - In the context of a function call, the function parameter types are 20278a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // used. 20288a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs()); 20298a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor for (unsigned I = 0; I != NumParams; ++I) 2030a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth if (DeduceTemplateArgumentsDuringPartialOrdering(S, 20318a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor TemplateParams, 20328a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Proto2->getArgType(I), 20338a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Proto1->getArgType(I), 20348a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Info, 20358a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Deduced, 20368a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor QualifierComparisons)) 20378a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return false; 20388a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 20398a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 20408a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 20418a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 20428a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case TPOC_Conversion: 20438a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // - In the context of a call to a conversion operator, the return types 20448a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // of the conversion function templates are used. 2045a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth if (DeduceTemplateArgumentsDuringPartialOrdering(S, 20468a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor TemplateParams, 20478a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Proto2->getResultType(), 20488a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Proto1->getResultType(), 20498a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Info, 20508a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Deduced, 20518a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor QualifierComparisons)) 20528a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return false; 20538a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 20548a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 20558a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case TPOC_Other: 20568a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // - In other contexts (14.6.6.2) the function template’s function type 20578a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // is used. 2058a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth if (DeduceTemplateArgumentsDuringPartialOrdering(S, 20598a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor TemplateParams, 20608a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor FD2->getType(), 20618a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor FD1->getType(), 20628a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Info, 20638a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Deduced, 20648a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor QualifierComparisons)) 20658a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return false; 20668a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 20678a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 20688a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 20698a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p11: 20708a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // In most cases, all template parameters must have values in order for 20718a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // deduction to succeed, but for partial ordering purposes a template 20728a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // parameter may remain without a value provided it is not used in the 20738a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // types being used for partial ordering. [ Note: a template parameter used 20748a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // in a non-deduced context is considered used. -end note] 20758a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor unsigned ArgIdx = 0, NumArgs = Deduced.size(); 20768a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor for (; ArgIdx != NumArgs; ++ArgIdx) 20778a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (Deduced[ArgIdx].isNull()) 20788a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 20798a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 20808a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (ArgIdx == NumArgs) { 20818a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // All template arguments were deduced. FT1 is at least as specialized 20828a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // as FT2. 20838a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return true; 20848a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 20858a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 2086e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor // Figure out which template parameters were used. 20878a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor llvm::SmallVector<bool, 4> UsedParameters; 20888a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor UsedParameters.resize(TemplateParams->size()); 20898a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor switch (TPOC) { 20908a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case TPOC_Call: { 20918a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor unsigned NumParams = std::min(Proto1->getNumArgs(), Proto2->getNumArgs()); 20928a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor for (unsigned I = 0; I != NumParams; ++I) 2093ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false, 2094ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor TemplateParams->getDepth(), 2095e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor UsedParameters); 20968a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 20978a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 20988a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 20998a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case TPOC_Conversion: 2100ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false, 2101ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor TemplateParams->getDepth(), 2102e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor UsedParameters); 21038a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 21048a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 21058a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case TPOC_Other: 2106ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor ::MarkUsedTemplateParameters(S, FD2->getType(), false, 2107ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor TemplateParams->getDepth(), 2108ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor UsedParameters); 21098a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 21108a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 21118a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 21128a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor for (; ArgIdx != NumArgs; ++ArgIdx) 21138a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // If this argument had no value deduced but was used in one of the types 21148a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // used for partial ordering, then deduction fails. 21158a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) 21168a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return false; 21178a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 21188a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return true; 21198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor} 21208a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 21218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 2122bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \brief Returns the more specialized function template according 212365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// to the rules of function template partial ordering (C++ [temp.func.order]). 212465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// 212565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \param FT1 the first function template 212665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// 212765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \param FT2 the second function template 212865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// 21298a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param TPOC the context in which we are performing partial ordering of 21308a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// function templates. 21311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 2132bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \returns the more specialized function template. If neither 213365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// template is more specialized, returns NULL. 213465ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorFunctionTemplateDecl * 213565ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, 213665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor FunctionTemplateDecl *FT2, 21375769d6195087229770d7ac90449443e026c47103John McCall SourceLocation Loc, 21388a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor TemplatePartialOrderingContext TPOC) { 21398a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor llvm::SmallVector<DeductionQualifierComparison, 4> QualifierComparisons; 21405769d6195087229770d7ac90449443e026c47103John McCall bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, 0); 21415769d6195087229770d7ac90449443e026c47103John McCall bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC, 21428a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor &QualifierComparisons); 21438a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 21448a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (Better1 != Better2) // We have a clear winner 21458a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return Better1? FT1 : FT2; 21468a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 21478a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (!Better1 && !Better2) // Neither is better than the other 214865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor return 0; 21498a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 21508a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 21518a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p10: 21528a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // If for each type being considered a given template is at least as 21538a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // specialized for all types and more specialized for some set of types and 21548a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // the other template is not more specialized for any types or is not at 21558a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // least as specialized for any types, then the given template is more 21568a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // specialized than the other template. Otherwise, neither template is more 21578a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // specialized than the other. 21588a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Better1 = false; 21598a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Better2 = false; 21608a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor for (unsigned I = 0, N = QualifierComparisons.size(); I != N; ++I) { 21618a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p9: 21628a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // If, for a given type, deduction succeeds in both directions (i.e., the 21638a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // types are identical after the transformations above) and if the type 21648a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // from the argument template is more cv-qualified than the type from the 21658a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // parameter template (as described above) that type is considered to be 21668a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // more specialized than the other. If neither type is more cv-qualified 21678a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // than the other then neither type is more specialized than the other. 21688a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor switch (QualifierComparisons[I]) { 21698a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case NeitherMoreQualified: 21708a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 21718a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 21728a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case ParamMoreQualified: 21738a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Better1 = true; 21748a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (Better2) 21758a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return 0; 21768a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 21778a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 21788a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case ArgMoreQualified: 21798a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Better2 = true; 21808a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (Better1) 21818a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return 0; 21828a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 21838a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 21848a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 21858a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 21868a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor assert(!(Better1 && Better2) && "Should have broken out in the loop above"); 218765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (Better1) 218865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor return FT1; 21898a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor else if (Better2) 21908a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return FT2; 21918a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor else 21928a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return 0; 219365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor} 219483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 2195d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \brief Determine if the two templates are equivalent. 2196d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregorstatic bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) { 2197d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor if (T1 == T2) 2198d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor return true; 2199d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 2200d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor if (!T1 || !T2) 2201d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor return false; 2202d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 2203d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor return T1->getCanonicalDecl() == T2->getCanonicalDecl(); 2204d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor} 2205d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 2206d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \brief Retrieve the most specialized of the given function template 2207d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specializations. 2208d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 2209c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// \param SpecBegin the start iterator of the function template 2210c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// specializations that we will be comparing. 2211d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 2212c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// \param SpecEnd the end iterator of the function template 2213c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// specializations, paired with \p SpecBegin. 2214d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 2215d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param TPOC the partial ordering context to use to compare the function 2216d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// template specializations. 2217d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 2218d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param Loc the location where the ambiguity or no-specializations 2219d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// diagnostic should occur. 2220d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 2221d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param NoneDiag partial diagnostic used to diagnose cases where there are 2222d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// no matching candidates. 2223d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 2224d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one 2225d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// occurs. 2226d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 2227d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param CandidateDiag partial diagnostic used for each function template 2228d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specialization that is a candidate in the ambiguous ordering. One parameter 2229d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// in this diagnostic should be unbound, which will correspond to the string 2230d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// describing the template arguments for the function template specialization. 2231d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 2232d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param Index if non-NULL and the result of this function is non-nULL, 2233d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// receives the index corresponding to the resulting function template 2234d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specialization. 2235d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 2236d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \returns the most specialized function template specialization, if 2237c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// found. Otherwise, returns SpecEnd. 2238d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 2239d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \todo FIXME: Consider passing in the "also-ran" candidates that failed 2240d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// template argument deduction. 2241c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallUnresolvedSetIterator 2242c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallSema::getMostSpecialized(UnresolvedSetIterator SpecBegin, 2243c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall UnresolvedSetIterator SpecEnd, 2244c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall TemplatePartialOrderingContext TPOC, 2245c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall SourceLocation Loc, 2246c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall const PartialDiagnostic &NoneDiag, 2247c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall const PartialDiagnostic &AmbigDiag, 2248c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall const PartialDiagnostic &CandidateDiag) { 2249c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall if (SpecBegin == SpecEnd) { 2250d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor Diag(Loc, NoneDiag); 2251c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall return SpecEnd; 2252d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor } 2253d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 2254c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall if (SpecBegin + 1 == SpecEnd) 2255c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall return SpecBegin; 2256d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 2257d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor // Find the function template that is better than all of the templates it 2258d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor // has been compared to. 2259c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall UnresolvedSetIterator Best = SpecBegin; 2260d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor FunctionTemplateDecl *BestTemplate 2261c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall = cast<FunctionDecl>(*Best)->getPrimaryTemplate(); 2262d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor assert(BestTemplate && "Not a function template specialization?"); 2263c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) { 2264c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall FunctionTemplateDecl *Challenger 2265c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall = cast<FunctionDecl>(*I)->getPrimaryTemplate(); 2266d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor assert(Challenger && "Not a function template specialization?"); 2267c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, 22685769d6195087229770d7ac90449443e026c47103John McCall Loc, TPOC), 2269d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor Challenger)) { 2270d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor Best = I; 2271d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor BestTemplate = Challenger; 2272d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor } 2273d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor } 2274d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 2275d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor // Make sure that the "best" function template is more specialized than all 2276d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor // of the others. 2277d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor bool Ambiguous = false; 2278c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { 2279c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall FunctionTemplateDecl *Challenger 2280c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall = cast<FunctionDecl>(*I)->getPrimaryTemplate(); 2281d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor if (I != Best && 2282d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, 22835769d6195087229770d7ac90449443e026c47103John McCall Loc, TPOC), 2284d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor BestTemplate)) { 2285d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor Ambiguous = true; 2286d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor break; 2287d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor } 2288d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor } 2289d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 2290d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor if (!Ambiguous) { 2291d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor // We found an answer. Return it. 2292c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall return Best; 2293d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor } 2294d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 2295d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor // Diagnose the ambiguity. 2296d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor Diag(Loc, AmbigDiag); 2297d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 2298d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor // FIXME: Can we order the candidates in some sane way? 2299c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) 2300c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall Diag((*I)->getLocation(), CandidateDiag) 2301d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor << getTemplateArgumentBindingsText( 2302c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(), 2303c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs()); 2304d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 2305c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall return SpecEnd; 2306d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor} 2307d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 2308bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \brief Returns the more specialized class template partial specialization 2309bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// according to the rules of partial ordering of class template partial 2310bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// specializations (C++ [temp.class.order]). 2311bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// 2312bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \param PS1 the first class template partial specialization 2313bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// 2314bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \param PS2 the second class template partial specialization 2315bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// 2316bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \returns the more specialized class template partial specialization. If 2317bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// neither partial specialization is more specialized, returns NULL. 2318bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas GregorClassTemplatePartialSpecializationDecl * 2319bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas GregorSema::getMoreSpecializedPartialSpecialization( 2320bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor ClassTemplatePartialSpecializationDecl *PS1, 23215769d6195087229770d7ac90449443e026c47103John McCall ClassTemplatePartialSpecializationDecl *PS2, 23225769d6195087229770d7ac90449443e026c47103John McCall SourceLocation Loc) { 2323bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // C++ [temp.class.order]p1: 2324bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // For two class template partial specializations, the first is at least as 2325bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // specialized as the second if, given the following rewrite to two 2326bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // function templates, the first function template is at least as 2327bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // specialized as the second according to the ordering rules for function 2328bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // templates (14.6.6.2): 2329bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // - the first function template has the same template parameters as the 2330bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // first partial specialization and has a single function parameter 2331bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // whose type is a class template specialization with the template 2332bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // arguments of the first partial specialization, and 2333bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // - the second function template has the same template parameters as the 2334bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // second partial specialization and has a single function parameter 2335bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // whose type is a class template specialization with the template 2336bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // arguments of the second partial specialization. 2337bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // 2338bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // Rather than synthesize function templates, we merely perform the 2339bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // equivalent partial ordering by performing deduction directly on the 2340bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // template arguments of the class template partial specializations. This 2341bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // computation is slightly simpler than the general problem of function 2342bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // template partial ordering, because class template partial specializations 2343bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // are more constrained. We know that every template parameter is deduc 234402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVector<DeducedTemplateArgument, 4> Deduced; 23455769d6195087229770d7ac90449443e026c47103John McCall Sema::TemplateDeductionInfo Info(Context, Loc); 234631f17ecbef57b5679c017c375db330546b7b5145John McCall 234731f17ecbef57b5679c017c375db330546b7b5145John McCall QualType PT1 = PS1->getInjectedSpecializationType(); 234831f17ecbef57b5679c017c375db330546b7b5145John McCall QualType PT2 = PS2->getInjectedSpecializationType(); 2349bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor 2350bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // Determine whether PS1 is at least as specialized as PS2 2351bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor Deduced.resize(PS2->getTemplateParameters()->size()); 2352a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth bool Better1 = !DeduceTemplateArgumentsDuringPartialOrdering(*this, 2353bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor PS2->getTemplateParameters(), 235431f17ecbef57b5679c017c375db330546b7b5145John McCall PT2, 235531f17ecbef57b5679c017c375db330546b7b5145John McCall PT1, 2356bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor Info, 2357bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor Deduced, 2358bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor 0); 2359bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor 2360bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // Determine whether PS2 is at least as specialized as PS1 2361db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor Deduced.clear(); 2362bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor Deduced.resize(PS1->getTemplateParameters()->size()); 2363a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth bool Better2 = !DeduceTemplateArgumentsDuringPartialOrdering(*this, 2364bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor PS1->getTemplateParameters(), 236531f17ecbef57b5679c017c375db330546b7b5145John McCall PT1, 236631f17ecbef57b5679c017c375db330546b7b5145John McCall PT2, 2367bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor Info, 2368bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor Deduced, 2369bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor 0); 2370bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor 2371bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor if (Better1 == Better2) 2372bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor return 0; 2373bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor 2374bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor return Better1? PS1 : PS2; 2375bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor} 2376bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor 23771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void 2378e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, 2379e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor const TemplateArgument &TemplateArg, 2380e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor bool OnlyDeduced, 2381ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor unsigned Depth, 2382e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor llvm::SmallVectorImpl<bool> &Used); 2383031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2384e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given 2385031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// expression. 23861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void 2387e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, 2388e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor const Expr *E, 2389e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor bool OnlyDeduced, 2390ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor unsigned Depth, 2391e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor llvm::SmallVectorImpl<bool> &Used) { 2392e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to 2393e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor // find other occurrences of template parameters. 2394f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); 2395c781f9cd854f3d5d1c826f4a13382c6abca4cff7Douglas Gregor if (!DRE) 2396031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor return; 2397031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 23981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const NonTypeTemplateParmDecl *NTTP 2399031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); 2400031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor if (!NTTP) 2401031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor return; 2402031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2403ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor if (NTTP->getDepth() == Depth) 2404ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Used[NTTP->getIndex()] = true; 2405031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor} 2406031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2407e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given 2408e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// nested name specifier. 2409e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregorstatic void 2410e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, 2411e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor NestedNameSpecifier *NNS, 2412e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor bool OnlyDeduced, 2413ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor unsigned Depth, 2414e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor llvm::SmallVectorImpl<bool> &Used) { 2415e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor if (!NNS) 2416e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor return; 2417e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor 2418ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth, 2419ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Used); 2420e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0), 2421ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor OnlyDeduced, Depth, Used); 2422e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor} 2423e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor 2424e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given 2425e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// template name. 2426e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregorstatic void 2427e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, 2428e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor TemplateName Name, 2429e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor bool OnlyDeduced, 2430ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor unsigned Depth, 2431e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor llvm::SmallVectorImpl<bool> &Used) { 2432e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor if (TemplateDecl *Template = Name.getAsTemplateDecl()) { 2433e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor if (TemplateTemplateParmDecl *TTP 2434ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor = dyn_cast<TemplateTemplateParmDecl>(Template)) { 2435ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor if (TTP->getDepth() == Depth) 2436ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Used[TTP->getIndex()] = true; 2437ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor } 2438e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor return; 2439e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor } 2440e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor 2441788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) 2442788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced, 2443788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor Depth, Used); 2444e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) 2445ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced, 2446ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, Used); 2447e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor} 2448e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor 2449e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given 2450031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// type. 24511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void 2452e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, QualType T, 2453e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor bool OnlyDeduced, 2454ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor unsigned Depth, 2455e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor llvm::SmallVectorImpl<bool> &Used) { 2456e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor if (T.isNull()) 2457e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor return; 2458e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor 2459031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor // Non-dependent types have nothing deducible 2460031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor if (!T->isDependentType()) 2461031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor return; 2462031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2463031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor T = SemaRef.Context.getCanonicalType(T); 2464031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor switch (T->getTypeClass()) { 2465031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::Pointer: 2466e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, 2467e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor cast<PointerType>(T)->getPointeeType(), 2468e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor OnlyDeduced, 2469ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, 2470e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor Used); 2471031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2472031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2473031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::BlockPointer: 2474e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, 2475e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor cast<BlockPointerType>(T)->getPointeeType(), 2476e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor OnlyDeduced, 2477ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, 2478e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor Used); 2479031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2480031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2481031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::LValueReference: 2482031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::RValueReference: 2483e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, 2484e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor cast<ReferenceType>(T)->getPointeeType(), 2485e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor OnlyDeduced, 2486ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, 2487e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor Used); 2488031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2489031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2490031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::MemberPointer: { 2491031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); 2492e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced, 2493ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, Used); 2494e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0), 2495ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor OnlyDeduced, Depth, Used); 2496031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2497031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor } 2498031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2499031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::DependentSizedArray: 2500e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, 2501e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor cast<DependentSizedArrayType>(T)->getSizeExpr(), 2502ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor OnlyDeduced, Depth, Used); 2503031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor // Fall through to check the element type 2504031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2505031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::ConstantArray: 2506031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::IncompleteArray: 2507e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, 2508e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor cast<ArrayType>(T)->getElementType(), 2509ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor OnlyDeduced, Depth, Used); 2510031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2511031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2512031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::Vector: 2513031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::ExtVector: 2514e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, 2515e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor cast<VectorType>(T)->getElementType(), 2516ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor OnlyDeduced, Depth, Used); 2517031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2518031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 25199cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor case Type::DependentSizedExtVector: { 25209cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor const DependentSizedExtVectorType *VecType 2521f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor = cast<DependentSizedExtVectorType>(T); 2522e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced, 2523ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, Used); 2524e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced, 2525ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, Used); 25269cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor break; 25279cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor } 25289cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor 2529031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::FunctionProto: { 2530f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor const FunctionProtoType *Proto = cast<FunctionProtoType>(T); 2531e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced, 2532ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, Used); 2533031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I) 2534e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced, 2535ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, Used); 2536031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2537031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor } 2538031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2539ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor case Type::TemplateTypeParm: { 2540ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T); 2541ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor if (TTP->getDepth() == Depth) 2542ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Used[TTP->getIndex()] = true; 2543031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2544ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor } 2545031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 254631f17ecbef57b5679c017c375db330546b7b5145John McCall case Type::InjectedClassName: 254731f17ecbef57b5679c017c375db330546b7b5145John McCall T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType(); 254831f17ecbef57b5679c017c375db330546b7b5145John McCall // fall through 254931f17ecbef57b5679c017c375db330546b7b5145John McCall 2550031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::TemplateSpecialization: { 25511eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const TemplateSpecializationType *Spec 2552f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor = cast<TemplateSpecializationType>(T); 2553e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced, 2554ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, Used); 2555e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) 2556ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth, 2557ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Used); 2558e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor break; 2559e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor } 25601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2561e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor case Type::Complex: 2562e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor if (!OnlyDeduced) 2563e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, 2564e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor cast<ComplexType>(T)->getElementType(), 2565ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor OnlyDeduced, Depth, Used); 2566e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor break; 2567031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 25684714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor case Type::DependentName: 2569e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor if (!OnlyDeduced) 2570e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, 25714714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor cast<DependentNameType>(T)->getQualifier(), 2572ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor OnlyDeduced, Depth, Used); 2573031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2574031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2575ad5e73887052193afda72db8efcb812bd083a4a8John McCall case Type::TypeOf: 2576ad5e73887052193afda72db8efcb812bd083a4a8John McCall if (!OnlyDeduced) 2577ad5e73887052193afda72db8efcb812bd083a4a8John McCall MarkUsedTemplateParameters(SemaRef, 2578ad5e73887052193afda72db8efcb812bd083a4a8John McCall cast<TypeOfType>(T)->getUnderlyingType(), 2579ad5e73887052193afda72db8efcb812bd083a4a8John McCall OnlyDeduced, Depth, Used); 2580ad5e73887052193afda72db8efcb812bd083a4a8John McCall break; 2581ad5e73887052193afda72db8efcb812bd083a4a8John McCall 2582ad5e73887052193afda72db8efcb812bd083a4a8John McCall case Type::TypeOfExpr: 2583ad5e73887052193afda72db8efcb812bd083a4a8John McCall if (!OnlyDeduced) 2584ad5e73887052193afda72db8efcb812bd083a4a8John McCall MarkUsedTemplateParameters(SemaRef, 2585ad5e73887052193afda72db8efcb812bd083a4a8John McCall cast<TypeOfExprType>(T)->getUnderlyingExpr(), 2586ad5e73887052193afda72db8efcb812bd083a4a8John McCall OnlyDeduced, Depth, Used); 2587ad5e73887052193afda72db8efcb812bd083a4a8John McCall break; 2588ad5e73887052193afda72db8efcb812bd083a4a8John McCall 2589ad5e73887052193afda72db8efcb812bd083a4a8John McCall case Type::Decltype: 2590ad5e73887052193afda72db8efcb812bd083a4a8John McCall if (!OnlyDeduced) 2591ad5e73887052193afda72db8efcb812bd083a4a8John McCall MarkUsedTemplateParameters(SemaRef, 2592ad5e73887052193afda72db8efcb812bd083a4a8John McCall cast<DecltypeType>(T)->getUnderlyingExpr(), 2593ad5e73887052193afda72db8efcb812bd083a4a8John McCall OnlyDeduced, Depth, Used); 2594ad5e73887052193afda72db8efcb812bd083a4a8John McCall break; 2595ad5e73887052193afda72db8efcb812bd083a4a8John McCall 2596e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor // None of these types have any template parameters in them. 2597031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::Builtin: 2598031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::VariableArray: 2599031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::FunctionNoProto: 2600031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::Record: 2601031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::Enum: 2602031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::ObjCInterface: 2603d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff case Type::ObjCObjectPointer: 2604ed97649e9574b9d854fa4d6109c9333ae0993554John McCall case Type::UnresolvedUsing: 2605031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define TYPE(Class, Base) 2606031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define ABSTRACT_TYPE(Class, Base) 2607031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define DEPENDENT_TYPE(Class, Base) 2608031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 2609031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#include "clang/AST/TypeNodes.def" 2610031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2611031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor } 2612031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor} 2613031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2614e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by this 2615031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// template argument. 26161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void 2617e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, 2618e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor const TemplateArgument &TemplateArg, 2619e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor bool OnlyDeduced, 2620ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor unsigned Depth, 2621e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor llvm::SmallVectorImpl<bool> &Used) { 2622031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor switch (TemplateArg.getKind()) { 2623031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case TemplateArgument::Null: 2624031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case TemplateArgument::Integral: 2625788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor case TemplateArgument::Declaration: 2626031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 26271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2628031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case TemplateArgument::Type: 2629e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced, 2630ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, Used); 2631031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2632031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2633788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor case TemplateArgument::Template: 2634788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsTemplate(), 2635788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor OnlyDeduced, Depth, Used); 2636031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2637031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2638031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case TemplateArgument::Expression: 2639e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced, 2640ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, Used); 2641031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 2642e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor 2643d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson case TemplateArgument::Pack: 2644e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(), 2645e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor PEnd = TemplateArg.pack_end(); 2646e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor P != PEnd; ++P) 2647ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used); 2648d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson break; 2649031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor } 2650031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor} 2651031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 2652031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \brief Mark the template parameters can be deduced by the given 2653031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// template argument list. 2654031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// 2655031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \param TemplateArgs the template argument list from which template 2656031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// parameters will be deduced. 2657031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// 2658031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \param Deduced a bit vector whose elements will be set to \c true 2659031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// to indicate when the corresponding template parameter will be 2660031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// deduced. 26611eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid 2662e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorSema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, 2663ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor bool OnlyDeduced, unsigned Depth, 2664e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor llvm::SmallVectorImpl<bool> &Used) { 2665031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) 2666ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced, 2667ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, Used); 2668031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor} 266963f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor 267063f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor/// \brief Marks all of the template parameters that will be deduced by a 267163f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor/// call to the given function template. 267202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregorvoid 267302024a9f0d8e6c898de276193af604c42ee41269Douglas GregorSema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate, 267402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVectorImpl<bool> &Deduced) { 267563f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor TemplateParameterList *TemplateParams 267663f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor = FunctionTemplate->getTemplateParameters(); 267763f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor Deduced.clear(); 267863f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor Deduced.resize(TemplateParams->size()); 267963f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor 268063f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 268163f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) 268263f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(), 2683ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor true, TemplateParams->getDepth(), Deduced); 268463f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor} 2685