SemaTemplateDeduction.cpp revision 73b3cf6503f72f054288cf474e1a8c8ae56383c2
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 13e737f5041a36d0befb39ffeed8d50ba15916d3daDouglas Gregor#include "clang/Sema/Sema.h" 1419510856727e0e14a3696b2a72c35163bff2a71fJohn McCall#include "clang/Sema/DeclSpec.h" 1520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor#include "clang/Sema/SemaDiagnostic.h" // FIXME: temporary! 167cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall#include "clang/Sema/Template.h" 172a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall#include "clang/Sema/TemplateDeduction.h" 180b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/ASTContext.h" 197cd088e519d7e6caa4c4c12db52e0e4ae35d25c2John McCall#include "clang/AST/DeclObjC.h" 200b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/DeclTemplate.h" 210b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/StmtVisitor.h" 220b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/Expr.h" 230b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor#include "clang/AST/ExprCXX.h" 24e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor#include "llvm/ADT/BitVector.h" 258a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor#include <algorithm> 26508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor 27508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregornamespace clang { 282a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall using namespace sema; 292a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall 30508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// \brief Various flags that control template argument deduction. 31508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// 32508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// These flags can be bitwise-OR'd together. 33508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor enum TemplateDeductionFlags { 34508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// \brief No template argument deduction flags, which indicates the 35508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// strictest results for template argument deduction (as used for, e.g., 36508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// matching class template partial specializations). 37508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor TDF_None = 0, 38508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// \brief Within template argument deduction from a function call, we are 39508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// matching with a parameter type for which the original parameter was 40508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// a reference. 41508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor TDF_ParamWithReferenceType = 0x1, 42508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// \brief Within template argument deduction from a function call, we 43508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// are matching in a case where we ignore cv-qualifiers. 44508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor TDF_IgnoreQualifiers = 0x02, 45508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// \brief Within template argument deduction from a function call, 46508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor /// we are matching in a case where we can perform template argument 474112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor /// deduction from a template-id of a derived class of the argument type. 481282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor TDF_DerivedClass = 0x04, 491282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor /// \brief Allow non-dependent types to differ, e.g., when performing 501282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor /// template argument deduction from a function call where conversions 511282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor /// may apply. 5273b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor TDF_SkipNonDependent = 0x08, 5373b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor /// \brief Whether we are performing template argument deduction for 5473b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor /// parameters and arguments in a top-level template argument 5573b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor TDF_TopLevelParameterTypeList = 0x10 56508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor }; 57508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor} 58508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor 590b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregorusing namespace clang; 600b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 619d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor/// \brief Compare two APSInts, extending and switching the sign as 629d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor/// necessary to compare their values regardless of underlying type. 639d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregorstatic bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) { 649d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor if (Y.getBitWidth() > X.getBitWidth()) 659f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad X = X.extend(Y.getBitWidth()); 669d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor else if (Y.getBitWidth() < X.getBitWidth()) 679f71a8f4c7a182a5236da9e747d57cc1d1bd24c2Jay Foad Y = Y.extend(X.getBitWidth()); 689d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor 699d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor // If there is a signedness mismatch, correct it. 709d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor if (X.isSigned() != Y.isSigned()) { 719d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor // If the signed value is negative, then the values cannot be the same. 729d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative())) 739d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor return false; 749d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor 759d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor Y.setIsSigned(true); 769d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor X.setIsSigned(true); 779d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor } 789d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor 799d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor return X == Y; 809d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor} 819d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor 82f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult 83a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S, 84f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateParameterList *TemplateParams, 85f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor const TemplateArgument &Param, 8677d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor TemplateArgument Arg, 872a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall TemplateDeductionInfo &Info, 880972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced); 89d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor 90b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor/// \brief Whether template argument deduction for two reference parameters 91b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor/// resulted in the argument type, parameter type, or neither type being more 92b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor/// qualified than the other. 935c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregorenum DeductionQualifierComparison { 945c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor NeitherMoreQualified = 0, 955c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor ParamMoreQualified, 965c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor ArgMoreQualified 975c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor}; 985c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor 99b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor/// \brief Stores the result of comparing two reference parameters while 100b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor/// performing template argument deduction for partial ordering of function 101b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor/// templates. 102b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregorstruct RefParamPartialOrderingComparison { 103b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor /// \brief Whether the parameter type is an rvalue reference type. 104b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor bool ParamIsRvalueRef; 105b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor /// \brief Whether the argument type is an rvalue reference type. 106b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor bool ArgIsRvalueRef; 107b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor 108b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor /// \brief Whether the parameter or argument (or neither) is more qualified. 109b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor DeductionQualifierComparison Qualifiers; 110b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor}; 111b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor 112b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor 1135c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor 11420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregorstatic Sema::TemplateDeductionResult 11520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas GregorDeduceTemplateArguments(Sema &S, 11620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor TemplateParameterList *TemplateParams, 117603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor QualType Param, 118603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor QualType Arg, 119603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor TemplateDeductionInfo &Info, 120603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced, 1215c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor unsigned TDF, 1225c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor bool PartialOrdering = false, 123b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor llvm::SmallVectorImpl<RefParamPartialOrderingComparison> * 124b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor RefParamComparisons = 0); 125603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor 126603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregorstatic Sema::TemplateDeductionResult 127603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas GregorDeduceTemplateArguments(Sema &S, 128603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor TemplateParameterList *TemplateParams, 12920a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor const TemplateArgument *Params, unsigned NumParams, 13020a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor const TemplateArgument *Args, unsigned NumArgs, 13120a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor TemplateDeductionInfo &Info, 1320972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced, 1330972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor bool NumberOfArgumentsMustMatch = true); 13420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor 135199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// \brief If the given expression is of a form that permits the deduction 136199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// of a non-type template parameter, return the declaration of that 137199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// non-type template parameter. 138199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregorstatic NonTypeTemplateParmDecl *getDeducedParameterFromExpr(Expr *E) { 139199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E)) 140199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor E = IC->getSubExpr(); 1411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 142199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) 143199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor return dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); 1441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 145199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor return 0; 146199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor} 147199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor 1480d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor/// \brief Determine whether two declaration pointers refer to the same 1490d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor/// declaration. 1500d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregorstatic bool isSameDeclaration(Decl *X, Decl *Y) { 1510d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor if (!X || !Y) 1520d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return !X && !Y; 1530d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 1540d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor if (NamedDecl *NX = dyn_cast<NamedDecl>(X)) 1550d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor X = NX->getUnderlyingDecl(); 1560d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor if (NamedDecl *NY = dyn_cast<NamedDecl>(Y)) 1570d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Y = NY->getUnderlyingDecl(); 1580d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 1590d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return X->getCanonicalDecl() == Y->getCanonicalDecl(); 1600d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor} 1610d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 1620d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor/// \brief Verify that the given, deduced template arguments are compatible. 1630d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor/// 1640d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor/// \returns The deduced template argument, or a NULL template argument if 1650d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor/// the deduced template arguments were incompatible. 1660d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregorstatic DeducedTemplateArgument 1670d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas GregorcheckDeducedTemplateArguments(ASTContext &Context, 1680d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor const DeducedTemplateArgument &X, 1690d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor const DeducedTemplateArgument &Y) { 1700d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor // We have no deduction for one or both of the arguments; they're compatible. 1710d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor if (X.isNull()) 1720d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return Y; 1730d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor if (Y.isNull()) 1740d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return X; 1750d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 1760d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor switch (X.getKind()) { 1770d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor case TemplateArgument::Null: 1780d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor llvm_unreachable("Non-deduced template arguments handled above"); 1790d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 1800d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor case TemplateArgument::Type: 1810d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor // If two template type arguments have the same type, they're compatible. 1820d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor if (Y.getKind() == TemplateArgument::Type && 1830d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Context.hasSameType(X.getAsType(), Y.getAsType())) 1840d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return X; 1850d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 1860d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return DeducedTemplateArgument(); 1870d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 1880d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor case TemplateArgument::Integral: 1890d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor // If we deduced a constant in one case and either a dependent expression or 1900d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor // declaration in another case, keep the integral constant. 1910d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor // If both are integral constants with the same value, keep that value. 1920d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor if (Y.getKind() == TemplateArgument::Expression || 1930d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Y.getKind() == TemplateArgument::Declaration || 1940d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor (Y.getKind() == TemplateArgument::Integral && 1950d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor hasSameExtendedValue(*X.getAsIntegral(), *Y.getAsIntegral()))) 1960d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return DeducedTemplateArgument(X, 1970d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor X.wasDeducedFromArrayBound() && 1980d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Y.wasDeducedFromArrayBound()); 1990d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 2000d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor // All other combinations are incompatible. 2010d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return DeducedTemplateArgument(); 2020d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 2030d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor case TemplateArgument::Template: 2040d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor if (Y.getKind() == TemplateArgument::Template && 2050d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate())) 2060d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return X; 2070d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 2080d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor // All other combinations are incompatible. 2090d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return DeducedTemplateArgument(); 210a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor 211a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor case TemplateArgument::TemplateExpansion: 212a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor if (Y.getKind() == TemplateArgument::TemplateExpansion && 213a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(), 214a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor Y.getAsTemplateOrTemplatePattern())) 215a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor return X; 216a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor 217a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor // All other combinations are incompatible. 218a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor return DeducedTemplateArgument(); 219a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor 2200d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor case TemplateArgument::Expression: 2210d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor // If we deduced a dependent expression in one case and either an integral 2220d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor // constant or a declaration in another case, keep the integral constant 2230d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor // or declaration. 2240d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor if (Y.getKind() == TemplateArgument::Integral || 2250d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Y.getKind() == TemplateArgument::Declaration) 2260d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return DeducedTemplateArgument(Y, X.wasDeducedFromArrayBound() && 2270d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Y.wasDeducedFromArrayBound()); 2280d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 2290d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor if (Y.getKind() == TemplateArgument::Expression) { 2300d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor // Compare the expressions for equality 2310d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor llvm::FoldingSetNodeID ID1, ID2; 2320d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor X.getAsExpr()->Profile(ID1, Context, true); 2330d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Y.getAsExpr()->Profile(ID2, Context, true); 2340d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor if (ID1 == ID2) 2350d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return X; 2360d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor } 2370d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 2380d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor // All other combinations are incompatible. 2390d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return DeducedTemplateArgument(); 2400d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 2410d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor case TemplateArgument::Declaration: 2420d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor // If we deduced a declaration and a dependent expression, keep the 2430d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor // declaration. 2440d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor if (Y.getKind() == TemplateArgument::Expression) 2450d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return X; 2460d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 2470d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor // If we deduced a declaration and an integral constant, keep the 2480d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor // integral constant. 2490d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor if (Y.getKind() == TemplateArgument::Integral) 2500d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return Y; 2510d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 2520d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor // If we deduced two declarations, make sure they they refer to the 2530d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor // same declaration. 2540d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor if (Y.getKind() == TemplateArgument::Declaration && 2550d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor isSameDeclaration(X.getAsDecl(), Y.getAsDecl())) 2560d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return X; 2570d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 2580d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor // All other combinations are incompatible. 2590d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return DeducedTemplateArgument(); 2600d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 2610d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor case TemplateArgument::Pack: 2620d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor if (Y.getKind() != TemplateArgument::Pack || 2630d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor X.pack_size() != Y.pack_size()) 2640d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return DeducedTemplateArgument(); 2650d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 2660d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor for (TemplateArgument::pack_iterator XA = X.pack_begin(), 2670d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor XAEnd = X.pack_end(), 2680d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor YA = Y.pack_begin(); 2690d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor XA != XAEnd; ++XA, ++YA) { 270135ffa7375fb5802b92f42774e02d0e6e4c78e5bDouglas Gregor if (checkDeducedTemplateArguments(Context, 271135ffa7375fb5802b92f42774e02d0e6e4c78e5bDouglas Gregor DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()), 272135ffa7375fb5802b92f42774e02d0e6e4c78e5bDouglas Gregor DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound())) 273135ffa7375fb5802b92f42774e02d0e6e4c78e5bDouglas Gregor .isNull()) 2740d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return DeducedTemplateArgument(); 2750d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor } 2760d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 2770d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return X; 2780d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor } 2790d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 2800d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return DeducedTemplateArgument(); 2810d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor} 2820d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 2831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Deduce the value of the given non-type template parameter 284199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// from the given constant. 285f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult 286a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceNonTypeTemplateArgument(Sema &S, 2871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump NonTypeTemplateParmDecl *NTTP, 2889d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor llvm::APSInt Value, QualType ValueType, 28902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor bool DeducedFromArrayBound, 2902a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall TemplateDeductionInfo &Info, 29102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 2921eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(NTTP->getDepth() == 0 && 293199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor "Cannot deduce non-type template argument with depth > 0"); 2941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2950d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor DeducedTemplateArgument NewDeduced(Value, ValueType, DeducedFromArrayBound); 2960d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, 2970d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Deduced[NTTP->getIndex()], 2980d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor NewDeduced); 2990d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor if (Result.isNull()) { 300f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.Param = NTTP; 301f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Deduced[NTTP->getIndex()]; 3020d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Info.SecondArg = NewDeduced; 3030d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return Sema::TDK_Inconsistent; 304f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 3050d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 3060d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Deduced[NTTP->getIndex()] = Result; 307f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 308199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor} 309199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor 3101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Deduce the value of the given non-type template parameter 311199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// from the given type- or value-dependent expression. 312199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// 313199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor/// \returns true if deduction succeeded, false otherwise. 314f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult 315a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceNonTypeTemplateArgument(Sema &S, 316f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor NonTypeTemplateParmDecl *NTTP, 317f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Expr *Value, 3182a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall TemplateDeductionInfo &Info, 31902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 3201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(NTTP->getDepth() == 0 && 321199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor "Cannot deduce non-type template argument with depth > 0"); 322199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor assert((Value->isTypeDependent() || Value->isValueDependent()) && 323199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor "Expression template argument must be type- or value-dependent."); 3241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3250d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor DeducedTemplateArgument NewDeduced(Value); 3260d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, 3270d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Deduced[NTTP->getIndex()], 3280d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor NewDeduced); 3290d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 3300d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor if (Result.isNull()) { 3310d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Info.Param = NTTP; 3320d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Info.FirstArg = Deduced[NTTP->getIndex()]; 3330d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Info.SecondArg = NewDeduced; 3340d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return Sema::TDK_Inconsistent; 3359eea08ba72f8b1e7faf38e43376b9157fc4a2af2Douglas Gregor } 3360d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 3370d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Deduced[NTTP->getIndex()] = Result; 338f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 339199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor} 340199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor 34115755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor/// \brief Deduce the value of the given non-type template parameter 34215755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor/// from the given declaration. 34315755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor/// 34415755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor/// \returns true if deduction succeeded, false otherwise. 34515755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregorstatic Sema::TemplateDeductionResult 346a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceNonTypeTemplateArgument(Sema &S, 34715755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor NonTypeTemplateParmDecl *NTTP, 34815755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor Decl *D, 3492a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall TemplateDeductionInfo &Info, 35002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 35115755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor assert(NTTP->getDepth() == 0 && 35215755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor "Cannot deduce non-type template argument with depth > 0"); 35315755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor 3540d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor DeducedTemplateArgument NewDeduced(D? D->getCanonicalDecl() : 0); 3550d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, 3560d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Deduced[NTTP->getIndex()], 3570d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor NewDeduced); 3580d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor if (Result.isNull()) { 3590d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Info.Param = NTTP; 3600d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Info.FirstArg = Deduced[NTTP->getIndex()]; 3610d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Info.SecondArg = NewDeduced; 3620d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return Sema::TDK_Inconsistent; 36315755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor } 36415755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor 3650d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Deduced[NTTP->getIndex()] = Result; 36615755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor return Sema::TDK_Success; 36715755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor} 36815755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor 369f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult 370a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S, 371db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor TemplateParameterList *TemplateParams, 372f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateName Param, 373f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateName Arg, 3742a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall TemplateDeductionInfo &Info, 37502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 376d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); 377db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor if (!ParamDecl) { 378db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor // The parameter type is dependent and is not a template template parameter, 379db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor // so there is nothing that we can deduce. 380db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor return Sema::TDK_Success; 381f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 382db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor 383db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor if (TemplateTemplateParmDecl *TempParam 384db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) { 3850d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg)); 3860d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, 3870d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Deduced[TempParam->getIndex()], 3880d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor NewDeduced); 3890d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor if (Result.isNull()) { 3900d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Info.Param = TempParam; 3910d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Info.FirstArg = Deduced[TempParam->getIndex()]; 3920d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Info.SecondArg = NewDeduced; 3930d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return Sema::TDK_Inconsistent; 394db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor } 395db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor 3960d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Deduced[TempParam->getIndex()] = Result; 3970d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return Sema::TDK_Success; 398f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 399db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor 400db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor // Verify that the two template names are equivalent. 401a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth if (S.Context.hasSameTemplateName(Param, Arg)) 402db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor return Sema::TDK_Success; 403db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor 404db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor // Mismatch of non-dependent template parameter to argument. 405db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor Info.FirstArg = TemplateArgument(Param); 406db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor Info.SecondArg = TemplateArgument(Arg); 407db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor return Sema::TDK_NonDeducedMismatch; 408d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor} 409d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor 4101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Deduce the template arguments by comparing the template parameter 411de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// type (which is a template-id) with the template argument type. 412de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 413a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth/// \param S the Sema 414de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 415de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param TemplateParams the template parameters that we are deducing 416de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 417de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Param the parameter type 418de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 419de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Arg the argument type 420de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 421de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Info information about the template argument deduction itself 422de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 423de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \param Deduced the deduced template arguments 424de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// 425de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// \returns the result of template argument deduction so far. Note that a 426de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// "success" result means that template argument deduction has not yet failed, 427de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor/// but it may still fail, later, for other reasons. 428de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregorstatic Sema::TemplateDeductionResult 429a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S, 430de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor TemplateParameterList *TemplateParams, 431de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor const TemplateSpecializationType *Param, 432de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor QualType Arg, 4332a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall TemplateDeductionInfo &Info, 43402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 435467b27b9a24bdc823218ad1ad0e37673b6cc1e83John McCall assert(Arg.isCanonical() && "Argument type must be canonical"); 4361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 437de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Check whether the template argument is a dependent template-id. 4381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (const TemplateSpecializationType *SpecArg 439de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor = dyn_cast<TemplateSpecializationType>(Arg)) { 440de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Perform template argument deduction for the template name. 441de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (Sema::TemplateDeductionResult Result 442a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = DeduceTemplateArguments(S, TemplateParams, 443de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Param->getTemplateName(), 444de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor SpecArg->getTemplateName(), 445de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Info, Deduced)) 446de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Result; 4471eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 449de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Perform template argument deduction on each template 4500972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor // argument. Ignore any missing/extra arguments, since they could be 4510972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor // filled in by default arguments. 45220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor return DeduceTemplateArguments(S, TemplateParams, 4530972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor Param->getArgs(), Param->getNumArgs(), 4540972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor SpecArg->getArgs(), SpecArg->getNumArgs(), 4550972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor Info, Deduced, 4560972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor /*NumberOfArgumentsMustMatch=*/false); 457de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor } 4581eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 459de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // If the argument type is a class template specialization, we 460de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // perform template argument deduction using its template 461de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // arguments. 462de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor const RecordType *RecordArg = dyn_cast<RecordType>(Arg); 463de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (!RecordArg) 464de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Sema::TDK_NonDeducedMismatch; 4651eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 4661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump ClassTemplateSpecializationDecl *SpecArg 467de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); 468de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (!SpecArg) 469de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Sema::TDK_NonDeducedMismatch; 4701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 471de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Perform template argument deduction for the template name. 472de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (Sema::TemplateDeductionResult Result 473a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = DeduceTemplateArguments(S, 474db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor TemplateParams, 475de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Param->getTemplateName(), 476de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor TemplateName(SpecArg->getSpecializedTemplate()), 477de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Info, Deduced)) 478de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Result; 4791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 48020a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor // Perform template argument deduction for the template arguments. 48120a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor return DeduceTemplateArguments(S, TemplateParams, 48220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor Param->getArgs(), Param->getNumArgs(), 48320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor SpecArg->getTemplateArgs().data(), 48420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor SpecArg->getTemplateArgs().size(), 48520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor Info, Deduced); 486de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor} 487de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor 488cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall/// \brief Determines whether the given type is an opaque type that 489cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall/// might be more qualified when instantiated. 490cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCallstatic bool IsPossiblyOpaquelyQualifiedType(QualType T) { 491cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall switch (T->getTypeClass()) { 492cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall case Type::TypeOfExpr: 493cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall case Type::TypeOf: 494cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall case Type::DependentName: 495cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall case Type::Decltype: 496cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall case Type::UnresolvedUsing: 49762c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall case Type::TemplateTypeParm: 498cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall return true; 499cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall 500cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall case Type::ConstantArray: 501cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall case Type::IncompleteArray: 502cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall case Type::VariableArray: 503cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall case Type::DependentSizedArray: 504cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall return IsPossiblyOpaquelyQualifiedType( 505cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall cast<ArrayType>(T)->getElementType()); 506cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall 507cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall default: 508cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall return false; 509cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall } 510cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall} 511cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall 512d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor/// \brief Retrieve the depth and index of a template parameter. 513603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregorstatic std::pair<unsigned, unsigned> 514d3731198193eee92796ddeb493973b7a598b003eDouglas GregorgetDepthAndIndex(NamedDecl *ND) { 515603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(ND)) 516603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor return std::make_pair(TTP->getDepth(), TTP->getIndex()); 517603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor 518603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(ND)) 519603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor return std::make_pair(NTTP->getDepth(), NTTP->getIndex()); 520603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor 521603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor TemplateTemplateParmDecl *TTP = cast<TemplateTemplateParmDecl>(ND); 522603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor return std::make_pair(TTP->getDepth(), TTP->getIndex()); 523603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor} 524603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor 525d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor/// \brief Retrieve the depth and index of an unexpanded parameter pack. 526d3731198193eee92796ddeb493973b7a598b003eDouglas Gregorstatic std::pair<unsigned, unsigned> 527d3731198193eee92796ddeb493973b7a598b003eDouglas GregorgetDepthAndIndex(UnexpandedParameterPack UPP) { 528d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor if (const TemplateTypeParmType *TTP 529d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor = UPP.first.dyn_cast<const TemplateTypeParmType *>()) 530d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor return std::make_pair(TTP->getDepth(), TTP->getIndex()); 531d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor 532d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor return getDepthAndIndex(UPP.first.get<NamedDecl *>()); 533d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor} 534d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor 535603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \brief Helper function to build a TemplateParameter when we don't 536603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// know its type statically. 537603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregorstatic TemplateParameter makeTemplateParameter(Decl *D) { 538603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) 539603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor return TemplateParameter(TTP); 540603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor else if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) 541603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor return TemplateParameter(NTTP); 542603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor 543603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); 544603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor} 545603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor 5465429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor/// \brief Prepare to perform template argument deduction for all of the 5475429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor/// arguments in a set of argument packs. 5485429385919a2b6fb3708635b967221705f4b1dafDouglas Gregorstatic void PrepareArgumentPackDeduction(Sema &S, 5495429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced, 5505429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor const llvm::SmallVectorImpl<unsigned> &PackIndices, 5515429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &SavedPacks, 5525429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor llvm::SmallVectorImpl< 5535429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor llvm::SmallVector<DeducedTemplateArgument, 4> > &NewlyDeducedPacks) { 5545429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor // Save the deduced template arguments for each parameter pack expanded 5555429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor // by this pack expansion, then clear out the deduction. 5565429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) { 5575429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor // Save the previously-deduced argument pack, then clear it out so that we 5585429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor // can deduce a new argument pack. 5595429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor SavedPacks[I] = Deduced[PackIndices[I]]; 5605429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor Deduced[PackIndices[I]] = TemplateArgument(); 5615429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor 5625429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor // If the template arugment pack was explicitly specified, add that to 5635429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor // the set of deduced arguments. 5645429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor const TemplateArgument *ExplicitArgs; 5655429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor unsigned NumExplicitArgs; 5665429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor if (NamedDecl *PartiallySubstitutedPack 5675429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor = S.CurrentInstantiationScope->getPartiallySubstitutedPack( 5685429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor &ExplicitArgs, 5695429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor &NumExplicitArgs)) { 5705429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor if (getDepthAndIndex(PartiallySubstitutedPack).second == PackIndices[I]) 5715429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor NewlyDeducedPacks[I].append(ExplicitArgs, 5725429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor ExplicitArgs + NumExplicitArgs); 5735429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor } 5745429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor } 5755429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor} 5765429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor 5770216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor/// \brief Finish template argument deduction for a set of argument packs, 5780216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor/// producing the argument packs and checking for consistency with prior 5790216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor/// deductions. 5800216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregorstatic Sema::TemplateDeductionResult 5810216f8121df32b320cab659d5b703fee50cdfda5Douglas GregorFinishArgumentPackDeduction(Sema &S, 5820216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor TemplateParameterList *TemplateParams, 5830216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor bool HasAnyArguments, 5840216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced, 5850216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor const llvm::SmallVectorImpl<unsigned> &PackIndices, 5860216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &SavedPacks, 5870216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor llvm::SmallVectorImpl< 5880216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor llvm::SmallVector<DeducedTemplateArgument, 4> > &NewlyDeducedPacks, 5890216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor TemplateDeductionInfo &Info) { 5900216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor // Build argument packs for each of the parameter packs expanded by this 5910216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor // pack expansion. 5920216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) { 5930216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor if (HasAnyArguments && NewlyDeducedPacks[I].empty()) { 5940216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor // We were not able to deduce anything for this parameter pack, 5950216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor // so just restore the saved argument pack. 5960216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor Deduced[PackIndices[I]] = SavedPacks[I]; 5970216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor continue; 5980216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor } 5990216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor 6000216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor DeducedTemplateArgument NewPack; 6010216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor 6020216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor if (NewlyDeducedPacks[I].empty()) { 6030216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor // If we deduced an empty argument pack, create it now. 6040216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor NewPack = DeducedTemplateArgument(TemplateArgument(0, 0)); 6050216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor } else { 6060216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor TemplateArgument *ArgumentPack 607203e6a322ae29d577acafcb1572a57ec16e1e730Douglas Gregor = new (S.Context) TemplateArgument [NewlyDeducedPacks[I].size()]; 6080216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor std::copy(NewlyDeducedPacks[I].begin(), NewlyDeducedPacks[I].end(), 6090216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor ArgumentPack); 6100216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor NewPack 611203e6a322ae29d577acafcb1572a57ec16e1e730Douglas Gregor = DeducedTemplateArgument(TemplateArgument(ArgumentPack, 612203e6a322ae29d577acafcb1572a57ec16e1e730Douglas Gregor NewlyDeducedPacks[I].size()), 613203e6a322ae29d577acafcb1572a57ec16e1e730Douglas Gregor NewlyDeducedPacks[I][0].wasDeducedFromArrayBound()); 6140216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor } 6150216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor 6160216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor DeducedTemplateArgument Result 6170216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor = checkDeducedTemplateArguments(S.Context, SavedPacks[I], NewPack); 6180216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor if (Result.isNull()) { 6190216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor Info.Param 6200216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor = makeTemplateParameter(TemplateParams->getParam(PackIndices[I])); 6210216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor Info.FirstArg = SavedPacks[I]; 6220216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor Info.SecondArg = NewPack; 6230216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor return Sema::TDK_Inconsistent; 6240216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor } 6250216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor 6260216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor Deduced[PackIndices[I]] = Result; 6270216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor } 6280216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor 6290216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor return Sema::TDK_Success; 6300216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor} 6310216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor 632603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \brief Deduce the template arguments by comparing the list of parameter 633603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// types to the list of argument types, as in the parameter-type-lists of 634603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// function types (C++ [temp.deduct.type]p10). 635603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// 636603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param S The semantic analysis object within which we are deducing 637603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// 638603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param TemplateParams The template parameters that we are deducing 639603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// 640603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param Params The list of parameter types 641603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// 642603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param NumParams The number of types in \c Params 643603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// 644603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param Args The list of argument types 645603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// 646603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param NumArgs The number of types in \c Args 647603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// 648603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param Info information about the template argument deduction itself 649603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// 650603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param Deduced the deduced template arguments 651603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// 652603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe 653603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// how template argument deduction is performed. 654603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// 6555c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// \param PartialOrdering If true, we are performing template argument 6565c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// deduction for during partial ordering for a call 6575c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// (C++0x [temp.deduct.partial]). 6585c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// 659b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor/// \param RefParamComparisons If we're performing template argument deduction 6605c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// in the context of partial ordering, the set of qualifier comparisons. 6615c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// 662603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// \returns the result of template argument deduction so far. Note that a 663603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// "success" result means that template argument deduction has not yet failed, 664603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor/// but it may still fail, later, for other reasons. 665603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregorstatic Sema::TemplateDeductionResult 666603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas GregorDeduceTemplateArguments(Sema &S, 667603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor TemplateParameterList *TemplateParams, 668603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor const QualType *Params, unsigned NumParams, 669603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor const QualType *Args, unsigned NumArgs, 670603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor TemplateDeductionInfo &Info, 671603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced, 6725c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor unsigned TDF, 6735c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor bool PartialOrdering = false, 674b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor llvm::SmallVectorImpl<RefParamPartialOrderingComparison> * 675b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor RefParamComparisons = 0) { 6760bbacf8d2b3c7a5f19f292f3201e371836445502Douglas Gregor // Fast-path check to see if we have too many/too few arguments. 6770bbacf8d2b3c7a5f19f292f3201e371836445502Douglas Gregor if (NumParams != NumArgs && 6780bbacf8d2b3c7a5f19f292f3201e371836445502Douglas Gregor !(NumParams && isa<PackExpansionType>(Params[NumParams - 1])) && 6790bbacf8d2b3c7a5f19f292f3201e371836445502Douglas Gregor !(NumArgs && isa<PackExpansionType>(Args[NumArgs - 1]))) 6803cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor return Sema::TDK_NonDeducedMismatch; 681603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor 682603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor // C++0x [temp.deduct.type]p10: 683603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor // Similarly, if P has a form that contains (T), then each parameter type 684603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor // Pi of the respective parameter-type- list of P is compared with the 685603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor // corresponding parameter type Ai of the corresponding parameter-type-list 686603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor // of A. [...] 687603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor unsigned ArgIdx = 0, ParamIdx = 0; 688603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor for (; ParamIdx != NumParams; ++ParamIdx) { 689603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor // Check argument types. 690603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor const PackExpansionType *Expansion 691603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor = dyn_cast<PackExpansionType>(Params[ParamIdx]); 692603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor if (!Expansion) { 693603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor // Simple case: compare the parameter and argument types at this point. 694603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor 695603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor // Make sure we have an argument. 696603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor if (ArgIdx >= NumArgs) 6973cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor return Sema::TDK_NonDeducedMismatch; 698603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor 69977d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor if (isa<PackExpansionType>(Args[ArgIdx])) { 70077d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor // C++0x [temp.deduct.type]p22: 70177d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor // If the original function parameter associated with A is a function 70277d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor // parameter pack and the function parameter associated with P is not 70377d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor // a function parameter pack, then template argument deduction fails. 70477d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor return Sema::TDK_NonDeducedMismatch; 70577d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor } 7065c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor 707603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor if (Sema::TemplateDeductionResult Result 7085c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor = DeduceTemplateArguments(S, TemplateParams, 7095c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor Params[ParamIdx], 7105c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor Args[ArgIdx], 7115c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor Info, Deduced, TDF, 7125c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor PartialOrdering, 713b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor RefParamComparisons)) 714603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor return Result; 715603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor 716603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor ++ArgIdx; 717603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor continue; 718603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor } 719603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor 7207d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor // C++0x [temp.deduct.type]p5: 7217d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor // The non-deduced contexts are: 7227d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor // - A function parameter pack that does not occur at the end of the 7237d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor // parameter-declaration-clause. 7247d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor if (ParamIdx + 1 < NumParams) 7257d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor return Sema::TDK_Success; 7267d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor 727603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor // C++0x [temp.deduct.type]p10: 728603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor // If the parameter-declaration corresponding to Pi is a function 729603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor // parameter pack, then the type of its declarator- id is compared with 730603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor // each remaining parameter type in the parameter-type-list of A. Each 731603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor // comparison deduces template arguments for subsequent positions in the 732603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor // template parameter packs expanded by the function parameter pack. 733603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor 734603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor // Compute the set of template parameter indices that correspond to 735603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor // parameter packs expanded by the pack expansion. 736603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor llvm::SmallVector<unsigned, 2> PackIndices; 737603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor QualType Pattern = Expansion->getPattern(); 738603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor { 739603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor llvm::BitVector SawIndices(TemplateParams->size()); 740603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded; 741603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor S.collectUnexpandedParameterPacks(Pattern, Unexpanded); 742603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { 743603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor unsigned Depth, Index; 744603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); 745603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor if (Depth == 0 && !SawIndices[Index]) { 746603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor SawIndices[Index] = true; 747603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor PackIndices.push_back(Index); 748603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor } 749603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor } 750603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor } 751603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?"); 752603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor 753d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor // Keep track of the deduced template arguments for each parameter pack 754d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor // expanded by this pack expansion (the outer index) and for each 755d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor // template argument (the inner SmallVectors). 756d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2> 757d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor NewlyDeducedPacks(PackIndices.size()); 758603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor llvm::SmallVector<DeducedTemplateArgument, 2> 7595429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor SavedPacks(PackIndices.size()); 7605429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor PrepareArgumentPackDeduction(S, Deduced, PackIndices, SavedPacks, 7615429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor NewlyDeducedPacks); 762603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor 763603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor bool HasAnyArguments = false; 764603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor for (; ArgIdx < NumArgs; ++ArgIdx) { 765603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor HasAnyArguments = true; 766603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor 767603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor // Deduce template arguments from the pattern. 768603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor if (Sema::TemplateDeductionResult Result 76973b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx], 77073b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor Info, Deduced, TDF, PartialOrdering, 77173b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor RefParamComparisons)) 772603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor return Result; 773603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor 774603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor // Capture the deduced template arguments for each parameter pack expanded 775603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor // by this pack expansion, add them to the list of arguments we've deduced 776603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor // for that pack, then clear out the deduced argument. 777603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) { 778603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]]; 779603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor if (!DeducedArg.isNull()) { 780603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor NewlyDeducedPacks[I].push_back(DeducedArg); 781603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor DeducedArg = DeducedTemplateArgument(); 782603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor } 783603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor } 784603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor } 785603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor 786603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor // Build argument packs for each of the parameter packs expanded by this 787603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor // pack expansion. 7880216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor if (Sema::TemplateDeductionResult Result 7890216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor = FinishArgumentPackDeduction(S, TemplateParams, HasAnyArguments, 7900216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor Deduced, PackIndices, SavedPacks, 7910216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor NewlyDeducedPacks, Info)) 7920216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor return Result; 793603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor } 794603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor 795603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor // Make sure we don't have any extra arguments. 796603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor if (ArgIdx < NumArgs) 7973cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor return Sema::TDK_NonDeducedMismatch; 798603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor 799603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor return Sema::TDK_Success; 800603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor} 801603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor 802500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \brief Deduce the template arguments by comparing the parameter type and 803500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// the argument type (C++ [temp.deduct.type]). 804500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 805a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth/// \param S the semantic analysis object within which we are deducing 806500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 807500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param TemplateParams the template parameters that we are deducing 808500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 809500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param ParamIn the parameter type 810500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 811500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param ArgIn the argument type 812500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 813500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param Info information about the template argument deduction itself 814500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 815500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \param Deduced the deduced template arguments 816500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 817508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor/// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe 8181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// how template argument deduction is performed. 819500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// 8205c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// \param PartialOrdering Whether we're performing template argument deduction 8215c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// in the context of partial ordering (C++0x [temp.deduct.partial]). 8225c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// 823b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor/// \param RefParamComparisons If we're performing template argument deduction 8245c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// in the context of partial ordering, the set of qualifier comparisons. 8255c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// 826500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// \returns the result of template argument deduction so far. Note that a 827500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// "success" result means that template argument deduction has not yet failed, 828500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor/// but it may still fail, later, for other reasons. 829f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult 830a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S, 831f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateParameterList *TemplateParams, 832f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor QualType ParamIn, QualType ArgIn, 8332a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall TemplateDeductionInfo &Info, 83402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced, 8355c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor unsigned TDF, 8365c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor bool PartialOrdering, 837b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *RefParamComparisons) { 8380b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // We only want to look at the canonical types, since typedefs and 8390b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // sugar are not part of template argument deduction. 840a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth QualType Param = S.Context.getCanonicalType(ParamIn); 841a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth QualType Arg = S.Context.getCanonicalType(ArgIn); 8420b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 84377d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor // If the argument type is a pack expansion, look at its pattern. 84477d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor // This isn't explicitly called out 84577d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor if (const PackExpansionType *ArgExpansion 84677d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor = dyn_cast<PackExpansionType>(Arg)) 84777d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor Arg = ArgExpansion->getPattern(); 84877d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor 8495c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor if (PartialOrdering) { 8505c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // C++0x [temp.deduct.partial]p5: 8515c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // Before the partial ordering is done, certain transformations are 8525c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // performed on the types used for partial ordering: 8535c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // - If P is a reference type, P is replaced by the type referred to. 8545c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor const ReferenceType *ParamRef = Param->getAs<ReferenceType>(); 8555c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor if (ParamRef) 8565c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor Param = ParamRef->getPointeeType(); 8575c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor 8585c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // - If A is a reference type, A is replaced by the type referred to. 8595c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor const ReferenceType *ArgRef = Arg->getAs<ReferenceType>(); 8605c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor if (ArgRef) 8615c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor Arg = ArgRef->getPointeeType(); 8625c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor 863b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor if (RefParamComparisons && ParamRef && ArgRef) { 8645c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // C++0x [temp.deduct.partial]p6: 8655c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // If both P and A were reference types (before being replaced with the 8665c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // type referred to above), determine which of the two types (if any) is 8675c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // more cv-qualified than the other; otherwise the types are considered 8685c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // to be equally cv-qualified for partial ordering purposes. The result 8695c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // of this determination will be used below. 8705c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // 8715c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // We save this information for later, using it only when deduction 8725c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // succeeds in both directions. 873b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor RefParamPartialOrderingComparison Comparison; 874b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor Comparison.ParamIsRvalueRef = ParamRef->getAs<RValueReferenceType>(); 875b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor Comparison.ArgIsRvalueRef = ArgRef->getAs<RValueReferenceType>(); 876b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor Comparison.Qualifiers = NeitherMoreQualified; 8775c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor if (Param.isMoreQualifiedThan(Arg)) 878b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor Comparison.Qualifiers = ParamMoreQualified; 8795c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor else if (Arg.isMoreQualifiedThan(Param)) 880b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor Comparison.Qualifiers = ArgMoreQualified; 881b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor RefParamComparisons->push_back(Comparison); 8825c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor } 8835c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor 8845c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // C++0x [temp.deduct.partial]p7: 8855c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // Remove any top-level cv-qualifiers: 8865c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // - If P is a cv-qualified type, P is replaced by the cv-unqualified 8875c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // version of P. 8885c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor Param = Param.getUnqualifiedType(); 8895c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // - If A is a cv-qualified type, A is replaced by the cv-unqualified 8905c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // version of A. 8915c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor Arg = Arg.getUnqualifiedType(); 8925c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor } else { 8935c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // C++0x [temp.deduct.call]p4 bullet 1: 8945c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // - If the original P is a reference type, the deduced A (i.e., the type 8955c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // referred to by the reference) can be more cv-qualified than the 8965c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // transformed A. 8975c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor if (TDF & TDF_ParamWithReferenceType) { 8985c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor Qualifiers Quals; 8995c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals); 9005c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor Quals.setCVRQualifiers(Quals.getCVRQualifiers() & 90162c28c831bbf207cc36e683e7c321fc33bf8928cJohn McCall Arg.getCVRQualifiers()); 9025c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor Param = S.Context.getQualifiedType(UnqualParam, Quals); 9035c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor } 90473b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor 90573b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) { 90673b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor // C++0x [temp.deduct.type]p10: 90773b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor // If P and A are function types that originated from deduction when 90873b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor // taking the address of a function template (14.8.2.2) or when deducing 90973b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor // template arguments from a function declaration (14.8.2.6) and Pi and 91073b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor // Ai are parameters of the top-level parameter-type-list of P and A, 91173b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor // respectively, Pi is adjusted if it is an rvalue reference to a 91273b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor // cv-unqualified template parameter and Ai is an lvalue reference, in 91373b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor // which case the type of Pi is changed to be the template parameter 91473b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor // type (i.e., T&& is changed to simply T). [ Note: As a result, when 91573b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor // Pi is T&& and Ai is X&, the adjusted Pi will be T, causing T to be 91673b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor // deduced as X&. — end note ] 91773b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor TDF &= ~TDF_TopLevelParameterTypeList; 91873b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor 91973b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor if (const RValueReferenceType *ParamRef 92073b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor = Param->getAs<RValueReferenceType>()) { 92173b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor if (isa<TemplateTypeParmType>(ParamRef->getPointeeType()) && 92273b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor !ParamRef->getPointeeType().getQualifiers()) 92373b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor if (Arg->isLValueReferenceType()) 92473b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor Param = ParamRef->getPointeeType(); 92573b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor } 92673b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor } 927500d331eade2f5070b66ba51d777224f9fda6e1dDouglas Gregor } 9285c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor 929f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor // If the parameter type is not dependent, there is nothing to deduce. 9301282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor if (!Param->isDependentType()) { 9313cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor if (!(TDF & TDF_SkipNonDependent) && Param != Arg) 9321282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor return Sema::TDK_NonDeducedMismatch; 9331282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor 934f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return Sema::TDK_Success; 9351282029f3d37f482bbba3c38ea9da17a78d11d40Douglas Gregor } 9360b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 937199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // C++ [temp.deduct.type]p9: 9381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // A template type argument T, a template template argument TT or a 9391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // template non-type argument i can be deduced if P and A have one of 940199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // the following forms: 941199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // 942199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // T 943199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // cv-list T 9441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (const TemplateTypeParmType *TemplateTypeParm 945183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall = Param->getAs<TemplateTypeParmType>()) { 946f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor unsigned Index = TemplateTypeParm->getIndex(); 947f290e0db835a619014538c41ed552696efc0e977Douglas Gregor bool RecanonicalizeArg = false; 9481eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9499e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor // If the argument type is an array type, move the qualifiers up to the 9509e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor // top level, so they can be matched with the qualifiers on the parameter. 9519e9fae4b8af47c15696da4ea3c30102e3a035179Douglas Gregor // FIXME: address spaces, ObjC GC qualifiers 952f290e0db835a619014538c41ed552696efc0e977Douglas Gregor if (isa<ArrayType>(Arg)) { 9530953e767ff7817f97b3ab20896b229891eeff45bJohn McCall Qualifiers Quals; 954a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth Arg = S.Context.getUnqualifiedArrayType(Arg, Quals); 9550953e767ff7817f97b3ab20896b229891eeff45bJohn McCall if (Quals) { 956a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth Arg = S.Context.getQualifiedType(Arg, Quals); 957f290e0db835a619014538c41ed552696efc0e977Douglas Gregor RecanonicalizeArg = true; 958f290e0db835a619014538c41ed552696efc0e977Douglas Gregor } 959f290e0db835a619014538c41ed552696efc0e977Douglas Gregor } 9601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9610b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // The argument type can not be less qualified than the parameter 9620b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // type. 963508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (Param.isMoreQualifiedThan(Arg) && !(TDF & TDF_IgnoreQualifiers)) { 964f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); 96557e97786433e70197a089360228d8f0d82e3ad4cJohn McCall Info.FirstArg = TemplateArgument(Param); 966833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall Info.SecondArg = TemplateArgument(Arg); 96757e97786433e70197a089360228d8f0d82e3ad4cJohn McCall return Sema::TDK_Underqualified; 968f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 9690b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 9700b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor assert(TemplateTypeParm->getDepth() == 0 && "Can't deduce with depth > 0"); 971a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function"); 9720953e767ff7817f97b3ab20896b229891eeff45bJohn McCall QualType DeducedType = Arg; 97349f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall 97449f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall // local manipulation is okay because it's canonical 97549f4e1cbd839da27ff4814b4ea6d85a79f786cbdJohn McCall DeducedType.removeLocalCVRQualifiers(Param.getCVRQualifiers()); 976f290e0db835a619014538c41ed552696efc0e977Douglas Gregor if (RecanonicalizeArg) 977a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth DeducedType = S.Context.getCanonicalType(DeducedType); 9781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 9790d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor DeducedTemplateArgument NewDeduced(DeducedType); 9800d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, 9810d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Deduced[Index], 9820d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor NewDeduced); 9830d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor if (Result.isNull()) { 9840d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); 9850d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Info.FirstArg = Deduced[Index]; 9860d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Info.SecondArg = NewDeduced; 9870d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return Sema::TDK_Inconsistent; 9880b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor } 9890d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor 9900d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor Deduced[Index] = Result; 9910d80abc3b7fb0dca26fb6b272d2c3484f86fb7e7Douglas Gregor return Sema::TDK_Success; 9920b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor } 9930b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 994f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor // Set up the template argument deduction information for a failure. 995833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall Info.FirstArg = TemplateArgument(ParamIn); 996833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall Info.SecondArg = TemplateArgument(ArgIn); 997f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 9980bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor // If the parameter is an already-substituted template parameter 9990bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor // pack, do nothing: we don't know which of its arguments to look 10000bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor // at, so we have to wait until all of the parameter packs in this 10010bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor // expansion have arguments. 10020bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor if (isa<SubstTemplateTypeParmPackType>(Param)) 10030bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor return Sema::TDK_Success; 10040bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor 1005508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor // Check the cv-qualifiers on the parameter and argument types. 1006508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (!(TDF & TDF_IgnoreQualifiers)) { 1007508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (TDF & TDF_ParamWithReferenceType) { 1008508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (Param.isMoreQualifiedThan(Arg)) 1009508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor return Sema::TDK_NonDeducedMismatch; 1010cd05e81e2e2640a0a0097658c660afc6c8a9f4fdJohn McCall } else if (!IsPossiblyOpaquelyQualifiedType(Param)) { 1011508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) 10121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return Sema::TDK_NonDeducedMismatch; 1013508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor } 1014508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor } 10150b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 1016d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor switch (Param->getTypeClass()) { 1017199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // No deduction possible for these types 1018199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor case Type::Builtin: 1019f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 10201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1021199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // T * 1022d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor case Type::Pointer: { 1023c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall QualType PointeeType; 1024c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall if (const PointerType *PointerArg = Arg->getAs<PointerType>()) { 1025c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall PointeeType = PointerArg->getPointeeType(); 1026c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall } else if (const ObjCObjectPointerType *PointerArg 1027c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall = Arg->getAs<ObjCObjectPointerType>()) { 1028c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall PointeeType = PointerArg->getPointeeType(); 1029c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall } else { 1030f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 1031c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall } 10321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10334112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); 1034a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceTemplateArguments(S, TemplateParams, 1035d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor cast<PointerType>(Param)->getPointeeType(), 1036c0008349f233e70e3ffabe7b31eab5d9859bc25fJohn McCall PointeeType, 10374112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor Info, Deduced, SubTDF); 1038d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor } 10391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1040199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // T & 1041d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor case Type::LValueReference: { 10426217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const LValueReferenceType *ReferenceArg = Arg->getAs<LValueReferenceType>(); 1043d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor if (!ReferenceArg) 1044f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 10451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1046a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceTemplateArguments(S, TemplateParams, 1047d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor cast<LValueReferenceType>(Param)->getPointeeType(), 1048d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor ReferenceArg->getPointeeType(), 1049508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0); 1050d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor } 10510b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 1052199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // T && [C++0x] 1053d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor case Type::RValueReference: { 10546217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek const RValueReferenceType *ReferenceArg = Arg->getAs<RValueReferenceType>(); 1055d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor if (!ReferenceArg) 1056f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 10571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1058a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceTemplateArguments(S, TemplateParams, 1059d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor cast<RValueReferenceType>(Param)->getPointeeType(), 1060d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor ReferenceArg->getPointeeType(), 1061508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0); 1062d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor } 10631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1064199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // T [] (implied, but not stated explicitly) 10654d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson case Type::IncompleteArray: { 10661eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const IncompleteArrayType *IncompleteArrayArg = 1067a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth S.Context.getAsIncompleteArrayType(Arg); 10684d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson if (!IncompleteArrayArg) 1069f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 10701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1071e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall unsigned SubTDF = TDF & TDF_IgnoreQualifiers; 1072a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceTemplateArguments(S, TemplateParams, 1073a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth S.Context.getAsIncompleteArrayType(Param)->getElementType(), 10744d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson IncompleteArrayArg->getElementType(), 1075e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall Info, Deduced, SubTDF); 10764d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson } 1077199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor 1078199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // T [integer-constant] 10794d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson case Type::ConstantArray: { 10801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const ConstantArrayType *ConstantArrayArg = 1081a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth S.Context.getAsConstantArrayType(Arg); 10824d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson if (!ConstantArrayArg) 1083f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 10841eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 10851eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const ConstantArrayType *ConstantArrayParm = 1086a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth S.Context.getAsConstantArrayType(Param); 10874d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) 1088f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 10891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1090e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall unsigned SubTDF = TDF & TDF_IgnoreQualifiers; 1091a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceTemplateArguments(S, TemplateParams, 10924d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson ConstantArrayParm->getElementType(), 10934d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson ConstantArrayArg->getElementType(), 1094e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall Info, Deduced, SubTDF); 10954d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson } 10964d6fb501ffc0568ca5ca7266005e96a6f1273845Anders Carlsson 1097199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // type [i] 1098199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor case Type::DependentSizedArray: { 1099a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg); 1100199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (!ArrayArg) 1101f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 11021eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1103e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall unsigned SubTDF = TDF & TDF_IgnoreQualifiers; 1104e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall 1105199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // Check the element type of the arrays 1106199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor const DependentSizedArrayType *DependentArrayParm 1107a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = S.Context.getAsDependentSizedArrayType(Param); 1108f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Sema::TemplateDeductionResult Result 1109a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = DeduceTemplateArguments(S, TemplateParams, 1110f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor DependentArrayParm->getElementType(), 1111f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor ArrayArg->getElementType(), 1112e4f26e54d81fb1987333132fe34cd927e62c803cJohn McCall Info, Deduced, SubTDF)) 1113f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Result; 11141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1115199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // Determine the array bound is something we can deduce. 11161eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump NonTypeTemplateParmDecl *NTTP 1117199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor = getDeducedParameterFromExpr(DependentArrayParm->getSizeExpr()); 1118199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (!NTTP) 1119f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 11201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11211eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // We can perform template argument deduction for the given non-type 1122199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // template parameter. 11231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(NTTP->getDepth() == 0 && 1124199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor "Cannot deduce non-type template argument at depth > 0"); 11251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (const ConstantArrayType *ConstantArrayArg 1126335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson = dyn_cast<ConstantArrayType>(ArrayArg)) { 1127335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson llvm::APSInt Size(ConstantArrayArg->getSize()); 11289d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor return DeduceNonTypeTemplateArgument(S, NTTP, Size, 11299d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor S.Context.getSizeType(), 113002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor /*ArrayBound=*/true, 1131f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info, Deduced); 1132335e24a194f2000086d298b800d6169ebc5a7ee6Anders Carlsson } 1133199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (const DependentSizedArrayType *DependentArrayArg 1134199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor = dyn_cast<DependentSizedArrayType>(ArrayArg)) 113534c2f8c8a16226f757947bf08c5f799d99c9ac1eDouglas Gregor if (DependentArrayArg->getSizeExpr()) 113634c2f8c8a16226f757947bf08c5f799d99c9ac1eDouglas Gregor return DeduceNonTypeTemplateArgument(S, NTTP, 113734c2f8c8a16226f757947bf08c5f799d99c9ac1eDouglas Gregor DependentArrayArg->getSizeExpr(), 113834c2f8c8a16226f757947bf08c5f799d99c9ac1eDouglas Gregor Info, Deduced); 11391eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1140199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // Incomplete type does not match a dependently-sized array type 1141f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 1142199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor } 11431eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11441eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // type(*)(T) 11451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // T(*)() 11461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // T(*)(T) 1147a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson case Type::FunctionProto: { 114873b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList; 11491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const FunctionProtoType *FunctionProtoArg = 1150a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson dyn_cast<FunctionProtoType>(Arg); 1151a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson if (!FunctionProtoArg) 1152f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 11531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const FunctionProtoType *FunctionProtoParam = 1155a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson cast<FunctionProtoType>(Param); 1156994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson 11571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (FunctionProtoParam->getTypeQuals() != 1158994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson FunctionProtoArg->getTypeQuals()) 1159f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 11601eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1161994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson if (FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) 1162f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 1163994b6cb65c9daba2128366bc4c64be6dbf953528Anders Carlsson 1164a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson // Check return types. 1165f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Sema::TemplateDeductionResult Result 1166a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = DeduceTemplateArguments(S, TemplateParams, 1167f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor FunctionProtoParam->getResultType(), 1168f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor FunctionProtoArg->getResultType(), 1169508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0)) 1170f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Result; 11711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1172603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor return DeduceTemplateArguments(S, TemplateParams, 1173603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor FunctionProtoParam->arg_type_begin(), 1174603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor FunctionProtoParam->getNumArgs(), 1175603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor FunctionProtoArg->arg_type_begin(), 1176603cfb4da2f7ba08a1c3452c2fbf70585b8e7621Douglas Gregor FunctionProtoArg->getNumArgs(), 117773b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor Info, Deduced, SubTDF); 1178a27fad59dc760252af025ef6a86d31bb7d11a44aAnders Carlsson } 11791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 11803cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall case Type::InjectedClassName: { 11813cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall // Treat a template's injected-class-name as if the template 11823cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall // specialization type had been used. 118331f17ecbef57b5679c017c375db330546b7b5145John McCall Param = cast<InjectedClassNameType>(Param) 118431f17ecbef57b5679c017c375db330546b7b5145John McCall ->getInjectedSpecializationType(); 11853cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall assert(isa<TemplateSpecializationType>(Param) && 11863cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall "injected class name is not a template specialization type"); 11873cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall // fall through 11883cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall } 11893cb0ebd5f76abcb776f7cb4062bd79e3268c0dc4John McCall 1190f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor // template-name<T> (where template-name refers to a class template) 1191d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor // template-name<i> 1192db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor // TT<T> 1193db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor // TT<i> 1194db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor // TT<> 1195d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor case Type::TemplateSpecialization: { 1196d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor const TemplateSpecializationType *SpecParam 1197d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor = cast<TemplateSpecializationType>(Param); 11981eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1199de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Try to deduce template arguments from the template-id. 1200de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Sema::TemplateDeductionResult Result 1201a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg, 1202de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Info, Deduced); 12031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 12044a5c15f75f76b95e1c2ceb6fa2737dcadd5f4be1Douglas Gregor if (Result && (TDF & TDF_DerivedClass)) { 1205de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // C++ [temp.deduct.call]p3b3: 1206de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // If P is a class, and P has the form template-id, then A can be a 1207de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // derived class of the deduced A. Likewise, if P is a pointer to a 12081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // class of the form template-id, A can be a pointer to a derived 1209de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // class pointed to by the deduced A. 1210de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // 1211de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // More importantly: 12121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // These alternatives are considered only if type deduction would 1213de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // otherwise fail. 1214a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth if (const RecordType *RecordT = Arg->getAs<RecordType>()) { 1215a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth // We cannot inspect base classes as part of deduction when the type 1216a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth // is incomplete, so either instantiate any templates necessary to 1217a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth // complete the type, or skip over it if it cannot be completed. 12185769d6195087229770d7ac90449443e026c47103John McCall if (S.RequireCompleteType(Info.getLocation(), Arg, 0)) 1219a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return Result; 1220a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth 1221de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Use data recursion to crawl through the list of base classes. 12221eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Visited contains the set of nodes we have already visited, while 1223de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // ToVisit is our stack of records that we still need to visit. 1224de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor llvm::SmallPtrSet<const RecordType *, 8> Visited; 1225de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor llvm::SmallVector<const RecordType *, 8> ToVisit; 1226de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor ToVisit.push_back(RecordT); 1227de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor bool Successful = false; 1228053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> DeducedOrig(0); 1229053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor DeducedOrig = Deduced; 1230de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor while (!ToVisit.empty()) { 1231de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Retrieve the next class in the inheritance hierarchy. 1232de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor const RecordType *NextT = ToVisit.back(); 1233de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor ToVisit.pop_back(); 12341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1235de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // If we have already seen this type, skip it. 1236de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (!Visited.insert(NextT)) 1237de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor continue; 12381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1239de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // If this is a base class, try to perform template argument 1240de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // deduction from it. 1241de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (NextT != RecordT) { 1242de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Sema::TemplateDeductionResult BaseResult 1243a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = DeduceTemplateArguments(S, TemplateParams, SpecParam, 1244de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor QualType(NextT, 0), Info, Deduced); 12451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1246de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // If template argument deduction for this base was successful, 1247053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor // note that we had some success. Otherwise, ignore any deductions 1248053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor // from this base class. 1249053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor if (BaseResult == Sema::TDK_Success) { 1250de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor Successful = true; 1251053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor DeducedOrig = Deduced; 1252053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor } 1253053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor else 1254053105d58552c600a2e56473592212a9bddafcd4Douglas Gregor Deduced = DeducedOrig; 1255de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor } 12561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1257de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor // Visit base classes 1258de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl()); 1259de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor for (CXXRecordDecl::base_class_iterator Base = Next->bases_begin(), 1260de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor BaseEnd = Next->bases_end(); 12619994a34f6cf842721ba7723edc0b9036229fe387Sebastian Redl Base != BaseEnd; ++Base) { 12621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump assert(Base->getType()->isRecordType() && 1263de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor "Base class that isn't a record?"); 12646217b80b7a1379b74cced1c076338262c3c980b3Ted Kremenek ToVisit.push_back(Base->getType()->getAs<RecordType>()); 1265de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor } 1266de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor } 12671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1268de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor if (Successful) 1269de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Sema::TDK_Success; 1270de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor } 12711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1272de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor } 12731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1274de0cb8b6c15c756e14b0716bebd40f4ce48ee717Douglas Gregor return Result; 1275d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor } 1276d708c72e3e5186662fb0045d2bd666bfd93a013dDouglas Gregor 1277637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // T type::* 1278637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // T T::* 1279637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // T (type::*)() 1280637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // type (T::*)() 1281637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // type (type::*)(T) 1282637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // type (T::*)(T) 1283637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // T (type::*)(T) 1284637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // T (T::*)() 1285637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // T (T::*)(T) 1286637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor case Type::MemberPointer: { 1287637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); 1288637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); 1289637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor if (!MemPtrArg) 1290f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 1291f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 1292f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Sema::TemplateDeductionResult Result 1293a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = DeduceTemplateArguments(S, TemplateParams, 1294f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor MemPtrParam->getPointeeType(), 1295f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor MemPtrArg->getPointeeType(), 1296508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 1297508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor TDF & TDF_IgnoreQualifiers)) 1298f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Result; 1299f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 1300a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceTemplateArguments(S, TemplateParams, 1301f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor QualType(MemPtrParam->getClass(), 0), 1302f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor QualType(MemPtrArg->getClass(), 0), 1303508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Info, Deduced, 0); 1304637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor } 1305637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor 13069a917e4fac79aba20fbd25983c78396475078918Anders Carlsson // (clang extension) 13079a917e4fac79aba20fbd25983c78396475078918Anders Carlsson // 13081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // type(^)(T) 13091eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // T(^)() 13101eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // T(^)(T) 1311859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson case Type::BlockPointer: { 1312859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); 1313859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); 13141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1315859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson if (!BlockPtrArg) 1316f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 13171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1318a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceTemplateArguments(S, TemplateParams, 1319859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson BlockPtrParam->getPointeeType(), 1320f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor BlockPtrArg->getPointeeType(), Info, 1321508f1c889b9833903ea394034fe0246d3a57a32dDouglas Gregor Deduced, 0); 1322859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson } 1323859ba504e754436e1ccf81f50800e5d2ea647447Anders Carlsson 1324637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor case Type::TypeOfExpr: 1325637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor case Type::TypeOf: 13264714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor case Type::DependentName: 1327637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor // No template argument deduction for these types 1328f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 1329637a4097f61b09d6ccf619298d9d121fafa044e4Douglas Gregor 1330d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor default: 1331d560d5025a0e5b1942d99d5f39005337b03a64c2Douglas Gregor break; 13320b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor } 13330b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 13340b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor // FIXME: Many more cases to go (to go). 1335f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return Sema::TDK_Success; 13360b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor} 13370b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 1338f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregorstatic Sema::TemplateDeductionResult 1339a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S, 1340f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateParameterList *TemplateParams, 1341f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor const TemplateArgument &Param, 134277d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor TemplateArgument Arg, 13432a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall TemplateDeductionInfo &Info, 134402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 134577d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor // If the template argument is a pack expansion, perform template argument 134677d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor // deduction against the pattern of that expansion. This only occurs during 134777d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor // partial ordering. 134877d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor if (Arg.isPackExpansion()) 134977d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor Arg = Arg.getPackExpansionPattern(); 135077d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor 13510b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor switch (Param.getKind()) { 1352199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor case TemplateArgument::Null: 1353199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor assert(false && "Null template argument in parameter list"); 1354199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor break; 13551eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 13561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump case TemplateArgument::Type: 1357788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor if (Arg.getKind() == TemplateArgument::Type) 1358a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceTemplateArguments(S, TemplateParams, Param.getAsType(), 1359788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor Arg.getAsType(), Info, Deduced, 0); 1360788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor Info.FirstArg = Param; 1361788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor Info.SecondArg = Arg; 1362788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor return Sema::TDK_NonDeducedMismatch; 1363a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor 1364788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor case TemplateArgument::Template: 1365db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor if (Arg.getKind() == TemplateArgument::Template) 1366a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceTemplateArguments(S, TemplateParams, 1367788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor Param.getAsTemplate(), 1368db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor Arg.getAsTemplate(), Info, Deduced); 1369788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor Info.FirstArg = Param; 1370788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor Info.SecondArg = Arg; 1371788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor return Sema::TDK_NonDeducedMismatch; 1372a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor 1373a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor case TemplateArgument::TemplateExpansion: 1374a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor llvm_unreachable("caller should handle pack expansions"); 1375a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor break; 1376788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor 1377199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor case TemplateArgument::Declaration: 1378788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor if (Arg.getKind() == TemplateArgument::Declaration && 1379788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor Param.getAsDecl()->getCanonicalDecl() == 1380788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor Arg.getAsDecl()->getCanonicalDecl()) 1381788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor return Sema::TDK_Success; 1382788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor 1383f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Param; 1384f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = Arg; 1385f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 13861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1387199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor case TemplateArgument::Integral: 1388199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (Arg.getKind() == TemplateArgument::Integral) { 13899d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor if (hasSameExtendedValue(*Param.getAsIntegral(), *Arg.getAsIntegral())) 1390f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 1391f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 1392f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Param; 1393f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = Arg; 1394f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 1395f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 1396f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor 1397f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Arg.getKind() == TemplateArgument::Expression) { 1398f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Param; 1399f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = Arg; 1400f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 1401199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor } 1402199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor 1403f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Param; 1404f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = Arg; 1405f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 14061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1407199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor case TemplateArgument::Expression: { 14081eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (NonTypeTemplateParmDecl *NTTP 1409199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor = getDeducedParameterFromExpr(Param.getAsExpr())) { 1410199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (Arg.getKind() == TemplateArgument::Integral) 1411a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceNonTypeTemplateArgument(S, NTTP, 14121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump *Arg.getAsIntegral(), 14139d0e441036de0faa59d8039ce0c9967bda112c7fDouglas Gregor Arg.getIntegralType(), 141402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor /*ArrayBound=*/false, 1415f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info, Deduced); 1416199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor if (Arg.getKind() == TemplateArgument::Expression) 1417a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsExpr(), 1418f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info, Deduced); 141915755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor if (Arg.getKind() == TemplateArgument::Declaration) 1420a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth return DeduceNonTypeTemplateArgument(S, NTTP, Arg.getAsDecl(), 142115755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor Info, Deduced); 142215755cb8399afa702575a21915daf2f6e56b5ac1Douglas Gregor 1423f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.FirstArg = Param; 1424f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.SecondArg = Arg; 1425f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_NonDeducedMismatch; 1426199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor } 14271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1428199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor // Can't deduce anything, but that's okay. 1429f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 1430199d99192fbcca9f043596c40ead4afab4999dbaDouglas Gregor } 1431d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson case TemplateArgument::Pack: 143220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor llvm_unreachable("Argument packs should be expanded by the caller!"); 14330b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor } 14341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1435f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 14360b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor} 14370b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 143820a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// \brief Determine whether there is a template argument to be used for 143920a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// deduction. 144020a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// 144120a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// This routine "expands" argument packs in-place, overriding its input 144220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// parameters so that \c Args[ArgIdx] will be the available template argument. 144320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// 144420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// \returns true if there is another template argument (which will be at 144520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor/// \c Args[ArgIdx]), false otherwise. 144620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregorstatic bool hasTemplateArgumentForDeduction(const TemplateArgument *&Args, 144720a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor unsigned &ArgIdx, 144820a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor unsigned &NumArgs) { 144920a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor if (ArgIdx == NumArgs) 145020a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor return false; 145120a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor 145220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor const TemplateArgument &Arg = Args[ArgIdx]; 145320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor if (Arg.getKind() != TemplateArgument::Pack) 145420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor return true; 145520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor 145620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor assert(ArgIdx == NumArgs - 1 && "Pack not at the end of argument list?"); 145720a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor Args = Arg.pack_begin(); 145820a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor NumArgs = Arg.pack_size(); 145920a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor ArgIdx = 0; 146020a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor return ArgIdx < NumArgs; 146120a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor} 146220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor 14637b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor/// \brief Determine whether the given set of template arguments has a pack 14647b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor/// expansion that is not the last template argument. 14657b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregorstatic bool hasPackExpansionBeforeEnd(const TemplateArgument *Args, 14667b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor unsigned NumArgs) { 14677b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor unsigned ArgIdx = 0; 14687b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor while (ArgIdx < NumArgs) { 14697b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor const TemplateArgument &Arg = Args[ArgIdx]; 14707b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor 14717b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor // Unwrap argument packs. 14727b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor if (Args[ArgIdx].getKind() == TemplateArgument::Pack) { 14737b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor Args = Arg.pack_begin(); 14747b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor NumArgs = Arg.pack_size(); 14757b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor ArgIdx = 0; 14767b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor continue; 14777b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor } 14787b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor 14797b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor ++ArgIdx; 14807b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor if (ArgIdx == NumArgs) 14817b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor return false; 14827b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor 14837b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor if (Arg.isPackExpansion()) 14847b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor return true; 14857b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor } 14867b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor 14877b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor return false; 14887b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor} 14897b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor 14901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic Sema::TemplateDeductionResult 1491a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler CarruthDeduceTemplateArguments(Sema &S, 1492f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor TemplateParameterList *TemplateParams, 149320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor const TemplateArgument *Params, unsigned NumParams, 149420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor const TemplateArgument *Args, unsigned NumArgs, 14952a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall TemplateDeductionInfo &Info, 14960972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced, 14970972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor bool NumberOfArgumentsMustMatch) { 1498e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // C++0x [temp.deduct.type]p9: 1499e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // If the template argument list of P contains a pack expansion that is not 1500e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // the last template argument, the entire template argument list is a 1501e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // non-deduced context. 15027b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor if (hasPackExpansionBeforeEnd(Params, NumParams)) 15037b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor return Sema::TDK_Success; 15047b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor 1505e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // C++0x [temp.deduct.type]p9: 1506e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // If P has a form that contains <T> or <i>, then each argument Pi of the 1507e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // respective template argument list P is compared with the corresponding 1508e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // argument Ai of the corresponding template argument list of A. 150920a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor unsigned ArgIdx = 0, ParamIdx = 0; 151020a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor for (; hasTemplateArgumentForDeduction(Params, ParamIdx, NumParams); 151120a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor ++ParamIdx) { 151220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor if (!Params[ParamIdx].isPackExpansion()) { 1513e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // The simple case: deduce template arguments by matching Pi and Ai. 151420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor 151520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor // Check whether we have enough arguments. 151620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor if (!hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs)) 15173cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor return NumberOfArgumentsMustMatch? Sema::TDK_NonDeducedMismatch 15180972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor : Sema::TDK_Success; 151920a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor 152077d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor if (Args[ArgIdx].isPackExpansion()) { 152177d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor // FIXME: We follow the logic of C++0x [temp.deduct.type]p22 here, 152277d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor // but applied to pack expansions that are template arguments. 152377d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor return Sema::TDK_NonDeducedMismatch; 152477d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor } 152577d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor 1526e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // Perform deduction for this Pi/Ai pair. 152720a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor if (Sema::TemplateDeductionResult Result 152877d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor = DeduceTemplateArguments(S, TemplateParams, 152977d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor Params[ParamIdx], Args[ArgIdx], 153077d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor Info, Deduced)) 153120a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor return Result; 153220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor 153320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor // Move to the next argument. 153420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor ++ArgIdx; 153520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor continue; 153620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor } 153720a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor 1538e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // The parameter is a pack expansion. 1539e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor 1540e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // C++0x [temp.deduct.type]p9: 1541e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // If Pi is a pack expansion, then the pattern of Pi is compared with 1542e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // each remaining argument in the template argument list of A. Each 1543e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // comparison deduces template arguments for subsequent positions in the 1544e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // template parameter packs expanded by Pi. 1545e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern(); 1546e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor 1547e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // Compute the set of template parameter indices that correspond to 1548e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // parameter packs expanded by the pack expansion. 1549e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor llvm::SmallVector<unsigned, 2> PackIndices; 1550e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor { 1551e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor llvm::BitVector SawIndices(TemplateParams->size()); 1552e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded; 1553e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor S.collectUnexpandedParameterPacks(Pattern, Unexpanded); 1554e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { 1555e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor unsigned Depth, Index; 1556e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); 1557e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor if (Depth == 0 && !SawIndices[Index]) { 1558e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor SawIndices[Index] = true; 1559e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor PackIndices.push_back(Index); 1560e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor } 1561e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor } 1562e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor } 1563e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?"); 1564e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor 1565e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // FIXME: If there are no remaining arguments, we can bail out early 1566e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // and set any deduced parameter packs to an empty argument pack. 1567e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // The latter part of this is a (minor) correctness issue. 1568e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor 1569e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // Save the deduced template arguments for each parameter pack expanded 1570e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // by this pack expansion, then clear out the deduction. 1571e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor llvm::SmallVector<DeducedTemplateArgument, 2> 1572e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor SavedPacks(PackIndices.size()); 15735429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2> 15745429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor NewlyDeducedPacks(PackIndices.size()); 15755429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor PrepareArgumentPackDeduction(S, Deduced, PackIndices, SavedPacks, 15765429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor NewlyDeducedPacks); 1577e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor 1578e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // Keep track of the deduced template arguments for each parameter pack 1579e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // expanded by this pack expansion (the outer index) and for each 1580e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // template argument (the inner SmallVectors). 1581e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor bool HasAnyArguments = false; 1582e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor while (hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs)) { 1583e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor HasAnyArguments = true; 1584e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor 1585e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // Deduce template arguments from the pattern. 1586e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor if (Sema::TemplateDeductionResult Result 1587e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx], 1588e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor Info, Deduced)) 1589e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor return Result; 1590e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor 1591e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // Capture the deduced template arguments for each parameter pack expanded 1592e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // by this pack expansion, add them to the list of arguments we've deduced 1593e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // for that pack, then clear out the deduced argument. 1594e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) { 1595e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]]; 1596e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor if (!DeducedArg.isNull()) { 1597e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor NewlyDeducedPacks[I].push_back(DeducedArg); 1598e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor DeducedArg = DeducedTemplateArgument(); 1599e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor } 1600e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor } 1601e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor 1602e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor ++ArgIdx; 1603e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor } 1604e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor 1605e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // Build argument packs for each of the parameter packs expanded by this 1606e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor // pack expansion. 16070216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor if (Sema::TemplateDeductionResult Result 16080216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor = FinishArgumentPackDeduction(S, TemplateParams, HasAnyArguments, 16090216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor Deduced, PackIndices, SavedPacks, 16100216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor NewlyDeducedPacks, Info)) 16110216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor return Result; 16120b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor } 161320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor 161420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor // If there is an argument remaining, then we had too many arguments. 16150972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor if (NumberOfArgumentsMustMatch && 16160972c867e72171f24052d7b6d307020c065f8a66Douglas Gregor hasTemplateArgumentForDeduction(Args, ArgIdx, NumArgs)) 16173cae5c9a79bfd2e27eb44c32b13dfacd2ce5c66fDouglas Gregor return Sema::TDK_NonDeducedMismatch; 161820a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor 1619f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor return Sema::TDK_Success; 16200b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor} 16210b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor 162220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregorstatic Sema::TemplateDeductionResult 162320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas GregorDeduceTemplateArguments(Sema &S, 162420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor TemplateParameterList *TemplateParams, 162520a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor const TemplateArgumentList &ParamList, 162620a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor const TemplateArgumentList &ArgList, 162720a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor TemplateDeductionInfo &Info, 162820a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 162920a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor return DeduceTemplateArguments(S, TemplateParams, 163020a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor ParamList.data(), ParamList.size(), 163120a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor ArgList.data(), ArgList.size(), 163220a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor Info, Deduced); 163320a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor} 163420a55e2515ce89ddf9993941f9b5d0f3a6c91b4fDouglas Gregor 1635f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor/// \brief Determine whether two template arguments are the same. 16361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic bool isSameTemplateArg(ASTContext &Context, 1637f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor const TemplateArgument &X, 1638f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor const TemplateArgument &Y) { 1639f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (X.getKind() != Y.getKind()) 1640f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return false; 16411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1642f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor switch (X.getKind()) { 1643f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor case TemplateArgument::Null: 1644f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor assert(false && "Comparing NULL template argument"); 1645f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor break; 16461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1647f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor case TemplateArgument::Type: 1648f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return Context.getCanonicalType(X.getAsType()) == 1649f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Context.getCanonicalType(Y.getAsType()); 16501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1651f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor case TemplateArgument::Declaration: 165297fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis return X.getAsDecl()->getCanonicalDecl() == 165397fbaa2a38804268a024f1a104b43fcf8b4411b0Argyrios Kyrtzidis Y.getAsDecl()->getCanonicalDecl(); 16541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1655788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor case TemplateArgument::Template: 1656a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor case TemplateArgument::TemplateExpansion: 1657a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor return Context.getCanonicalTemplateName( 1658a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() == 1659a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor Context.getCanonicalTemplateName( 1660a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer(); 1661788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor 1662f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor case TemplateArgument::Integral: 1663f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return *X.getAsIntegral() == *Y.getAsIntegral(); 16641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1665788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor case TemplateArgument::Expression: { 1666788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor llvm::FoldingSetNodeID XID, YID; 1667788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor X.getAsExpr()->Profile(XID, Context, true); 1668788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor Y.getAsExpr()->Profile(YID, Context, true); 1669788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor return XID == YID; 1670788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor } 16711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 1672f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor case TemplateArgument::Pack: 1673f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (X.pack_size() != Y.pack_size()) 1674f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return false; 16751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 16761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump for (TemplateArgument::pack_iterator XP = X.pack_begin(), 16771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump XPEnd = X.pack_end(), 1678f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor YP = Y.pack_begin(); 16791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump XP != XPEnd; ++XP, ++YP) 1680f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor if (!isSameTemplateArg(Context, *XP, *YP)) 1681f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return false; 1682f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor 1683f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return true; 1684f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor } 1685f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor 1686f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor return false; 1687f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor} 1688f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor 168954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// \brief Allocate a TemplateArgumentLoc where all locations have 169054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// been initialized to the given location. 169154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// 169254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// \param S The semantic analysis object. 169354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// 169454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// \param The template argument we are producing template argument 169554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// location information for. 169654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// 169754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// \param NTTPType For a declaration template argument, the type of 169854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// the non-type template parameter that corresponds to this template 169954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// argument. 170054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// 170154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// \param Loc The source location to use for the resulting template 170254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// argument. 170354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregorstatic TemplateArgumentLoc 170454c53cca105ed595e12fecf04e415c3712bda936Douglas GregorgetTrivialTemplateArgumentLoc(Sema &S, 170554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor const TemplateArgument &Arg, 170654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor QualType NTTPType, 170754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor SourceLocation Loc) { 170854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor switch (Arg.getKind()) { 170954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor case TemplateArgument::Null: 171054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor llvm_unreachable("Can't get a NULL template argument here"); 171154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor break; 171254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor 171354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor case TemplateArgument::Type: 171454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor return TemplateArgumentLoc(Arg, 171554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor S.Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc)); 171654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor 171754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor case TemplateArgument::Declaration: { 171854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor Expr *E 1719ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor = S.BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) 172054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor .takeAs<Expr>(); 172154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor return TemplateArgumentLoc(TemplateArgument(E), E); 172254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor } 172354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor 172454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor case TemplateArgument::Integral: { 172554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor Expr *E 1726ba68eca7582a62e3e2ff4b0eba1b2b73a6b80895Douglas Gregor = S.BuildExpressionFromIntegralTemplateArgument(Arg, Loc).takeAs<Expr>(); 172754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor return TemplateArgumentLoc(TemplateArgument(E), E); 172854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor } 172954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor 173054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor case TemplateArgument::Template: 1731a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor return TemplateArgumentLoc(Arg, SourceRange(), Loc); 1732a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor 1733a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor case TemplateArgument::TemplateExpansion: 1734a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor return TemplateArgumentLoc(Arg, SourceRange(), Loc, Loc); 1735a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor 173654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor case TemplateArgument::Expression: 173754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor return TemplateArgumentLoc(Arg, Arg.getAsExpr()); 173854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor 173954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor case TemplateArgument::Pack: 174054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo()); 174154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor } 174254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor 174354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor return TemplateArgumentLoc(); 174454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor} 174554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor 174654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor 174754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// \brief Convert the given deduced template argument and add it to the set of 174854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor/// fully-converted template arguments. 174954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregorstatic bool ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param, 175054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor DeducedTemplateArgument Arg, 175154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor NamedDecl *Template, 175254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor QualType NTTPType, 17536952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor unsigned ArgumentPackIndex, 175454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor TemplateDeductionInfo &Info, 175554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor bool InFunctionTemplate, 175654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor llvm::SmallVectorImpl<TemplateArgument> &Output) { 175754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor if (Arg.getKind() == TemplateArgument::Pack) { 175854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor // This is a template argument pack, so check each of its arguments against 175954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor // the template parameter. 176054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor llvm::SmallVector<TemplateArgument, 2> PackedArgsBuilder; 176154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor for (TemplateArgument::pack_iterator PA = Arg.pack_begin(), 1762135ffa7375fb5802b92f42774e02d0e6e4c78e5bDouglas Gregor PAEnd = Arg.pack_end(); 176354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor PA != PAEnd; ++PA) { 1764d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor // When converting the deduced template argument, append it to the 1765d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor // general output list. We need to do this so that the template argument 1766d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor // checking logic has all of the prior template arguments available. 176754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor DeducedTemplateArgument InnerArg(*PA); 176854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound()); 176954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor if (ConvertDeducedTemplateArgument(S, Param, InnerArg, Template, 17706952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor NTTPType, PackedArgsBuilder.size(), 17716952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor Info, InFunctionTemplate, Output)) 177254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor return true; 1773d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor 1774d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor // Move the converted template argument into our argument pack. 1775d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor PackedArgsBuilder.push_back(Output.back()); 1776d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor Output.pop_back(); 177754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor } 177854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor 177954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor // Create the resulting argument pack. 1780203e6a322ae29d577acafcb1572a57ec16e1e730Douglas Gregor Output.push_back(TemplateArgument::CreatePackCopy(S.Context, 1781203e6a322ae29d577acafcb1572a57ec16e1e730Douglas Gregor PackedArgsBuilder.data(), 1782203e6a322ae29d577acafcb1572a57ec16e1e730Douglas Gregor PackedArgsBuilder.size())); 178354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor return false; 178454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor } 178554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor 178654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor // Convert the deduced template argument into a template 178754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor // argument that we can check, almost as if the user had written 178854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor // the template argument explicitly. 178954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor TemplateArgumentLoc ArgLoc = getTrivialTemplateArgumentLoc(S, Arg, NTTPType, 179054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor Info.getLocation()); 179154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor 179254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor // Check the template argument, converting it as necessary. 179354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor return S.CheckTemplateArgument(Param, ArgLoc, 179454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor Template, 179554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor Template->getLocation(), 179654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor Template->getSourceRange().getEnd(), 17976952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor ArgumentPackIndex, 179854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor Output, 179954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor InFunctionTemplate 180054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor ? (Arg.wasDeducedFromArrayBound() 180154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor ? Sema::CTAK_DeducedFromArrayBound 180254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor : Sema::CTAK_Deduced) 180354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor : Sema::CTAK_Specified); 180454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor} 180554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor 180631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// Complete template argument deduction for a class template partial 180731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// specialization. 180831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregorstatic Sema::TemplateDeductionResult 180931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas GregorFinishTemplateArgumentDeduction(Sema &S, 181031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor ClassTemplatePartialSpecializationDecl *Partial, 181131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor const TemplateArgumentList &TemplateArgs, 181231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced, 18132a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall TemplateDeductionInfo &Info) { 181431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor // Trap errors. 181531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor Sema::SFINAETrap Trap(S); 181631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor 181731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor Sema::ContextRAII SavedContext(S, Partial); 1818f5813826802c2e76cdc13cae834ebfd4518d74a6John McCall 181902cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // C++ [temp.deduct.type]p2: 182002cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // [...] or if any template argument remains neither deduced nor 182102cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // explicitly specified, template argument deduction fails. 1822910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor llvm::SmallVector<TemplateArgument, 4> Builder; 1823033a3cad525e5b221ba035ffc3e0abfa1241d90eDouglas Gregor TemplateParameterList *PartialParams = Partial->getTemplateParameters(); 1824033a3cad525e5b221ba035ffc3e0abfa1241d90eDouglas Gregor for (unsigned I = 0, N = PartialParams->size(); I != N; ++I) { 182554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor NamedDecl *Param = PartialParams->getParam(I); 1826f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor if (Deduced[I].isNull()) { 182754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor Info.Param = makeTemplateParameter(Param); 182831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor return Sema::TDK_Incomplete; 1829f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor } 183031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor 183154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor // We have deduced this argument, so it still needs to be 183254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor // checked and converted. 183354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor 183454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor // First, for a non-type template parameter type that is 183554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor // initialized by a declaration, we need the type of the 183654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor // corresponding non-type template parameter. 183754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor QualType NTTPType; 183854c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor if (NonTypeTemplateParmDecl *NTTP 1839d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor = dyn_cast<NonTypeTemplateParmDecl>(Param)) { 184054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor NTTPType = NTTP->getType(); 1841d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor if (NTTPType->isDependentType()) { 1842d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, 1843d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor Builder.data(), Builder.size()); 1844d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor NTTPType = S.SubstType(NTTPType, 1845d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor MultiLevelTemplateArgumentList(TemplateArgs), 1846d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor NTTP->getLocation(), 1847d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor NTTP->getDeclName()); 1848d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor if (NTTPType.isNull()) { 1849d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor Info.Param = makeTemplateParameter(Param); 1850d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor // FIXME: These template arguments are temporary. Free them! 1851d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor Info.reset(TemplateArgumentList::CreateCopy(S.Context, 1852d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor Builder.data(), 1853d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor Builder.size())); 1854d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor return Sema::TDK_SubstitutionFailure; 1855d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor } 1856d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor } 1857d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor } 1858d53e16abd1dcaa2942d5183f48e7f63d0e75b35aDouglas Gregor 185954c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], 18606952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor Partial, NTTPType, 0, Info, false, 186154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor Builder)) { 186254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor Info.Param = makeTemplateParameter(Param); 186354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor // FIXME: These template arguments are temporary. Free them! 186454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder.data(), 186554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor Builder.size())); 186654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor return Sema::TDK_SubstitutionFailure; 186754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor } 186802cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor } 186977d6bb9e223496aa5288294f34e7225d1f65dddcDouglas Gregor 187002cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // Form the template argument list from the deduced template arguments. 18711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateArgumentList *DeducedArgumentList 1872910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor = TemplateArgumentList::CreateCopy(S.Context, Builder.data(), 1873910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor Builder.size()); 1874910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor 1875f67875d5addf36b951ad37fb04509ab2b572c88aDouglas Gregor Info.reset(DeducedArgumentList); 187602cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor 187702cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // Substitute the deduced template arguments into the template 187802cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // arguments of the class template partial specialization, and 187902cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // verify that the instantiated template arguments are both valid 188002cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor // and are equivalent to the template arguments originally provided 18811eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // to the class template. 18822a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall LocalInstantiationScope InstScope(S); 188302cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor ClassTemplateDecl *ClassTemplate = Partial->getSpecializedTemplate(); 1884833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall const TemplateArgumentLoc *PartialTemplateArgs 1885833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall = Partial->getTemplateArgsAsWritten(); 1886d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall 1887d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall // Note that we don't provide the langle and rangle locations. 1888d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall TemplateArgumentListInfo InstArgs; 1889d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall 1890e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor if (S.Subst(PartialTemplateArgs, 1891e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor Partial->getNumTemplateArgsAsWritten(), 1892e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) { 1893e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; 1894e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor if (ParamIdx >= Partial->getTemplateParameters()->size()) 1895e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor ParamIdx = Partial->getTemplateParameters()->size() - 1; 1896e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor 1897e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor Decl *Param 1898e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor = const_cast<NamedDecl *>( 1899e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor Partial->getTemplateParameters()->getParam(ParamIdx)); 1900e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor Info.Param = makeTemplateParameter(Param); 1901e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument(); 1902e02e26293cf8e3bad1059b39cea75c6582896da6Douglas Gregor return Sema::TDK_SubstitutionFailure; 1903833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall } 1904833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall 1905910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor llvm::SmallVector<TemplateArgument, 4> ConvertedInstArgs; 190631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor if (S.CheckTemplateArgumentList(ClassTemplate, Partial->getLocation(), 190754c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor InstArgs, false, ConvertedInstArgs)) 190831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor return Sema::TDK_SubstitutionFailure; 1909833ca991c1bfc967f0995974ca86f66ba1f666b5John McCall 191054c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor TemplateParameterList *TemplateParams 191154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor = ClassTemplate->getTemplateParameters(); 191254c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { 1913910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor TemplateArgument InstArg = ConvertedInstArgs.data()[I]; 191431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { 19152fdc5e8199e1e239620f2faae88997153703e16fDouglas Gregor Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); 1916f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.FirstArg = TemplateArgs[I]; 1917f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor Info.SecondArg = InstArg; 191831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor return Sema::TDK_NonDeducedMismatch; 1919f670c8cfa58b4c224eb8fb566130dc47844dd3deDouglas Gregor } 192002cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor } 192102cbbd2d945e466174bee536758e5a2e6a1c0ea2Douglas Gregor 1922bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor if (Trap.hasErrorOccurred()) 192331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor return Sema::TDK_SubstitutionFailure; 1924bb2604122f4186b6f666481f699b27c7e7a95790Douglas Gregor 192531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor return Sema::TDK_Success; 192631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor} 192731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor 192831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// \brief Perform template argument deduction to determine whether 192931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// the given template arguments match the given class template 193031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor/// partial specialization per C++ [temp.class.spec.match]. 193131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas GregorSema::TemplateDeductionResult 193231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas GregorSema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, 193331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor const TemplateArgumentList &TemplateArgs, 193431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor TemplateDeductionInfo &Info) { 193531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor // C++ [temp.class.spec.match]p2: 193631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor // A partial specialization matches a given actual template 193731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor // argument list if the template arguments of the partial 193831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor // specialization can be deduced from the actual template argument 193931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor // list (14.8.2). 194031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor SFINAETrap Trap(*this); 194131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor llvm::SmallVector<DeducedTemplateArgument, 4> Deduced; 194231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor Deduced.resize(Partial->getTemplateParameters()->size()); 194331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor if (TemplateDeductionResult Result 194431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor = ::DeduceTemplateArguments(*this, 194531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor Partial->getTemplateParameters(), 194631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor Partial->getTemplateArgs(), 194731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor TemplateArgs, Info, Deduced)) 194831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor return Result; 194931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor 195031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor InstantiatingTemplate Inst(*this, Partial->getLocation(), Partial, 19519b623639378d53a675921ddfa7316034d571881eDouglas Gregor Deduced.data(), Deduced.size(), Info); 195231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor if (Inst) 195331dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor return TDK_InstantiationDepth; 195431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor 195531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor if (Trap.hasErrorOccurred()) 195631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor return Sema::TDK_SubstitutionFailure; 195731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor 195831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor return ::FinishTemplateArgumentDeduction(*this, Partial, TemplateArgs, 195931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor Deduced, Info); 19600b9247f129401b4849682b2e2bcdea8fc977068aDouglas Gregor} 1961031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 19624112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor/// \brief Determine whether the given type T is a simple-template-id type. 19634112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregorstatic bool isSimpleTemplateIdType(QualType T) { 19641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (const TemplateSpecializationType *Spec 1965183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall = T->getAs<TemplateSpecializationType>()) 19664112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor return Spec->getTemplateName().getAsTemplateDecl() != 0; 19671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 19684112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor return false; 19694112877d1043d0b937f5abcf043bc7fa48138f05Douglas Gregor} 197083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 197183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \brief Substitute the explicitly-provided template arguments into the 197283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// given function template according to C++ [temp.arg.explicit]. 197383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 197483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionTemplate the function template into which the explicit 197583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template arguments will be substituted. 197683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 19771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ExplicitTemplateArguments the explicitly-specified template 197883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// arguments. 197983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 19801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param Deduced the deduced template arguments, which will be populated 198183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// with the converted and checked explicit template arguments. 198283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 19831eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \param ParamTypes will be populated with the instantiated function 198483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// parameters. 198583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 198683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionType if non-NULL, the result type of the function template 198783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// will also be instantiated and the pointed-to value will be updated with 198883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// the instantiated function type. 198983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 199083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Info if substitution fails for any reason, this object will be 199183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// populated with more information about the failure. 199283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 199383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \returns TDK_Success if substitution was successful, or some failure 199483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// condition. 199583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::TemplateDeductionResult 199683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::SubstituteExplicitTemplateArguments( 199783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionTemplateDecl *FunctionTemplate, 1998d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall const TemplateArgumentListInfo &ExplicitTemplateArgs, 199902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced, 200083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor llvm::SmallVectorImpl<QualType> &ParamTypes, 200183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor QualType *FunctionType, 200283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateDeductionInfo &Info) { 200383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 200483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateParameterList *TemplateParams 200583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor = FunctionTemplate->getTemplateParameters(); 200683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 2007d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall if (ExplicitTemplateArgs.size() == 0) { 200883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // No arguments to substitute; just copy over the parameter types and 200983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // fill in the function type. 201083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor for (FunctionDecl::param_iterator P = Function->param_begin(), 201183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor PEnd = Function->param_end(); 201283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor P != PEnd; 201383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ++P) 201483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ParamTypes.push_back((*P)->getType()); 20151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 201683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (FunctionType) 201783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor *FunctionType = Function->getType(); 201883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_Success; 201983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor } 20201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 202183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Substitution of the explicit template arguments into a function template 202283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor /// is a SFINAE context. Trap any errors that might occur. 20231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump SFINAETrap Trap(*this); 20241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 202583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // C++ [temp.arg.explicit]p3: 20261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Template arguments that are present shall be specified in the 20271eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // declaration order of their corresponding template-parameters. The 202883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // template argument list shall not specify more template-arguments than 20291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // there are corresponding template-parameters. 2030910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor llvm::SmallVector<TemplateArgument, 4> Builder; 20311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20321eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Enter a new template instantiation context where we check the 203383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // explicitly-specified template arguments against this function template, 203483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // and then substitute them into the function parameter types. 20351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(), 203683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionTemplate, Deduced.data(), Deduced.size(), 20379b623639378d53a675921ddfa7316034d571881eDouglas Gregor ActiveTemplateInstantiation::ExplicitTemplateArgumentSubstitution, 20389b623639378d53a675921ddfa7316034d571881eDouglas Gregor Info); 203983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (Inst) 204083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_InstantiationDepth; 20411eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 204283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (CheckTemplateArgumentList(FunctionTemplate, 204383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor SourceLocation(), 2044d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall ExplicitTemplateArgs, 204583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor true, 2046f1a8445036a2d047c7165d4170e3058cdeaba6ebDouglas Gregor Builder) || Trap.hasErrorOccurred()) { 2047910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor unsigned Index = Builder.size(); 2048fe52c91dcd1ecda90b579f789baf70f7a992e3c9Douglas Gregor if (Index >= TemplateParams->size()) 2049fe52c91dcd1ecda90b579f789baf70f7a992e3c9Douglas Gregor Index = TemplateParams->size() - 1; 2050fe52c91dcd1ecda90b579f789baf70f7a992e3c9Douglas Gregor Info.Param = makeTemplateParameter(TemplateParams->getParam(Index)); 205183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_InvalidExplicitArguments; 2052f1a8445036a2d047c7165d4170e3058cdeaba6ebDouglas Gregor } 20531eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 205483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Form the template argument list from the explicitly-specified 205583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // template arguments. 20561eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateArgumentList *ExplicitArgumentList 2057910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size()); 205883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Info.reset(ExplicitArgumentList); 2059d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor 2060df41f18936693f7c62e457eefb9fad5b2d2fe3cdJohn McCall // Template argument deduction and the final substitution should be 2061df41f18936693f7c62e457eefb9fad5b2d2fe3cdJohn McCall // done in the context of the templated declaration. Explicit 2062df41f18936693f7c62e457eefb9fad5b2d2fe3cdJohn McCall // argument substitution, on the other hand, needs to happen in the 2063df41f18936693f7c62e457eefb9fad5b2d2fe3cdJohn McCall // calling context. 2064df41f18936693f7c62e457eefb9fad5b2d2fe3cdJohn McCall ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); 2065df41f18936693f7c62e457eefb9fad5b2d2fe3cdJohn McCall 2066d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor // If we deduced template arguments for a template parameter pack, 2067d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor // note that the template argument pack is partially substituted and record 2068d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor // the explicit template arguments. They'll be used as part of deduction 2069d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor // for this template parameter pack. 2070d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor for (unsigned I = 0, N = Builder.size(); I != N; ++I) { 2071d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor const TemplateArgument &Arg = Builder[I]; 2072d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor if (Arg.getKind() == TemplateArgument::Pack) { 2073d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor CurrentInstantiationScope->SetPartiallySubstitutedPack( 2074d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor TemplateParams->getParam(I), 2075d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor Arg.pack_begin(), 2076d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor Arg.pack_size()); 2077d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor break; 2078d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor } 2079d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor } 2080d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor 208183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Instantiate the types of each of the function parameters given the 208283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // explicitly-specified template arguments. 2083a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor if (SubstParmTypes(Function->getLocation(), 2084a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor Function->param_begin(), Function->getNumParams(), 2085a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor MultiLevelTemplateArgumentList(*ExplicitArgumentList), 2086a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor ParamTypes)) 2087a009b59fc2c550a229b9146aabda8e33fe3a7771Douglas Gregor return TDK_SubstitutionFailure; 208883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 208983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // If the caller wants a full function type back, instantiate the return 209083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // type and form that function type. 209183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (FunctionType) { 209283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // FIXME: exception-specifications? 20931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const FunctionProtoType *Proto 2094183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall = Function->getType()->getAs<FunctionProtoType>(); 209583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor assert(Proto && "Function template does not have a prototype?"); 20961eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 20971eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump QualType ResultType 2098357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor = SubstType(Proto->getResultType(), 2099357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor MultiLevelTemplateArgumentList(*ExplicitArgumentList), 2100357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor Function->getTypeSpecStartLoc(), 2101357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor Function->getDeclName()); 210283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (ResultType.isNull() || Trap.hasErrorOccurred()) 210383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_SubstitutionFailure; 21041eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 21051eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump *FunctionType = BuildFunctionType(ResultType, 210683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ParamTypes.data(), ParamTypes.size(), 210783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Proto->isVariadic(), 210883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Proto->getTypeQuals(), 210983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Function->getLocation(), 2110fa869547eb1cab12d7e0c0dfa8ba594e336b9b32Eli Friedman Function->getDeclName(), 2111fa869547eb1cab12d7e0c0dfa8ba594e336b9b32Eli Friedman Proto->getExtInfo()); 211283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (FunctionType->isNull() || Trap.hasErrorOccurred()) 211383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_SubstitutionFailure; 211483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor } 21151eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 211683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // C++ [temp.arg.explicit]p2: 21171eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Trailing template arguments that can be deduced (14.8.2) may be 21181eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // omitted from the list of explicit template-arguments. If all of the 211983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // template arguments can be deduced, they may all be omitted; in this 212083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // case, the empty template argument list <> itself may also be omitted. 212183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // 2122d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor // Take all of the explicitly-specified arguments and put them into 2123d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor // the set of deduced template arguments. Explicitly-specified 2124d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor // parameter packs, however, will be set to NULL since the deduction 2125d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor // mechanisms handle explicitly-specified argument packs directly. 212683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Deduced.reserve(TemplateParams->size()); 2127d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) { 2128d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor const TemplateArgument &Arg = ExplicitArgumentList->get(I); 2129d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor if (Arg.getKind() == TemplateArgument::Pack) 2130d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor Deduced.push_back(DeducedTemplateArgument()); 2131d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor else 2132d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor Deduced.push_back(Arg); 2133d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor } 21341eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 213583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_Success; 213683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor} 213783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 21381eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// \brief Finish template argument deduction for a function template, 213983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// checking the deduced template arguments for completeness and forming 214083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// the function template specialization. 21411eb4433ac451dc16f4133a88af2d002ac26c58efMike StumpSema::TemplateDeductionResult 214283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::FinishTemplateArgumentDeduction(FunctionTemplateDecl *FunctionTemplate, 214302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVectorImpl<DeducedTemplateArgument> &Deduced, 214402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor unsigned NumExplicitlySpecified, 214583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionDecl *&Specialization, 214683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateDeductionInfo &Info) { 214783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateParameterList *TemplateParams 214883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor = FunctionTemplate->getTemplateParameters(); 21491eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 215051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor // Template argument deduction for function templates in a SFINAE context. 215151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor // Trap any errors that might occur. 215251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor SFINAETrap Trap(*this); 215351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor 215451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor // Enter a new template instantiation context while we instantiate the 215551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor // actual function declaration. 215651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor InstantiatingTemplate Inst(*this, FunctionTemplate->getLocation(), 215751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor FunctionTemplate, Deduced.data(), Deduced.size(), 21589b623639378d53a675921ddfa7316034d571881eDouglas Gregor ActiveTemplateInstantiation::DeducedTemplateArgumentSubstitution, 21599b623639378d53a675921ddfa7316034d571881eDouglas Gregor Info); 216051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor if (Inst) 216151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor return TDK_InstantiationDepth; 216251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor 216396db310ab7ca59e1890ddef25a3701bc2909d20fJohn McCall ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); 2164f5813826802c2e76cdc13cae834ebfd4518d74a6John McCall 216583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // C++ [temp.deduct.type]p2: 216683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // [...] or if any template argument remains neither deduced nor 216783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // explicitly specified, template argument deduction fails. 2168910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor llvm::SmallVector<TemplateArgument, 4> Builder; 2169b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { 2170b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor NamedDecl *Param = TemplateParams->getParam(I); 2171ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor 217251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor if (!Deduced[I].isNull()) { 21733273b0cea879c7af345d6bf98502bbf73fc4fde1Douglas Gregor if (I < NumExplicitlySpecified) { 217402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor // We have already fully type-checked and converted this 21753273b0cea879c7af345d6bf98502bbf73fc4fde1Douglas Gregor // argument, because it was explicitly-specified. Just record the 21763273b0cea879c7af345d6bf98502bbf73fc4fde1Douglas Gregor // presence of this argument. 2177910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor Builder.push_back(Deduced[I]); 217802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor continue; 217902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor } 218002024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor 218102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor // We have deduced this argument, so it still needs to be 218202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor // checked and converted. 218302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor 218402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor // First, for a non-type template parameter type that is 218502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor // initialized by a declaration, we need the type of the 218602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor // corresponding non-type template parameter. 218702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor QualType NTTPType; 218802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor if (NonTypeTemplateParmDecl *NTTP 218902024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor = dyn_cast<NonTypeTemplateParmDecl>(Param)) { 2190b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor NTTPType = NTTP->getType(); 2191b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor if (NTTPType->isDependentType()) { 2192b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, 2193b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor Builder.data(), Builder.size()); 2194b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor NTTPType = SubstType(NTTPType, 2195b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor MultiLevelTemplateArgumentList(TemplateArgs), 2196b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor NTTP->getLocation(), 2197b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor NTTP->getDeclName()); 2198b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor if (NTTPType.isNull()) { 2199b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor Info.Param = makeTemplateParameter(Param); 2200b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor // FIXME: These template arguments are temporary. Free them! 2201b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor Info.reset(TemplateArgumentList::CreateCopy(Context, 2202b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor Builder.data(), 2203b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor Builder.size())); 2204b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor return TDK_SubstitutionFailure; 220502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor } 220602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor } 220702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor } 220802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor 2209b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor if (ConvertDeducedTemplateArgument(*this, Param, Deduced[I], 22106952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor FunctionTemplate, NTTPType, 0, Info, 221154c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor true, Builder)) { 2212b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor Info.Param = makeTemplateParameter(Param); 2213910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor // FIXME: These template arguments are temporary. Free them! 2214910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(), 2215b9a7d6fb53f2b76df2ef832146a1edb4cb01b9f6Douglas Gregor Builder.size())); 221602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor return TDK_SubstitutionFailure; 221702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor } 221802024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor 221951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor continue; 222051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor } 2221ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor 2222ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor // C++0x [temp.arg.explicit]p3: 2223ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor // A trailing template parameter pack (14.5.3) not otherwise deduced will 2224ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor // be deduced to an empty sequence of template arguments. 2225ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor // FIXME: Where did the word "trailing" come from? 2226ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor if (Param->isTemplateParameterPack()) { 2227d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor // We may have had explicitly-specified template arguments for this 2228d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor // template parameter pack. If so, our empty deduction extends the 2229d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor // explicitly-specified set (C++0x [temp.arg.explicit]p9). 2230d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor const TemplateArgument *ExplicitArgs; 2231d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor unsigned NumExplicitArgs; 2232d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor if (CurrentInstantiationScope->getPartiallySubstitutedPack(&ExplicitArgs, 2233d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor &NumExplicitArgs) 2234d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor == Param) 2235d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor Builder.push_back(TemplateArgument(ExplicitArgs, NumExplicitArgs)); 2236d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor else 2237d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor Builder.push_back(TemplateArgument(0, 0)); 2238d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor 2239ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor continue; 2240ea6c96f63a45b4ffdcdf9824a9cf31a32825c0f6Douglas Gregor } 224151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor 224251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor // Substitute into the default template argument, if available. 224351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor TemplateArgumentLoc DefArg 224451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor = SubstDefaultTemplateArgumentIfAvailable(FunctionTemplate, 224551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor FunctionTemplate->getLocation(), 224651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor FunctionTemplate->getSourceRange().getEnd(), 224751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor Param, 224851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor Builder); 224951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor 225051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor // If there was no default argument, deduction is incomplete. 225151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor if (DefArg.getArgument().isNull()) { 225283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Info.Param = makeTemplateParameter( 225351ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor const_cast<NamedDecl *>(TemplateParams->getParam(I))); 225483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_Incomplete; 225583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor } 225651ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor 225751ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor // Check whether we can actually use the default argument. 225851ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor if (CheckTemplateArgument(Param, DefArg, 225951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor FunctionTemplate, 226051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor FunctionTemplate->getLocation(), 226151ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor FunctionTemplate->getSourceRange().getEnd(), 22626952f1e4256c5b43aee5e98cea4e9b663bd1d413Douglas Gregor 0, Builder, 226302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor CTAK_Deduced)) { 226451ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor Info.Param = makeTemplateParameter( 226551ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor const_cast<NamedDecl *>(TemplateParams->getParam(I))); 2266910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor // FIXME: These template arguments are temporary. Free them! 2267910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor Info.reset(TemplateArgumentList::CreateCopy(Context, Builder.data(), 2268910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor Builder.size())); 226951ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor return TDK_SubstitutionFailure; 227051ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor } 22711eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 227251ffb0c9d43b2d3fd210e51ecdd67ba5d1790d70Douglas Gregor // If we get here, we successfully used the default template argument. 227383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor } 22741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 227583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Form the template argument list from the deduced template arguments. 22761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump TemplateArgumentList *DeducedArgumentList 2277910f8008fea79120489a53593fe971b0b8a4a740Douglas Gregor = TemplateArgumentList::CreateCopy(Context, Builder.data(), Builder.size()); 227883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Info.reset(DeducedArgumentList); 22791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 22801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // Substitute the deduced template arguments into the function template 228183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // declaration to produce the function template specialization. 2282d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor DeclContext *Owner = FunctionTemplate->getDeclContext(); 2283d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor if (FunctionTemplate->getFriendObjectKind()) 2284d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor Owner = FunctionTemplate->getLexicalDeclContext(); 228583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Specialization = cast_or_null<FunctionDecl>( 2286d4598a2cc7576c06f69d3cf64d0e2c9783ddf529Douglas Gregor SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner, 2287357bbd022c1d340c8e255aea7a684ddb34bc76e5Douglas Gregor MultiLevelTemplateArgumentList(*DeducedArgumentList))); 228883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (!Specialization) 228983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_SubstitutionFailure; 22901eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2291f882574cf640d5c8355965e1c486f9d8d8ffcf47Douglas Gregor assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == 2292f882574cf640d5c8355965e1c486f9d8d8ffcf47Douglas Gregor FunctionTemplate->getCanonicalDecl()); 2293f882574cf640d5c8355965e1c486f9d8d8ffcf47Douglas Gregor 22941eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // If the template argument list is owned by the function template 229583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // specialization, release it. 2296ec20f46740a59758b12c22108002395bcf5b6f9bDouglas Gregor if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList && 2297ec20f46740a59758b12c22108002395bcf5b6f9bDouglas Gregor !Trap.hasErrorOccurred()) 229883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Info.take(); 22991eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 230083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // There may have been an error that did not prevent us from constructing a 230183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // declaration. Mark the declaration invalid and return with a substitution 230283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // failure. 230383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (Trap.hasErrorOccurred()) { 230483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Specialization->setInvalidDecl(true); 230583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return TDK_SubstitutionFailure; 230683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor } 23071eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 23089b623639378d53a675921ddfa7316034d571881eDouglas Gregor // If we suppressed any diagnostics while performing template argument 23099b623639378d53a675921ddfa7316034d571881eDouglas Gregor // deduction, and if we haven't already instantiated this declaration, 23109b623639378d53a675921ddfa7316034d571881eDouglas Gregor // keep track of these diagnostics. They'll be emitted if this specialization 23119b623639378d53a675921ddfa7316034d571881eDouglas Gregor // is actually used. 23129b623639378d53a675921ddfa7316034d571881eDouglas Gregor if (Info.diag_begin() != Info.diag_end()) { 23139b623639378d53a675921ddfa7316034d571881eDouglas Gregor llvm::DenseMap<Decl *, llvm::SmallVector<PartialDiagnosticAt, 1> >::iterator 23149b623639378d53a675921ddfa7316034d571881eDouglas Gregor Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl()); 23159b623639378d53a675921ddfa7316034d571881eDouglas Gregor if (Pos == SuppressedDiagnostics.end()) 23169b623639378d53a675921ddfa7316034d571881eDouglas Gregor SuppressedDiagnostics[Specialization->getCanonicalDecl()] 23179b623639378d53a675921ddfa7316034d571881eDouglas Gregor .append(Info.diag_begin(), Info.diag_end()); 23189b623639378d53a675921ddfa7316034d571881eDouglas Gregor } 23199b623639378d53a675921ddfa7316034d571881eDouglas Gregor 23201eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return TDK_Success; 232183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor} 232283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 23239c72c6088d591ace8503b842d39448c2040f3033John McCall/// Gets the type of a function for template-argument-deducton 23249c72c6088d591ace8503b842d39448c2040f3033John McCall/// purposes when it's considered as part of an overload set. 2325eff92135d32039c9874dc356f3e93143af6069c1John McCallstatic QualType GetTypeOfFunction(ASTContext &Context, 23269c72c6088d591ace8503b842d39448c2040f3033John McCall const OverloadExpr::FindResult &R, 2327eff92135d32039c9874dc356f3e93143af6069c1John McCall FunctionDecl *Fn) { 2328eff92135d32039c9874dc356f3e93143af6069c1John McCall if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) 23299c72c6088d591ace8503b842d39448c2040f3033John McCall if (Method->isInstance()) { 23309c72c6088d591ace8503b842d39448c2040f3033John McCall // An instance method that's referenced in a form that doesn't 23319c72c6088d591ace8503b842d39448c2040f3033John McCall // look like a member pointer is just invalid. 23329c72c6088d591ace8503b842d39448c2040f3033John McCall if (!R.HasFormOfMemberPointer) return QualType(); 23339c72c6088d591ace8503b842d39448c2040f3033John McCall 2334eff92135d32039c9874dc356f3e93143af6069c1John McCall return Context.getMemberPointerType(Fn->getType(), 2335eff92135d32039c9874dc356f3e93143af6069c1John McCall Context.getTypeDeclType(Method->getParent()).getTypePtr()); 23369c72c6088d591ace8503b842d39448c2040f3033John McCall } 23379c72c6088d591ace8503b842d39448c2040f3033John McCall 23389c72c6088d591ace8503b842d39448c2040f3033John McCall if (!R.IsAddressOfOperand) return Fn->getType(); 2339eff92135d32039c9874dc356f3e93143af6069c1John McCall return Context.getPointerType(Fn->getType()); 2340eff92135d32039c9874dc356f3e93143af6069c1John McCall} 2341eff92135d32039c9874dc356f3e93143af6069c1John McCall 2342eff92135d32039c9874dc356f3e93143af6069c1John McCall/// Apply the deduction rules for overload sets. 2343eff92135d32039c9874dc356f3e93143af6069c1John McCall/// 2344eff92135d32039c9874dc356f3e93143af6069c1John McCall/// \return the null type if this argument should be treated as an 2345eff92135d32039c9874dc356f3e93143af6069c1John McCall/// undeduced context 2346eff92135d32039c9874dc356f3e93143af6069c1John McCallstatic QualType 2347eff92135d32039c9874dc356f3e93143af6069c1John McCallResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams, 234875f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor Expr *Arg, QualType ParamType, 234975f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor bool ParamWasReference) { 23509c72c6088d591ace8503b842d39448c2040f3033John McCall 23519c72c6088d591ace8503b842d39448c2040f3033John McCall OverloadExpr::FindResult R = OverloadExpr::find(Arg); 2352eff92135d32039c9874dc356f3e93143af6069c1John McCall 23539c72c6088d591ace8503b842d39448c2040f3033John McCall OverloadExpr *Ovl = R.Expression; 2354eff92135d32039c9874dc356f3e93143af6069c1John McCall 235575f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor // C++0x [temp.deduct.call]p4 235675f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor unsigned TDF = 0; 235775f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor if (ParamWasReference) 235875f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor TDF |= TDF_ParamWithReferenceType; 235975f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor if (R.IsAddressOfOperand) 236075f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor TDF |= TDF_IgnoreQualifiers; 236175f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor 2362eff92135d32039c9874dc356f3e93143af6069c1John McCall // If there were explicit template arguments, we can only find 2363eff92135d32039c9874dc356f3e93143af6069c1John McCall // something via C++ [temp.arg.explicit]p3, i.e. if the arguments 2364eff92135d32039c9874dc356f3e93143af6069c1John McCall // unambiguously name a full specialization. 23657bb12da2b0749eeebb21854c77877736969e59f2John McCall if (Ovl->hasExplicitTemplateArgs()) { 2366eff92135d32039c9874dc356f3e93143af6069c1John McCall // But we can still look for an explicit specialization. 2367eff92135d32039c9874dc356f3e93143af6069c1John McCall if (FunctionDecl *ExplicitSpec 23687bb12da2b0749eeebb21854c77877736969e59f2John McCall = S.ResolveSingleFunctionTemplateSpecialization(Ovl)) 23699c72c6088d591ace8503b842d39448c2040f3033John McCall return GetTypeOfFunction(S.Context, R, ExplicitSpec); 2370eff92135d32039c9874dc356f3e93143af6069c1John McCall return QualType(); 2371eff92135d32039c9874dc356f3e93143af6069c1John McCall } 2372eff92135d32039c9874dc356f3e93143af6069c1John McCall 2373eff92135d32039c9874dc356f3e93143af6069c1John McCall // C++0x [temp.deduct.call]p6: 2374eff92135d32039c9874dc356f3e93143af6069c1John McCall // When P is a function type, pointer to function type, or pointer 2375eff92135d32039c9874dc356f3e93143af6069c1John McCall // to member function type: 2376eff92135d32039c9874dc356f3e93143af6069c1John McCall 2377eff92135d32039c9874dc356f3e93143af6069c1John McCall if (!ParamType->isFunctionType() && 2378eff92135d32039c9874dc356f3e93143af6069c1John McCall !ParamType->isFunctionPointerType() && 2379eff92135d32039c9874dc356f3e93143af6069c1John McCall !ParamType->isMemberFunctionPointerType()) 2380eff92135d32039c9874dc356f3e93143af6069c1John McCall return QualType(); 2381eff92135d32039c9874dc356f3e93143af6069c1John McCall 2382eff92135d32039c9874dc356f3e93143af6069c1John McCall QualType Match; 23837bb12da2b0749eeebb21854c77877736969e59f2John McCall for (UnresolvedSetIterator I = Ovl->decls_begin(), 23847bb12da2b0749eeebb21854c77877736969e59f2John McCall E = Ovl->decls_end(); I != E; ++I) { 2385eff92135d32039c9874dc356f3e93143af6069c1John McCall NamedDecl *D = (*I)->getUnderlyingDecl(); 2386eff92135d32039c9874dc356f3e93143af6069c1John McCall 2387eff92135d32039c9874dc356f3e93143af6069c1John McCall // - If the argument is an overload set containing one or more 2388eff92135d32039c9874dc356f3e93143af6069c1John McCall // function templates, the parameter is treated as a 2389eff92135d32039c9874dc356f3e93143af6069c1John McCall // non-deduced context. 2390eff92135d32039c9874dc356f3e93143af6069c1John McCall if (isa<FunctionTemplateDecl>(D)) 2391eff92135d32039c9874dc356f3e93143af6069c1John McCall return QualType(); 2392eff92135d32039c9874dc356f3e93143af6069c1John McCall 2393eff92135d32039c9874dc356f3e93143af6069c1John McCall FunctionDecl *Fn = cast<FunctionDecl>(D); 23949c72c6088d591ace8503b842d39448c2040f3033John McCall QualType ArgType = GetTypeOfFunction(S.Context, R, Fn); 23959c72c6088d591ace8503b842d39448c2040f3033John McCall if (ArgType.isNull()) continue; 2396eff92135d32039c9874dc356f3e93143af6069c1John McCall 239775f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor // Function-to-pointer conversion. 239875f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor if (!ParamWasReference && ParamType->isPointerType() && 239975f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor ArgType->isFunctionType()) 240075f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor ArgType = S.Context.getPointerType(ArgType); 240175f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor 2402eff92135d32039c9874dc356f3e93143af6069c1John McCall // - If the argument is an overload set (not containing function 2403eff92135d32039c9874dc356f3e93143af6069c1John McCall // templates), trial argument deduction is attempted using each 2404eff92135d32039c9874dc356f3e93143af6069c1John McCall // of the members of the set. If deduction succeeds for only one 2405eff92135d32039c9874dc356f3e93143af6069c1John McCall // of the overload set members, that member is used as the 2406eff92135d32039c9874dc356f3e93143af6069c1John McCall // argument value for the deduction. If deduction succeeds for 2407eff92135d32039c9874dc356f3e93143af6069c1John McCall // more than one member of the overload set the parameter is 2408eff92135d32039c9874dc356f3e93143af6069c1John McCall // treated as a non-deduced context. 2409eff92135d32039c9874dc356f3e93143af6069c1John McCall 2410eff92135d32039c9874dc356f3e93143af6069c1John McCall // We do all of this in a fresh context per C++0x [temp.deduct.type]p2: 2411eff92135d32039c9874dc356f3e93143af6069c1John McCall // Type deduction is done independently for each P/A pair, and 2412eff92135d32039c9874dc356f3e93143af6069c1John McCall // the deduced template argument values are then combined. 2413eff92135d32039c9874dc356f3e93143af6069c1John McCall // So we do not reject deductions which were made elsewhere. 241402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVector<DeducedTemplateArgument, 8> 241502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor Deduced(TemplateParams->size()); 24162a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall TemplateDeductionInfo Info(S.Context, Ovl->getNameLoc()); 2417eff92135d32039c9874dc356f3e93143af6069c1John McCall Sema::TemplateDeductionResult Result 2418a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = DeduceTemplateArguments(S, TemplateParams, 2419eff92135d32039c9874dc356f3e93143af6069c1John McCall ParamType, ArgType, 2420eff92135d32039c9874dc356f3e93143af6069c1John McCall Info, Deduced, TDF); 2421eff92135d32039c9874dc356f3e93143af6069c1John McCall if (Result) continue; 2422eff92135d32039c9874dc356f3e93143af6069c1John McCall if (!Match.isNull()) return QualType(); 2423eff92135d32039c9874dc356f3e93143af6069c1John McCall Match = ArgType; 2424eff92135d32039c9874dc356f3e93143af6069c1John McCall } 2425eff92135d32039c9874dc356f3e93143af6069c1John McCall 2426eff92135d32039c9874dc356f3e93143af6069c1John McCall return Match; 2427eff92135d32039c9874dc356f3e93143af6069c1John McCall} 2428eff92135d32039c9874dc356f3e93143af6069c1John McCall 2429f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor/// \brief Perform the adjustments to the parameter and argument types 2430f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor/// described in C++ [temp.deduct.call]. 2431f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor/// 2432f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor/// \returns true if the caller should not attempt to perform any template 2433f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor/// argument deduction based on this P/A pair. 2434f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregorstatic bool AdjustFunctionParmAndArgTypesForDeduction(Sema &S, 2435f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor TemplateParameterList *TemplateParams, 2436f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor QualType &ParamType, 2437f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor QualType &ArgType, 2438f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor Expr *Arg, 2439f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor unsigned &TDF) { 2440f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // C++0x [temp.deduct.call]p3: 2441f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // If P is a cv-qualified type, the top level cv-qualifiers of P’s type 2442f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // are ignored for type deduction. 2443f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor if (ParamType.getCVRQualifiers()) 2444f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor ParamType = ParamType.getLocalUnqualifiedType(); 2445f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>(); 2446f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor if (ParamRefType) { 24472ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor // [C++0x] If P is an rvalue reference to a cv-unqualified 24482ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor // template parameter and the argument is an lvalue, the type 24492ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor // "lvalue reference to A" is used in place of A for type 24502ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor // deduction. 24512ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor if (const RValueReferenceType *RValueRef 24522ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor = dyn_cast<RValueReferenceType>(ParamType)) { 24532ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor if (!RValueRef->getPointeeType().getQualifiers() && 24542ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor isa<TemplateTypeParmType>(RValueRef->getPointeeType()) && 24552ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor Arg->Classify(S.Context).isLValue()) 24562ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor ArgType = S.Context.getLValueReferenceType(ArgType); 24572ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor } 24582ad746aeb90e86cea7afaf552a02ae3f3b5ec859Douglas Gregor 2459f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // [...] If P is a reference type, the type referred to by P is used 2460f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // for type deduction. 2461f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor ParamType = ParamRefType->getPointeeType(); 2462f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor } 24635c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor 2464f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // Overload sets usually make this parameter an undeduced 2465f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // context, but there are sometimes special circumstances. 2466f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor if (ArgType == S.Context.OverloadTy) { 2467f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor ArgType = ResolveOverloadForDeduction(S, TemplateParams, 2468f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor Arg, ParamType, 2469f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor ParamRefType != 0); 2470f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor if (ArgType.isNull()) 2471f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor return true; 2472f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor } 2473f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor 2474f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor if (ParamRefType) { 2475f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // C++0x [temp.deduct.call]p3: 2476f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // [...] If P is of the form T&&, where T is a template parameter, and 2477f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // the argument is an lvalue, the type A& is used in place of A for 2478f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // type deduction. 2479f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor if (ParamRefType->isRValueReferenceType() && 2480f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor ParamRefType->getAs<TemplateTypeParmType>() && 2481f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor Arg->isLValue()) 2482f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor ArgType = S.Context.getLValueReferenceType(ArgType); 2483f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor } else { 2484f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // C++ [temp.deduct.call]p2: 2485f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // If P is not a reference type: 2486f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // - If A is an array type, the pointer type produced by the 2487f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // array-to-pointer standard conversion (4.2) is used in place of 2488f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // A for type deduction; otherwise, 2489f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor if (ArgType->isArrayType()) 2490f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor ArgType = S.Context.getArrayDecayedType(ArgType); 2491f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // - If A is a function type, the pointer type produced by the 2492f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // function-to-pointer standard conversion (4.3) is used in place 2493f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // of A for type deduction; otherwise, 2494f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor else if (ArgType->isFunctionType()) 2495f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor ArgType = S.Context.getPointerType(ArgType); 2496f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor else { 2497f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // - If A is a cv-qualified type, the top level cv-qualifiers of A’s 2498f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // type are ignored for type deduction. 2499f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor if (ArgType.getCVRQualifiers()) 2500f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor ArgType = ArgType.getUnqualifiedType(); 2501f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor } 2502f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor } 2503f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor 2504f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // C++0x [temp.deduct.call]p4: 2505f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // In general, the deduction process attempts to find template argument 2506f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // values that will make the deduced A identical to A (after the type A 2507f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // is transformed as described above). [...] 2508f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor TDF = TDF_SkipNonDependent; 2509f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor 2510f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // - If the original P is a reference type, the deduced A (i.e., the 2511f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // type referred to by the reference) can be more cv-qualified than 2512f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // the transformed A. 2513f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor if (ParamRefType) 2514f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor TDF |= TDF_ParamWithReferenceType; 2515f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // - The transformed A can be another pointer or pointer to member 2516f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // type that can be converted to the deduced A via a qualification 2517f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // conversion (4.4). 2518f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor if (ArgType->isPointerType() || ArgType->isMemberPointerType() || 2519f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor ArgType->isObjCObjectPointerType()) 2520f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor TDF |= TDF_IgnoreQualifiers; 2521f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // - If P is a class and P has the form simple-template-id, then the 2522f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // transformed A can be a derived class of the deduced A. Likewise, 2523f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // if P is a pointer to a class of the form simple-template-id, the 2524f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // transformed A can be a pointer to a derived class pointed to by 2525f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // the deduced A. 2526f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor if (isSimpleTemplateIdType(ParamType) || 2527f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor (isa<PointerType>(ParamType) && 2528f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor isSimpleTemplateIdType( 2529f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor ParamType->getAs<PointerType>()->getPointeeType()))) 2530f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor TDF |= TDF_DerivedClass; 2531f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor 2532f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor return false; 2533f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor} 2534f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor 2535e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \brief Perform template argument deduction from a function call 2536e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// (C++ [temp.deduct.call]). 2537e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 2538e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param FunctionTemplate the function template for which we are performing 2539e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// template argument deduction. 2540e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 254148026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// \param ExplicitTemplateArguments the explicit template arguments provided 254248026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// for this call. 25436db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor/// 2544e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Args the function call arguments 2545e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 2546e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param NumArgs the number of arguments in Args 2547e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 254848026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// \param Name the name of the function being called. This is only significant 254948026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// when the function template is a conversion function template, in which 255048026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// case this routine will also perform template argument deduction based on 255148026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// the function to which 255248026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor/// 2553e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Specialization if template argument deduction was successful, 25541eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// this will be set to the function template specialization produced by 2555e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// template argument deduction. 2556e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 2557e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \param Info the argument will be updated to provide additional information 2558e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// about template argument deduction. 2559e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// 2560e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor/// \returns the result of template argument deduction. 2561e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorSema::TemplateDeductionResult 2562e53060fa78ad7e98352049f72787bdb7543e2a48Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, 256348026d26fb58e413544874eead5491b1452e2ebfDouglas Gregor const TemplateArgumentListInfo *ExplicitTemplateArgs, 2564e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor Expr **Args, unsigned NumArgs, 2565e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor FunctionDecl *&Specialization, 2566e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor TemplateDeductionInfo &Info) { 2567e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 25686db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor 2569e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // C++ [temp.deduct.call]p1: 2570e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // Template argument deduction is done by comparing each function template 2571e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // parameter type (call it P) with the type of the corresponding argument 2572e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor // of the call (call it A) as described below. 2573e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor unsigned CheckArgs = NumArgs; 25746db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor if (NumArgs < Function->getMinRequiredArguments()) 2575e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor return TDK_TooFewArguments; 2576e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor else if (NumArgs > Function->getNumParams()) { 25771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const FunctionProtoType *Proto 2578183700f494ec9b6701b6efe82bcb25f4c79ba561John McCall = Function->getType()->getAs<FunctionProtoType>(); 2579f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor if (Proto->isTemplateVariadic()) 2580f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor /* Do nothing */; 2581f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor else if (Proto->isVariadic()) 2582f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor CheckArgs = Function->getNumParams(); 2583f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor else 2584e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor return TDK_TooManyArguments; 2585e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor } 25861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 25876db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor // The types of the parameters from which we will perform template argument 25886db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor // deduction. 25892a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall LocalInstantiationScope InstScope(*this); 2590e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor TemplateParameterList *TemplateParams 2591e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor = FunctionTemplate->getTemplateParameters(); 259202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVector<DeducedTemplateArgument, 4> Deduced; 25936db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor llvm::SmallVector<QualType, 4> ParamTypes; 259402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor unsigned NumExplicitlySpecified = 0; 2595d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall if (ExplicitTemplateArgs) { 259683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateDeductionResult Result = 259783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor SubstituteExplicitTemplateArguments(FunctionTemplate, 2598d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall *ExplicitTemplateArgs, 259983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Deduced, 260083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor ParamTypes, 260183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 0, 260283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Info); 260383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor if (Result) 260483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return Result; 260502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor 260602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor NumExplicitlySpecified = Deduced.size(); 26076db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor } else { 26086db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor // Just fill in the parameter types from the function declaration. 2609f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) 26106db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor ParamTypes.push_back(Function->getParamDecl(I)->getType()); 26116db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor } 26121eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 26136db8ed4498b83fe9336e3855a4ba1a298b04ee00Douglas Gregor // Deduce template arguments from the function parameters. 26141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Deduced.resize(TemplateParams->size()); 2615f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor unsigned ArgIdx = 0; 2616f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor for (unsigned ParamIdx = 0, NumParams = ParamTypes.size(); 2617f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor ParamIdx != NumParams; ++ParamIdx) { 2618f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor QualType ParamType = ParamTypes[ParamIdx]; 2619f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor 2620f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor const PackExpansionType *ParamExpansion 2621f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor = dyn_cast<PackExpansionType>(ParamType); 2622f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor if (!ParamExpansion) { 2623f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // Simple case: matching a function parameter to a function argument. 2624f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor if (ArgIdx >= CheckArgs) 2625f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor break; 2626f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor 2627f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor Expr *Arg = Args[ArgIdx++]; 2628f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor QualType ArgType = Arg->getType(); 2629f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor unsigned TDF = 0; 2630f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams, 2631f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor ParamType, ArgType, Arg, 2632f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor TDF)) 2633f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor continue; 2634f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor 2635f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor if (TemplateDeductionResult Result 2636f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor = ::DeduceTemplateArguments(*this, TemplateParams, 2637f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor ParamType, ArgType, Info, Deduced, 2638f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor TDF)) 2639f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor return Result; 26401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2641f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // FIXME: we need to check that the deduced A is the same as A, 2642f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // modulo the various allowed differences. 2643f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor continue; 264475f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor } 264575f21af57f3dce1577d6c27bbe7bb45b49ced732Douglas Gregor 2646f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // C++0x [temp.deduct.call]p1: 2647f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // For a function parameter pack that occurs at the end of the 2648f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // parameter-declaration-list, the type A of each remaining argument of 2649f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // the call is compared with the type P of the declarator-id of the 2650f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // function parameter pack. Each comparison deduces template arguments 2651f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // for subsequent positions in the template parameter packs expanded by 26527d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor // the function parameter pack. For a function parameter pack that does 26537d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor // not occur at the end of the parameter-declaration-list, the type of 26547d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor // the parameter pack is a non-deduced context. 26557d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor if (ParamIdx + 1 < NumParams) 26567d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor break; 26577d5c0c1273bdc1cb3dff1cb5a62d07b1439e82c7Douglas Gregor 2658f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor QualType ParamPattern = ParamExpansion->getPattern(); 2659f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor llvm::SmallVector<unsigned, 2> PackIndices; 2660f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor { 2661f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor llvm::BitVector SawIndices(TemplateParams->size()); 2662f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor llvm::SmallVector<UnexpandedParameterPack, 2> Unexpanded; 2663f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor collectUnexpandedParameterPacks(ParamPattern, Unexpanded); 2664f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { 2665f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor unsigned Depth, Index; 2666f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor llvm::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); 2667f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor if (Depth == 0 && !SawIndices[Index]) { 2668f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor SawIndices[Index] = true; 2669f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor PackIndices.push_back(Index); 2670f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor } 2671f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor } 2672f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor } 2673f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor assert(!PackIndices.empty() && "Pack expansion without unexpanded packs?"); 2674f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor 2675d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor // Keep track of the deduced template arguments for each parameter pack 2676d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor // expanded by this pack expansion (the outer index) and for each 2677d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor // template argument (the inner SmallVectors). 2678d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor llvm::SmallVector<llvm::SmallVector<DeducedTemplateArgument, 4>, 2> 2679d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor NewlyDeducedPacks(PackIndices.size()); 2680f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor llvm::SmallVector<DeducedTemplateArgument, 2> 2681d3731198193eee92796ddeb493973b7a598b003eDouglas Gregor SavedPacks(PackIndices.size()); 26825429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor PrepareArgumentPackDeduction(*this, Deduced, PackIndices, SavedPacks, 26835429385919a2b6fb3708635b967221705f4b1dafDouglas Gregor NewlyDeducedPacks); 2684f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor bool HasAnyArguments = false; 2685f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor for (; ArgIdx < NumArgs; ++ArgIdx) { 2686f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor HasAnyArguments = true; 2687f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor 2688f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor ParamType = ParamPattern; 2689f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor Expr *Arg = Args[ArgIdx]; 2690f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor QualType ArgType = Arg->getType(); 2691f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor unsigned TDF = 0; 2692f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor if (AdjustFunctionParmAndArgTypesForDeduction(*this, TemplateParams, 2693f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor ParamType, ArgType, Arg, 2694f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor TDF)) { 2695f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // We can't actually perform any deduction for this argument, so stop 2696f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // deduction at this point. 2697f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor ++ArgIdx; 2698f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor break; 2699f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor } 2700f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor 2701f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor if (TemplateDeductionResult Result 2702f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor = ::DeduceTemplateArguments(*this, TemplateParams, 2703f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor ParamType, ArgType, Info, Deduced, 2704f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor TDF)) 2705f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor return Result; 2706eff92135d32039c9874dc356f3e93143af6069c1John McCall 2707f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // Capture the deduced template arguments for each parameter pack expanded 2708f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // by this pack expansion, add them to the list of arguments we've deduced 2709f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // for that pack, then clear out the deduced argument. 2710f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor for (unsigned I = 0, N = PackIndices.size(); I != N; ++I) { 2711f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor DeducedTemplateArgument &DeducedArg = Deduced[PackIndices[I]]; 2712f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor if (!DeducedArg.isNull()) { 2713f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor NewlyDeducedPacks[I].push_back(DeducedArg); 2714f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor DeducedArg = DeducedTemplateArgument(); 2715f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor } 2716e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor } 2717e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor } 2718f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor 2719f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // Build argument packs for each of the parameter packs expanded by this 2720f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // pack expansion. 27210216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor if (Sema::TemplateDeductionResult Result 27220216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor = FinishArgumentPackDeduction(*this, TemplateParams, HasAnyArguments, 27230216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor Deduced, PackIndices, SavedPacks, 27240216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor NewlyDeducedPacks, Info)) 27250216f8121df32b320cab659d5b703fee50cdfda5Douglas Gregor return Result; 27261eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2727f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor // After we've matching against a parameter pack, we're done. 2728f5c65ffbd7374b6c8d9f1e361041578640cab320Douglas Gregor break; 2729e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor } 273065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 27311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump return FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 273202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor NumExplicitlySpecified, 273383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor Specialization, Info); 273483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor} 2735127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor 273683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \brief Deduce template arguments when taking the address of a function 27374b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template (C++ [temp.deduct.funcaddr]) or matching a specialization to 27384b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// a template. 273983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 274083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param FunctionTemplate the function template for which we are performing 274183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template argument deduction. 274283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 27434b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param ExplicitTemplateArguments the explicitly-specified template 27444b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// arguments. 274583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 274683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param ArgFunctionType the function type that will be used as the 274783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// "argument" type (A) when performing template argument deduction from the 27484b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// function template's function type. This type may be NULL, if there is no 27494b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// argument type to compare against, in C++0x [temp.arg.explicit]p3. 275083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 275183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Specialization if template argument deduction was successful, 27521eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// this will be set to the function template specialization produced by 275383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// template argument deduction. 275483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 275583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \param Info the argument will be updated to provide additional information 275683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// about template argument deduction. 275783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// 275883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor/// \returns the result of template argument deduction. 275983314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::TemplateDeductionResult 276083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, 2761d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall const TemplateArgumentListInfo *ExplicitTemplateArgs, 276283314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor QualType ArgFunctionType, 276383314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionDecl *&Specialization, 276483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateDeductionInfo &Info) { 276583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 276683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor TemplateParameterList *TemplateParams 276783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor = FunctionTemplate->getTemplateParameters(); 276883314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor QualType FunctionType = Function->getType(); 27691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 277083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Substitute any explicit template arguments. 27712a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall LocalInstantiationScope InstScope(*this); 277202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVector<DeducedTemplateArgument, 4> Deduced; 277302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor unsigned NumExplicitlySpecified = 0; 277483314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor llvm::SmallVector<QualType, 4> ParamTypes; 2775d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall if (ExplicitTemplateArgs) { 27761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump if (TemplateDeductionResult Result 27771eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump = SubstituteExplicitTemplateArguments(FunctionTemplate, 2778d5532b6cfff2977e0c59fa6ead7f7973984a620dJohn McCall *ExplicitTemplateArgs, 27791eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Deduced, ParamTypes, 278083314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor &FunctionType, Info)) 278183314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor return Result; 278202024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor 278302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor NumExplicitlySpecified = Deduced.size(); 2784127102b5196ffe04bdb70fd553fe62c265ab10a9Douglas Gregor } 278583314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 278683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Template argument deduction for function templates in a SFINAE context. 278783314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor // Trap any errors that might occur. 27881eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump SFINAETrap Trap(*this); 27891eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 2790eff92135d32039c9874dc356f3e93143af6069c1John McCall Deduced.resize(TemplateParams->size()); 2791eff92135d32039c9874dc356f3e93143af6069c1John McCall 27924b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor if (!ArgFunctionType.isNull()) { 27934b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor // Deduce template arguments from the function type. 27944b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor if (TemplateDeductionResult Result 2795a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = ::DeduceTemplateArguments(*this, TemplateParams, 27964b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor FunctionType, ArgFunctionType, Info, 279773b3cf6503f72f054288cf474e1a8c8ae56383c2Douglas Gregor Deduced, TDF_TopLevelParameterTypeList)) 27984b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor return Result; 27994b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor } 2800fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor 2801fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor if (TemplateDeductionResult Result 2802fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 2803fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor NumExplicitlySpecified, 2804fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor Specialization, Info)) 2805fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor return Result; 2806fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor 2807fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor // If the requested function type does not match the actual type of the 2808fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor // specialization, template argument deduction fails. 2809fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor if (!ArgFunctionType.isNull() && 2810fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor !Context.hasSameType(ArgFunctionType, Specialization->getType())) 2811fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor return TDK_NonDeducedMismatch; 2812fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor 2813fbb6fad63acac2bef36cfa13e0706fa3e2a1ed7dDouglas Gregor return TDK_Success; 2814e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor} 2815e53060fa78ad7e98352049f72787bdb7543e2a48Douglas Gregor 281665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \brief Deduce template arguments for a templated conversion 281765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// function (C++ [temp.deduct.conv]) and, if successful, produce a 281865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// conversion function template specialization. 281965ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::TemplateDeductionResult 282065ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, 282165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor QualType ToType, 282265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor CXXConversionDecl *&Specialization, 282365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor TemplateDeductionInfo &Info) { 28241eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump CXXConversionDecl *Conv 282565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor = cast<CXXConversionDecl>(FunctionTemplate->getTemplatedDecl()); 282665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor QualType FromType = Conv->getConversionType(); 282765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 282865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // Canonicalize the types for deduction. 282965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor QualType P = Context.getCanonicalType(FromType); 283065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor QualType A = Context.getCanonicalType(ToType); 283165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 283265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // C++0x [temp.deduct.conv]p3: 283365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // If P is a reference type, the type referred to by P is used for 283465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // type deduction. 283565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (const ReferenceType *PRef = P->getAs<ReferenceType>()) 283665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor P = PRef->getPointeeType(); 283765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 283865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // C++0x [temp.deduct.conv]p3: 283965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // If A is a reference type, the type referred to by A is used 284065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // for type deduction. 284165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (const ReferenceType *ARef = A->getAs<ReferenceType>()) 284265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor A = ARef->getPointeeType(); 284365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // C++ [temp.deduct.conv]p2: 284465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // 28451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // If A is not a reference type: 284665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor else { 284765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor assert(!A->isReferenceType() && "Reference types were handled above"); 284865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 284965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // - If P is an array type, the pointer type produced by the 28501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump // array-to-pointer standard conversion (4.2) is used in place 285165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // of P for type deduction; otherwise, 285265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (P->isArrayType()) 285365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor P = Context.getArrayDecayedType(P); 285465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // - If P is a function type, the pointer type produced by the 285565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // function-to-pointer standard conversion (4.3) is used in 285665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // place of P for type deduction; otherwise, 285765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor else if (P->isFunctionType()) 285865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor P = Context.getPointerType(P); 285965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // - If P is a cv-qualified type, the top level cv-qualifiers of 286065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // P’s type are ignored for type deduction. 286165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor else 286265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor P = P.getUnqualifiedType(); 286365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 286465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // C++0x [temp.deduct.conv]p3: 286565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // If A is a cv-qualified type, the top level cv-qualifiers of A’s 286665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // type are ignored for type deduction. 286765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor A = A.getUnqualifiedType(); 286865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor } 286965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 287065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // Template argument deduction for function templates in a SFINAE context. 287165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // Trap any errors that might occur. 28721eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump SFINAETrap Trap(*this); 287365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 287465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // C++ [temp.deduct.conv]p1: 287565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // Template argument deduction is done by comparing the return 287665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // type of the template conversion function (call it P) with the 287765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // type that is required as the result of the conversion (call it 287865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // A) as described in 14.8.2.4. 287965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor TemplateParameterList *TemplateParams 288065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor = FunctionTemplate->getTemplateParameters(); 288102024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVector<DeducedTemplateArgument, 4> Deduced; 28821eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump Deduced.resize(TemplateParams->size()); 288365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 288465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // C++0x [temp.deduct.conv]p4: 288565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // In general, the deduction process attempts to find template 288665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // argument values that will make the deduced A identical to 288765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // A. However, there are two cases that allow a difference: 288865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor unsigned TDF = 0; 288965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // - If the original A is a reference type, A can be more 289065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // cv-qualified than the deduced A (i.e., the type referred to 289165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // by the reference) 289265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (ToType->isReferenceType()) 289365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor TDF |= TDF_ParamWithReferenceType; 289465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // - The deduced A can be another pointer or pointer to member 289565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // type that can be converted to A via a qualification 289665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // conversion. 289765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // 289865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when 289965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // both P and A are pointers or member pointers. In this case, we 290065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // just ignore cv-qualifiers completely). 290165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if ((P->isPointerType() && A->isPointerType()) || 290265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor (P->isMemberPointerType() && P->isMemberPointerType())) 290365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor TDF |= TDF_IgnoreQualifiers; 290465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (TemplateDeductionResult Result 2905a7ef13024e4cc3dfb75e3bc1695371b39d9a5240Chandler Carruth = ::DeduceTemplateArguments(*this, TemplateParams, 290665ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor P, A, Info, Deduced, TDF)) 290765ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor return Result; 290865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 290965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // FIXME: we need to check that the deduced A is the same as A, 291065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // modulo the various allowed differences. 29111eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 291265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor // Finish template argument deduction. 29132a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall LocalInstantiationScope InstScope(*this); 291465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor FunctionDecl *Spec = 0; 291565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor TemplateDeductionResult Result 291602024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 0, Spec, 291702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor Info); 291865ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor Specialization = cast_or_null<CXXConversionDecl>(Spec); 291965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor return Result; 292065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor} 292165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor 29224b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \brief Deduce template arguments for a function template when there is 29234b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// nothing to deduce against (C++0x [temp.arg.explicit]p3). 29244b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// 29254b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param FunctionTemplate the function template for which we are performing 29264b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template argument deduction. 29274b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// 29284b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param ExplicitTemplateArguments the explicitly-specified template 29294b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// arguments. 29304b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// 29314b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param Specialization if template argument deduction was successful, 29324b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// this will be set to the function template specialization produced by 29334b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// template argument deduction. 29344b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// 29354b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \param Info the argument will be updated to provide additional information 29364b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// about template argument deduction. 29374b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// 29384b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor/// \returns the result of template argument deduction. 29394b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas GregorSema::TemplateDeductionResult 29404b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas GregorSema::DeduceTemplateArguments(FunctionTemplateDecl *FunctionTemplate, 29414b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor const TemplateArgumentListInfo *ExplicitTemplateArgs, 29424b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor FunctionDecl *&Specialization, 29434b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor TemplateDeductionInfo &Info) { 29444b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, 29454b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor QualType(), Specialization, Info); 29464b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor} 29474b52e25f3b05ab0f9d2492276a52323a50a84fb7Douglas Gregor 29488a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic void 2949e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, QualType T, 2950e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor bool OnlyDeduced, 2951ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor unsigned Level, 2952e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor llvm::SmallVectorImpl<bool> &Deduced); 295377bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor 295477bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor/// \brief If this is a non-static member function, 295577bc572138543ef97ebec137522b763d6a6ee909Douglas Gregorstatic void MaybeAddImplicitObjectParameterType(ASTContext &Context, 295677bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor CXXMethodDecl *Method, 295777bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor llvm::SmallVectorImpl<QualType> &ArgTypes) { 295877bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor if (Method->isStatic()) 295977bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor return; 296077bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor 296177bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor // C++ [over.match.funcs]p4: 296277bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor // 296377bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor // For non-static member functions, the type of the implicit 296477bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor // object parameter is 296577bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor // — "lvalue reference to cv X" for functions declared without a 296677bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor // ref-qualifier or with the & ref-qualifier 296777bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor // - "rvalue reference to cv X" for functions declared with the 296877bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor // && ref-qualifier 296977bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor // 297077bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor // FIXME: We don't have ref-qualifiers yet, so we don't do that part. 297177bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor QualType ArgTy = Context.getTypeDeclType(Method->getParent()); 297277bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor ArgTy = Context.getQualifiedType(ArgTy, 297377bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor Qualifiers::fromCVRMask(Method->getTypeQualifiers())); 297477bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor ArgTy = Context.getLValueReferenceType(ArgTy); 297577bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor ArgTypes.push_back(ArgTy); 297677bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor} 297777bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor 29788a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \brief Determine whether the function template \p FT1 is at least as 29798a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// specialized as \p FT2. 29808a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregorstatic bool isAtLeastAsSpecializedAs(Sema &S, 29815769d6195087229770d7ac90449443e026c47103John McCall SourceLocation Loc, 29828a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor FunctionTemplateDecl *FT1, 29838a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor FunctionTemplateDecl *FT2, 29848a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor TemplatePartialOrderingContext TPOC, 2985b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor unsigned NumCallArguments, 2986b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor llvm::SmallVectorImpl<RefParamPartialOrderingComparison> *RefParamComparisons) { 29878a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor FunctionDecl *FD1 = FT1->getTemplatedDecl(); 29888a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor FunctionDecl *FD2 = FT2->getTemplatedDecl(); 29898a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); 29908a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); 29918a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 29928a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor assert(Proto1 && Proto2 && "Function templates must have prototypes"); 29938a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); 299402024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVector<DeducedTemplateArgument, 4> Deduced; 29958a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Deduced.resize(TemplateParams->size()); 29968a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 29978a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p3: 29988a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // The types used to determine the ordering depend on the context in which 29998a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // the partial ordering is done: 30002a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall TemplateDeductionInfo Info(S.Context, Loc); 30018d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor CXXMethodDecl *Method1 = 0; 30028d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor CXXMethodDecl *Method2 = 0; 30038d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor bool IsNonStatic2 = false; 30048d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor bool IsNonStatic1 = false; 30058d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor unsigned Skip2 = 0; 30068a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor switch (TPOC) { 30078a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case TPOC_Call: { 30088a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // - In the context of a function call, the function parameter types are 30098a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // used. 30108d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor Method1 = dyn_cast<CXXMethodDecl>(FD1); 30118d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor Method2 = dyn_cast<CXXMethodDecl>(FD2); 30128d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor IsNonStatic1 = Method1 && !Method1->isStatic(); 30138d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor IsNonStatic2 = Method2 && !Method2->isStatic(); 30148d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor 30158d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor // C++0x [temp.func.order]p3: 30168d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor // [...] If only one of the function templates is a non-static 30178d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor // member, that function template is considered to have a new 30188d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor // first parameter inserted in its function parameter list. The 30198d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor // new parameter is of type "reference to cv A," where cv are 30208d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor // the cv-qualifiers of the function template (if any) and A is 30218d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor // the class of which the function template is a member. 30228d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor // 30238d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor // C++98/03 doesn't have this provision, so instead we drop the 30248d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor // first argument of the free function or static member, which 30258d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor // seems to match existing practice. 302677bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor llvm::SmallVector<QualType, 4> Args1; 30278d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor unsigned Skip1 = !S.getLangOptions().CPlusPlus0x && 30288d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor IsNonStatic2 && !IsNonStatic1; 30298d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor if (S.getLangOptions().CPlusPlus0x && IsNonStatic1 && !IsNonStatic2) 30305c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor MaybeAddImplicitObjectParameterType(S.Context, Method1, Args1); 303177bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor Args1.insert(Args1.end(), 30328d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor Proto1->arg_type_begin() + Skip1, Proto1->arg_type_end()); 303377bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor 303477bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor llvm::SmallVector<QualType, 4> Args2; 30358d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor Skip2 = !S.getLangOptions().CPlusPlus0x && 30368d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor IsNonStatic1 && !IsNonStatic2; 30378d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor if (S.getLangOptions().CPlusPlus0x && IsNonStatic2 && !IsNonStatic1) 303877bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor MaybeAddImplicitObjectParameterType(S.Context, Method2, Args2); 303977bc572138543ef97ebec137522b763d6a6ee909Douglas Gregor Args2.insert(Args2.end(), 30408d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor Proto2->arg_type_begin() + Skip2, Proto2->arg_type_end()); 30415c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor 30425c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // C++ [temp.func.order]p5: 30435c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // The presence of unused ellipsis and default arguments has no effect on 30445c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // the partial ordering of function templates. 30455c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor if (Args1.size() > NumCallArguments) 30465c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor Args1.resize(NumCallArguments); 30475c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor if (Args2.size() > NumCallArguments) 30485c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor Args2.resize(NumCallArguments); 30495c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(), 30505c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor Args1.data(), Args1.size(), Info, Deduced, 30515c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor TDF_None, /*PartialOrdering=*/true, 3052b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor RefParamComparisons)) 30538a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return false; 30548a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 30558a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 30568a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 30578a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 30588a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case TPOC_Conversion: 30598a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // - In the context of a call to a conversion operator, the return types 30608a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // of the conversion function templates are used. 30615c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor if (DeduceTemplateArguments(S, TemplateParams, Proto2->getResultType(), 30625c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor Proto1->getResultType(), Info, Deduced, 30635c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor TDF_None, /*PartialOrdering=*/true, 3064b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor RefParamComparisons)) 30658a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return false; 30668a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 30678a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 30688a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case TPOC_Other: 30698a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // - In other contexts (14.6.6.2) the function template’s function type 30708a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // is used. 30715c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // FIXME: Don't we actually want to perform the adjustments on the parameter 30725c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor // types? 30735c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor if (DeduceTemplateArguments(S, TemplateParams, FD2->getType(), 30745c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor FD1->getType(), Info, Deduced, TDF_None, 3075b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor /*PartialOrdering=*/true, RefParamComparisons)) 30768a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return false; 30778a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 30788a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 30798a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 30808a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p11: 30818a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // In most cases, all template parameters must have values in order for 30828a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // deduction to succeed, but for partial ordering purposes a template 30838a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // parameter may remain without a value provided it is not used in the 30848a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // types being used for partial ordering. [ Note: a template parameter used 30858a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // in a non-deduced context is considered used. -end note] 30868a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor unsigned ArgIdx = 0, NumArgs = Deduced.size(); 30878a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor for (; ArgIdx != NumArgs; ++ArgIdx) 30888a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (Deduced[ArgIdx].isNull()) 30898a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 30908a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 30918a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (ArgIdx == NumArgs) { 30928a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // All template arguments were deduced. FT1 is at least as specialized 30938a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // as FT2. 30948a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return true; 30958a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 30968a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 3097e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor // Figure out which template parameters were used. 30988a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor llvm::SmallVector<bool, 4> UsedParameters; 30998a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor UsedParameters.resize(TemplateParams->size()); 31008a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor switch (TPOC) { 31018a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case TPOC_Call: { 31025c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor unsigned NumParams = std::min(NumCallArguments, 31035c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor std::min(Proto1->getNumArgs(), 31045c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor Proto2->getNumArgs())); 31058d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor if (S.getLangOptions().CPlusPlus0x && IsNonStatic2 && !IsNonStatic1) 31068d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor ::MarkUsedTemplateParameters(S, Method2->getThisType(S.Context), false, 31078d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor TemplateParams->getDepth(), UsedParameters); 31088d706ecf488c4f548c4f8cf0aa08ea82a4d6a5baDouglas Gregor for (unsigned I = Skip2; I < NumParams; ++I) 3109ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor ::MarkUsedTemplateParameters(S, Proto2->getArgType(I), false, 3110ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor TemplateParams->getDepth(), 3111e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor UsedParameters); 31128a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 31138a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 31148a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 31158a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case TPOC_Conversion: 3116ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor ::MarkUsedTemplateParameters(S, Proto2->getResultType(), false, 3117ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor TemplateParams->getDepth(), 3118e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor UsedParameters); 31198a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 31208a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 31218a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor case TPOC_Other: 3122ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor ::MarkUsedTemplateParameters(S, FD2->getType(), false, 3123ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor TemplateParams->getDepth(), 3124ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor UsedParameters); 31258a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor break; 31268a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 31278a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 31288a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor for (; ArgIdx != NumArgs; ++ArgIdx) 31298a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // If this argument had no value deduced but was used in one of the types 31308a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // used for partial ordering, then deduction fails. 31318a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) 31328a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return false; 31338a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 31348a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return true; 31358a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor} 31368a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 31379da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor/// \brief Determine whether this a function template whose parameter-type-list 31389da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor/// ends with a function parameter pack. 31399da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregorstatic bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) { 31409da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor FunctionDecl *Function = FunTmpl->getTemplatedDecl(); 31419da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor unsigned NumParams = Function->getNumParams(); 31429da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor if (NumParams == 0) 31439da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor return false; 31449da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor 31459da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor ParmVarDecl *Last = Function->getParamDecl(NumParams - 1); 31469da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor if (!Last->isParameterPack()) 31479da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor return false; 31489da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor 31499da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor // Make sure that no previous parameter is a parameter pack. 31509da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor while (--NumParams > 0) { 31519da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor if (Function->getParamDecl(NumParams - 1)->isParameterPack()) 31529da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor return false; 31539da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor } 31549da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor 31559da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor return true; 31569da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor} 31578a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 3158bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \brief Returns the more specialized function template according 315965ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// to the rules of function template partial ordering (C++ [temp.func.order]). 316065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// 316165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \param FT1 the first function template 316265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// 316365ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// \param FT2 the second function template 316465ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// 31658a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// \param TPOC the context in which we are performing partial ordering of 31668a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor/// function templates. 31671eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump/// 31685c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// \param NumCallArguments The number of arguments in a call, used only 31695c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// when \c TPOC is \c TPOC_Call. 31705c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// 3171bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \returns the more specialized function template. If neither 317265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor/// template is more specialized, returns NULL. 317365ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorFunctionTemplateDecl * 317465ec1fda479688d143fe2403242cd9c730c800a1Douglas GregorSema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, 317565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor FunctionTemplateDecl *FT2, 31765769d6195087229770d7ac90449443e026c47103John McCall SourceLocation Loc, 31775c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor TemplatePartialOrderingContext TPOC, 31785c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor unsigned NumCallArguments) { 3179b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor llvm::SmallVector<RefParamPartialOrderingComparison, 4> RefParamComparisons; 31805c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, 31815c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor NumCallArguments, 0); 31825769d6195087229770d7ac90449443e026c47103John McCall bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC, 31835c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor NumCallArguments, 3184b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor &RefParamComparisons); 31858a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 31868a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (Better1 != Better2) // We have a clear winner 31878a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return Better1? FT1 : FT2; 31888a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 31898a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor if (!Better1 && !Better2) // Neither is better than the other 319065ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor return 0; 31918a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 31928a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p10: 31938a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // If for each type being considered a given template is at least as 31948a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // specialized for all types and more specialized for some set of types and 31958a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // the other template is not more specialized for any types or is not at 31968a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // least as specialized for any types, then the given template is more 31978a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // specialized than the other template. Otherwise, neither template is more 31988a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // specialized than the other. 31998a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Better1 = false; 32008a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor Better2 = false; 3201b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor for (unsigned I = 0, N = RefParamComparisons.size(); I != N; ++I) { 32028a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // C++0x [temp.deduct.partial]p9: 32038a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor // If, for a given type, deduction succeeds in both directions (i.e., the 3204b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor // types are identical after the transformations above) and both P and A 3205b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor // were reference types (before being replaced with the type referred to 3206b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor // above): 3207b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor 3208b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor // -- if the type from the argument template was an lvalue reference 3209b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor // and the type from the parameter template was not, the argument 3210b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor // type is considered to be more specialized than the other; 3211b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor // otherwise, 3212b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor if (!RefParamComparisons[I].ArgIsRvalueRef && 3213b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor RefParamComparisons[I].ParamIsRvalueRef) { 3214b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor Better2 = true; 3215b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor if (Better1) 3216b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor return 0; 3217b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor continue; 3218b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor } else if (!RefParamComparisons[I].ParamIsRvalueRef && 3219b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor RefParamComparisons[I].ArgIsRvalueRef) { 3220b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor Better1 = true; 3221b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor if (Better2) 3222b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor return 0; 3223b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor continue; 3224b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor } 3225b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor 3226b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor // -- if the type from the argument template is more cv-qualified than 3227b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor // the type from the parameter template (as described above), the 3228b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor // argument type is considered to be more specialized than the 3229b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor // other; otherwise, 3230b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor switch (RefParamComparisons[I].Qualifiers) { 3231b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor case NeitherMoreQualified: 3232b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor break; 3233b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor 3234b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor case ParamMoreQualified: 3235b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor Better1 = true; 3236b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor if (Better2) 3237b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor return 0; 3238b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor continue; 3239b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor 3240b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor case ArgMoreQualified: 3241b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor Better2 = true; 3242b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor if (Better1) 3243b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor return 0; 3244b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor continue; 32458a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 3246b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor 3247b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor // -- neither type is more specialized than the other. 32488a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor } 32498a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor 32508a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor assert(!(Better1 && Better2) && "Should have broken out in the loop above"); 325165ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor if (Better1) 325265ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor return FT1; 32538a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor else if (Better2) 32548a51491d936d8c50480f3c3ca6647be12a7ad51fDouglas Gregor return FT2; 32559da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor 32569da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor // FIXME: This mimics what GCC implements, but doesn't match up with the 32579da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor // proposed resolution for core issue 692. This area needs to be sorted out, 32589da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor // but for now we attempt to maintain compatibility. 32599da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor bool Variadic1 = isVariadicFunctionTemplate(FT1); 32609da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor bool Variadic2 = isVariadicFunctionTemplate(FT2); 32619da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor if (Variadic1 != Variadic2) 32629da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor return Variadic1? FT2 : FT1; 32639da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor 32649da95e6eefc4b0ca25e18bdab1b703f5c185deabDouglas Gregor return 0; 326565ec1fda479688d143fe2403242cd9c730c800a1Douglas Gregor} 326683314aa1cf61ed2458a8a20c83b2d4708192d5dcDouglas Gregor 3267d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \brief Determine if the two templates are equivalent. 3268d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregorstatic bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) { 3269d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor if (T1 == T2) 3270d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor return true; 3271d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 3272d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor if (!T1 || !T2) 3273d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor return false; 3274d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 3275d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor return T1->getCanonicalDecl() == T2->getCanonicalDecl(); 3276d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor} 3277d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 3278d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \brief Retrieve the most specialized of the given function template 3279d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specializations. 3280d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 3281c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// \param SpecBegin the start iterator of the function template 3282c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// specializations that we will be comparing. 3283d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 3284c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// \param SpecEnd the end iterator of the function template 3285c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// specializations, paired with \p SpecBegin. 3286d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 3287d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param TPOC the partial ordering context to use to compare the function 3288d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// template specializations. 3289d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 32905c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// \param NumCallArguments The number of arguments in a call, used only 32915c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// when \c TPOC is \c TPOC_Call. 32925c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor/// 3293d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param Loc the location where the ambiguity or no-specializations 3294d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// diagnostic should occur. 3295d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 3296d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param NoneDiag partial diagnostic used to diagnose cases where there are 3297d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// no matching candidates. 3298d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 3299d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one 3300d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// occurs. 3301d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 3302d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param CandidateDiag partial diagnostic used for each function template 3303d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specialization that is a candidate in the ambiguous ordering. One parameter 3304d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// in this diagnostic should be unbound, which will correspond to the string 3305d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// describing the template arguments for the function template specialization. 3306d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 3307d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \param Index if non-NULL and the result of this function is non-nULL, 3308d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// receives the index corresponding to the resulting function template 3309d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// specialization. 3310d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 3311d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \returns the most specialized function template specialization, if 3312c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall/// found. Otherwise, returns SpecEnd. 3313d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// 3314d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// \todo FIXME: Consider passing in the "also-ran" candidates that failed 3315d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor/// template argument deduction. 3316c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallUnresolvedSetIterator 3317c373d48502ca7683ab55385f5bd624d778eb288dJohn McCallSema::getMostSpecialized(UnresolvedSetIterator SpecBegin, 33185c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor UnresolvedSetIterator SpecEnd, 3319c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall TemplatePartialOrderingContext TPOC, 33205c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor unsigned NumCallArguments, 3321c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall SourceLocation Loc, 3322c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall const PartialDiagnostic &NoneDiag, 3323c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall const PartialDiagnostic &AmbigDiag, 3324c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall const PartialDiagnostic &CandidateDiag) { 3325c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall if (SpecBegin == SpecEnd) { 3326d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor Diag(Loc, NoneDiag); 3327c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall return SpecEnd; 3328d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor } 3329d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 3330c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall if (SpecBegin + 1 == SpecEnd) 3331c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall return SpecBegin; 3332d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 3333d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor // Find the function template that is better than all of the templates it 3334d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor // has been compared to. 3335c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall UnresolvedSetIterator Best = SpecBegin; 3336d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor FunctionTemplateDecl *BestTemplate 3337c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall = cast<FunctionDecl>(*Best)->getPrimaryTemplate(); 3338d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor assert(BestTemplate && "Not a function template specialization?"); 3339c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) { 3340c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall FunctionTemplateDecl *Challenger 3341c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall = cast<FunctionDecl>(*I)->getPrimaryTemplate(); 3342d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor assert(Challenger && "Not a function template specialization?"); 3343c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, 33445c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor Loc, TPOC, NumCallArguments), 3345d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor Challenger)) { 3346d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor Best = I; 3347d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor BestTemplate = Challenger; 3348d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor } 3349d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor } 3350d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 3351d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor // Make sure that the "best" function template is more specialized than all 3352d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor // of the others. 3353d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor bool Ambiguous = false; 3354c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { 3355c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall FunctionTemplateDecl *Challenger 3356c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall = cast<FunctionDecl>(*I)->getPrimaryTemplate(); 3357d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor if (I != Best && 3358d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, 33595c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor Loc, TPOC, NumCallArguments), 3360d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor BestTemplate)) { 3361d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor Ambiguous = true; 3362d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor break; 3363d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor } 3364d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor } 3365d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 3366d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor if (!Ambiguous) { 3367d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor // We found an answer. Return it. 3368c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall return Best; 3369d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor } 3370d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 3371d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor // Diagnose the ambiguity. 3372d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor Diag(Loc, AmbigDiag); 3373d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 3374d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor // FIXME: Can we order the candidates in some sane way? 3375c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) 3376c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall Diag((*I)->getLocation(), CandidateDiag) 3377d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor << getTemplateArgumentBindingsText( 3378c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall cast<FunctionDecl>(*I)->getPrimaryTemplate()->getTemplateParameters(), 3379c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall *cast<FunctionDecl>(*I)->getTemplateSpecializationArgs()); 3380d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 3381c373d48502ca7683ab55385f5bd624d778eb288dJohn McCall return SpecEnd; 3382d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor} 3383d5a423b279e787e9fdd8309fe52cb515388c54eaDouglas Gregor 3384bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \brief Returns the more specialized class template partial specialization 3385bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// according to the rules of partial ordering of class template partial 3386bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// specializations (C++ [temp.class.order]). 3387bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// 3388bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \param PS1 the first class template partial specialization 3389bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// 3390bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \param PS2 the second class template partial specialization 3391bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// 3392bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// \returns the more specialized class template partial specialization. If 3393bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor/// neither partial specialization is more specialized, returns NULL. 3394bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas GregorClassTemplatePartialSpecializationDecl * 3395bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas GregorSema::getMoreSpecializedPartialSpecialization( 3396bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor ClassTemplatePartialSpecializationDecl *PS1, 33975769d6195087229770d7ac90449443e026c47103John McCall ClassTemplatePartialSpecializationDecl *PS2, 33985769d6195087229770d7ac90449443e026c47103John McCall SourceLocation Loc) { 3399bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // C++ [temp.class.order]p1: 3400bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // For two class template partial specializations, the first is at least as 3401bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // specialized as the second if, given the following rewrite to two 3402bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // function templates, the first function template is at least as 3403bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // specialized as the second according to the ordering rules for function 3404bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // templates (14.6.6.2): 3405bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // - the first function template has the same template parameters as the 3406bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // first partial specialization and has a single function parameter 3407bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // whose type is a class template specialization with the template 3408bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // arguments of the first partial specialization, and 3409bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // - the second function template has the same template parameters as the 3410bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // second partial specialization and has a single function parameter 3411bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // whose type is a class template specialization with the template 3412bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // arguments of the second partial specialization. 3413bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // 341431dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor // Rather than synthesize function templates, we merely perform the 341531dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor // equivalent partial ordering by performing deduction directly on 341631dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor // the template arguments of the class template partial 341731dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor // specializations. This computation is slightly simpler than the 341831dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor // general problem of function template partial ordering, because 341931dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor // class template partial specializations are more constrained. We 342031dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor // know that every template parameter is deducible from the class 342131dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor // template partial specialization's template arguments, for 342231dce8f606c5ff4a51db67caa24ac3312ccea3f3Douglas Gregor // example. 342302024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVector<DeducedTemplateArgument, 4> Deduced; 34242a7fb27913999d132cf9e10e03dc5271faa2e9d3John McCall TemplateDeductionInfo Info(Context, Loc); 342531f17ecbef57b5679c017c375db330546b7b5145John McCall 342631f17ecbef57b5679c017c375db330546b7b5145John McCall QualType PT1 = PS1->getInjectedSpecializationType(); 342731f17ecbef57b5679c017c375db330546b7b5145John McCall QualType PT2 = PS2->getInjectedSpecializationType(); 3428bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor 3429bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // Determine whether PS1 is at least as specialized as PS2 3430bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor Deduced.resize(PS2->getTemplateParameters()->size()); 34315c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor bool Better1 = !::DeduceTemplateArguments(*this, PS2->getTemplateParameters(), 34325c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor PT2, PT1, Info, Deduced, TDF_None, 34335c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor /*PartialOrdering=*/true, 3434b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor /*RefParamComparisons=*/0); 34352c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis if (Better1) { 34362c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis InstantiatingTemplate Inst(*this, PS2->getLocation(), PS2, 34372c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis Deduced.data(), Deduced.size(), Info); 3438516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor Better1 = !::FinishTemplateArgumentDeduction(*this, PS2, 3439516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor PS1->getTemplateArgs(), 3440516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor Deduced, Info); 34412c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis } 3442516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor 3443bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor // Determine whether PS2 is at least as specialized as PS1 3444db0d4b751e83b8841b8f48f913f17e50467f13d4Douglas Gregor Deduced.clear(); 3445bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor Deduced.resize(PS1->getTemplateParameters()->size()); 34465c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor bool Better2 = !::DeduceTemplateArguments(*this, PS1->getTemplateParameters(), 34475c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor PT1, PT2, Info, Deduced, TDF_None, 34485c7bf42ef16dc767615bed10f3b7b3c1265314e1Douglas Gregor /*PartialOrdering=*/true, 3449b939a1987318f802fd25f89e15ae7d2423161cacDouglas Gregor /*RefParamComparisons=*/0); 34502c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis if (Better2) { 34512c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis InstantiatingTemplate Inst(*this, PS1->getLocation(), PS1, 34522c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis Deduced.data(), Deduced.size(), Info); 3453516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor Better2 = !::FinishTemplateArgumentDeduction(*this, PS1, 3454516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor PS2->getTemplateArgs(), 3455516e6e09821a61e8975c787e189949723249e7c5Douglas Gregor Deduced, Info); 34562c4792c731690dbbcbe69dd0625977adffc2961aArgyrios Kyrtzidis } 3457bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor 3458bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor if (Better1 == Better2) 3459bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor return 0; 3460bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor 3461bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor return Better1? PS1 : PS2; 3462bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor} 3463bf4ea56cdc376cef5a12abf6bf18dc34805c2226Douglas Gregor 34641eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void 3465e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, 3466e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor const TemplateArgument &TemplateArg, 3467e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor bool OnlyDeduced, 3468ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor unsigned Depth, 3469e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor llvm::SmallVectorImpl<bool> &Used); 3470031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 3471e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given 3472031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// expression. 34731eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void 3474e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, 3475e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor const Expr *E, 3476e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor bool OnlyDeduced, 3477ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor unsigned Depth, 3478e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor llvm::SmallVectorImpl<bool> &Used) { 3479be230c36e32142cbdcdbe9c97511d097beeecbabDouglas Gregor // We can deduce from a pack expansion. 3480be230c36e32142cbdcdbe9c97511d097beeecbabDouglas Gregor if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E)) 3481be230c36e32142cbdcdbe9c97511d097beeecbabDouglas Gregor E = Expansion->getPattern(); 3482be230c36e32142cbdcdbe9c97511d097beeecbabDouglas Gregor 348354c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor // Skip through any implicit casts we added while type-checking. 348454c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) 348554c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor E = ICE->getSubExpr(); 348654c53cca105ed595e12fecf04e415c3712bda936Douglas Gregor 3487e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to 3488e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor // find other occurrences of template parameters. 3489f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); 3490c781f9cd854f3d5d1c826f4a13382c6abca4cff7Douglas Gregor if (!DRE) 3491031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor return; 3492031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 34931eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const NonTypeTemplateParmDecl *NTTP 3494031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); 3495031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor if (!NTTP) 3496031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor return; 3497031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 3498ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor if (NTTP->getDepth() == Depth) 3499ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Used[NTTP->getIndex()] = true; 3500031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor} 3501031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 3502e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given 3503e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// nested name specifier. 3504e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregorstatic void 3505e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, 3506e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor NestedNameSpecifier *NNS, 3507e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor bool OnlyDeduced, 3508ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor unsigned Depth, 3509e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor llvm::SmallVectorImpl<bool> &Used) { 3510e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor if (!NNS) 3511e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor return; 3512e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor 3513ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor MarkUsedTemplateParameters(SemaRef, NNS->getPrefix(), OnlyDeduced, Depth, 3514ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Used); 3515e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, QualType(NNS->getAsType(), 0), 3516ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor OnlyDeduced, Depth, Used); 3517e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor} 3518e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor 3519e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given 3520e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// template name. 3521e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregorstatic void 3522e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, 3523e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor TemplateName Name, 3524e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor bool OnlyDeduced, 3525ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor unsigned Depth, 3526e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor llvm::SmallVectorImpl<bool> &Used) { 3527e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor if (TemplateDecl *Template = Name.getAsTemplateDecl()) { 3528e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor if (TemplateTemplateParmDecl *TTP 3529ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor = dyn_cast<TemplateTemplateParmDecl>(Template)) { 3530ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor if (TTP->getDepth() == Depth) 3531ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Used[TTP->getIndex()] = true; 3532ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor } 3533e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor return; 3534e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor } 3535e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor 3536788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) 3537788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor MarkUsedTemplateParameters(SemaRef, QTN->getQualifier(), OnlyDeduced, 3538788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor Depth, Used); 3539e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) 3540ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor MarkUsedTemplateParameters(SemaRef, DTN->getQualifier(), OnlyDeduced, 3541ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, Used); 3542e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor} 3543e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor 3544e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by the given 3545031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// type. 35461eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void 3547e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, QualType T, 3548e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor bool OnlyDeduced, 3549ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor unsigned Depth, 3550e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor llvm::SmallVectorImpl<bool> &Used) { 3551e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor if (T.isNull()) 3552e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor return; 3553e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor 3554031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor // Non-dependent types have nothing deducible 3555031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor if (!T->isDependentType()) 3556031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor return; 3557031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 3558031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor T = SemaRef.Context.getCanonicalType(T); 3559031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor switch (T->getTypeClass()) { 3560031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::Pointer: 3561e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, 3562e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor cast<PointerType>(T)->getPointeeType(), 3563e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor OnlyDeduced, 3564ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, 3565e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor Used); 3566031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 3567031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 3568031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::BlockPointer: 3569e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, 3570e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor cast<BlockPointerType>(T)->getPointeeType(), 3571e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor OnlyDeduced, 3572ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, 3573e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor Used); 3574031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 3575031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 3576031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::LValueReference: 3577031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::RValueReference: 3578e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, 3579e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor cast<ReferenceType>(T)->getPointeeType(), 3580e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor OnlyDeduced, 3581ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, 3582e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor Used); 3583031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 3584031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 3585031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::MemberPointer: { 3586031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); 3587e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, MemPtr->getPointeeType(), OnlyDeduced, 3588ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, Used); 3589e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, QualType(MemPtr->getClass(), 0), 3590ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor OnlyDeduced, Depth, Used); 3591031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 3592031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor } 3593031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 3594031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::DependentSizedArray: 3595e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, 3596e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor cast<DependentSizedArrayType>(T)->getSizeExpr(), 3597ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor OnlyDeduced, Depth, Used); 3598031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor // Fall through to check the element type 3599031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 3600031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::ConstantArray: 3601031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::IncompleteArray: 3602e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, 3603e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor cast<ArrayType>(T)->getElementType(), 3604ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor OnlyDeduced, Depth, Used); 3605031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 3606031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 3607031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::Vector: 3608031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::ExtVector: 3609e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, 3610e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor cast<VectorType>(T)->getElementType(), 3611ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor OnlyDeduced, Depth, Used); 3612031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 3613031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 36149cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor case Type::DependentSizedExtVector: { 36159cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor const DependentSizedExtVectorType *VecType 3616f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor = cast<DependentSizedExtVectorType>(T); 3617e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, VecType->getElementType(), OnlyDeduced, 3618ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, Used); 3619e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, VecType->getSizeExpr(), OnlyDeduced, 3620ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, Used); 36219cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor break; 36229cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor } 36239cdda0cf8528e3d595be9bfa002f0450074beb4dDouglas Gregor 3624031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::FunctionProto: { 3625f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor const FunctionProtoType *Proto = cast<FunctionProtoType>(T); 3626e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, Proto->getResultType(), OnlyDeduced, 3627ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, Used); 3628031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor for (unsigned I = 0, N = Proto->getNumArgs(); I != N; ++I) 3629e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, Proto->getArgType(I), OnlyDeduced, 3630ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, Used); 3631031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 3632031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor } 3633031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 3634ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor case Type::TemplateTypeParm: { 3635ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T); 3636ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor if (TTP->getDepth() == Depth) 3637ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Used[TTP->getIndex()] = true; 3638031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 3639ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor } 3640031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 36410bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor case Type::SubstTemplateTypeParmPack: { 36420bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor const SubstTemplateTypeParmPackType *Subst 36430bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor = cast<SubstTemplateTypeParmPackType>(T); 36440bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor MarkUsedTemplateParameters(SemaRef, 36450bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor QualType(Subst->getReplacedParameter(), 0), 36460bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor OnlyDeduced, Depth, Used); 36470bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor MarkUsedTemplateParameters(SemaRef, Subst->getArgumentPack(), 36480bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor OnlyDeduced, Depth, Used); 36490bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor break; 36500bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor } 36510bc15d92bf98cd01e7904d7fca9895dacc237618Douglas Gregor 365231f17ecbef57b5679c017c375db330546b7b5145John McCall case Type::InjectedClassName: 365331f17ecbef57b5679c017c375db330546b7b5145John McCall T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType(); 365431f17ecbef57b5679c017c375db330546b7b5145John McCall // fall through 365531f17ecbef57b5679c017c375db330546b7b5145John McCall 3656031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::TemplateSpecialization: { 36571eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump const TemplateSpecializationType *Spec 3658f6ddb737cb882ffbf0b75a9abd50b930cc2b9068Douglas Gregor = cast<TemplateSpecializationType>(T); 3659e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, Spec->getTemplateName(), OnlyDeduced, 3660ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, Used); 36617b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor 36627b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor // C++0x [temp.deduct.type]p9: 36637b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor // If the template argument list of P contains a pack expansion that is not 36647b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor // the last template argument, the entire template argument list is a 36657b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor // non-deduced context. 36667b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor if (OnlyDeduced && 36677b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs())) 36687b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor break; 36697b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor 3670e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) 3671ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth, 3672ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Used); 3673e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor break; 3674e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor } 36751eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3676e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor case Type::Complex: 3677e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor if (!OnlyDeduced) 3678e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, 3679e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor cast<ComplexType>(T)->getElementType(), 3680ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor OnlyDeduced, Depth, Used); 3681e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor break; 3682031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 36834714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor case Type::DependentName: 3684e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor if (!OnlyDeduced) 3685e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, 36864714c12a1ab759156b78be8f109ea4c12213af57Douglas Gregor cast<DependentNameType>(T)->getQualifier(), 3687ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor OnlyDeduced, Depth, Used); 3688031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 3689031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 369033500955d731c73717af52088b7fc0e7a85681e7John McCall case Type::DependentTemplateSpecialization: { 369133500955d731c73717af52088b7fc0e7a85681e7John McCall const DependentTemplateSpecializationType *Spec 369233500955d731c73717af52088b7fc0e7a85681e7John McCall = cast<DependentTemplateSpecializationType>(T); 369333500955d731c73717af52088b7fc0e7a85681e7John McCall if (!OnlyDeduced) 369433500955d731c73717af52088b7fc0e7a85681e7John McCall MarkUsedTemplateParameters(SemaRef, Spec->getQualifier(), 369533500955d731c73717af52088b7fc0e7a85681e7John McCall OnlyDeduced, Depth, Used); 36967b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor 36977b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor // C++0x [temp.deduct.type]p9: 36987b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor // If the template argument list of P contains a pack expansion that is not 36997b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor // the last template argument, the entire template argument list is a 37007b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor // non-deduced context. 37017b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor if (OnlyDeduced && 37027b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor hasPackExpansionBeforeEnd(Spec->getArgs(), Spec->getNumArgs())) 37037b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor break; 37047b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor 370533500955d731c73717af52088b7fc0e7a85681e7John McCall for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) 370633500955d731c73717af52088b7fc0e7a85681e7John McCall MarkUsedTemplateParameters(SemaRef, Spec->getArg(I), OnlyDeduced, Depth, 370733500955d731c73717af52088b7fc0e7a85681e7John McCall Used); 370833500955d731c73717af52088b7fc0e7a85681e7John McCall break; 370933500955d731c73717af52088b7fc0e7a85681e7John McCall } 371033500955d731c73717af52088b7fc0e7a85681e7John McCall 3711ad5e73887052193afda72db8efcb812bd083a4a8John McCall case Type::TypeOf: 3712ad5e73887052193afda72db8efcb812bd083a4a8John McCall if (!OnlyDeduced) 3713ad5e73887052193afda72db8efcb812bd083a4a8John McCall MarkUsedTemplateParameters(SemaRef, 3714ad5e73887052193afda72db8efcb812bd083a4a8John McCall cast<TypeOfType>(T)->getUnderlyingType(), 3715ad5e73887052193afda72db8efcb812bd083a4a8John McCall OnlyDeduced, Depth, Used); 3716ad5e73887052193afda72db8efcb812bd083a4a8John McCall break; 3717ad5e73887052193afda72db8efcb812bd083a4a8John McCall 3718ad5e73887052193afda72db8efcb812bd083a4a8John McCall case Type::TypeOfExpr: 3719ad5e73887052193afda72db8efcb812bd083a4a8John McCall if (!OnlyDeduced) 3720ad5e73887052193afda72db8efcb812bd083a4a8John McCall MarkUsedTemplateParameters(SemaRef, 3721ad5e73887052193afda72db8efcb812bd083a4a8John McCall cast<TypeOfExprType>(T)->getUnderlyingExpr(), 3722ad5e73887052193afda72db8efcb812bd083a4a8John McCall OnlyDeduced, Depth, Used); 3723ad5e73887052193afda72db8efcb812bd083a4a8John McCall break; 3724ad5e73887052193afda72db8efcb812bd083a4a8John McCall 3725ad5e73887052193afda72db8efcb812bd083a4a8John McCall case Type::Decltype: 3726ad5e73887052193afda72db8efcb812bd083a4a8John McCall if (!OnlyDeduced) 3727ad5e73887052193afda72db8efcb812bd083a4a8John McCall MarkUsedTemplateParameters(SemaRef, 3728ad5e73887052193afda72db8efcb812bd083a4a8John McCall cast<DecltypeType>(T)->getUnderlyingExpr(), 3729ad5e73887052193afda72db8efcb812bd083a4a8John McCall OnlyDeduced, Depth, Used); 3730ad5e73887052193afda72db8efcb812bd083a4a8John McCall break; 3731ad5e73887052193afda72db8efcb812bd083a4a8John McCall 37327536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor case Type::PackExpansion: 37337536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor MarkUsedTemplateParameters(SemaRef, 37347536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor cast<PackExpansionType>(T)->getPattern(), 37357536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor OnlyDeduced, Depth, Used); 37367536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor break; 37377536dd5e6c99584481b7dab68b7e7d8df9c54054Douglas Gregor 3738e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor // None of these types have any template parameters in them. 3739031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::Builtin: 3740031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::VariableArray: 3741031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::FunctionNoProto: 3742031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::Record: 3743031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::Enum: 3744031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case Type::ObjCInterface: 3745c12c5bba6ceb6acd4e51e7a0fc03257da9cfd44eJohn McCall case Type::ObjCObject: 3746d1b3c2dd5bc1f3103bee6137957aa7c5f8f2f0bcSteve Naroff case Type::ObjCObjectPointer: 3747ed97649e9574b9d854fa4d6109c9333ae0993554John McCall case Type::UnresolvedUsing: 3748031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define TYPE(Class, Base) 3749031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define ABSTRACT_TYPE(Class, Base) 3750031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define DEPENDENT_TYPE(Class, Base) 3751031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 3752031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor#include "clang/AST/TypeNodes.def" 3753031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 3754031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor } 3755031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor} 3756031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 3757e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor/// \brief Mark the template parameters that are used by this 3758031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// template argument. 37591eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpstatic void 3760e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorMarkUsedTemplateParameters(Sema &SemaRef, 3761e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor const TemplateArgument &TemplateArg, 3762e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor bool OnlyDeduced, 3763ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor unsigned Depth, 3764e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor llvm::SmallVectorImpl<bool> &Used) { 3765031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor switch (TemplateArg.getKind()) { 3766031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case TemplateArgument::Null: 3767031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case TemplateArgument::Integral: 3768788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor case TemplateArgument::Declaration: 3769031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 37701eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump 3771031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case TemplateArgument::Type: 3772e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsType(), OnlyDeduced, 3773ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, Used); 3774031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 3775031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 3776788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor case TemplateArgument::Template: 3777a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor case TemplateArgument::TemplateExpansion: 3778a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor MarkUsedTemplateParameters(SemaRef, 3779a7fc901a2e39bfe55bfcff5934b2d9fdf9656491Douglas Gregor TemplateArg.getAsTemplateOrTemplatePattern(), 3780788cd06cf8e868a67158aafec5de3a1f408d14f3Douglas Gregor OnlyDeduced, Depth, Used); 3781031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 3782031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 3783031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor case TemplateArgument::Expression: 3784e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor MarkUsedTemplateParameters(SemaRef, TemplateArg.getAsExpr(), OnlyDeduced, 3785ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, Used); 3786031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor break; 3787e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor 3788d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson case TemplateArgument::Pack: 3789e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor for (TemplateArgument::pack_iterator P = TemplateArg.pack_begin(), 3790e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor PEnd = TemplateArg.pack_end(); 3791e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor P != PEnd; ++P) 3792ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor MarkUsedTemplateParameters(SemaRef, *P, OnlyDeduced, Depth, Used); 3793d01b1da213aeb71fd40ff7fb78a194613cc1ece7Anders Carlsson break; 3794031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor } 3795031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor} 3796031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor 3797031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \brief Mark the template parameters can be deduced by the given 3798031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// template argument list. 3799031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// 3800031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \param TemplateArgs the template argument list from which template 3801031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// parameters will be deduced. 3802031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// 3803031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// \param Deduced a bit vector whose elements will be set to \c true 3804031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// to indicate when the corresponding template parameter will be 3805031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor/// deduced. 38061eb4433ac451dc16f4133a88af2d002ac26c58efMike Stumpvoid 3807e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas GregorSema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, 3808ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor bool OnlyDeduced, unsigned Depth, 3809e73bb60de3c7c60453a86e097fc428d1cd367a42Douglas Gregor llvm::SmallVectorImpl<bool> &Used) { 38107b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor // C++0x [temp.deduct.type]p9: 38117b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor // If the template argument list of P contains a pack expansion that is not 38127b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor // the last template argument, the entire template argument list is a 38137b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor // non-deduced context. 38147b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor if (OnlyDeduced && 38157b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor hasPackExpansionBeforeEnd(TemplateArgs.data(), TemplateArgs.size())) 38167b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor return; 38177b976ece336d209977b25b5c28ee09c2d2146e6aDouglas Gregor 3818031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) 3819ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor ::MarkUsedTemplateParameters(*this, TemplateArgs[I], OnlyDeduced, 3820ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor Depth, Used); 3821031a5880e19d06624551aed9d74594356f4f9db1Douglas Gregor} 382263f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor 382363f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor/// \brief Marks all of the template parameters that will be deduced by a 382463f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor/// call to the given function template. 382502024a9f0d8e6c898de276193af604c42ee41269Douglas Gregorvoid 382602024a9f0d8e6c898de276193af604c42ee41269Douglas GregorSema::MarkDeducedTemplateParameters(FunctionTemplateDecl *FunctionTemplate, 382702024a9f0d8e6c898de276193af604c42ee41269Douglas Gregor llvm::SmallVectorImpl<bool> &Deduced) { 382863f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor TemplateParameterList *TemplateParams 382963f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor = FunctionTemplate->getTemplateParameters(); 383063f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor Deduced.clear(); 383163f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor Deduced.resize(TemplateParams->size()); 383263f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor 383363f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 383463f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) 383563f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor ::MarkUsedTemplateParameters(*this, Function->getParamDecl(I)->getType(), 3836ed9c0f90b7e0811c209b95e39fe07c211c531285Douglas Gregor true, TemplateParams->getDepth(), Deduced); 383763f07c55d58951574afe9bbb9f7cb3f92eecdd9bDouglas Gregor} 3838