1*0b57cec5SDimitry Andric //===- SemaTemplateDeduction.cpp - Template Argument Deduction ------------===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This file implements C++ template argument deduction. 10*0b57cec5SDimitry Andric // 11*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 12*0b57cec5SDimitry Andric 13*0b57cec5SDimitry Andric #include "clang/Sema/TemplateDeduction.h" 14*0b57cec5SDimitry Andric #include "TreeTransform.h" 15*0b57cec5SDimitry Andric #include "TypeLocBuilder.h" 16*0b57cec5SDimitry Andric #include "clang/AST/ASTContext.h" 17*0b57cec5SDimitry Andric #include "clang/AST/ASTLambda.h" 18*0b57cec5SDimitry Andric #include "clang/AST/Decl.h" 19*0b57cec5SDimitry Andric #include "clang/AST/DeclAccessPair.h" 20*0b57cec5SDimitry Andric #include "clang/AST/DeclBase.h" 21*0b57cec5SDimitry Andric #include "clang/AST/DeclCXX.h" 22*0b57cec5SDimitry Andric #include "clang/AST/DeclTemplate.h" 23*0b57cec5SDimitry Andric #include "clang/AST/DeclarationName.h" 24*0b57cec5SDimitry Andric #include "clang/AST/Expr.h" 25*0b57cec5SDimitry Andric #include "clang/AST/ExprCXX.h" 26*0b57cec5SDimitry Andric #include "clang/AST/NestedNameSpecifier.h" 27*0b57cec5SDimitry Andric #include "clang/AST/TemplateBase.h" 28*0b57cec5SDimitry Andric #include "clang/AST/TemplateName.h" 29*0b57cec5SDimitry Andric #include "clang/AST/Type.h" 30*0b57cec5SDimitry Andric #include "clang/AST/TypeLoc.h" 31*0b57cec5SDimitry Andric #include "clang/AST/UnresolvedSet.h" 32*0b57cec5SDimitry Andric #include "clang/Basic/AddressSpaces.h" 33*0b57cec5SDimitry Andric #include "clang/Basic/ExceptionSpecificationType.h" 34*0b57cec5SDimitry Andric #include "clang/Basic/LLVM.h" 35*0b57cec5SDimitry Andric #include "clang/Basic/LangOptions.h" 36*0b57cec5SDimitry Andric #include "clang/Basic/PartialDiagnostic.h" 37*0b57cec5SDimitry Andric #include "clang/Basic/SourceLocation.h" 38*0b57cec5SDimitry Andric #include "clang/Basic/Specifiers.h" 39*0b57cec5SDimitry Andric #include "clang/Sema/Ownership.h" 40*0b57cec5SDimitry Andric #include "clang/Sema/Sema.h" 41*0b57cec5SDimitry Andric #include "clang/Sema/Template.h" 42*0b57cec5SDimitry Andric #include "llvm/ADT/APInt.h" 43*0b57cec5SDimitry Andric #include "llvm/ADT/APSInt.h" 44*0b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h" 45*0b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h" 46*0b57cec5SDimitry Andric #include "llvm/ADT/FoldingSet.h" 47*0b57cec5SDimitry Andric #include "llvm/ADT/Optional.h" 48*0b57cec5SDimitry Andric #include "llvm/ADT/SmallBitVector.h" 49*0b57cec5SDimitry Andric #include "llvm/ADT/SmallPtrSet.h" 50*0b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 51*0b57cec5SDimitry Andric #include "llvm/Support/Casting.h" 52*0b57cec5SDimitry Andric #include "llvm/Support/Compiler.h" 53*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h" 54*0b57cec5SDimitry Andric #include <algorithm> 55*0b57cec5SDimitry Andric #include <cassert> 56*0b57cec5SDimitry Andric #include <tuple> 57*0b57cec5SDimitry Andric #include <utility> 58*0b57cec5SDimitry Andric 59*0b57cec5SDimitry Andric namespace clang { 60*0b57cec5SDimitry Andric 61*0b57cec5SDimitry Andric /// Various flags that control template argument deduction. 62*0b57cec5SDimitry Andric /// 63*0b57cec5SDimitry Andric /// These flags can be bitwise-OR'd together. 64*0b57cec5SDimitry Andric enum TemplateDeductionFlags { 65*0b57cec5SDimitry Andric /// No template argument deduction flags, which indicates the 66*0b57cec5SDimitry Andric /// strictest results for template argument deduction (as used for, e.g., 67*0b57cec5SDimitry Andric /// matching class template partial specializations). 68*0b57cec5SDimitry Andric TDF_None = 0, 69*0b57cec5SDimitry Andric 70*0b57cec5SDimitry Andric /// Within template argument deduction from a function call, we are 71*0b57cec5SDimitry Andric /// matching with a parameter type for which the original parameter was 72*0b57cec5SDimitry Andric /// a reference. 73*0b57cec5SDimitry Andric TDF_ParamWithReferenceType = 0x1, 74*0b57cec5SDimitry Andric 75*0b57cec5SDimitry Andric /// Within template argument deduction from a function call, we 76*0b57cec5SDimitry Andric /// are matching in a case where we ignore cv-qualifiers. 77*0b57cec5SDimitry Andric TDF_IgnoreQualifiers = 0x02, 78*0b57cec5SDimitry Andric 79*0b57cec5SDimitry Andric /// Within template argument deduction from a function call, 80*0b57cec5SDimitry Andric /// we are matching in a case where we can perform template argument 81*0b57cec5SDimitry Andric /// deduction from a template-id of a derived class of the argument type. 82*0b57cec5SDimitry Andric TDF_DerivedClass = 0x04, 83*0b57cec5SDimitry Andric 84*0b57cec5SDimitry Andric /// Allow non-dependent types to differ, e.g., when performing 85*0b57cec5SDimitry Andric /// template argument deduction from a function call where conversions 86*0b57cec5SDimitry Andric /// may apply. 87*0b57cec5SDimitry Andric TDF_SkipNonDependent = 0x08, 88*0b57cec5SDimitry Andric 89*0b57cec5SDimitry Andric /// Whether we are performing template argument deduction for 90*0b57cec5SDimitry Andric /// parameters and arguments in a top-level template argument 91*0b57cec5SDimitry Andric TDF_TopLevelParameterTypeList = 0x10, 92*0b57cec5SDimitry Andric 93*0b57cec5SDimitry Andric /// Within template argument deduction from overload resolution per 94*0b57cec5SDimitry Andric /// C++ [over.over] allow matching function types that are compatible in 95*0b57cec5SDimitry Andric /// terms of noreturn and default calling convention adjustments, or 96*0b57cec5SDimitry Andric /// similarly matching a declared template specialization against a 97*0b57cec5SDimitry Andric /// possible template, per C++ [temp.deduct.decl]. In either case, permit 98*0b57cec5SDimitry Andric /// deduction where the parameter is a function type that can be converted 99*0b57cec5SDimitry Andric /// to the argument type. 100*0b57cec5SDimitry Andric TDF_AllowCompatibleFunctionType = 0x20, 101*0b57cec5SDimitry Andric 102*0b57cec5SDimitry Andric /// Within template argument deduction for a conversion function, we are 103*0b57cec5SDimitry Andric /// matching with an argument type for which the original argument was 104*0b57cec5SDimitry Andric /// a reference. 105*0b57cec5SDimitry Andric TDF_ArgWithReferenceType = 0x40, 106*0b57cec5SDimitry Andric }; 107*0b57cec5SDimitry Andric } 108*0b57cec5SDimitry Andric 109*0b57cec5SDimitry Andric using namespace clang; 110*0b57cec5SDimitry Andric using namespace sema; 111*0b57cec5SDimitry Andric 112*0b57cec5SDimitry Andric /// Compare two APSInts, extending and switching the sign as 113*0b57cec5SDimitry Andric /// necessary to compare their values regardless of underlying type. 114*0b57cec5SDimitry Andric static bool hasSameExtendedValue(llvm::APSInt X, llvm::APSInt Y) { 115*0b57cec5SDimitry Andric if (Y.getBitWidth() > X.getBitWidth()) 116*0b57cec5SDimitry Andric X = X.extend(Y.getBitWidth()); 117*0b57cec5SDimitry Andric else if (Y.getBitWidth() < X.getBitWidth()) 118*0b57cec5SDimitry Andric Y = Y.extend(X.getBitWidth()); 119*0b57cec5SDimitry Andric 120*0b57cec5SDimitry Andric // If there is a signedness mismatch, correct it. 121*0b57cec5SDimitry Andric if (X.isSigned() != Y.isSigned()) { 122*0b57cec5SDimitry Andric // If the signed value is negative, then the values cannot be the same. 123*0b57cec5SDimitry Andric if ((Y.isSigned() && Y.isNegative()) || (X.isSigned() && X.isNegative())) 124*0b57cec5SDimitry Andric return false; 125*0b57cec5SDimitry Andric 126*0b57cec5SDimitry Andric Y.setIsSigned(true); 127*0b57cec5SDimitry Andric X.setIsSigned(true); 128*0b57cec5SDimitry Andric } 129*0b57cec5SDimitry Andric 130*0b57cec5SDimitry Andric return X == Y; 131*0b57cec5SDimitry Andric } 132*0b57cec5SDimitry Andric 133*0b57cec5SDimitry Andric static Sema::TemplateDeductionResult 134*0b57cec5SDimitry Andric DeduceTemplateArguments(Sema &S, 135*0b57cec5SDimitry Andric TemplateParameterList *TemplateParams, 136*0b57cec5SDimitry Andric const TemplateArgument &Param, 137*0b57cec5SDimitry Andric TemplateArgument Arg, 138*0b57cec5SDimitry Andric TemplateDeductionInfo &Info, 139*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced); 140*0b57cec5SDimitry Andric 141*0b57cec5SDimitry Andric static Sema::TemplateDeductionResult 142*0b57cec5SDimitry Andric DeduceTemplateArgumentsByTypeMatch(Sema &S, 143*0b57cec5SDimitry Andric TemplateParameterList *TemplateParams, 144*0b57cec5SDimitry Andric QualType Param, 145*0b57cec5SDimitry Andric QualType Arg, 146*0b57cec5SDimitry Andric TemplateDeductionInfo &Info, 147*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> & 148*0b57cec5SDimitry Andric Deduced, 149*0b57cec5SDimitry Andric unsigned TDF, 150*0b57cec5SDimitry Andric bool PartialOrdering = false, 151*0b57cec5SDimitry Andric bool DeducedFromArrayBound = false); 152*0b57cec5SDimitry Andric 153*0b57cec5SDimitry Andric static Sema::TemplateDeductionResult 154*0b57cec5SDimitry Andric DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, 155*0b57cec5SDimitry Andric ArrayRef<TemplateArgument> Params, 156*0b57cec5SDimitry Andric ArrayRef<TemplateArgument> Args, 157*0b57cec5SDimitry Andric TemplateDeductionInfo &Info, 158*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 159*0b57cec5SDimitry Andric bool NumberOfArgumentsMustMatch); 160*0b57cec5SDimitry Andric 161*0b57cec5SDimitry Andric static void MarkUsedTemplateParameters(ASTContext &Ctx, 162*0b57cec5SDimitry Andric const TemplateArgument &TemplateArg, 163*0b57cec5SDimitry Andric bool OnlyDeduced, unsigned Depth, 164*0b57cec5SDimitry Andric llvm::SmallBitVector &Used); 165*0b57cec5SDimitry Andric 166*0b57cec5SDimitry Andric static void MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, 167*0b57cec5SDimitry Andric bool OnlyDeduced, unsigned Level, 168*0b57cec5SDimitry Andric llvm::SmallBitVector &Deduced); 169*0b57cec5SDimitry Andric 170*0b57cec5SDimitry Andric /// If the given expression is of a form that permits the deduction 171*0b57cec5SDimitry Andric /// of a non-type template parameter, return the declaration of that 172*0b57cec5SDimitry Andric /// non-type template parameter. 173*0b57cec5SDimitry Andric static NonTypeTemplateParmDecl * 174*0b57cec5SDimitry Andric getDeducedParameterFromExpr(TemplateDeductionInfo &Info, Expr *E) { 175*0b57cec5SDimitry Andric // If we are within an alias template, the expression may have undergone 176*0b57cec5SDimitry Andric // any number of parameter substitutions already. 177*0b57cec5SDimitry Andric while (true) { 178*0b57cec5SDimitry Andric if (ImplicitCastExpr *IC = dyn_cast<ImplicitCastExpr>(E)) 179*0b57cec5SDimitry Andric E = IC->getSubExpr(); 180*0b57cec5SDimitry Andric else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(E)) 181*0b57cec5SDimitry Andric E = CE->getSubExpr(); 182*0b57cec5SDimitry Andric else if (SubstNonTypeTemplateParmExpr *Subst = 183*0b57cec5SDimitry Andric dyn_cast<SubstNonTypeTemplateParmExpr>(E)) 184*0b57cec5SDimitry Andric E = Subst->getReplacement(); 185*0b57cec5SDimitry Andric else 186*0b57cec5SDimitry Andric break; 187*0b57cec5SDimitry Andric } 188*0b57cec5SDimitry Andric 189*0b57cec5SDimitry Andric if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) 190*0b57cec5SDimitry Andric if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl())) 191*0b57cec5SDimitry Andric if (NTTP->getDepth() == Info.getDeducedDepth()) 192*0b57cec5SDimitry Andric return NTTP; 193*0b57cec5SDimitry Andric 194*0b57cec5SDimitry Andric return nullptr; 195*0b57cec5SDimitry Andric } 196*0b57cec5SDimitry Andric 197*0b57cec5SDimitry Andric /// Determine whether two declaration pointers refer to the same 198*0b57cec5SDimitry Andric /// declaration. 199*0b57cec5SDimitry Andric static bool isSameDeclaration(Decl *X, Decl *Y) { 200*0b57cec5SDimitry Andric if (NamedDecl *NX = dyn_cast<NamedDecl>(X)) 201*0b57cec5SDimitry Andric X = NX->getUnderlyingDecl(); 202*0b57cec5SDimitry Andric if (NamedDecl *NY = dyn_cast<NamedDecl>(Y)) 203*0b57cec5SDimitry Andric Y = NY->getUnderlyingDecl(); 204*0b57cec5SDimitry Andric 205*0b57cec5SDimitry Andric return X->getCanonicalDecl() == Y->getCanonicalDecl(); 206*0b57cec5SDimitry Andric } 207*0b57cec5SDimitry Andric 208*0b57cec5SDimitry Andric /// Verify that the given, deduced template arguments are compatible. 209*0b57cec5SDimitry Andric /// 210*0b57cec5SDimitry Andric /// \returns The deduced template argument, or a NULL template argument if 211*0b57cec5SDimitry Andric /// the deduced template arguments were incompatible. 212*0b57cec5SDimitry Andric static DeducedTemplateArgument 213*0b57cec5SDimitry Andric checkDeducedTemplateArguments(ASTContext &Context, 214*0b57cec5SDimitry Andric const DeducedTemplateArgument &X, 215*0b57cec5SDimitry Andric const DeducedTemplateArgument &Y) { 216*0b57cec5SDimitry Andric // We have no deduction for one or both of the arguments; they're compatible. 217*0b57cec5SDimitry Andric if (X.isNull()) 218*0b57cec5SDimitry Andric return Y; 219*0b57cec5SDimitry Andric if (Y.isNull()) 220*0b57cec5SDimitry Andric return X; 221*0b57cec5SDimitry Andric 222*0b57cec5SDimitry Andric // If we have two non-type template argument values deduced for the same 223*0b57cec5SDimitry Andric // parameter, they must both match the type of the parameter, and thus must 224*0b57cec5SDimitry Andric // match each other's type. As we're only keeping one of them, we must check 225*0b57cec5SDimitry Andric // for that now. The exception is that if either was deduced from an array 226*0b57cec5SDimitry Andric // bound, the type is permitted to differ. 227*0b57cec5SDimitry Andric if (!X.wasDeducedFromArrayBound() && !Y.wasDeducedFromArrayBound()) { 228*0b57cec5SDimitry Andric QualType XType = X.getNonTypeTemplateArgumentType(); 229*0b57cec5SDimitry Andric if (!XType.isNull()) { 230*0b57cec5SDimitry Andric QualType YType = Y.getNonTypeTemplateArgumentType(); 231*0b57cec5SDimitry Andric if (YType.isNull() || !Context.hasSameType(XType, YType)) 232*0b57cec5SDimitry Andric return DeducedTemplateArgument(); 233*0b57cec5SDimitry Andric } 234*0b57cec5SDimitry Andric } 235*0b57cec5SDimitry Andric 236*0b57cec5SDimitry Andric switch (X.getKind()) { 237*0b57cec5SDimitry Andric case TemplateArgument::Null: 238*0b57cec5SDimitry Andric llvm_unreachable("Non-deduced template arguments handled above"); 239*0b57cec5SDimitry Andric 240*0b57cec5SDimitry Andric case TemplateArgument::Type: 241*0b57cec5SDimitry Andric // If two template type arguments have the same type, they're compatible. 242*0b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::Type && 243*0b57cec5SDimitry Andric Context.hasSameType(X.getAsType(), Y.getAsType())) 244*0b57cec5SDimitry Andric return X; 245*0b57cec5SDimitry Andric 246*0b57cec5SDimitry Andric // If one of the two arguments was deduced from an array bound, the other 247*0b57cec5SDimitry Andric // supersedes it. 248*0b57cec5SDimitry Andric if (X.wasDeducedFromArrayBound() != Y.wasDeducedFromArrayBound()) 249*0b57cec5SDimitry Andric return X.wasDeducedFromArrayBound() ? Y : X; 250*0b57cec5SDimitry Andric 251*0b57cec5SDimitry Andric // The arguments are not compatible. 252*0b57cec5SDimitry Andric return DeducedTemplateArgument(); 253*0b57cec5SDimitry Andric 254*0b57cec5SDimitry Andric case TemplateArgument::Integral: 255*0b57cec5SDimitry Andric // If we deduced a constant in one case and either a dependent expression or 256*0b57cec5SDimitry Andric // declaration in another case, keep the integral constant. 257*0b57cec5SDimitry Andric // If both are integral constants with the same value, keep that value. 258*0b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::Expression || 259*0b57cec5SDimitry Andric Y.getKind() == TemplateArgument::Declaration || 260*0b57cec5SDimitry Andric (Y.getKind() == TemplateArgument::Integral && 261*0b57cec5SDimitry Andric hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()))) 262*0b57cec5SDimitry Andric return X.wasDeducedFromArrayBound() ? Y : X; 263*0b57cec5SDimitry Andric 264*0b57cec5SDimitry Andric // All other combinations are incompatible. 265*0b57cec5SDimitry Andric return DeducedTemplateArgument(); 266*0b57cec5SDimitry Andric 267*0b57cec5SDimitry Andric case TemplateArgument::Template: 268*0b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::Template && 269*0b57cec5SDimitry Andric Context.hasSameTemplateName(X.getAsTemplate(), Y.getAsTemplate())) 270*0b57cec5SDimitry Andric return X; 271*0b57cec5SDimitry Andric 272*0b57cec5SDimitry Andric // All other combinations are incompatible. 273*0b57cec5SDimitry Andric return DeducedTemplateArgument(); 274*0b57cec5SDimitry Andric 275*0b57cec5SDimitry Andric case TemplateArgument::TemplateExpansion: 276*0b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::TemplateExpansion && 277*0b57cec5SDimitry Andric Context.hasSameTemplateName(X.getAsTemplateOrTemplatePattern(), 278*0b57cec5SDimitry Andric Y.getAsTemplateOrTemplatePattern())) 279*0b57cec5SDimitry Andric return X; 280*0b57cec5SDimitry Andric 281*0b57cec5SDimitry Andric // All other combinations are incompatible. 282*0b57cec5SDimitry Andric return DeducedTemplateArgument(); 283*0b57cec5SDimitry Andric 284*0b57cec5SDimitry Andric case TemplateArgument::Expression: { 285*0b57cec5SDimitry Andric if (Y.getKind() != TemplateArgument::Expression) 286*0b57cec5SDimitry Andric return checkDeducedTemplateArguments(Context, Y, X); 287*0b57cec5SDimitry Andric 288*0b57cec5SDimitry Andric // Compare the expressions for equality 289*0b57cec5SDimitry Andric llvm::FoldingSetNodeID ID1, ID2; 290*0b57cec5SDimitry Andric X.getAsExpr()->Profile(ID1, Context, true); 291*0b57cec5SDimitry Andric Y.getAsExpr()->Profile(ID2, Context, true); 292*0b57cec5SDimitry Andric if (ID1 == ID2) 293*0b57cec5SDimitry Andric return X.wasDeducedFromArrayBound() ? Y : X; 294*0b57cec5SDimitry Andric 295*0b57cec5SDimitry Andric // Differing dependent expressions are incompatible. 296*0b57cec5SDimitry Andric return DeducedTemplateArgument(); 297*0b57cec5SDimitry Andric } 298*0b57cec5SDimitry Andric 299*0b57cec5SDimitry Andric case TemplateArgument::Declaration: 300*0b57cec5SDimitry Andric assert(!X.wasDeducedFromArrayBound()); 301*0b57cec5SDimitry Andric 302*0b57cec5SDimitry Andric // If we deduced a declaration and a dependent expression, keep the 303*0b57cec5SDimitry Andric // declaration. 304*0b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::Expression) 305*0b57cec5SDimitry Andric return X; 306*0b57cec5SDimitry Andric 307*0b57cec5SDimitry Andric // If we deduced a declaration and an integral constant, keep the 308*0b57cec5SDimitry Andric // integral constant and whichever type did not come from an array 309*0b57cec5SDimitry Andric // bound. 310*0b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::Integral) { 311*0b57cec5SDimitry Andric if (Y.wasDeducedFromArrayBound()) 312*0b57cec5SDimitry Andric return TemplateArgument(Context, Y.getAsIntegral(), 313*0b57cec5SDimitry Andric X.getParamTypeForDecl()); 314*0b57cec5SDimitry Andric return Y; 315*0b57cec5SDimitry Andric } 316*0b57cec5SDimitry Andric 317*0b57cec5SDimitry Andric // If we deduced two declarations, make sure that they refer to the 318*0b57cec5SDimitry Andric // same declaration. 319*0b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::Declaration && 320*0b57cec5SDimitry Andric isSameDeclaration(X.getAsDecl(), Y.getAsDecl())) 321*0b57cec5SDimitry Andric return X; 322*0b57cec5SDimitry Andric 323*0b57cec5SDimitry Andric // All other combinations are incompatible. 324*0b57cec5SDimitry Andric return DeducedTemplateArgument(); 325*0b57cec5SDimitry Andric 326*0b57cec5SDimitry Andric case TemplateArgument::NullPtr: 327*0b57cec5SDimitry Andric // If we deduced a null pointer and a dependent expression, keep the 328*0b57cec5SDimitry Andric // null pointer. 329*0b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::Expression) 330*0b57cec5SDimitry Andric return X; 331*0b57cec5SDimitry Andric 332*0b57cec5SDimitry Andric // If we deduced a null pointer and an integral constant, keep the 333*0b57cec5SDimitry Andric // integral constant. 334*0b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::Integral) 335*0b57cec5SDimitry Andric return Y; 336*0b57cec5SDimitry Andric 337*0b57cec5SDimitry Andric // If we deduced two null pointers, they are the same. 338*0b57cec5SDimitry Andric if (Y.getKind() == TemplateArgument::NullPtr) 339*0b57cec5SDimitry Andric return X; 340*0b57cec5SDimitry Andric 341*0b57cec5SDimitry Andric // All other combinations are incompatible. 342*0b57cec5SDimitry Andric return DeducedTemplateArgument(); 343*0b57cec5SDimitry Andric 344*0b57cec5SDimitry Andric case TemplateArgument::Pack: { 345*0b57cec5SDimitry Andric if (Y.getKind() != TemplateArgument::Pack || 346*0b57cec5SDimitry Andric X.pack_size() != Y.pack_size()) 347*0b57cec5SDimitry Andric return DeducedTemplateArgument(); 348*0b57cec5SDimitry Andric 349*0b57cec5SDimitry Andric llvm::SmallVector<TemplateArgument, 8> NewPack; 350*0b57cec5SDimitry Andric for (TemplateArgument::pack_iterator XA = X.pack_begin(), 351*0b57cec5SDimitry Andric XAEnd = X.pack_end(), 352*0b57cec5SDimitry Andric YA = Y.pack_begin(); 353*0b57cec5SDimitry Andric XA != XAEnd; ++XA, ++YA) { 354*0b57cec5SDimitry Andric TemplateArgument Merged = checkDeducedTemplateArguments( 355*0b57cec5SDimitry Andric Context, DeducedTemplateArgument(*XA, X.wasDeducedFromArrayBound()), 356*0b57cec5SDimitry Andric DeducedTemplateArgument(*YA, Y.wasDeducedFromArrayBound())); 357*0b57cec5SDimitry Andric if (Merged.isNull()) 358*0b57cec5SDimitry Andric return DeducedTemplateArgument(); 359*0b57cec5SDimitry Andric NewPack.push_back(Merged); 360*0b57cec5SDimitry Andric } 361*0b57cec5SDimitry Andric 362*0b57cec5SDimitry Andric return DeducedTemplateArgument( 363*0b57cec5SDimitry Andric TemplateArgument::CreatePackCopy(Context, NewPack), 364*0b57cec5SDimitry Andric X.wasDeducedFromArrayBound() && Y.wasDeducedFromArrayBound()); 365*0b57cec5SDimitry Andric } 366*0b57cec5SDimitry Andric } 367*0b57cec5SDimitry Andric 368*0b57cec5SDimitry Andric llvm_unreachable("Invalid TemplateArgument Kind!"); 369*0b57cec5SDimitry Andric } 370*0b57cec5SDimitry Andric 371*0b57cec5SDimitry Andric /// Deduce the value of the given non-type template parameter 372*0b57cec5SDimitry Andric /// as the given deduced template argument. All non-type template parameter 373*0b57cec5SDimitry Andric /// deduction is funneled through here. 374*0b57cec5SDimitry Andric static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( 375*0b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, 376*0b57cec5SDimitry Andric NonTypeTemplateParmDecl *NTTP, const DeducedTemplateArgument &NewDeduced, 377*0b57cec5SDimitry Andric QualType ValueType, TemplateDeductionInfo &Info, 378*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 379*0b57cec5SDimitry Andric assert(NTTP->getDepth() == Info.getDeducedDepth() && 380*0b57cec5SDimitry Andric "deducing non-type template argument with wrong depth"); 381*0b57cec5SDimitry Andric 382*0b57cec5SDimitry Andric DeducedTemplateArgument Result = checkDeducedTemplateArguments( 383*0b57cec5SDimitry Andric S.Context, Deduced[NTTP->getIndex()], NewDeduced); 384*0b57cec5SDimitry Andric if (Result.isNull()) { 385*0b57cec5SDimitry Andric Info.Param = NTTP; 386*0b57cec5SDimitry Andric Info.FirstArg = Deduced[NTTP->getIndex()]; 387*0b57cec5SDimitry Andric Info.SecondArg = NewDeduced; 388*0b57cec5SDimitry Andric return Sema::TDK_Inconsistent; 389*0b57cec5SDimitry Andric } 390*0b57cec5SDimitry Andric 391*0b57cec5SDimitry Andric Deduced[NTTP->getIndex()] = Result; 392*0b57cec5SDimitry Andric if (!S.getLangOpts().CPlusPlus17) 393*0b57cec5SDimitry Andric return Sema::TDK_Success; 394*0b57cec5SDimitry Andric 395*0b57cec5SDimitry Andric if (NTTP->isExpandedParameterPack()) 396*0b57cec5SDimitry Andric // FIXME: We may still need to deduce parts of the type here! But we 397*0b57cec5SDimitry Andric // don't have any way to find which slice of the type to use, and the 398*0b57cec5SDimitry Andric // type stored on the NTTP itself is nonsense. Perhaps the type of an 399*0b57cec5SDimitry Andric // expanded NTTP should be a pack expansion type? 400*0b57cec5SDimitry Andric return Sema::TDK_Success; 401*0b57cec5SDimitry Andric 402*0b57cec5SDimitry Andric // Get the type of the parameter for deduction. If it's a (dependent) array 403*0b57cec5SDimitry Andric // or function type, we will not have decayed it yet, so do that now. 404*0b57cec5SDimitry Andric QualType ParamType = S.Context.getAdjustedParameterType(NTTP->getType()); 405*0b57cec5SDimitry Andric if (auto *Expansion = dyn_cast<PackExpansionType>(ParamType)) 406*0b57cec5SDimitry Andric ParamType = Expansion->getPattern(); 407*0b57cec5SDimitry Andric 408*0b57cec5SDimitry Andric // FIXME: It's not clear how deduction of a parameter of reference 409*0b57cec5SDimitry Andric // type from an argument (of non-reference type) should be performed. 410*0b57cec5SDimitry Andric // For now, we just remove reference types from both sides and let 411*0b57cec5SDimitry Andric // the final check for matching types sort out the mess. 412*0b57cec5SDimitry Andric return DeduceTemplateArgumentsByTypeMatch( 413*0b57cec5SDimitry Andric S, TemplateParams, ParamType.getNonReferenceType(), 414*0b57cec5SDimitry Andric ValueType.getNonReferenceType(), Info, Deduced, TDF_SkipNonDependent, 415*0b57cec5SDimitry Andric /*PartialOrdering=*/false, 416*0b57cec5SDimitry Andric /*ArrayBound=*/NewDeduced.wasDeducedFromArrayBound()); 417*0b57cec5SDimitry Andric } 418*0b57cec5SDimitry Andric 419*0b57cec5SDimitry Andric /// Deduce the value of the given non-type template parameter 420*0b57cec5SDimitry Andric /// from the given integral constant. 421*0b57cec5SDimitry Andric static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( 422*0b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, 423*0b57cec5SDimitry Andric NonTypeTemplateParmDecl *NTTP, const llvm::APSInt &Value, 424*0b57cec5SDimitry Andric QualType ValueType, bool DeducedFromArrayBound, TemplateDeductionInfo &Info, 425*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 426*0b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument( 427*0b57cec5SDimitry Andric S, TemplateParams, NTTP, 428*0b57cec5SDimitry Andric DeducedTemplateArgument(S.Context, Value, ValueType, 429*0b57cec5SDimitry Andric DeducedFromArrayBound), 430*0b57cec5SDimitry Andric ValueType, Info, Deduced); 431*0b57cec5SDimitry Andric } 432*0b57cec5SDimitry Andric 433*0b57cec5SDimitry Andric /// Deduce the value of the given non-type template parameter 434*0b57cec5SDimitry Andric /// from the given null pointer template argument type. 435*0b57cec5SDimitry Andric static Sema::TemplateDeductionResult DeduceNullPtrTemplateArgument( 436*0b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, 437*0b57cec5SDimitry Andric NonTypeTemplateParmDecl *NTTP, QualType NullPtrType, 438*0b57cec5SDimitry Andric TemplateDeductionInfo &Info, 439*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 440*0b57cec5SDimitry Andric Expr *Value = 441*0b57cec5SDimitry Andric S.ImpCastExprToType(new (S.Context) CXXNullPtrLiteralExpr( 442*0b57cec5SDimitry Andric S.Context.NullPtrTy, NTTP->getLocation()), 443*0b57cec5SDimitry Andric NullPtrType, CK_NullToPointer) 444*0b57cec5SDimitry Andric .get(); 445*0b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, 446*0b57cec5SDimitry Andric DeducedTemplateArgument(Value), 447*0b57cec5SDimitry Andric Value->getType(), Info, Deduced); 448*0b57cec5SDimitry Andric } 449*0b57cec5SDimitry Andric 450*0b57cec5SDimitry Andric /// Deduce the value of the given non-type template parameter 451*0b57cec5SDimitry Andric /// from the given type- or value-dependent expression. 452*0b57cec5SDimitry Andric /// 453*0b57cec5SDimitry Andric /// \returns true if deduction succeeded, false otherwise. 454*0b57cec5SDimitry Andric static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( 455*0b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, 456*0b57cec5SDimitry Andric NonTypeTemplateParmDecl *NTTP, Expr *Value, TemplateDeductionInfo &Info, 457*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 458*0b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, 459*0b57cec5SDimitry Andric DeducedTemplateArgument(Value), 460*0b57cec5SDimitry Andric Value->getType(), Info, Deduced); 461*0b57cec5SDimitry Andric } 462*0b57cec5SDimitry Andric 463*0b57cec5SDimitry Andric /// Deduce the value of the given non-type template parameter 464*0b57cec5SDimitry Andric /// from the given declaration. 465*0b57cec5SDimitry Andric /// 466*0b57cec5SDimitry Andric /// \returns true if deduction succeeded, false otherwise. 467*0b57cec5SDimitry Andric static Sema::TemplateDeductionResult DeduceNonTypeTemplateArgument( 468*0b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, 469*0b57cec5SDimitry Andric NonTypeTemplateParmDecl *NTTP, ValueDecl *D, QualType T, 470*0b57cec5SDimitry Andric TemplateDeductionInfo &Info, 471*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 472*0b57cec5SDimitry Andric D = D ? cast<ValueDecl>(D->getCanonicalDecl()) : nullptr; 473*0b57cec5SDimitry Andric TemplateArgument New(D, T); 474*0b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument( 475*0b57cec5SDimitry Andric S, TemplateParams, NTTP, DeducedTemplateArgument(New), T, Info, Deduced); 476*0b57cec5SDimitry Andric } 477*0b57cec5SDimitry Andric 478*0b57cec5SDimitry Andric static Sema::TemplateDeductionResult 479*0b57cec5SDimitry Andric DeduceTemplateArguments(Sema &S, 480*0b57cec5SDimitry Andric TemplateParameterList *TemplateParams, 481*0b57cec5SDimitry Andric TemplateName Param, 482*0b57cec5SDimitry Andric TemplateName Arg, 483*0b57cec5SDimitry Andric TemplateDeductionInfo &Info, 484*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 485*0b57cec5SDimitry Andric TemplateDecl *ParamDecl = Param.getAsTemplateDecl(); 486*0b57cec5SDimitry Andric if (!ParamDecl) { 487*0b57cec5SDimitry Andric // The parameter type is dependent and is not a template template parameter, 488*0b57cec5SDimitry Andric // so there is nothing that we can deduce. 489*0b57cec5SDimitry Andric return Sema::TDK_Success; 490*0b57cec5SDimitry Andric } 491*0b57cec5SDimitry Andric 492*0b57cec5SDimitry Andric if (TemplateTemplateParmDecl *TempParam 493*0b57cec5SDimitry Andric = dyn_cast<TemplateTemplateParmDecl>(ParamDecl)) { 494*0b57cec5SDimitry Andric // If we're not deducing at this depth, there's nothing to deduce. 495*0b57cec5SDimitry Andric if (TempParam->getDepth() != Info.getDeducedDepth()) 496*0b57cec5SDimitry Andric return Sema::TDK_Success; 497*0b57cec5SDimitry Andric 498*0b57cec5SDimitry Andric DeducedTemplateArgument NewDeduced(S.Context.getCanonicalTemplateName(Arg)); 499*0b57cec5SDimitry Andric DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, 500*0b57cec5SDimitry Andric Deduced[TempParam->getIndex()], 501*0b57cec5SDimitry Andric NewDeduced); 502*0b57cec5SDimitry Andric if (Result.isNull()) { 503*0b57cec5SDimitry Andric Info.Param = TempParam; 504*0b57cec5SDimitry Andric Info.FirstArg = Deduced[TempParam->getIndex()]; 505*0b57cec5SDimitry Andric Info.SecondArg = NewDeduced; 506*0b57cec5SDimitry Andric return Sema::TDK_Inconsistent; 507*0b57cec5SDimitry Andric } 508*0b57cec5SDimitry Andric 509*0b57cec5SDimitry Andric Deduced[TempParam->getIndex()] = Result; 510*0b57cec5SDimitry Andric return Sema::TDK_Success; 511*0b57cec5SDimitry Andric } 512*0b57cec5SDimitry Andric 513*0b57cec5SDimitry Andric // Verify that the two template names are equivalent. 514*0b57cec5SDimitry Andric if (S.Context.hasSameTemplateName(Param, Arg)) 515*0b57cec5SDimitry Andric return Sema::TDK_Success; 516*0b57cec5SDimitry Andric 517*0b57cec5SDimitry Andric // Mismatch of non-dependent template parameter to argument. 518*0b57cec5SDimitry Andric Info.FirstArg = TemplateArgument(Param); 519*0b57cec5SDimitry Andric Info.SecondArg = TemplateArgument(Arg); 520*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 521*0b57cec5SDimitry Andric } 522*0b57cec5SDimitry Andric 523*0b57cec5SDimitry Andric /// Deduce the template arguments by comparing the template parameter 524*0b57cec5SDimitry Andric /// type (which is a template-id) with the template argument type. 525*0b57cec5SDimitry Andric /// 526*0b57cec5SDimitry Andric /// \param S the Sema 527*0b57cec5SDimitry Andric /// 528*0b57cec5SDimitry Andric /// \param TemplateParams the template parameters that we are deducing 529*0b57cec5SDimitry Andric /// 530*0b57cec5SDimitry Andric /// \param Param the parameter type 531*0b57cec5SDimitry Andric /// 532*0b57cec5SDimitry Andric /// \param Arg the argument type 533*0b57cec5SDimitry Andric /// 534*0b57cec5SDimitry Andric /// \param Info information about the template argument deduction itself 535*0b57cec5SDimitry Andric /// 536*0b57cec5SDimitry Andric /// \param Deduced the deduced template arguments 537*0b57cec5SDimitry Andric /// 538*0b57cec5SDimitry Andric /// \returns the result of template argument deduction so far. Note that a 539*0b57cec5SDimitry Andric /// "success" result means that template argument deduction has not yet failed, 540*0b57cec5SDimitry Andric /// but it may still fail, later, for other reasons. 541*0b57cec5SDimitry Andric static Sema::TemplateDeductionResult 542*0b57cec5SDimitry Andric DeduceTemplateArguments(Sema &S, 543*0b57cec5SDimitry Andric TemplateParameterList *TemplateParams, 544*0b57cec5SDimitry Andric const TemplateSpecializationType *Param, 545*0b57cec5SDimitry Andric QualType Arg, 546*0b57cec5SDimitry Andric TemplateDeductionInfo &Info, 547*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 548*0b57cec5SDimitry Andric assert(Arg.isCanonical() && "Argument type must be canonical"); 549*0b57cec5SDimitry Andric 550*0b57cec5SDimitry Andric // Treat an injected-class-name as its underlying template-id. 551*0b57cec5SDimitry Andric if (auto *Injected = dyn_cast<InjectedClassNameType>(Arg)) 552*0b57cec5SDimitry Andric Arg = Injected->getInjectedSpecializationType(); 553*0b57cec5SDimitry Andric 554*0b57cec5SDimitry Andric // Check whether the template argument is a dependent template-id. 555*0b57cec5SDimitry Andric if (const TemplateSpecializationType *SpecArg 556*0b57cec5SDimitry Andric = dyn_cast<TemplateSpecializationType>(Arg)) { 557*0b57cec5SDimitry Andric // Perform template argument deduction for the template name. 558*0b57cec5SDimitry Andric if (Sema::TemplateDeductionResult Result 559*0b57cec5SDimitry Andric = DeduceTemplateArguments(S, TemplateParams, 560*0b57cec5SDimitry Andric Param->getTemplateName(), 561*0b57cec5SDimitry Andric SpecArg->getTemplateName(), 562*0b57cec5SDimitry Andric Info, Deduced)) 563*0b57cec5SDimitry Andric return Result; 564*0b57cec5SDimitry Andric 565*0b57cec5SDimitry Andric 566*0b57cec5SDimitry Andric // Perform template argument deduction on each template 567*0b57cec5SDimitry Andric // argument. Ignore any missing/extra arguments, since they could be 568*0b57cec5SDimitry Andric // filled in by default arguments. 569*0b57cec5SDimitry Andric return DeduceTemplateArguments(S, TemplateParams, 570*0b57cec5SDimitry Andric Param->template_arguments(), 571*0b57cec5SDimitry Andric SpecArg->template_arguments(), Info, Deduced, 572*0b57cec5SDimitry Andric /*NumberOfArgumentsMustMatch=*/false); 573*0b57cec5SDimitry Andric } 574*0b57cec5SDimitry Andric 575*0b57cec5SDimitry Andric // If the argument type is a class template specialization, we 576*0b57cec5SDimitry Andric // perform template argument deduction using its template 577*0b57cec5SDimitry Andric // arguments. 578*0b57cec5SDimitry Andric const RecordType *RecordArg = dyn_cast<RecordType>(Arg); 579*0b57cec5SDimitry Andric if (!RecordArg) { 580*0b57cec5SDimitry Andric Info.FirstArg = TemplateArgument(QualType(Param, 0)); 581*0b57cec5SDimitry Andric Info.SecondArg = TemplateArgument(Arg); 582*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 583*0b57cec5SDimitry Andric } 584*0b57cec5SDimitry Andric 585*0b57cec5SDimitry Andric ClassTemplateSpecializationDecl *SpecArg 586*0b57cec5SDimitry Andric = dyn_cast<ClassTemplateSpecializationDecl>(RecordArg->getDecl()); 587*0b57cec5SDimitry Andric if (!SpecArg) { 588*0b57cec5SDimitry Andric Info.FirstArg = TemplateArgument(QualType(Param, 0)); 589*0b57cec5SDimitry Andric Info.SecondArg = TemplateArgument(Arg); 590*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 591*0b57cec5SDimitry Andric } 592*0b57cec5SDimitry Andric 593*0b57cec5SDimitry Andric // Perform template argument deduction for the template name. 594*0b57cec5SDimitry Andric if (Sema::TemplateDeductionResult Result 595*0b57cec5SDimitry Andric = DeduceTemplateArguments(S, 596*0b57cec5SDimitry Andric TemplateParams, 597*0b57cec5SDimitry Andric Param->getTemplateName(), 598*0b57cec5SDimitry Andric TemplateName(SpecArg->getSpecializedTemplate()), 599*0b57cec5SDimitry Andric Info, Deduced)) 600*0b57cec5SDimitry Andric return Result; 601*0b57cec5SDimitry Andric 602*0b57cec5SDimitry Andric // Perform template argument deduction for the template arguments. 603*0b57cec5SDimitry Andric return DeduceTemplateArguments(S, TemplateParams, Param->template_arguments(), 604*0b57cec5SDimitry Andric SpecArg->getTemplateArgs().asArray(), Info, 605*0b57cec5SDimitry Andric Deduced, /*NumberOfArgumentsMustMatch=*/true); 606*0b57cec5SDimitry Andric } 607*0b57cec5SDimitry Andric 608*0b57cec5SDimitry Andric /// Determines whether the given type is an opaque type that 609*0b57cec5SDimitry Andric /// might be more qualified when instantiated. 610*0b57cec5SDimitry Andric static bool IsPossiblyOpaquelyQualifiedType(QualType T) { 611*0b57cec5SDimitry Andric switch (T->getTypeClass()) { 612*0b57cec5SDimitry Andric case Type::TypeOfExpr: 613*0b57cec5SDimitry Andric case Type::TypeOf: 614*0b57cec5SDimitry Andric case Type::DependentName: 615*0b57cec5SDimitry Andric case Type::Decltype: 616*0b57cec5SDimitry Andric case Type::UnresolvedUsing: 617*0b57cec5SDimitry Andric case Type::TemplateTypeParm: 618*0b57cec5SDimitry Andric return true; 619*0b57cec5SDimitry Andric 620*0b57cec5SDimitry Andric case Type::ConstantArray: 621*0b57cec5SDimitry Andric case Type::IncompleteArray: 622*0b57cec5SDimitry Andric case Type::VariableArray: 623*0b57cec5SDimitry Andric case Type::DependentSizedArray: 624*0b57cec5SDimitry Andric return IsPossiblyOpaquelyQualifiedType( 625*0b57cec5SDimitry Andric cast<ArrayType>(T)->getElementType()); 626*0b57cec5SDimitry Andric 627*0b57cec5SDimitry Andric default: 628*0b57cec5SDimitry Andric return false; 629*0b57cec5SDimitry Andric } 630*0b57cec5SDimitry Andric } 631*0b57cec5SDimitry Andric 632*0b57cec5SDimitry Andric /// Helper function to build a TemplateParameter when we don't 633*0b57cec5SDimitry Andric /// know its type statically. 634*0b57cec5SDimitry Andric static TemplateParameter makeTemplateParameter(Decl *D) { 635*0b57cec5SDimitry Andric if (TemplateTypeParmDecl *TTP = dyn_cast<TemplateTypeParmDecl>(D)) 636*0b57cec5SDimitry Andric return TemplateParameter(TTP); 637*0b57cec5SDimitry Andric if (NonTypeTemplateParmDecl *NTTP = dyn_cast<NonTypeTemplateParmDecl>(D)) 638*0b57cec5SDimitry Andric return TemplateParameter(NTTP); 639*0b57cec5SDimitry Andric 640*0b57cec5SDimitry Andric return TemplateParameter(cast<TemplateTemplateParmDecl>(D)); 641*0b57cec5SDimitry Andric } 642*0b57cec5SDimitry Andric 643*0b57cec5SDimitry Andric /// If \p Param is an expanded parameter pack, get the number of expansions. 644*0b57cec5SDimitry Andric static Optional<unsigned> getExpandedPackSize(NamedDecl *Param) { 645*0b57cec5SDimitry Andric if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) 646*0b57cec5SDimitry Andric if (NTTP->isExpandedParameterPack()) 647*0b57cec5SDimitry Andric return NTTP->getNumExpansionTypes(); 648*0b57cec5SDimitry Andric 649*0b57cec5SDimitry Andric if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param)) 650*0b57cec5SDimitry Andric if (TTP->isExpandedParameterPack()) 651*0b57cec5SDimitry Andric return TTP->getNumExpansionTemplateParameters(); 652*0b57cec5SDimitry Andric 653*0b57cec5SDimitry Andric return None; 654*0b57cec5SDimitry Andric } 655*0b57cec5SDimitry Andric 656*0b57cec5SDimitry Andric /// A pack that we're currently deducing. 657*0b57cec5SDimitry Andric struct clang::DeducedPack { 658*0b57cec5SDimitry Andric // The index of the pack. 659*0b57cec5SDimitry Andric unsigned Index; 660*0b57cec5SDimitry Andric 661*0b57cec5SDimitry Andric // The old value of the pack before we started deducing it. 662*0b57cec5SDimitry Andric DeducedTemplateArgument Saved; 663*0b57cec5SDimitry Andric 664*0b57cec5SDimitry Andric // A deferred value of this pack from an inner deduction, that couldn't be 665*0b57cec5SDimitry Andric // deduced because this deduction hadn't happened yet. 666*0b57cec5SDimitry Andric DeducedTemplateArgument DeferredDeduction; 667*0b57cec5SDimitry Andric 668*0b57cec5SDimitry Andric // The new value of the pack. 669*0b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 4> New; 670*0b57cec5SDimitry Andric 671*0b57cec5SDimitry Andric // The outer deduction for this pack, if any. 672*0b57cec5SDimitry Andric DeducedPack *Outer = nullptr; 673*0b57cec5SDimitry Andric 674*0b57cec5SDimitry Andric DeducedPack(unsigned Index) : Index(Index) {} 675*0b57cec5SDimitry Andric }; 676*0b57cec5SDimitry Andric 677*0b57cec5SDimitry Andric namespace { 678*0b57cec5SDimitry Andric 679*0b57cec5SDimitry Andric /// A scope in which we're performing pack deduction. 680*0b57cec5SDimitry Andric class PackDeductionScope { 681*0b57cec5SDimitry Andric public: 682*0b57cec5SDimitry Andric /// Prepare to deduce the packs named within Pattern. 683*0b57cec5SDimitry Andric PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, 684*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 685*0b57cec5SDimitry Andric TemplateDeductionInfo &Info, TemplateArgument Pattern) 686*0b57cec5SDimitry Andric : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) { 687*0b57cec5SDimitry Andric unsigned NumNamedPacks = addPacks(Pattern); 688*0b57cec5SDimitry Andric finishConstruction(NumNamedPacks); 689*0b57cec5SDimitry Andric } 690*0b57cec5SDimitry Andric 691*0b57cec5SDimitry Andric /// Prepare to directly deduce arguments of the parameter with index \p Index. 692*0b57cec5SDimitry Andric PackDeductionScope(Sema &S, TemplateParameterList *TemplateParams, 693*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 694*0b57cec5SDimitry Andric TemplateDeductionInfo &Info, unsigned Index) 695*0b57cec5SDimitry Andric : S(S), TemplateParams(TemplateParams), Deduced(Deduced), Info(Info) { 696*0b57cec5SDimitry Andric addPack(Index); 697*0b57cec5SDimitry Andric finishConstruction(1); 698*0b57cec5SDimitry Andric } 699*0b57cec5SDimitry Andric 700*0b57cec5SDimitry Andric private: 701*0b57cec5SDimitry Andric void addPack(unsigned Index) { 702*0b57cec5SDimitry Andric // Save the deduced template argument for the parameter pack expanded 703*0b57cec5SDimitry Andric // by this pack expansion, then clear out the deduction. 704*0b57cec5SDimitry Andric DeducedPack Pack(Index); 705*0b57cec5SDimitry Andric Pack.Saved = Deduced[Index]; 706*0b57cec5SDimitry Andric Deduced[Index] = TemplateArgument(); 707*0b57cec5SDimitry Andric 708*0b57cec5SDimitry Andric // FIXME: What if we encounter multiple packs with different numbers of 709*0b57cec5SDimitry Andric // pre-expanded expansions? (This should already have been diagnosed 710*0b57cec5SDimitry Andric // during substitution.) 711*0b57cec5SDimitry Andric if (Optional<unsigned> ExpandedPackExpansions = 712*0b57cec5SDimitry Andric getExpandedPackSize(TemplateParams->getParam(Index))) 713*0b57cec5SDimitry Andric FixedNumExpansions = ExpandedPackExpansions; 714*0b57cec5SDimitry Andric 715*0b57cec5SDimitry Andric Packs.push_back(Pack); 716*0b57cec5SDimitry Andric } 717*0b57cec5SDimitry Andric 718*0b57cec5SDimitry Andric unsigned addPacks(TemplateArgument Pattern) { 719*0b57cec5SDimitry Andric // Compute the set of template parameter indices that correspond to 720*0b57cec5SDimitry Andric // parameter packs expanded by the pack expansion. 721*0b57cec5SDimitry Andric llvm::SmallBitVector SawIndices(TemplateParams->size()); 722*0b57cec5SDimitry Andric 723*0b57cec5SDimitry Andric auto AddPack = [&](unsigned Index) { 724*0b57cec5SDimitry Andric if (SawIndices[Index]) 725*0b57cec5SDimitry Andric return; 726*0b57cec5SDimitry Andric SawIndices[Index] = true; 727*0b57cec5SDimitry Andric addPack(Index); 728*0b57cec5SDimitry Andric }; 729*0b57cec5SDimitry Andric 730*0b57cec5SDimitry Andric // First look for unexpanded packs in the pattern. 731*0b57cec5SDimitry Andric SmallVector<UnexpandedParameterPack, 2> Unexpanded; 732*0b57cec5SDimitry Andric S.collectUnexpandedParameterPacks(Pattern, Unexpanded); 733*0b57cec5SDimitry Andric for (unsigned I = 0, N = Unexpanded.size(); I != N; ++I) { 734*0b57cec5SDimitry Andric unsigned Depth, Index; 735*0b57cec5SDimitry Andric std::tie(Depth, Index) = getDepthAndIndex(Unexpanded[I]); 736*0b57cec5SDimitry Andric if (Depth == Info.getDeducedDepth()) 737*0b57cec5SDimitry Andric AddPack(Index); 738*0b57cec5SDimitry Andric } 739*0b57cec5SDimitry Andric assert(!Packs.empty() && "Pack expansion without unexpanded packs?"); 740*0b57cec5SDimitry Andric 741*0b57cec5SDimitry Andric unsigned NumNamedPacks = Packs.size(); 742*0b57cec5SDimitry Andric 743*0b57cec5SDimitry Andric // We can also have deduced template parameters that do not actually 744*0b57cec5SDimitry Andric // appear in the pattern, but can be deduced by it (the type of a non-type 745*0b57cec5SDimitry Andric // template parameter pack, in particular). These won't have prevented us 746*0b57cec5SDimitry Andric // from partially expanding the pack. 747*0b57cec5SDimitry Andric llvm::SmallBitVector Used(TemplateParams->size()); 748*0b57cec5SDimitry Andric MarkUsedTemplateParameters(S.Context, Pattern, /*OnlyDeduced*/true, 749*0b57cec5SDimitry Andric Info.getDeducedDepth(), Used); 750*0b57cec5SDimitry Andric for (int Index = Used.find_first(); Index != -1; 751*0b57cec5SDimitry Andric Index = Used.find_next(Index)) 752*0b57cec5SDimitry Andric if (TemplateParams->getParam(Index)->isParameterPack()) 753*0b57cec5SDimitry Andric AddPack(Index); 754*0b57cec5SDimitry Andric 755*0b57cec5SDimitry Andric return NumNamedPacks; 756*0b57cec5SDimitry Andric } 757*0b57cec5SDimitry Andric 758*0b57cec5SDimitry Andric void finishConstruction(unsigned NumNamedPacks) { 759*0b57cec5SDimitry Andric // Dig out the partially-substituted pack, if there is one. 760*0b57cec5SDimitry Andric const TemplateArgument *PartialPackArgs = nullptr; 761*0b57cec5SDimitry Andric unsigned NumPartialPackArgs = 0; 762*0b57cec5SDimitry Andric std::pair<unsigned, unsigned> PartialPackDepthIndex(-1u, -1u); 763*0b57cec5SDimitry Andric if (auto *Scope = S.CurrentInstantiationScope) 764*0b57cec5SDimitry Andric if (auto *Partial = Scope->getPartiallySubstitutedPack( 765*0b57cec5SDimitry Andric &PartialPackArgs, &NumPartialPackArgs)) 766*0b57cec5SDimitry Andric PartialPackDepthIndex = getDepthAndIndex(Partial); 767*0b57cec5SDimitry Andric 768*0b57cec5SDimitry Andric // This pack expansion will have been partially or fully expanded if 769*0b57cec5SDimitry Andric // it only names explicitly-specified parameter packs (including the 770*0b57cec5SDimitry Andric // partially-substituted one, if any). 771*0b57cec5SDimitry Andric bool IsExpanded = true; 772*0b57cec5SDimitry Andric for (unsigned I = 0; I != NumNamedPacks; ++I) { 773*0b57cec5SDimitry Andric if (Packs[I].Index >= Info.getNumExplicitArgs()) { 774*0b57cec5SDimitry Andric IsExpanded = false; 775*0b57cec5SDimitry Andric IsPartiallyExpanded = false; 776*0b57cec5SDimitry Andric break; 777*0b57cec5SDimitry Andric } 778*0b57cec5SDimitry Andric if (PartialPackDepthIndex == 779*0b57cec5SDimitry Andric std::make_pair(Info.getDeducedDepth(), Packs[I].Index)) { 780*0b57cec5SDimitry Andric IsPartiallyExpanded = true; 781*0b57cec5SDimitry Andric } 782*0b57cec5SDimitry Andric } 783*0b57cec5SDimitry Andric 784*0b57cec5SDimitry Andric // Skip over the pack elements that were expanded into separate arguments. 785*0b57cec5SDimitry Andric // If we partially expanded, this is the number of partial arguments. 786*0b57cec5SDimitry Andric if (IsPartiallyExpanded) 787*0b57cec5SDimitry Andric PackElements += NumPartialPackArgs; 788*0b57cec5SDimitry Andric else if (IsExpanded) 789*0b57cec5SDimitry Andric PackElements += *FixedNumExpansions; 790*0b57cec5SDimitry Andric 791*0b57cec5SDimitry Andric for (auto &Pack : Packs) { 792*0b57cec5SDimitry Andric if (Info.PendingDeducedPacks.size() > Pack.Index) 793*0b57cec5SDimitry Andric Pack.Outer = Info.PendingDeducedPacks[Pack.Index]; 794*0b57cec5SDimitry Andric else 795*0b57cec5SDimitry Andric Info.PendingDeducedPacks.resize(Pack.Index + 1); 796*0b57cec5SDimitry Andric Info.PendingDeducedPacks[Pack.Index] = &Pack; 797*0b57cec5SDimitry Andric 798*0b57cec5SDimitry Andric if (PartialPackDepthIndex == 799*0b57cec5SDimitry Andric std::make_pair(Info.getDeducedDepth(), Pack.Index)) { 800*0b57cec5SDimitry Andric Pack.New.append(PartialPackArgs, PartialPackArgs + NumPartialPackArgs); 801*0b57cec5SDimitry Andric // We pre-populate the deduced value of the partially-substituted 802*0b57cec5SDimitry Andric // pack with the specified value. This is not entirely correct: the 803*0b57cec5SDimitry Andric // value is supposed to have been substituted, not deduced, but the 804*0b57cec5SDimitry Andric // cases where this is observable require an exact type match anyway. 805*0b57cec5SDimitry Andric // 806*0b57cec5SDimitry Andric // FIXME: If we could represent a "depth i, index j, pack elem k" 807*0b57cec5SDimitry Andric // parameter, we could substitute the partially-substituted pack 808*0b57cec5SDimitry Andric // everywhere and avoid this. 809*0b57cec5SDimitry Andric if (!IsPartiallyExpanded) 810*0b57cec5SDimitry Andric Deduced[Pack.Index] = Pack.New[PackElements]; 811*0b57cec5SDimitry Andric } 812*0b57cec5SDimitry Andric } 813*0b57cec5SDimitry Andric } 814*0b57cec5SDimitry Andric 815*0b57cec5SDimitry Andric public: 816*0b57cec5SDimitry Andric ~PackDeductionScope() { 817*0b57cec5SDimitry Andric for (auto &Pack : Packs) 818*0b57cec5SDimitry Andric Info.PendingDeducedPacks[Pack.Index] = Pack.Outer; 819*0b57cec5SDimitry Andric } 820*0b57cec5SDimitry Andric 821*0b57cec5SDimitry Andric /// Determine whether this pack has already been partially expanded into a 822*0b57cec5SDimitry Andric /// sequence of (prior) function parameters / template arguments. 823*0b57cec5SDimitry Andric bool isPartiallyExpanded() { return IsPartiallyExpanded; } 824*0b57cec5SDimitry Andric 825*0b57cec5SDimitry Andric /// Determine whether this pack expansion scope has a known, fixed arity. 826*0b57cec5SDimitry Andric /// This happens if it involves a pack from an outer template that has 827*0b57cec5SDimitry Andric /// (notionally) already been expanded. 828*0b57cec5SDimitry Andric bool hasFixedArity() { return FixedNumExpansions.hasValue(); } 829*0b57cec5SDimitry Andric 830*0b57cec5SDimitry Andric /// Determine whether the next element of the argument is still part of this 831*0b57cec5SDimitry Andric /// pack. This is the case unless the pack is already expanded to a fixed 832*0b57cec5SDimitry Andric /// length. 833*0b57cec5SDimitry Andric bool hasNextElement() { 834*0b57cec5SDimitry Andric return !FixedNumExpansions || *FixedNumExpansions > PackElements; 835*0b57cec5SDimitry Andric } 836*0b57cec5SDimitry Andric 837*0b57cec5SDimitry Andric /// Move to deducing the next element in each pack that is being deduced. 838*0b57cec5SDimitry Andric void nextPackElement() { 839*0b57cec5SDimitry Andric // Capture the deduced template arguments for each parameter pack expanded 840*0b57cec5SDimitry Andric // by this pack expansion, add them to the list of arguments we've deduced 841*0b57cec5SDimitry Andric // for that pack, then clear out the deduced argument. 842*0b57cec5SDimitry Andric for (auto &Pack : Packs) { 843*0b57cec5SDimitry Andric DeducedTemplateArgument &DeducedArg = Deduced[Pack.Index]; 844*0b57cec5SDimitry Andric if (!Pack.New.empty() || !DeducedArg.isNull()) { 845*0b57cec5SDimitry Andric while (Pack.New.size() < PackElements) 846*0b57cec5SDimitry Andric Pack.New.push_back(DeducedTemplateArgument()); 847*0b57cec5SDimitry Andric if (Pack.New.size() == PackElements) 848*0b57cec5SDimitry Andric Pack.New.push_back(DeducedArg); 849*0b57cec5SDimitry Andric else 850*0b57cec5SDimitry Andric Pack.New[PackElements] = DeducedArg; 851*0b57cec5SDimitry Andric DeducedArg = Pack.New.size() > PackElements + 1 852*0b57cec5SDimitry Andric ? Pack.New[PackElements + 1] 853*0b57cec5SDimitry Andric : DeducedTemplateArgument(); 854*0b57cec5SDimitry Andric } 855*0b57cec5SDimitry Andric } 856*0b57cec5SDimitry Andric ++PackElements; 857*0b57cec5SDimitry Andric } 858*0b57cec5SDimitry Andric 859*0b57cec5SDimitry Andric /// Finish template argument deduction for a set of argument packs, 860*0b57cec5SDimitry Andric /// producing the argument packs and checking for consistency with prior 861*0b57cec5SDimitry Andric /// deductions. 862*0b57cec5SDimitry Andric Sema::TemplateDeductionResult 863*0b57cec5SDimitry Andric finish(bool TreatNoDeductionsAsNonDeduced = true) { 864*0b57cec5SDimitry Andric // Build argument packs for each of the parameter packs expanded by this 865*0b57cec5SDimitry Andric // pack expansion. 866*0b57cec5SDimitry Andric for (auto &Pack : Packs) { 867*0b57cec5SDimitry Andric // Put back the old value for this pack. 868*0b57cec5SDimitry Andric Deduced[Pack.Index] = Pack.Saved; 869*0b57cec5SDimitry Andric 870*0b57cec5SDimitry Andric // If we are deducing the size of this pack even if we didn't deduce any 871*0b57cec5SDimitry Andric // values for it, then make sure we build a pack of the right size. 872*0b57cec5SDimitry Andric // FIXME: Should we always deduce the size, even if the pack appears in 873*0b57cec5SDimitry Andric // a non-deduced context? 874*0b57cec5SDimitry Andric if (!TreatNoDeductionsAsNonDeduced) 875*0b57cec5SDimitry Andric Pack.New.resize(PackElements); 876*0b57cec5SDimitry Andric 877*0b57cec5SDimitry Andric // Build or find a new value for this pack. 878*0b57cec5SDimitry Andric DeducedTemplateArgument NewPack; 879*0b57cec5SDimitry Andric if (PackElements && Pack.New.empty()) { 880*0b57cec5SDimitry Andric if (Pack.DeferredDeduction.isNull()) { 881*0b57cec5SDimitry Andric // We were not able to deduce anything for this parameter pack 882*0b57cec5SDimitry Andric // (because it only appeared in non-deduced contexts), so just 883*0b57cec5SDimitry Andric // restore the saved argument pack. 884*0b57cec5SDimitry Andric continue; 885*0b57cec5SDimitry Andric } 886*0b57cec5SDimitry Andric 887*0b57cec5SDimitry Andric NewPack = Pack.DeferredDeduction; 888*0b57cec5SDimitry Andric Pack.DeferredDeduction = TemplateArgument(); 889*0b57cec5SDimitry Andric } else if (Pack.New.empty()) { 890*0b57cec5SDimitry Andric // If we deduced an empty argument pack, create it now. 891*0b57cec5SDimitry Andric NewPack = DeducedTemplateArgument(TemplateArgument::getEmptyPack()); 892*0b57cec5SDimitry Andric } else { 893*0b57cec5SDimitry Andric TemplateArgument *ArgumentPack = 894*0b57cec5SDimitry Andric new (S.Context) TemplateArgument[Pack.New.size()]; 895*0b57cec5SDimitry Andric std::copy(Pack.New.begin(), Pack.New.end(), ArgumentPack); 896*0b57cec5SDimitry Andric NewPack = DeducedTemplateArgument( 897*0b57cec5SDimitry Andric TemplateArgument(llvm::makeArrayRef(ArgumentPack, Pack.New.size())), 898*0b57cec5SDimitry Andric // FIXME: This is wrong, it's possible that some pack elements are 899*0b57cec5SDimitry Andric // deduced from an array bound and others are not: 900*0b57cec5SDimitry Andric // template<typename ...T, T ...V> void g(const T (&...p)[V]); 901*0b57cec5SDimitry Andric // g({1, 2, 3}, {{}, {}}); 902*0b57cec5SDimitry Andric // ... should deduce T = {int, size_t (from array bound)}. 903*0b57cec5SDimitry Andric Pack.New[0].wasDeducedFromArrayBound()); 904*0b57cec5SDimitry Andric } 905*0b57cec5SDimitry Andric 906*0b57cec5SDimitry Andric // Pick where we're going to put the merged pack. 907*0b57cec5SDimitry Andric DeducedTemplateArgument *Loc; 908*0b57cec5SDimitry Andric if (Pack.Outer) { 909*0b57cec5SDimitry Andric if (Pack.Outer->DeferredDeduction.isNull()) { 910*0b57cec5SDimitry Andric // Defer checking this pack until we have a complete pack to compare 911*0b57cec5SDimitry Andric // it against. 912*0b57cec5SDimitry Andric Pack.Outer->DeferredDeduction = NewPack; 913*0b57cec5SDimitry Andric continue; 914*0b57cec5SDimitry Andric } 915*0b57cec5SDimitry Andric Loc = &Pack.Outer->DeferredDeduction; 916*0b57cec5SDimitry Andric } else { 917*0b57cec5SDimitry Andric Loc = &Deduced[Pack.Index]; 918*0b57cec5SDimitry Andric } 919*0b57cec5SDimitry Andric 920*0b57cec5SDimitry Andric // Check the new pack matches any previous value. 921*0b57cec5SDimitry Andric DeducedTemplateArgument OldPack = *Loc; 922*0b57cec5SDimitry Andric DeducedTemplateArgument Result = 923*0b57cec5SDimitry Andric checkDeducedTemplateArguments(S.Context, OldPack, NewPack); 924*0b57cec5SDimitry Andric 925*0b57cec5SDimitry Andric // If we deferred a deduction of this pack, check that one now too. 926*0b57cec5SDimitry Andric if (!Result.isNull() && !Pack.DeferredDeduction.isNull()) { 927*0b57cec5SDimitry Andric OldPack = Result; 928*0b57cec5SDimitry Andric NewPack = Pack.DeferredDeduction; 929*0b57cec5SDimitry Andric Result = checkDeducedTemplateArguments(S.Context, OldPack, NewPack); 930*0b57cec5SDimitry Andric } 931*0b57cec5SDimitry Andric 932*0b57cec5SDimitry Andric NamedDecl *Param = TemplateParams->getParam(Pack.Index); 933*0b57cec5SDimitry Andric if (Result.isNull()) { 934*0b57cec5SDimitry Andric Info.Param = makeTemplateParameter(Param); 935*0b57cec5SDimitry Andric Info.FirstArg = OldPack; 936*0b57cec5SDimitry Andric Info.SecondArg = NewPack; 937*0b57cec5SDimitry Andric return Sema::TDK_Inconsistent; 938*0b57cec5SDimitry Andric } 939*0b57cec5SDimitry Andric 940*0b57cec5SDimitry Andric // If we have a pre-expanded pack and we didn't deduce enough elements 941*0b57cec5SDimitry Andric // for it, fail deduction. 942*0b57cec5SDimitry Andric if (Optional<unsigned> Expansions = getExpandedPackSize(Param)) { 943*0b57cec5SDimitry Andric if (*Expansions != PackElements) { 944*0b57cec5SDimitry Andric Info.Param = makeTemplateParameter(Param); 945*0b57cec5SDimitry Andric Info.FirstArg = Result; 946*0b57cec5SDimitry Andric return Sema::TDK_IncompletePack; 947*0b57cec5SDimitry Andric } 948*0b57cec5SDimitry Andric } 949*0b57cec5SDimitry Andric 950*0b57cec5SDimitry Andric *Loc = Result; 951*0b57cec5SDimitry Andric } 952*0b57cec5SDimitry Andric 953*0b57cec5SDimitry Andric return Sema::TDK_Success; 954*0b57cec5SDimitry Andric } 955*0b57cec5SDimitry Andric 956*0b57cec5SDimitry Andric private: 957*0b57cec5SDimitry Andric Sema &S; 958*0b57cec5SDimitry Andric TemplateParameterList *TemplateParams; 959*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced; 960*0b57cec5SDimitry Andric TemplateDeductionInfo &Info; 961*0b57cec5SDimitry Andric unsigned PackElements = 0; 962*0b57cec5SDimitry Andric bool IsPartiallyExpanded = false; 963*0b57cec5SDimitry Andric /// The number of expansions, if we have a fully-expanded pack in this scope. 964*0b57cec5SDimitry Andric Optional<unsigned> FixedNumExpansions; 965*0b57cec5SDimitry Andric 966*0b57cec5SDimitry Andric SmallVector<DeducedPack, 2> Packs; 967*0b57cec5SDimitry Andric }; 968*0b57cec5SDimitry Andric 969*0b57cec5SDimitry Andric } // namespace 970*0b57cec5SDimitry Andric 971*0b57cec5SDimitry Andric /// Deduce the template arguments by comparing the list of parameter 972*0b57cec5SDimitry Andric /// types to the list of argument types, as in the parameter-type-lists of 973*0b57cec5SDimitry Andric /// function types (C++ [temp.deduct.type]p10). 974*0b57cec5SDimitry Andric /// 975*0b57cec5SDimitry Andric /// \param S The semantic analysis object within which we are deducing 976*0b57cec5SDimitry Andric /// 977*0b57cec5SDimitry Andric /// \param TemplateParams The template parameters that we are deducing 978*0b57cec5SDimitry Andric /// 979*0b57cec5SDimitry Andric /// \param Params The list of parameter types 980*0b57cec5SDimitry Andric /// 981*0b57cec5SDimitry Andric /// \param NumParams The number of types in \c Params 982*0b57cec5SDimitry Andric /// 983*0b57cec5SDimitry Andric /// \param Args The list of argument types 984*0b57cec5SDimitry Andric /// 985*0b57cec5SDimitry Andric /// \param NumArgs The number of types in \c Args 986*0b57cec5SDimitry Andric /// 987*0b57cec5SDimitry Andric /// \param Info information about the template argument deduction itself 988*0b57cec5SDimitry Andric /// 989*0b57cec5SDimitry Andric /// \param Deduced the deduced template arguments 990*0b57cec5SDimitry Andric /// 991*0b57cec5SDimitry Andric /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe 992*0b57cec5SDimitry Andric /// how template argument deduction is performed. 993*0b57cec5SDimitry Andric /// 994*0b57cec5SDimitry Andric /// \param PartialOrdering If true, we are performing template argument 995*0b57cec5SDimitry Andric /// deduction for during partial ordering for a call 996*0b57cec5SDimitry Andric /// (C++0x [temp.deduct.partial]). 997*0b57cec5SDimitry Andric /// 998*0b57cec5SDimitry Andric /// \returns the result of template argument deduction so far. Note that a 999*0b57cec5SDimitry Andric /// "success" result means that template argument deduction has not yet failed, 1000*0b57cec5SDimitry Andric /// but it may still fail, later, for other reasons. 1001*0b57cec5SDimitry Andric static Sema::TemplateDeductionResult 1002*0b57cec5SDimitry Andric DeduceTemplateArguments(Sema &S, 1003*0b57cec5SDimitry Andric TemplateParameterList *TemplateParams, 1004*0b57cec5SDimitry Andric const QualType *Params, unsigned NumParams, 1005*0b57cec5SDimitry Andric const QualType *Args, unsigned NumArgs, 1006*0b57cec5SDimitry Andric TemplateDeductionInfo &Info, 1007*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 1008*0b57cec5SDimitry Andric unsigned TDF, 1009*0b57cec5SDimitry Andric bool PartialOrdering = false) { 1010*0b57cec5SDimitry Andric // C++0x [temp.deduct.type]p10: 1011*0b57cec5SDimitry Andric // Similarly, if P has a form that contains (T), then each parameter type 1012*0b57cec5SDimitry Andric // Pi of the respective parameter-type- list of P is compared with the 1013*0b57cec5SDimitry Andric // corresponding parameter type Ai of the corresponding parameter-type-list 1014*0b57cec5SDimitry Andric // of A. [...] 1015*0b57cec5SDimitry Andric unsigned ArgIdx = 0, ParamIdx = 0; 1016*0b57cec5SDimitry Andric for (; ParamIdx != NumParams; ++ParamIdx) { 1017*0b57cec5SDimitry Andric // Check argument types. 1018*0b57cec5SDimitry Andric const PackExpansionType *Expansion 1019*0b57cec5SDimitry Andric = dyn_cast<PackExpansionType>(Params[ParamIdx]); 1020*0b57cec5SDimitry Andric if (!Expansion) { 1021*0b57cec5SDimitry Andric // Simple case: compare the parameter and argument types at this point. 1022*0b57cec5SDimitry Andric 1023*0b57cec5SDimitry Andric // Make sure we have an argument. 1024*0b57cec5SDimitry Andric if (ArgIdx >= NumArgs) 1025*0b57cec5SDimitry Andric return Sema::TDK_MiscellaneousDeductionFailure; 1026*0b57cec5SDimitry Andric 1027*0b57cec5SDimitry Andric if (isa<PackExpansionType>(Args[ArgIdx])) { 1028*0b57cec5SDimitry Andric // C++0x [temp.deduct.type]p22: 1029*0b57cec5SDimitry Andric // If the original function parameter associated with A is a function 1030*0b57cec5SDimitry Andric // parameter pack and the function parameter associated with P is not 1031*0b57cec5SDimitry Andric // a function parameter pack, then template argument deduction fails. 1032*0b57cec5SDimitry Andric return Sema::TDK_MiscellaneousDeductionFailure; 1033*0b57cec5SDimitry Andric } 1034*0b57cec5SDimitry Andric 1035*0b57cec5SDimitry Andric if (Sema::TemplateDeductionResult Result 1036*0b57cec5SDimitry Andric = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, 1037*0b57cec5SDimitry Andric Params[ParamIdx], Args[ArgIdx], 1038*0b57cec5SDimitry Andric Info, Deduced, TDF, 1039*0b57cec5SDimitry Andric PartialOrdering)) 1040*0b57cec5SDimitry Andric return Result; 1041*0b57cec5SDimitry Andric 1042*0b57cec5SDimitry Andric ++ArgIdx; 1043*0b57cec5SDimitry Andric continue; 1044*0b57cec5SDimitry Andric } 1045*0b57cec5SDimitry Andric 1046*0b57cec5SDimitry Andric // C++0x [temp.deduct.type]p10: 1047*0b57cec5SDimitry Andric // If the parameter-declaration corresponding to Pi is a function 1048*0b57cec5SDimitry Andric // parameter pack, then the type of its declarator- id is compared with 1049*0b57cec5SDimitry Andric // each remaining parameter type in the parameter-type-list of A. Each 1050*0b57cec5SDimitry Andric // comparison deduces template arguments for subsequent positions in the 1051*0b57cec5SDimitry Andric // template parameter packs expanded by the function parameter pack. 1052*0b57cec5SDimitry Andric 1053*0b57cec5SDimitry Andric QualType Pattern = Expansion->getPattern(); 1054*0b57cec5SDimitry Andric PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); 1055*0b57cec5SDimitry Andric 1056*0b57cec5SDimitry Andric // A pack scope with fixed arity is not really a pack any more, so is not 1057*0b57cec5SDimitry Andric // a non-deduced context. 1058*0b57cec5SDimitry Andric if (ParamIdx + 1 == NumParams || PackScope.hasFixedArity()) { 1059*0b57cec5SDimitry Andric for (; ArgIdx < NumArgs && PackScope.hasNextElement(); ++ArgIdx) { 1060*0b57cec5SDimitry Andric // Deduce template arguments from the pattern. 1061*0b57cec5SDimitry Andric if (Sema::TemplateDeductionResult Result 1062*0b57cec5SDimitry Andric = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, Pattern, 1063*0b57cec5SDimitry Andric Args[ArgIdx], Info, Deduced, 1064*0b57cec5SDimitry Andric TDF, PartialOrdering)) 1065*0b57cec5SDimitry Andric return Result; 1066*0b57cec5SDimitry Andric 1067*0b57cec5SDimitry Andric PackScope.nextPackElement(); 1068*0b57cec5SDimitry Andric } 1069*0b57cec5SDimitry Andric } else { 1070*0b57cec5SDimitry Andric // C++0x [temp.deduct.type]p5: 1071*0b57cec5SDimitry Andric // The non-deduced contexts are: 1072*0b57cec5SDimitry Andric // - A function parameter pack that does not occur at the end of the 1073*0b57cec5SDimitry Andric // parameter-declaration-clause. 1074*0b57cec5SDimitry Andric // 1075*0b57cec5SDimitry Andric // FIXME: There is no wording to say what we should do in this case. We 1076*0b57cec5SDimitry Andric // choose to resolve this by applying the same rule that is applied for a 1077*0b57cec5SDimitry Andric // function call: that is, deduce all contained packs to their 1078*0b57cec5SDimitry Andric // explicitly-specified values (or to <> if there is no such value). 1079*0b57cec5SDimitry Andric // 1080*0b57cec5SDimitry Andric // This is seemingly-arbitrarily different from the case of a template-id 1081*0b57cec5SDimitry Andric // with a non-trailing pack-expansion in its arguments, which renders the 1082*0b57cec5SDimitry Andric // entire template-argument-list a non-deduced context. 1083*0b57cec5SDimitry Andric 1084*0b57cec5SDimitry Andric // If the parameter type contains an explicitly-specified pack that we 1085*0b57cec5SDimitry Andric // could not expand, skip the number of parameters notionally created 1086*0b57cec5SDimitry Andric // by the expansion. 1087*0b57cec5SDimitry Andric Optional<unsigned> NumExpansions = Expansion->getNumExpansions(); 1088*0b57cec5SDimitry Andric if (NumExpansions && !PackScope.isPartiallyExpanded()) { 1089*0b57cec5SDimitry Andric for (unsigned I = 0; I != *NumExpansions && ArgIdx < NumArgs; 1090*0b57cec5SDimitry Andric ++I, ++ArgIdx) 1091*0b57cec5SDimitry Andric PackScope.nextPackElement(); 1092*0b57cec5SDimitry Andric } 1093*0b57cec5SDimitry Andric } 1094*0b57cec5SDimitry Andric 1095*0b57cec5SDimitry Andric // Build argument packs for each of the parameter packs expanded by this 1096*0b57cec5SDimitry Andric // pack expansion. 1097*0b57cec5SDimitry Andric if (auto Result = PackScope.finish()) 1098*0b57cec5SDimitry Andric return Result; 1099*0b57cec5SDimitry Andric } 1100*0b57cec5SDimitry Andric 1101*0b57cec5SDimitry Andric // Make sure we don't have any extra arguments. 1102*0b57cec5SDimitry Andric if (ArgIdx < NumArgs) 1103*0b57cec5SDimitry Andric return Sema::TDK_MiscellaneousDeductionFailure; 1104*0b57cec5SDimitry Andric 1105*0b57cec5SDimitry Andric return Sema::TDK_Success; 1106*0b57cec5SDimitry Andric } 1107*0b57cec5SDimitry Andric 1108*0b57cec5SDimitry Andric /// Determine whether the parameter has qualifiers that the argument 1109*0b57cec5SDimitry Andric /// lacks. Put another way, determine whether there is no way to add 1110*0b57cec5SDimitry Andric /// a deduced set of qualifiers to the ParamType that would result in 1111*0b57cec5SDimitry Andric /// its qualifiers matching those of the ArgType. 1112*0b57cec5SDimitry Andric static bool hasInconsistentOrSupersetQualifiersOf(QualType ParamType, 1113*0b57cec5SDimitry Andric QualType ArgType) { 1114*0b57cec5SDimitry Andric Qualifiers ParamQs = ParamType.getQualifiers(); 1115*0b57cec5SDimitry Andric Qualifiers ArgQs = ArgType.getQualifiers(); 1116*0b57cec5SDimitry Andric 1117*0b57cec5SDimitry Andric if (ParamQs == ArgQs) 1118*0b57cec5SDimitry Andric return false; 1119*0b57cec5SDimitry Andric 1120*0b57cec5SDimitry Andric // Mismatched (but not missing) Objective-C GC attributes. 1121*0b57cec5SDimitry Andric if (ParamQs.getObjCGCAttr() != ArgQs.getObjCGCAttr() && 1122*0b57cec5SDimitry Andric ParamQs.hasObjCGCAttr()) 1123*0b57cec5SDimitry Andric return true; 1124*0b57cec5SDimitry Andric 1125*0b57cec5SDimitry Andric // Mismatched (but not missing) address spaces. 1126*0b57cec5SDimitry Andric if (ParamQs.getAddressSpace() != ArgQs.getAddressSpace() && 1127*0b57cec5SDimitry Andric ParamQs.hasAddressSpace()) 1128*0b57cec5SDimitry Andric return true; 1129*0b57cec5SDimitry Andric 1130*0b57cec5SDimitry Andric // Mismatched (but not missing) Objective-C lifetime qualifiers. 1131*0b57cec5SDimitry Andric if (ParamQs.getObjCLifetime() != ArgQs.getObjCLifetime() && 1132*0b57cec5SDimitry Andric ParamQs.hasObjCLifetime()) 1133*0b57cec5SDimitry Andric return true; 1134*0b57cec5SDimitry Andric 1135*0b57cec5SDimitry Andric // CVR qualifiers inconsistent or a superset. 1136*0b57cec5SDimitry Andric return (ParamQs.getCVRQualifiers() & ~ArgQs.getCVRQualifiers()) != 0; 1137*0b57cec5SDimitry Andric } 1138*0b57cec5SDimitry Andric 1139*0b57cec5SDimitry Andric /// Compare types for equality with respect to possibly compatible 1140*0b57cec5SDimitry Andric /// function types (noreturn adjustment, implicit calling conventions). If any 1141*0b57cec5SDimitry Andric /// of parameter and argument is not a function, just perform type comparison. 1142*0b57cec5SDimitry Andric /// 1143*0b57cec5SDimitry Andric /// \param Param the template parameter type. 1144*0b57cec5SDimitry Andric /// 1145*0b57cec5SDimitry Andric /// \param Arg the argument type. 1146*0b57cec5SDimitry Andric bool Sema::isSameOrCompatibleFunctionType(CanQualType Param, 1147*0b57cec5SDimitry Andric CanQualType Arg) { 1148*0b57cec5SDimitry Andric const FunctionType *ParamFunction = Param->getAs<FunctionType>(), 1149*0b57cec5SDimitry Andric *ArgFunction = Arg->getAs<FunctionType>(); 1150*0b57cec5SDimitry Andric 1151*0b57cec5SDimitry Andric // Just compare if not functions. 1152*0b57cec5SDimitry Andric if (!ParamFunction || !ArgFunction) 1153*0b57cec5SDimitry Andric return Param == Arg; 1154*0b57cec5SDimitry Andric 1155*0b57cec5SDimitry Andric // Noreturn and noexcept adjustment. 1156*0b57cec5SDimitry Andric QualType AdjustedParam; 1157*0b57cec5SDimitry Andric if (IsFunctionConversion(Param, Arg, AdjustedParam)) 1158*0b57cec5SDimitry Andric return Arg == Context.getCanonicalType(AdjustedParam); 1159*0b57cec5SDimitry Andric 1160*0b57cec5SDimitry Andric // FIXME: Compatible calling conventions. 1161*0b57cec5SDimitry Andric 1162*0b57cec5SDimitry Andric return Param == Arg; 1163*0b57cec5SDimitry Andric } 1164*0b57cec5SDimitry Andric 1165*0b57cec5SDimitry Andric /// Get the index of the first template parameter that was originally from the 1166*0b57cec5SDimitry Andric /// innermost template-parameter-list. This is 0 except when we concatenate 1167*0b57cec5SDimitry Andric /// the template parameter lists of a class template and a constructor template 1168*0b57cec5SDimitry Andric /// when forming an implicit deduction guide. 1169*0b57cec5SDimitry Andric static unsigned getFirstInnerIndex(FunctionTemplateDecl *FTD) { 1170*0b57cec5SDimitry Andric auto *Guide = dyn_cast<CXXDeductionGuideDecl>(FTD->getTemplatedDecl()); 1171*0b57cec5SDimitry Andric if (!Guide || !Guide->isImplicit()) 1172*0b57cec5SDimitry Andric return 0; 1173*0b57cec5SDimitry Andric return Guide->getDeducedTemplate()->getTemplateParameters()->size(); 1174*0b57cec5SDimitry Andric } 1175*0b57cec5SDimitry Andric 1176*0b57cec5SDimitry Andric /// Determine whether a type denotes a forwarding reference. 1177*0b57cec5SDimitry Andric static bool isForwardingReference(QualType Param, unsigned FirstInnerIndex) { 1178*0b57cec5SDimitry Andric // C++1z [temp.deduct.call]p3: 1179*0b57cec5SDimitry Andric // A forwarding reference is an rvalue reference to a cv-unqualified 1180*0b57cec5SDimitry Andric // template parameter that does not represent a template parameter of a 1181*0b57cec5SDimitry Andric // class template. 1182*0b57cec5SDimitry Andric if (auto *ParamRef = Param->getAs<RValueReferenceType>()) { 1183*0b57cec5SDimitry Andric if (ParamRef->getPointeeType().getQualifiers()) 1184*0b57cec5SDimitry Andric return false; 1185*0b57cec5SDimitry Andric auto *TypeParm = ParamRef->getPointeeType()->getAs<TemplateTypeParmType>(); 1186*0b57cec5SDimitry Andric return TypeParm && TypeParm->getIndex() >= FirstInnerIndex; 1187*0b57cec5SDimitry Andric } 1188*0b57cec5SDimitry Andric return false; 1189*0b57cec5SDimitry Andric } 1190*0b57cec5SDimitry Andric 1191*0b57cec5SDimitry Andric /// Deduce the template arguments by comparing the parameter type and 1192*0b57cec5SDimitry Andric /// the argument type (C++ [temp.deduct.type]). 1193*0b57cec5SDimitry Andric /// 1194*0b57cec5SDimitry Andric /// \param S the semantic analysis object within which we are deducing 1195*0b57cec5SDimitry Andric /// 1196*0b57cec5SDimitry Andric /// \param TemplateParams the template parameters that we are deducing 1197*0b57cec5SDimitry Andric /// 1198*0b57cec5SDimitry Andric /// \param ParamIn the parameter type 1199*0b57cec5SDimitry Andric /// 1200*0b57cec5SDimitry Andric /// \param ArgIn the argument type 1201*0b57cec5SDimitry Andric /// 1202*0b57cec5SDimitry Andric /// \param Info information about the template argument deduction itself 1203*0b57cec5SDimitry Andric /// 1204*0b57cec5SDimitry Andric /// \param Deduced the deduced template arguments 1205*0b57cec5SDimitry Andric /// 1206*0b57cec5SDimitry Andric /// \param TDF bitwise OR of the TemplateDeductionFlags bits that describe 1207*0b57cec5SDimitry Andric /// how template argument deduction is performed. 1208*0b57cec5SDimitry Andric /// 1209*0b57cec5SDimitry Andric /// \param PartialOrdering Whether we're performing template argument deduction 1210*0b57cec5SDimitry Andric /// in the context of partial ordering (C++0x [temp.deduct.partial]). 1211*0b57cec5SDimitry Andric /// 1212*0b57cec5SDimitry Andric /// \returns the result of template argument deduction so far. Note that a 1213*0b57cec5SDimitry Andric /// "success" result means that template argument deduction has not yet failed, 1214*0b57cec5SDimitry Andric /// but it may still fail, later, for other reasons. 1215*0b57cec5SDimitry Andric static Sema::TemplateDeductionResult 1216*0b57cec5SDimitry Andric DeduceTemplateArgumentsByTypeMatch(Sema &S, 1217*0b57cec5SDimitry Andric TemplateParameterList *TemplateParams, 1218*0b57cec5SDimitry Andric QualType ParamIn, QualType ArgIn, 1219*0b57cec5SDimitry Andric TemplateDeductionInfo &Info, 1220*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 1221*0b57cec5SDimitry Andric unsigned TDF, 1222*0b57cec5SDimitry Andric bool PartialOrdering, 1223*0b57cec5SDimitry Andric bool DeducedFromArrayBound) { 1224*0b57cec5SDimitry Andric // We only want to look at the canonical types, since typedefs and 1225*0b57cec5SDimitry Andric // sugar are not part of template argument deduction. 1226*0b57cec5SDimitry Andric QualType Param = S.Context.getCanonicalType(ParamIn); 1227*0b57cec5SDimitry Andric QualType Arg = S.Context.getCanonicalType(ArgIn); 1228*0b57cec5SDimitry Andric 1229*0b57cec5SDimitry Andric // If the argument type is a pack expansion, look at its pattern. 1230*0b57cec5SDimitry Andric // This isn't explicitly called out 1231*0b57cec5SDimitry Andric if (const PackExpansionType *ArgExpansion 1232*0b57cec5SDimitry Andric = dyn_cast<PackExpansionType>(Arg)) 1233*0b57cec5SDimitry Andric Arg = ArgExpansion->getPattern(); 1234*0b57cec5SDimitry Andric 1235*0b57cec5SDimitry Andric if (PartialOrdering) { 1236*0b57cec5SDimitry Andric // C++11 [temp.deduct.partial]p5: 1237*0b57cec5SDimitry Andric // Before the partial ordering is done, certain transformations are 1238*0b57cec5SDimitry Andric // performed on the types used for partial ordering: 1239*0b57cec5SDimitry Andric // - If P is a reference type, P is replaced by the type referred to. 1240*0b57cec5SDimitry Andric const ReferenceType *ParamRef = Param->getAs<ReferenceType>(); 1241*0b57cec5SDimitry Andric if (ParamRef) 1242*0b57cec5SDimitry Andric Param = ParamRef->getPointeeType(); 1243*0b57cec5SDimitry Andric 1244*0b57cec5SDimitry Andric // - If A is a reference type, A is replaced by the type referred to. 1245*0b57cec5SDimitry Andric const ReferenceType *ArgRef = Arg->getAs<ReferenceType>(); 1246*0b57cec5SDimitry Andric if (ArgRef) 1247*0b57cec5SDimitry Andric Arg = ArgRef->getPointeeType(); 1248*0b57cec5SDimitry Andric 1249*0b57cec5SDimitry Andric if (ParamRef && ArgRef && S.Context.hasSameUnqualifiedType(Param, Arg)) { 1250*0b57cec5SDimitry Andric // C++11 [temp.deduct.partial]p9: 1251*0b57cec5SDimitry Andric // If, for a given type, deduction succeeds in both directions (i.e., 1252*0b57cec5SDimitry Andric // the types are identical after the transformations above) and both 1253*0b57cec5SDimitry Andric // P and A were reference types [...]: 1254*0b57cec5SDimitry Andric // - if [one type] was an lvalue reference and [the other type] was 1255*0b57cec5SDimitry Andric // not, [the other type] is not considered to be at least as 1256*0b57cec5SDimitry Andric // specialized as [the first type] 1257*0b57cec5SDimitry Andric // - if [one type] is more cv-qualified than [the other type], 1258*0b57cec5SDimitry Andric // [the other type] is not considered to be at least as specialized 1259*0b57cec5SDimitry Andric // as [the first type] 1260*0b57cec5SDimitry Andric // Objective-C ARC adds: 1261*0b57cec5SDimitry Andric // - [one type] has non-trivial lifetime, [the other type] has 1262*0b57cec5SDimitry Andric // __unsafe_unretained lifetime, and the types are otherwise 1263*0b57cec5SDimitry Andric // identical 1264*0b57cec5SDimitry Andric // 1265*0b57cec5SDimitry Andric // A is "considered to be at least as specialized" as P iff deduction 1266*0b57cec5SDimitry Andric // succeeds, so we model this as a deduction failure. Note that 1267*0b57cec5SDimitry Andric // [the first type] is P and [the other type] is A here; the standard 1268*0b57cec5SDimitry Andric // gets this backwards. 1269*0b57cec5SDimitry Andric Qualifiers ParamQuals = Param.getQualifiers(); 1270*0b57cec5SDimitry Andric Qualifiers ArgQuals = Arg.getQualifiers(); 1271*0b57cec5SDimitry Andric if ((ParamRef->isLValueReferenceType() && 1272*0b57cec5SDimitry Andric !ArgRef->isLValueReferenceType()) || 1273*0b57cec5SDimitry Andric ParamQuals.isStrictSupersetOf(ArgQuals) || 1274*0b57cec5SDimitry Andric (ParamQuals.hasNonTrivialObjCLifetime() && 1275*0b57cec5SDimitry Andric ArgQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone && 1276*0b57cec5SDimitry Andric ParamQuals.withoutObjCLifetime() == 1277*0b57cec5SDimitry Andric ArgQuals.withoutObjCLifetime())) { 1278*0b57cec5SDimitry Andric Info.FirstArg = TemplateArgument(ParamIn); 1279*0b57cec5SDimitry Andric Info.SecondArg = TemplateArgument(ArgIn); 1280*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1281*0b57cec5SDimitry Andric } 1282*0b57cec5SDimitry Andric } 1283*0b57cec5SDimitry Andric 1284*0b57cec5SDimitry Andric // C++11 [temp.deduct.partial]p7: 1285*0b57cec5SDimitry Andric // Remove any top-level cv-qualifiers: 1286*0b57cec5SDimitry Andric // - If P is a cv-qualified type, P is replaced by the cv-unqualified 1287*0b57cec5SDimitry Andric // version of P. 1288*0b57cec5SDimitry Andric Param = Param.getUnqualifiedType(); 1289*0b57cec5SDimitry Andric // - If A is a cv-qualified type, A is replaced by the cv-unqualified 1290*0b57cec5SDimitry Andric // version of A. 1291*0b57cec5SDimitry Andric Arg = Arg.getUnqualifiedType(); 1292*0b57cec5SDimitry Andric } else { 1293*0b57cec5SDimitry Andric // C++0x [temp.deduct.call]p4 bullet 1: 1294*0b57cec5SDimitry Andric // - If the original P is a reference type, the deduced A (i.e., the type 1295*0b57cec5SDimitry Andric // referred to by the reference) can be more cv-qualified than the 1296*0b57cec5SDimitry Andric // transformed A. 1297*0b57cec5SDimitry Andric if (TDF & TDF_ParamWithReferenceType) { 1298*0b57cec5SDimitry Andric Qualifiers Quals; 1299*0b57cec5SDimitry Andric QualType UnqualParam = S.Context.getUnqualifiedArrayType(Param, Quals); 1300*0b57cec5SDimitry Andric Quals.setCVRQualifiers(Quals.getCVRQualifiers() & 1301*0b57cec5SDimitry Andric Arg.getCVRQualifiers()); 1302*0b57cec5SDimitry Andric Param = S.Context.getQualifiedType(UnqualParam, Quals); 1303*0b57cec5SDimitry Andric } 1304*0b57cec5SDimitry Andric 1305*0b57cec5SDimitry Andric if ((TDF & TDF_TopLevelParameterTypeList) && !Param->isFunctionType()) { 1306*0b57cec5SDimitry Andric // C++0x [temp.deduct.type]p10: 1307*0b57cec5SDimitry Andric // If P and A are function types that originated from deduction when 1308*0b57cec5SDimitry Andric // taking the address of a function template (14.8.2.2) or when deducing 1309*0b57cec5SDimitry Andric // template arguments from a function declaration (14.8.2.6) and Pi and 1310*0b57cec5SDimitry Andric // Ai are parameters of the top-level parameter-type-list of P and A, 1311*0b57cec5SDimitry Andric // respectively, Pi is adjusted if it is a forwarding reference and Ai 1312*0b57cec5SDimitry Andric // is an lvalue reference, in 1313*0b57cec5SDimitry Andric // which case the type of Pi is changed to be the template parameter 1314*0b57cec5SDimitry Andric // type (i.e., T&& is changed to simply T). [ Note: As a result, when 1315*0b57cec5SDimitry Andric // Pi is T&& and Ai is X&, the adjusted Pi will be T, causing T to be 1316*0b57cec5SDimitry Andric // deduced as X&. - end note ] 1317*0b57cec5SDimitry Andric TDF &= ~TDF_TopLevelParameterTypeList; 1318*0b57cec5SDimitry Andric if (isForwardingReference(Param, 0) && Arg->isLValueReferenceType()) 1319*0b57cec5SDimitry Andric Param = Param->getPointeeType(); 1320*0b57cec5SDimitry Andric } 1321*0b57cec5SDimitry Andric } 1322*0b57cec5SDimitry Andric 1323*0b57cec5SDimitry Andric // C++ [temp.deduct.type]p9: 1324*0b57cec5SDimitry Andric // A template type argument T, a template template argument TT or a 1325*0b57cec5SDimitry Andric // template non-type argument i can be deduced if P and A have one of 1326*0b57cec5SDimitry Andric // the following forms: 1327*0b57cec5SDimitry Andric // 1328*0b57cec5SDimitry Andric // T 1329*0b57cec5SDimitry Andric // cv-list T 1330*0b57cec5SDimitry Andric if (const TemplateTypeParmType *TemplateTypeParm 1331*0b57cec5SDimitry Andric = Param->getAs<TemplateTypeParmType>()) { 1332*0b57cec5SDimitry Andric // Just skip any attempts to deduce from a placeholder type or a parameter 1333*0b57cec5SDimitry Andric // at a different depth. 1334*0b57cec5SDimitry Andric if (Arg->isPlaceholderType() || 1335*0b57cec5SDimitry Andric Info.getDeducedDepth() != TemplateTypeParm->getDepth()) 1336*0b57cec5SDimitry Andric return Sema::TDK_Success; 1337*0b57cec5SDimitry Andric 1338*0b57cec5SDimitry Andric unsigned Index = TemplateTypeParm->getIndex(); 1339*0b57cec5SDimitry Andric bool RecanonicalizeArg = false; 1340*0b57cec5SDimitry Andric 1341*0b57cec5SDimitry Andric // If the argument type is an array type, move the qualifiers up to the 1342*0b57cec5SDimitry Andric // top level, so they can be matched with the qualifiers on the parameter. 1343*0b57cec5SDimitry Andric if (isa<ArrayType>(Arg)) { 1344*0b57cec5SDimitry Andric Qualifiers Quals; 1345*0b57cec5SDimitry Andric Arg = S.Context.getUnqualifiedArrayType(Arg, Quals); 1346*0b57cec5SDimitry Andric if (Quals) { 1347*0b57cec5SDimitry Andric Arg = S.Context.getQualifiedType(Arg, Quals); 1348*0b57cec5SDimitry Andric RecanonicalizeArg = true; 1349*0b57cec5SDimitry Andric } 1350*0b57cec5SDimitry Andric } 1351*0b57cec5SDimitry Andric 1352*0b57cec5SDimitry Andric // The argument type can not be less qualified than the parameter 1353*0b57cec5SDimitry Andric // type. 1354*0b57cec5SDimitry Andric if (!(TDF & TDF_IgnoreQualifiers) && 1355*0b57cec5SDimitry Andric hasInconsistentOrSupersetQualifiersOf(Param, Arg)) { 1356*0b57cec5SDimitry Andric Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); 1357*0b57cec5SDimitry Andric Info.FirstArg = TemplateArgument(Param); 1358*0b57cec5SDimitry Andric Info.SecondArg = TemplateArgument(Arg); 1359*0b57cec5SDimitry Andric return Sema::TDK_Underqualified; 1360*0b57cec5SDimitry Andric } 1361*0b57cec5SDimitry Andric 1362*0b57cec5SDimitry Andric // Do not match a function type with a cv-qualified type. 1363*0b57cec5SDimitry Andric // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1584 1364*0b57cec5SDimitry Andric if (Arg->isFunctionType() && Param.hasQualifiers()) { 1365*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1366*0b57cec5SDimitry Andric } 1367*0b57cec5SDimitry Andric 1368*0b57cec5SDimitry Andric assert(TemplateTypeParm->getDepth() == Info.getDeducedDepth() && 1369*0b57cec5SDimitry Andric "saw template type parameter with wrong depth"); 1370*0b57cec5SDimitry Andric assert(Arg != S.Context.OverloadTy && "Unresolved overloaded function"); 1371*0b57cec5SDimitry Andric QualType DeducedType = Arg; 1372*0b57cec5SDimitry Andric 1373*0b57cec5SDimitry Andric // Remove any qualifiers on the parameter from the deduced type. 1374*0b57cec5SDimitry Andric // We checked the qualifiers for consistency above. 1375*0b57cec5SDimitry Andric Qualifiers DeducedQs = DeducedType.getQualifiers(); 1376*0b57cec5SDimitry Andric Qualifiers ParamQs = Param.getQualifiers(); 1377*0b57cec5SDimitry Andric DeducedQs.removeCVRQualifiers(ParamQs.getCVRQualifiers()); 1378*0b57cec5SDimitry Andric if (ParamQs.hasObjCGCAttr()) 1379*0b57cec5SDimitry Andric DeducedQs.removeObjCGCAttr(); 1380*0b57cec5SDimitry Andric if (ParamQs.hasAddressSpace()) 1381*0b57cec5SDimitry Andric DeducedQs.removeAddressSpace(); 1382*0b57cec5SDimitry Andric if (ParamQs.hasObjCLifetime()) 1383*0b57cec5SDimitry Andric DeducedQs.removeObjCLifetime(); 1384*0b57cec5SDimitry Andric 1385*0b57cec5SDimitry Andric // Objective-C ARC: 1386*0b57cec5SDimitry Andric // If template deduction would produce a lifetime qualifier on a type 1387*0b57cec5SDimitry Andric // that is not a lifetime type, template argument deduction fails. 1388*0b57cec5SDimitry Andric if (ParamQs.hasObjCLifetime() && !DeducedType->isObjCLifetimeType() && 1389*0b57cec5SDimitry Andric !DeducedType->isDependentType()) { 1390*0b57cec5SDimitry Andric Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); 1391*0b57cec5SDimitry Andric Info.FirstArg = TemplateArgument(Param); 1392*0b57cec5SDimitry Andric Info.SecondArg = TemplateArgument(Arg); 1393*0b57cec5SDimitry Andric return Sema::TDK_Underqualified; 1394*0b57cec5SDimitry Andric } 1395*0b57cec5SDimitry Andric 1396*0b57cec5SDimitry Andric // Objective-C ARC: 1397*0b57cec5SDimitry Andric // If template deduction would produce an argument type with lifetime type 1398*0b57cec5SDimitry Andric // but no lifetime qualifier, the __strong lifetime qualifier is inferred. 1399*0b57cec5SDimitry Andric if (S.getLangOpts().ObjCAutoRefCount && 1400*0b57cec5SDimitry Andric DeducedType->isObjCLifetimeType() && 1401*0b57cec5SDimitry Andric !DeducedQs.hasObjCLifetime()) 1402*0b57cec5SDimitry Andric DeducedQs.setObjCLifetime(Qualifiers::OCL_Strong); 1403*0b57cec5SDimitry Andric 1404*0b57cec5SDimitry Andric DeducedType = S.Context.getQualifiedType(DeducedType.getUnqualifiedType(), 1405*0b57cec5SDimitry Andric DeducedQs); 1406*0b57cec5SDimitry Andric 1407*0b57cec5SDimitry Andric if (RecanonicalizeArg) 1408*0b57cec5SDimitry Andric DeducedType = S.Context.getCanonicalType(DeducedType); 1409*0b57cec5SDimitry Andric 1410*0b57cec5SDimitry Andric DeducedTemplateArgument NewDeduced(DeducedType, DeducedFromArrayBound); 1411*0b57cec5SDimitry Andric DeducedTemplateArgument Result = checkDeducedTemplateArguments(S.Context, 1412*0b57cec5SDimitry Andric Deduced[Index], 1413*0b57cec5SDimitry Andric NewDeduced); 1414*0b57cec5SDimitry Andric if (Result.isNull()) { 1415*0b57cec5SDimitry Andric Info.Param = cast<TemplateTypeParmDecl>(TemplateParams->getParam(Index)); 1416*0b57cec5SDimitry Andric Info.FirstArg = Deduced[Index]; 1417*0b57cec5SDimitry Andric Info.SecondArg = NewDeduced; 1418*0b57cec5SDimitry Andric return Sema::TDK_Inconsistent; 1419*0b57cec5SDimitry Andric } 1420*0b57cec5SDimitry Andric 1421*0b57cec5SDimitry Andric Deduced[Index] = Result; 1422*0b57cec5SDimitry Andric return Sema::TDK_Success; 1423*0b57cec5SDimitry Andric } 1424*0b57cec5SDimitry Andric 1425*0b57cec5SDimitry Andric // Set up the template argument deduction information for a failure. 1426*0b57cec5SDimitry Andric Info.FirstArg = TemplateArgument(ParamIn); 1427*0b57cec5SDimitry Andric Info.SecondArg = TemplateArgument(ArgIn); 1428*0b57cec5SDimitry Andric 1429*0b57cec5SDimitry Andric // If the parameter is an already-substituted template parameter 1430*0b57cec5SDimitry Andric // pack, do nothing: we don't know which of its arguments to look 1431*0b57cec5SDimitry Andric // at, so we have to wait until all of the parameter packs in this 1432*0b57cec5SDimitry Andric // expansion have arguments. 1433*0b57cec5SDimitry Andric if (isa<SubstTemplateTypeParmPackType>(Param)) 1434*0b57cec5SDimitry Andric return Sema::TDK_Success; 1435*0b57cec5SDimitry Andric 1436*0b57cec5SDimitry Andric // Check the cv-qualifiers on the parameter and argument types. 1437*0b57cec5SDimitry Andric CanQualType CanParam = S.Context.getCanonicalType(Param); 1438*0b57cec5SDimitry Andric CanQualType CanArg = S.Context.getCanonicalType(Arg); 1439*0b57cec5SDimitry Andric if (!(TDF & TDF_IgnoreQualifiers)) { 1440*0b57cec5SDimitry Andric if (TDF & TDF_ParamWithReferenceType) { 1441*0b57cec5SDimitry Andric if (hasInconsistentOrSupersetQualifiersOf(Param, Arg)) 1442*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1443*0b57cec5SDimitry Andric } else if (TDF & TDF_ArgWithReferenceType) { 1444*0b57cec5SDimitry Andric // C++ [temp.deduct.conv]p4: 1445*0b57cec5SDimitry Andric // If the original A is a reference type, A can be more cv-qualified 1446*0b57cec5SDimitry Andric // than the deduced A 1447*0b57cec5SDimitry Andric if (!Arg.getQualifiers().compatiblyIncludes(Param.getQualifiers())) 1448*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1449*0b57cec5SDimitry Andric 1450*0b57cec5SDimitry Andric // Strip out all extra qualifiers from the argument to figure out the 1451*0b57cec5SDimitry Andric // type we're converting to, prior to the qualification conversion. 1452*0b57cec5SDimitry Andric Qualifiers Quals; 1453*0b57cec5SDimitry Andric Arg = S.Context.getUnqualifiedArrayType(Arg, Quals); 1454*0b57cec5SDimitry Andric Arg = S.Context.getQualifiedType(Arg, Param.getQualifiers()); 1455*0b57cec5SDimitry Andric } else if (!IsPossiblyOpaquelyQualifiedType(Param)) { 1456*0b57cec5SDimitry Andric if (Param.getCVRQualifiers() != Arg.getCVRQualifiers()) 1457*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1458*0b57cec5SDimitry Andric } 1459*0b57cec5SDimitry Andric 1460*0b57cec5SDimitry Andric // If the parameter type is not dependent, there is nothing to deduce. 1461*0b57cec5SDimitry Andric if (!Param->isDependentType()) { 1462*0b57cec5SDimitry Andric if (!(TDF & TDF_SkipNonDependent)) { 1463*0b57cec5SDimitry Andric bool NonDeduced = 1464*0b57cec5SDimitry Andric (TDF & TDF_AllowCompatibleFunctionType) 1465*0b57cec5SDimitry Andric ? !S.isSameOrCompatibleFunctionType(CanParam, CanArg) 1466*0b57cec5SDimitry Andric : Param != Arg; 1467*0b57cec5SDimitry Andric if (NonDeduced) { 1468*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1469*0b57cec5SDimitry Andric } 1470*0b57cec5SDimitry Andric } 1471*0b57cec5SDimitry Andric return Sema::TDK_Success; 1472*0b57cec5SDimitry Andric } 1473*0b57cec5SDimitry Andric } else if (!Param->isDependentType()) { 1474*0b57cec5SDimitry Andric CanQualType ParamUnqualType = CanParam.getUnqualifiedType(), 1475*0b57cec5SDimitry Andric ArgUnqualType = CanArg.getUnqualifiedType(); 1476*0b57cec5SDimitry Andric bool Success = 1477*0b57cec5SDimitry Andric (TDF & TDF_AllowCompatibleFunctionType) 1478*0b57cec5SDimitry Andric ? S.isSameOrCompatibleFunctionType(ParamUnqualType, ArgUnqualType) 1479*0b57cec5SDimitry Andric : ParamUnqualType == ArgUnqualType; 1480*0b57cec5SDimitry Andric if (Success) 1481*0b57cec5SDimitry Andric return Sema::TDK_Success; 1482*0b57cec5SDimitry Andric } 1483*0b57cec5SDimitry Andric 1484*0b57cec5SDimitry Andric switch (Param->getTypeClass()) { 1485*0b57cec5SDimitry Andric // Non-canonical types cannot appear here. 1486*0b57cec5SDimitry Andric #define NON_CANONICAL_TYPE(Class, Base) \ 1487*0b57cec5SDimitry Andric case Type::Class: llvm_unreachable("deducing non-canonical type: " #Class); 1488*0b57cec5SDimitry Andric #define TYPE(Class, Base) 1489*0b57cec5SDimitry Andric #include "clang/AST/TypeNodes.def" 1490*0b57cec5SDimitry Andric 1491*0b57cec5SDimitry Andric case Type::TemplateTypeParm: 1492*0b57cec5SDimitry Andric case Type::SubstTemplateTypeParmPack: 1493*0b57cec5SDimitry Andric llvm_unreachable("Type nodes handled above"); 1494*0b57cec5SDimitry Andric 1495*0b57cec5SDimitry Andric // These types cannot be dependent, so simply check whether the types are 1496*0b57cec5SDimitry Andric // the same. 1497*0b57cec5SDimitry Andric case Type::Builtin: 1498*0b57cec5SDimitry Andric case Type::VariableArray: 1499*0b57cec5SDimitry Andric case Type::Vector: 1500*0b57cec5SDimitry Andric case Type::FunctionNoProto: 1501*0b57cec5SDimitry Andric case Type::Record: 1502*0b57cec5SDimitry Andric case Type::Enum: 1503*0b57cec5SDimitry Andric case Type::ObjCObject: 1504*0b57cec5SDimitry Andric case Type::ObjCInterface: 1505*0b57cec5SDimitry Andric case Type::ObjCObjectPointer: 1506*0b57cec5SDimitry Andric if (TDF & TDF_SkipNonDependent) 1507*0b57cec5SDimitry Andric return Sema::TDK_Success; 1508*0b57cec5SDimitry Andric 1509*0b57cec5SDimitry Andric if (TDF & TDF_IgnoreQualifiers) { 1510*0b57cec5SDimitry Andric Param = Param.getUnqualifiedType(); 1511*0b57cec5SDimitry Andric Arg = Arg.getUnqualifiedType(); 1512*0b57cec5SDimitry Andric } 1513*0b57cec5SDimitry Andric 1514*0b57cec5SDimitry Andric return Param == Arg? Sema::TDK_Success : Sema::TDK_NonDeducedMismatch; 1515*0b57cec5SDimitry Andric 1516*0b57cec5SDimitry Andric // _Complex T [placeholder extension] 1517*0b57cec5SDimitry Andric case Type::Complex: 1518*0b57cec5SDimitry Andric if (const ComplexType *ComplexArg = Arg->getAs<ComplexType>()) 1519*0b57cec5SDimitry Andric return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, 1520*0b57cec5SDimitry Andric cast<ComplexType>(Param)->getElementType(), 1521*0b57cec5SDimitry Andric ComplexArg->getElementType(), 1522*0b57cec5SDimitry Andric Info, Deduced, TDF); 1523*0b57cec5SDimitry Andric 1524*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1525*0b57cec5SDimitry Andric 1526*0b57cec5SDimitry Andric // _Atomic T [extension] 1527*0b57cec5SDimitry Andric case Type::Atomic: 1528*0b57cec5SDimitry Andric if (const AtomicType *AtomicArg = Arg->getAs<AtomicType>()) 1529*0b57cec5SDimitry Andric return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, 1530*0b57cec5SDimitry Andric cast<AtomicType>(Param)->getValueType(), 1531*0b57cec5SDimitry Andric AtomicArg->getValueType(), 1532*0b57cec5SDimitry Andric Info, Deduced, TDF); 1533*0b57cec5SDimitry Andric 1534*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1535*0b57cec5SDimitry Andric 1536*0b57cec5SDimitry Andric // T * 1537*0b57cec5SDimitry Andric case Type::Pointer: { 1538*0b57cec5SDimitry Andric QualType PointeeType; 1539*0b57cec5SDimitry Andric if (const PointerType *PointerArg = Arg->getAs<PointerType>()) { 1540*0b57cec5SDimitry Andric PointeeType = PointerArg->getPointeeType(); 1541*0b57cec5SDimitry Andric } else if (const ObjCObjectPointerType *PointerArg 1542*0b57cec5SDimitry Andric = Arg->getAs<ObjCObjectPointerType>()) { 1543*0b57cec5SDimitry Andric PointeeType = PointerArg->getPointeeType(); 1544*0b57cec5SDimitry Andric } else { 1545*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1546*0b57cec5SDimitry Andric } 1547*0b57cec5SDimitry Andric 1548*0b57cec5SDimitry Andric unsigned SubTDF = TDF & (TDF_IgnoreQualifiers | TDF_DerivedClass); 1549*0b57cec5SDimitry Andric return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, 1550*0b57cec5SDimitry Andric cast<PointerType>(Param)->getPointeeType(), 1551*0b57cec5SDimitry Andric PointeeType, 1552*0b57cec5SDimitry Andric Info, Deduced, SubTDF); 1553*0b57cec5SDimitry Andric } 1554*0b57cec5SDimitry Andric 1555*0b57cec5SDimitry Andric // T & 1556*0b57cec5SDimitry Andric case Type::LValueReference: { 1557*0b57cec5SDimitry Andric const LValueReferenceType *ReferenceArg = 1558*0b57cec5SDimitry Andric Arg->getAs<LValueReferenceType>(); 1559*0b57cec5SDimitry Andric if (!ReferenceArg) 1560*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1561*0b57cec5SDimitry Andric 1562*0b57cec5SDimitry Andric return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, 1563*0b57cec5SDimitry Andric cast<LValueReferenceType>(Param)->getPointeeType(), 1564*0b57cec5SDimitry Andric ReferenceArg->getPointeeType(), Info, Deduced, 0); 1565*0b57cec5SDimitry Andric } 1566*0b57cec5SDimitry Andric 1567*0b57cec5SDimitry Andric // T && [C++0x] 1568*0b57cec5SDimitry Andric case Type::RValueReference: { 1569*0b57cec5SDimitry Andric const RValueReferenceType *ReferenceArg = 1570*0b57cec5SDimitry Andric Arg->getAs<RValueReferenceType>(); 1571*0b57cec5SDimitry Andric if (!ReferenceArg) 1572*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1573*0b57cec5SDimitry Andric 1574*0b57cec5SDimitry Andric return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, 1575*0b57cec5SDimitry Andric cast<RValueReferenceType>(Param)->getPointeeType(), 1576*0b57cec5SDimitry Andric ReferenceArg->getPointeeType(), 1577*0b57cec5SDimitry Andric Info, Deduced, 0); 1578*0b57cec5SDimitry Andric } 1579*0b57cec5SDimitry Andric 1580*0b57cec5SDimitry Andric // T [] (implied, but not stated explicitly) 1581*0b57cec5SDimitry Andric case Type::IncompleteArray: { 1582*0b57cec5SDimitry Andric const IncompleteArrayType *IncompleteArrayArg = 1583*0b57cec5SDimitry Andric S.Context.getAsIncompleteArrayType(Arg); 1584*0b57cec5SDimitry Andric if (!IncompleteArrayArg) 1585*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1586*0b57cec5SDimitry Andric 1587*0b57cec5SDimitry Andric unsigned SubTDF = TDF & TDF_IgnoreQualifiers; 1588*0b57cec5SDimitry Andric return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, 1589*0b57cec5SDimitry Andric S.Context.getAsIncompleteArrayType(Param)->getElementType(), 1590*0b57cec5SDimitry Andric IncompleteArrayArg->getElementType(), 1591*0b57cec5SDimitry Andric Info, Deduced, SubTDF); 1592*0b57cec5SDimitry Andric } 1593*0b57cec5SDimitry Andric 1594*0b57cec5SDimitry Andric // T [integer-constant] 1595*0b57cec5SDimitry Andric case Type::ConstantArray: { 1596*0b57cec5SDimitry Andric const ConstantArrayType *ConstantArrayArg = 1597*0b57cec5SDimitry Andric S.Context.getAsConstantArrayType(Arg); 1598*0b57cec5SDimitry Andric if (!ConstantArrayArg) 1599*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1600*0b57cec5SDimitry Andric 1601*0b57cec5SDimitry Andric const ConstantArrayType *ConstantArrayParm = 1602*0b57cec5SDimitry Andric S.Context.getAsConstantArrayType(Param); 1603*0b57cec5SDimitry Andric if (ConstantArrayArg->getSize() != ConstantArrayParm->getSize()) 1604*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1605*0b57cec5SDimitry Andric 1606*0b57cec5SDimitry Andric unsigned SubTDF = TDF & TDF_IgnoreQualifiers; 1607*0b57cec5SDimitry Andric return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, 1608*0b57cec5SDimitry Andric ConstantArrayParm->getElementType(), 1609*0b57cec5SDimitry Andric ConstantArrayArg->getElementType(), 1610*0b57cec5SDimitry Andric Info, Deduced, SubTDF); 1611*0b57cec5SDimitry Andric } 1612*0b57cec5SDimitry Andric 1613*0b57cec5SDimitry Andric // type [i] 1614*0b57cec5SDimitry Andric case Type::DependentSizedArray: { 1615*0b57cec5SDimitry Andric const ArrayType *ArrayArg = S.Context.getAsArrayType(Arg); 1616*0b57cec5SDimitry Andric if (!ArrayArg) 1617*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1618*0b57cec5SDimitry Andric 1619*0b57cec5SDimitry Andric unsigned SubTDF = TDF & TDF_IgnoreQualifiers; 1620*0b57cec5SDimitry Andric 1621*0b57cec5SDimitry Andric // Check the element type of the arrays 1622*0b57cec5SDimitry Andric const DependentSizedArrayType *DependentArrayParm 1623*0b57cec5SDimitry Andric = S.Context.getAsDependentSizedArrayType(Param); 1624*0b57cec5SDimitry Andric if (Sema::TemplateDeductionResult Result 1625*0b57cec5SDimitry Andric = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, 1626*0b57cec5SDimitry Andric DependentArrayParm->getElementType(), 1627*0b57cec5SDimitry Andric ArrayArg->getElementType(), 1628*0b57cec5SDimitry Andric Info, Deduced, SubTDF)) 1629*0b57cec5SDimitry Andric return Result; 1630*0b57cec5SDimitry Andric 1631*0b57cec5SDimitry Andric // Determine the array bound is something we can deduce. 1632*0b57cec5SDimitry Andric NonTypeTemplateParmDecl *NTTP 1633*0b57cec5SDimitry Andric = getDeducedParameterFromExpr(Info, DependentArrayParm->getSizeExpr()); 1634*0b57cec5SDimitry Andric if (!NTTP) 1635*0b57cec5SDimitry Andric return Sema::TDK_Success; 1636*0b57cec5SDimitry Andric 1637*0b57cec5SDimitry Andric // We can perform template argument deduction for the given non-type 1638*0b57cec5SDimitry Andric // template parameter. 1639*0b57cec5SDimitry Andric assert(NTTP->getDepth() == Info.getDeducedDepth() && 1640*0b57cec5SDimitry Andric "saw non-type template parameter with wrong depth"); 1641*0b57cec5SDimitry Andric if (const ConstantArrayType *ConstantArrayArg 1642*0b57cec5SDimitry Andric = dyn_cast<ConstantArrayType>(ArrayArg)) { 1643*0b57cec5SDimitry Andric llvm::APSInt Size(ConstantArrayArg->getSize()); 1644*0b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, Size, 1645*0b57cec5SDimitry Andric S.Context.getSizeType(), 1646*0b57cec5SDimitry Andric /*ArrayBound=*/true, 1647*0b57cec5SDimitry Andric Info, Deduced); 1648*0b57cec5SDimitry Andric } 1649*0b57cec5SDimitry Andric if (const DependentSizedArrayType *DependentArrayArg 1650*0b57cec5SDimitry Andric = dyn_cast<DependentSizedArrayType>(ArrayArg)) 1651*0b57cec5SDimitry Andric if (DependentArrayArg->getSizeExpr()) 1652*0b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, 1653*0b57cec5SDimitry Andric DependentArrayArg->getSizeExpr(), 1654*0b57cec5SDimitry Andric Info, Deduced); 1655*0b57cec5SDimitry Andric 1656*0b57cec5SDimitry Andric // Incomplete type does not match a dependently-sized array type 1657*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1658*0b57cec5SDimitry Andric } 1659*0b57cec5SDimitry Andric 1660*0b57cec5SDimitry Andric // type(*)(T) 1661*0b57cec5SDimitry Andric // T(*)() 1662*0b57cec5SDimitry Andric // T(*)(T) 1663*0b57cec5SDimitry Andric case Type::FunctionProto: { 1664*0b57cec5SDimitry Andric unsigned SubTDF = TDF & TDF_TopLevelParameterTypeList; 1665*0b57cec5SDimitry Andric const FunctionProtoType *FunctionProtoArg = 1666*0b57cec5SDimitry Andric dyn_cast<FunctionProtoType>(Arg); 1667*0b57cec5SDimitry Andric if (!FunctionProtoArg) 1668*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1669*0b57cec5SDimitry Andric 1670*0b57cec5SDimitry Andric const FunctionProtoType *FunctionProtoParam = 1671*0b57cec5SDimitry Andric cast<FunctionProtoType>(Param); 1672*0b57cec5SDimitry Andric 1673*0b57cec5SDimitry Andric if (FunctionProtoParam->getMethodQuals() 1674*0b57cec5SDimitry Andric != FunctionProtoArg->getMethodQuals() || 1675*0b57cec5SDimitry Andric FunctionProtoParam->getRefQualifier() 1676*0b57cec5SDimitry Andric != FunctionProtoArg->getRefQualifier() || 1677*0b57cec5SDimitry Andric FunctionProtoParam->isVariadic() != FunctionProtoArg->isVariadic()) 1678*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1679*0b57cec5SDimitry Andric 1680*0b57cec5SDimitry Andric // Check return types. 1681*0b57cec5SDimitry Andric if (auto Result = DeduceTemplateArgumentsByTypeMatch( 1682*0b57cec5SDimitry Andric S, TemplateParams, FunctionProtoParam->getReturnType(), 1683*0b57cec5SDimitry Andric FunctionProtoArg->getReturnType(), Info, Deduced, 0)) 1684*0b57cec5SDimitry Andric return Result; 1685*0b57cec5SDimitry Andric 1686*0b57cec5SDimitry Andric // Check parameter types. 1687*0b57cec5SDimitry Andric if (auto Result = DeduceTemplateArguments( 1688*0b57cec5SDimitry Andric S, TemplateParams, FunctionProtoParam->param_type_begin(), 1689*0b57cec5SDimitry Andric FunctionProtoParam->getNumParams(), 1690*0b57cec5SDimitry Andric FunctionProtoArg->param_type_begin(), 1691*0b57cec5SDimitry Andric FunctionProtoArg->getNumParams(), Info, Deduced, SubTDF)) 1692*0b57cec5SDimitry Andric return Result; 1693*0b57cec5SDimitry Andric 1694*0b57cec5SDimitry Andric if (TDF & TDF_AllowCompatibleFunctionType) 1695*0b57cec5SDimitry Andric return Sema::TDK_Success; 1696*0b57cec5SDimitry Andric 1697*0b57cec5SDimitry Andric // FIXME: Per core-2016/10/1019 (no corresponding core issue yet), permit 1698*0b57cec5SDimitry Andric // deducing through the noexcept-specifier if it's part of the canonical 1699*0b57cec5SDimitry Andric // type. libstdc++ relies on this. 1700*0b57cec5SDimitry Andric Expr *NoexceptExpr = FunctionProtoParam->getNoexceptExpr(); 1701*0b57cec5SDimitry Andric if (NonTypeTemplateParmDecl *NTTP = 1702*0b57cec5SDimitry Andric NoexceptExpr ? getDeducedParameterFromExpr(Info, NoexceptExpr) 1703*0b57cec5SDimitry Andric : nullptr) { 1704*0b57cec5SDimitry Andric assert(NTTP->getDepth() == Info.getDeducedDepth() && 1705*0b57cec5SDimitry Andric "saw non-type template parameter with wrong depth"); 1706*0b57cec5SDimitry Andric 1707*0b57cec5SDimitry Andric llvm::APSInt Noexcept(1); 1708*0b57cec5SDimitry Andric switch (FunctionProtoArg->canThrow()) { 1709*0b57cec5SDimitry Andric case CT_Cannot: 1710*0b57cec5SDimitry Andric Noexcept = 1; 1711*0b57cec5SDimitry Andric LLVM_FALLTHROUGH; 1712*0b57cec5SDimitry Andric 1713*0b57cec5SDimitry Andric case CT_Can: 1714*0b57cec5SDimitry Andric // We give E in noexcept(E) the "deduced from array bound" treatment. 1715*0b57cec5SDimitry Andric // FIXME: Should we? 1716*0b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument( 1717*0b57cec5SDimitry Andric S, TemplateParams, NTTP, Noexcept, S.Context.BoolTy, 1718*0b57cec5SDimitry Andric /*ArrayBound*/true, Info, Deduced); 1719*0b57cec5SDimitry Andric 1720*0b57cec5SDimitry Andric case CT_Dependent: 1721*0b57cec5SDimitry Andric if (Expr *ArgNoexceptExpr = FunctionProtoArg->getNoexceptExpr()) 1722*0b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument( 1723*0b57cec5SDimitry Andric S, TemplateParams, NTTP, ArgNoexceptExpr, Info, Deduced); 1724*0b57cec5SDimitry Andric // Can't deduce anything from throw(T...). 1725*0b57cec5SDimitry Andric break; 1726*0b57cec5SDimitry Andric } 1727*0b57cec5SDimitry Andric } 1728*0b57cec5SDimitry Andric // FIXME: Detect non-deduced exception specification mismatches? 1729*0b57cec5SDimitry Andric // 1730*0b57cec5SDimitry Andric // Careful about [temp.deduct.call] and [temp.deduct.conv], which allow 1731*0b57cec5SDimitry Andric // top-level differences in noexcept-specifications. 1732*0b57cec5SDimitry Andric 1733*0b57cec5SDimitry Andric return Sema::TDK_Success; 1734*0b57cec5SDimitry Andric } 1735*0b57cec5SDimitry Andric 1736*0b57cec5SDimitry Andric case Type::InjectedClassName: 1737*0b57cec5SDimitry Andric // Treat a template's injected-class-name as if the template 1738*0b57cec5SDimitry Andric // specialization type had been used. 1739*0b57cec5SDimitry Andric Param = cast<InjectedClassNameType>(Param) 1740*0b57cec5SDimitry Andric ->getInjectedSpecializationType(); 1741*0b57cec5SDimitry Andric assert(isa<TemplateSpecializationType>(Param) && 1742*0b57cec5SDimitry Andric "injected class name is not a template specialization type"); 1743*0b57cec5SDimitry Andric LLVM_FALLTHROUGH; 1744*0b57cec5SDimitry Andric 1745*0b57cec5SDimitry Andric // template-name<T> (where template-name refers to a class template) 1746*0b57cec5SDimitry Andric // template-name<i> 1747*0b57cec5SDimitry Andric // TT<T> 1748*0b57cec5SDimitry Andric // TT<i> 1749*0b57cec5SDimitry Andric // TT<> 1750*0b57cec5SDimitry Andric case Type::TemplateSpecialization: { 1751*0b57cec5SDimitry Andric const TemplateSpecializationType *SpecParam = 1752*0b57cec5SDimitry Andric cast<TemplateSpecializationType>(Param); 1753*0b57cec5SDimitry Andric 1754*0b57cec5SDimitry Andric // When Arg cannot be a derived class, we can just try to deduce template 1755*0b57cec5SDimitry Andric // arguments from the template-id. 1756*0b57cec5SDimitry Andric const RecordType *RecordT = Arg->getAs<RecordType>(); 1757*0b57cec5SDimitry Andric if (!(TDF & TDF_DerivedClass) || !RecordT) 1758*0b57cec5SDimitry Andric return DeduceTemplateArguments(S, TemplateParams, SpecParam, Arg, Info, 1759*0b57cec5SDimitry Andric Deduced); 1760*0b57cec5SDimitry Andric 1761*0b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 8> DeducedOrig(Deduced.begin(), 1762*0b57cec5SDimitry Andric Deduced.end()); 1763*0b57cec5SDimitry Andric 1764*0b57cec5SDimitry Andric Sema::TemplateDeductionResult Result = DeduceTemplateArguments( 1765*0b57cec5SDimitry Andric S, TemplateParams, SpecParam, Arg, Info, Deduced); 1766*0b57cec5SDimitry Andric 1767*0b57cec5SDimitry Andric if (Result == Sema::TDK_Success) 1768*0b57cec5SDimitry Andric return Result; 1769*0b57cec5SDimitry Andric 1770*0b57cec5SDimitry Andric // We cannot inspect base classes as part of deduction when the type 1771*0b57cec5SDimitry Andric // is incomplete, so either instantiate any templates necessary to 1772*0b57cec5SDimitry Andric // complete the type, or skip over it if it cannot be completed. 1773*0b57cec5SDimitry Andric if (!S.isCompleteType(Info.getLocation(), Arg)) 1774*0b57cec5SDimitry Andric return Result; 1775*0b57cec5SDimitry Andric 1776*0b57cec5SDimitry Andric // C++14 [temp.deduct.call] p4b3: 1777*0b57cec5SDimitry Andric // If P is a class and P has the form simple-template-id, then the 1778*0b57cec5SDimitry Andric // transformed A can be a derived class of the deduced A. Likewise if 1779*0b57cec5SDimitry Andric // P is a pointer to a class of the form simple-template-id, the 1780*0b57cec5SDimitry Andric // transformed A can be a pointer to a derived class pointed to by the 1781*0b57cec5SDimitry Andric // deduced A. 1782*0b57cec5SDimitry Andric // 1783*0b57cec5SDimitry Andric // These alternatives are considered only if type deduction would 1784*0b57cec5SDimitry Andric // otherwise fail. If they yield more than one possible deduced A, the 1785*0b57cec5SDimitry Andric // type deduction fails. 1786*0b57cec5SDimitry Andric 1787*0b57cec5SDimitry Andric // Reset the incorrectly deduced argument from above. 1788*0b57cec5SDimitry Andric Deduced = DeducedOrig; 1789*0b57cec5SDimitry Andric 1790*0b57cec5SDimitry Andric // Use data recursion to crawl through the list of base classes. 1791*0b57cec5SDimitry Andric // Visited contains the set of nodes we have already visited, while 1792*0b57cec5SDimitry Andric // ToVisit is our stack of records that we still need to visit. 1793*0b57cec5SDimitry Andric llvm::SmallPtrSet<const RecordType *, 8> Visited; 1794*0b57cec5SDimitry Andric SmallVector<const RecordType *, 8> ToVisit; 1795*0b57cec5SDimitry Andric ToVisit.push_back(RecordT); 1796*0b57cec5SDimitry Andric bool Successful = false; 1797*0b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 8> SuccessfulDeduced; 1798*0b57cec5SDimitry Andric while (!ToVisit.empty()) { 1799*0b57cec5SDimitry Andric // Retrieve the next class in the inheritance hierarchy. 1800*0b57cec5SDimitry Andric const RecordType *NextT = ToVisit.pop_back_val(); 1801*0b57cec5SDimitry Andric 1802*0b57cec5SDimitry Andric // If we have already seen this type, skip it. 1803*0b57cec5SDimitry Andric if (!Visited.insert(NextT).second) 1804*0b57cec5SDimitry Andric continue; 1805*0b57cec5SDimitry Andric 1806*0b57cec5SDimitry Andric // If this is a base class, try to perform template argument 1807*0b57cec5SDimitry Andric // deduction from it. 1808*0b57cec5SDimitry Andric if (NextT != RecordT) { 1809*0b57cec5SDimitry Andric TemplateDeductionInfo BaseInfo(Info.getLocation()); 1810*0b57cec5SDimitry Andric Sema::TemplateDeductionResult BaseResult = 1811*0b57cec5SDimitry Andric DeduceTemplateArguments(S, TemplateParams, SpecParam, 1812*0b57cec5SDimitry Andric QualType(NextT, 0), BaseInfo, Deduced); 1813*0b57cec5SDimitry Andric 1814*0b57cec5SDimitry Andric // If template argument deduction for this base was successful, 1815*0b57cec5SDimitry Andric // note that we had some success. Otherwise, ignore any deductions 1816*0b57cec5SDimitry Andric // from this base class. 1817*0b57cec5SDimitry Andric if (BaseResult == Sema::TDK_Success) { 1818*0b57cec5SDimitry Andric // If we've already seen some success, then deduction fails due to 1819*0b57cec5SDimitry Andric // an ambiguity (temp.deduct.call p5). 1820*0b57cec5SDimitry Andric if (Successful) 1821*0b57cec5SDimitry Andric return Sema::TDK_MiscellaneousDeductionFailure; 1822*0b57cec5SDimitry Andric 1823*0b57cec5SDimitry Andric Successful = true; 1824*0b57cec5SDimitry Andric std::swap(SuccessfulDeduced, Deduced); 1825*0b57cec5SDimitry Andric 1826*0b57cec5SDimitry Andric Info.Param = BaseInfo.Param; 1827*0b57cec5SDimitry Andric Info.FirstArg = BaseInfo.FirstArg; 1828*0b57cec5SDimitry Andric Info.SecondArg = BaseInfo.SecondArg; 1829*0b57cec5SDimitry Andric } 1830*0b57cec5SDimitry Andric 1831*0b57cec5SDimitry Andric Deduced = DeducedOrig; 1832*0b57cec5SDimitry Andric } 1833*0b57cec5SDimitry Andric 1834*0b57cec5SDimitry Andric // Visit base classes 1835*0b57cec5SDimitry Andric CXXRecordDecl *Next = cast<CXXRecordDecl>(NextT->getDecl()); 1836*0b57cec5SDimitry Andric for (const auto &Base : Next->bases()) { 1837*0b57cec5SDimitry Andric assert(Base.getType()->isRecordType() && 1838*0b57cec5SDimitry Andric "Base class that isn't a record?"); 1839*0b57cec5SDimitry Andric ToVisit.push_back(Base.getType()->getAs<RecordType>()); 1840*0b57cec5SDimitry Andric } 1841*0b57cec5SDimitry Andric } 1842*0b57cec5SDimitry Andric 1843*0b57cec5SDimitry Andric if (Successful) { 1844*0b57cec5SDimitry Andric std::swap(SuccessfulDeduced, Deduced); 1845*0b57cec5SDimitry Andric return Sema::TDK_Success; 1846*0b57cec5SDimitry Andric } 1847*0b57cec5SDimitry Andric 1848*0b57cec5SDimitry Andric return Result; 1849*0b57cec5SDimitry Andric } 1850*0b57cec5SDimitry Andric 1851*0b57cec5SDimitry Andric // T type::* 1852*0b57cec5SDimitry Andric // T T::* 1853*0b57cec5SDimitry Andric // T (type::*)() 1854*0b57cec5SDimitry Andric // type (T::*)() 1855*0b57cec5SDimitry Andric // type (type::*)(T) 1856*0b57cec5SDimitry Andric // type (T::*)(T) 1857*0b57cec5SDimitry Andric // T (type::*)(T) 1858*0b57cec5SDimitry Andric // T (T::*)() 1859*0b57cec5SDimitry Andric // T (T::*)(T) 1860*0b57cec5SDimitry Andric case Type::MemberPointer: { 1861*0b57cec5SDimitry Andric const MemberPointerType *MemPtrParam = cast<MemberPointerType>(Param); 1862*0b57cec5SDimitry Andric const MemberPointerType *MemPtrArg = dyn_cast<MemberPointerType>(Arg); 1863*0b57cec5SDimitry Andric if (!MemPtrArg) 1864*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1865*0b57cec5SDimitry Andric 1866*0b57cec5SDimitry Andric QualType ParamPointeeType = MemPtrParam->getPointeeType(); 1867*0b57cec5SDimitry Andric if (ParamPointeeType->isFunctionType()) 1868*0b57cec5SDimitry Andric S.adjustMemberFunctionCC(ParamPointeeType, /*IsStatic=*/true, 1869*0b57cec5SDimitry Andric /*IsCtorOrDtor=*/false, Info.getLocation()); 1870*0b57cec5SDimitry Andric QualType ArgPointeeType = MemPtrArg->getPointeeType(); 1871*0b57cec5SDimitry Andric if (ArgPointeeType->isFunctionType()) 1872*0b57cec5SDimitry Andric S.adjustMemberFunctionCC(ArgPointeeType, /*IsStatic=*/true, 1873*0b57cec5SDimitry Andric /*IsCtorOrDtor=*/false, Info.getLocation()); 1874*0b57cec5SDimitry Andric 1875*0b57cec5SDimitry Andric if (Sema::TemplateDeductionResult Result 1876*0b57cec5SDimitry Andric = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, 1877*0b57cec5SDimitry Andric ParamPointeeType, 1878*0b57cec5SDimitry Andric ArgPointeeType, 1879*0b57cec5SDimitry Andric Info, Deduced, 1880*0b57cec5SDimitry Andric TDF & TDF_IgnoreQualifiers)) 1881*0b57cec5SDimitry Andric return Result; 1882*0b57cec5SDimitry Andric 1883*0b57cec5SDimitry Andric return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, 1884*0b57cec5SDimitry Andric QualType(MemPtrParam->getClass(), 0), 1885*0b57cec5SDimitry Andric QualType(MemPtrArg->getClass(), 0), 1886*0b57cec5SDimitry Andric Info, Deduced, 1887*0b57cec5SDimitry Andric TDF & TDF_IgnoreQualifiers); 1888*0b57cec5SDimitry Andric } 1889*0b57cec5SDimitry Andric 1890*0b57cec5SDimitry Andric // (clang extension) 1891*0b57cec5SDimitry Andric // 1892*0b57cec5SDimitry Andric // type(^)(T) 1893*0b57cec5SDimitry Andric // T(^)() 1894*0b57cec5SDimitry Andric // T(^)(T) 1895*0b57cec5SDimitry Andric case Type::BlockPointer: { 1896*0b57cec5SDimitry Andric const BlockPointerType *BlockPtrParam = cast<BlockPointerType>(Param); 1897*0b57cec5SDimitry Andric const BlockPointerType *BlockPtrArg = dyn_cast<BlockPointerType>(Arg); 1898*0b57cec5SDimitry Andric 1899*0b57cec5SDimitry Andric if (!BlockPtrArg) 1900*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1901*0b57cec5SDimitry Andric 1902*0b57cec5SDimitry Andric return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, 1903*0b57cec5SDimitry Andric BlockPtrParam->getPointeeType(), 1904*0b57cec5SDimitry Andric BlockPtrArg->getPointeeType(), 1905*0b57cec5SDimitry Andric Info, Deduced, 0); 1906*0b57cec5SDimitry Andric } 1907*0b57cec5SDimitry Andric 1908*0b57cec5SDimitry Andric // (clang extension) 1909*0b57cec5SDimitry Andric // 1910*0b57cec5SDimitry Andric // T __attribute__(((ext_vector_type(<integral constant>)))) 1911*0b57cec5SDimitry Andric case Type::ExtVector: { 1912*0b57cec5SDimitry Andric const ExtVectorType *VectorParam = cast<ExtVectorType>(Param); 1913*0b57cec5SDimitry Andric if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { 1914*0b57cec5SDimitry Andric // Make sure that the vectors have the same number of elements. 1915*0b57cec5SDimitry Andric if (VectorParam->getNumElements() != VectorArg->getNumElements()) 1916*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1917*0b57cec5SDimitry Andric 1918*0b57cec5SDimitry Andric // Perform deduction on the element types. 1919*0b57cec5SDimitry Andric return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, 1920*0b57cec5SDimitry Andric VectorParam->getElementType(), 1921*0b57cec5SDimitry Andric VectorArg->getElementType(), 1922*0b57cec5SDimitry Andric Info, Deduced, TDF); 1923*0b57cec5SDimitry Andric } 1924*0b57cec5SDimitry Andric 1925*0b57cec5SDimitry Andric if (const DependentSizedExtVectorType *VectorArg 1926*0b57cec5SDimitry Andric = dyn_cast<DependentSizedExtVectorType>(Arg)) { 1927*0b57cec5SDimitry Andric // We can't check the number of elements, since the argument has a 1928*0b57cec5SDimitry Andric // dependent number of elements. This can only occur during partial 1929*0b57cec5SDimitry Andric // ordering. 1930*0b57cec5SDimitry Andric 1931*0b57cec5SDimitry Andric // Perform deduction on the element types. 1932*0b57cec5SDimitry Andric return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, 1933*0b57cec5SDimitry Andric VectorParam->getElementType(), 1934*0b57cec5SDimitry Andric VectorArg->getElementType(), 1935*0b57cec5SDimitry Andric Info, Deduced, TDF); 1936*0b57cec5SDimitry Andric } 1937*0b57cec5SDimitry Andric 1938*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1939*0b57cec5SDimitry Andric } 1940*0b57cec5SDimitry Andric 1941*0b57cec5SDimitry Andric case Type::DependentVector: { 1942*0b57cec5SDimitry Andric const auto *VectorParam = cast<DependentVectorType>(Param); 1943*0b57cec5SDimitry Andric 1944*0b57cec5SDimitry Andric if (const auto *VectorArg = dyn_cast<VectorType>(Arg)) { 1945*0b57cec5SDimitry Andric // Perform deduction on the element types. 1946*0b57cec5SDimitry Andric if (Sema::TemplateDeductionResult Result = 1947*0b57cec5SDimitry Andric DeduceTemplateArgumentsByTypeMatch( 1948*0b57cec5SDimitry Andric S, TemplateParams, VectorParam->getElementType(), 1949*0b57cec5SDimitry Andric VectorArg->getElementType(), Info, Deduced, TDF)) 1950*0b57cec5SDimitry Andric return Result; 1951*0b57cec5SDimitry Andric 1952*0b57cec5SDimitry Andric // Perform deduction on the vector size, if we can. 1953*0b57cec5SDimitry Andric NonTypeTemplateParmDecl *NTTP = 1954*0b57cec5SDimitry Andric getDeducedParameterFromExpr(Info, VectorParam->getSizeExpr()); 1955*0b57cec5SDimitry Andric if (!NTTP) 1956*0b57cec5SDimitry Andric return Sema::TDK_Success; 1957*0b57cec5SDimitry Andric 1958*0b57cec5SDimitry Andric llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); 1959*0b57cec5SDimitry Andric ArgSize = VectorArg->getNumElements(); 1960*0b57cec5SDimitry Andric // Note that we use the "array bound" rules here; just like in that 1961*0b57cec5SDimitry Andric // case, we don't have any particular type for the vector size, but 1962*0b57cec5SDimitry Andric // we can provide one if necessary. 1963*0b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, 1964*0b57cec5SDimitry Andric S.Context.UnsignedIntTy, true, 1965*0b57cec5SDimitry Andric Info, Deduced); 1966*0b57cec5SDimitry Andric } 1967*0b57cec5SDimitry Andric 1968*0b57cec5SDimitry Andric if (const auto *VectorArg = dyn_cast<DependentVectorType>(Arg)) { 1969*0b57cec5SDimitry Andric // Perform deduction on the element types. 1970*0b57cec5SDimitry Andric if (Sema::TemplateDeductionResult Result = 1971*0b57cec5SDimitry Andric DeduceTemplateArgumentsByTypeMatch( 1972*0b57cec5SDimitry Andric S, TemplateParams, VectorParam->getElementType(), 1973*0b57cec5SDimitry Andric VectorArg->getElementType(), Info, Deduced, TDF)) 1974*0b57cec5SDimitry Andric return Result; 1975*0b57cec5SDimitry Andric 1976*0b57cec5SDimitry Andric // Perform deduction on the vector size, if we can. 1977*0b57cec5SDimitry Andric NonTypeTemplateParmDecl *NTTP = getDeducedParameterFromExpr( 1978*0b57cec5SDimitry Andric Info, VectorParam->getSizeExpr()); 1979*0b57cec5SDimitry Andric if (!NTTP) 1980*0b57cec5SDimitry Andric return Sema::TDK_Success; 1981*0b57cec5SDimitry Andric 1982*0b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument( 1983*0b57cec5SDimitry Andric S, TemplateParams, NTTP, VectorArg->getSizeExpr(), Info, Deduced); 1984*0b57cec5SDimitry Andric } 1985*0b57cec5SDimitry Andric 1986*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 1987*0b57cec5SDimitry Andric } 1988*0b57cec5SDimitry Andric 1989*0b57cec5SDimitry Andric // (clang extension) 1990*0b57cec5SDimitry Andric // 1991*0b57cec5SDimitry Andric // T __attribute__(((ext_vector_type(N)))) 1992*0b57cec5SDimitry Andric case Type::DependentSizedExtVector: { 1993*0b57cec5SDimitry Andric const DependentSizedExtVectorType *VectorParam 1994*0b57cec5SDimitry Andric = cast<DependentSizedExtVectorType>(Param); 1995*0b57cec5SDimitry Andric 1996*0b57cec5SDimitry Andric if (const ExtVectorType *VectorArg = dyn_cast<ExtVectorType>(Arg)) { 1997*0b57cec5SDimitry Andric // Perform deduction on the element types. 1998*0b57cec5SDimitry Andric if (Sema::TemplateDeductionResult Result 1999*0b57cec5SDimitry Andric = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, 2000*0b57cec5SDimitry Andric VectorParam->getElementType(), 2001*0b57cec5SDimitry Andric VectorArg->getElementType(), 2002*0b57cec5SDimitry Andric Info, Deduced, TDF)) 2003*0b57cec5SDimitry Andric return Result; 2004*0b57cec5SDimitry Andric 2005*0b57cec5SDimitry Andric // Perform deduction on the vector size, if we can. 2006*0b57cec5SDimitry Andric NonTypeTemplateParmDecl *NTTP 2007*0b57cec5SDimitry Andric = getDeducedParameterFromExpr(Info, VectorParam->getSizeExpr()); 2008*0b57cec5SDimitry Andric if (!NTTP) 2009*0b57cec5SDimitry Andric return Sema::TDK_Success; 2010*0b57cec5SDimitry Andric 2011*0b57cec5SDimitry Andric llvm::APSInt ArgSize(S.Context.getTypeSize(S.Context.IntTy), false); 2012*0b57cec5SDimitry Andric ArgSize = VectorArg->getNumElements(); 2013*0b57cec5SDimitry Andric // Note that we use the "array bound" rules here; just like in that 2014*0b57cec5SDimitry Andric // case, we don't have any particular type for the vector size, but 2015*0b57cec5SDimitry Andric // we can provide one if necessary. 2016*0b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, ArgSize, 2017*0b57cec5SDimitry Andric S.Context.IntTy, true, Info, 2018*0b57cec5SDimitry Andric Deduced); 2019*0b57cec5SDimitry Andric } 2020*0b57cec5SDimitry Andric 2021*0b57cec5SDimitry Andric if (const DependentSizedExtVectorType *VectorArg 2022*0b57cec5SDimitry Andric = dyn_cast<DependentSizedExtVectorType>(Arg)) { 2023*0b57cec5SDimitry Andric // Perform deduction on the element types. 2024*0b57cec5SDimitry Andric if (Sema::TemplateDeductionResult Result 2025*0b57cec5SDimitry Andric = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, 2026*0b57cec5SDimitry Andric VectorParam->getElementType(), 2027*0b57cec5SDimitry Andric VectorArg->getElementType(), 2028*0b57cec5SDimitry Andric Info, Deduced, TDF)) 2029*0b57cec5SDimitry Andric return Result; 2030*0b57cec5SDimitry Andric 2031*0b57cec5SDimitry Andric // Perform deduction on the vector size, if we can. 2032*0b57cec5SDimitry Andric NonTypeTemplateParmDecl *NTTP 2033*0b57cec5SDimitry Andric = getDeducedParameterFromExpr(Info, VectorParam->getSizeExpr()); 2034*0b57cec5SDimitry Andric if (!NTTP) 2035*0b57cec5SDimitry Andric return Sema::TDK_Success; 2036*0b57cec5SDimitry Andric 2037*0b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, 2038*0b57cec5SDimitry Andric VectorArg->getSizeExpr(), 2039*0b57cec5SDimitry Andric Info, Deduced); 2040*0b57cec5SDimitry Andric } 2041*0b57cec5SDimitry Andric 2042*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 2043*0b57cec5SDimitry Andric } 2044*0b57cec5SDimitry Andric 2045*0b57cec5SDimitry Andric // (clang extension) 2046*0b57cec5SDimitry Andric // 2047*0b57cec5SDimitry Andric // T __attribute__(((address_space(N)))) 2048*0b57cec5SDimitry Andric case Type::DependentAddressSpace: { 2049*0b57cec5SDimitry Andric const DependentAddressSpaceType *AddressSpaceParam = 2050*0b57cec5SDimitry Andric cast<DependentAddressSpaceType>(Param); 2051*0b57cec5SDimitry Andric 2052*0b57cec5SDimitry Andric if (const DependentAddressSpaceType *AddressSpaceArg = 2053*0b57cec5SDimitry Andric dyn_cast<DependentAddressSpaceType>(Arg)) { 2054*0b57cec5SDimitry Andric // Perform deduction on the pointer type. 2055*0b57cec5SDimitry Andric if (Sema::TemplateDeductionResult Result = 2056*0b57cec5SDimitry Andric DeduceTemplateArgumentsByTypeMatch( 2057*0b57cec5SDimitry Andric S, TemplateParams, AddressSpaceParam->getPointeeType(), 2058*0b57cec5SDimitry Andric AddressSpaceArg->getPointeeType(), Info, Deduced, TDF)) 2059*0b57cec5SDimitry Andric return Result; 2060*0b57cec5SDimitry Andric 2061*0b57cec5SDimitry Andric // Perform deduction on the address space, if we can. 2062*0b57cec5SDimitry Andric NonTypeTemplateParmDecl *NTTP = getDeducedParameterFromExpr( 2063*0b57cec5SDimitry Andric Info, AddressSpaceParam->getAddrSpaceExpr()); 2064*0b57cec5SDimitry Andric if (!NTTP) 2065*0b57cec5SDimitry Andric return Sema::TDK_Success; 2066*0b57cec5SDimitry Andric 2067*0b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument( 2068*0b57cec5SDimitry Andric S, TemplateParams, NTTP, AddressSpaceArg->getAddrSpaceExpr(), Info, 2069*0b57cec5SDimitry Andric Deduced); 2070*0b57cec5SDimitry Andric } 2071*0b57cec5SDimitry Andric 2072*0b57cec5SDimitry Andric if (isTargetAddressSpace(Arg.getAddressSpace())) { 2073*0b57cec5SDimitry Andric llvm::APSInt ArgAddressSpace(S.Context.getTypeSize(S.Context.IntTy), 2074*0b57cec5SDimitry Andric false); 2075*0b57cec5SDimitry Andric ArgAddressSpace = toTargetAddressSpace(Arg.getAddressSpace()); 2076*0b57cec5SDimitry Andric 2077*0b57cec5SDimitry Andric // Perform deduction on the pointer types. 2078*0b57cec5SDimitry Andric if (Sema::TemplateDeductionResult Result = 2079*0b57cec5SDimitry Andric DeduceTemplateArgumentsByTypeMatch( 2080*0b57cec5SDimitry Andric S, TemplateParams, AddressSpaceParam->getPointeeType(), 2081*0b57cec5SDimitry Andric S.Context.removeAddrSpaceQualType(Arg), Info, Deduced, TDF)) 2082*0b57cec5SDimitry Andric return Result; 2083*0b57cec5SDimitry Andric 2084*0b57cec5SDimitry Andric // Perform deduction on the address space, if we can. 2085*0b57cec5SDimitry Andric NonTypeTemplateParmDecl *NTTP = getDeducedParameterFromExpr( 2086*0b57cec5SDimitry Andric Info, AddressSpaceParam->getAddrSpaceExpr()); 2087*0b57cec5SDimitry Andric if (!NTTP) 2088*0b57cec5SDimitry Andric return Sema::TDK_Success; 2089*0b57cec5SDimitry Andric 2090*0b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, 2091*0b57cec5SDimitry Andric ArgAddressSpace, S.Context.IntTy, 2092*0b57cec5SDimitry Andric true, Info, Deduced); 2093*0b57cec5SDimitry Andric } 2094*0b57cec5SDimitry Andric 2095*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 2096*0b57cec5SDimitry Andric } 2097*0b57cec5SDimitry Andric 2098*0b57cec5SDimitry Andric case Type::TypeOfExpr: 2099*0b57cec5SDimitry Andric case Type::TypeOf: 2100*0b57cec5SDimitry Andric case Type::DependentName: 2101*0b57cec5SDimitry Andric case Type::UnresolvedUsing: 2102*0b57cec5SDimitry Andric case Type::Decltype: 2103*0b57cec5SDimitry Andric case Type::UnaryTransform: 2104*0b57cec5SDimitry Andric case Type::Auto: 2105*0b57cec5SDimitry Andric case Type::DeducedTemplateSpecialization: 2106*0b57cec5SDimitry Andric case Type::DependentTemplateSpecialization: 2107*0b57cec5SDimitry Andric case Type::PackExpansion: 2108*0b57cec5SDimitry Andric case Type::Pipe: 2109*0b57cec5SDimitry Andric // No template argument deduction for these types 2110*0b57cec5SDimitry Andric return Sema::TDK_Success; 2111*0b57cec5SDimitry Andric } 2112*0b57cec5SDimitry Andric 2113*0b57cec5SDimitry Andric llvm_unreachable("Invalid Type Class!"); 2114*0b57cec5SDimitry Andric } 2115*0b57cec5SDimitry Andric 2116*0b57cec5SDimitry Andric static Sema::TemplateDeductionResult 2117*0b57cec5SDimitry Andric DeduceTemplateArguments(Sema &S, 2118*0b57cec5SDimitry Andric TemplateParameterList *TemplateParams, 2119*0b57cec5SDimitry Andric const TemplateArgument &Param, 2120*0b57cec5SDimitry Andric TemplateArgument Arg, 2121*0b57cec5SDimitry Andric TemplateDeductionInfo &Info, 2122*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 2123*0b57cec5SDimitry Andric // If the template argument is a pack expansion, perform template argument 2124*0b57cec5SDimitry Andric // deduction against the pattern of that expansion. This only occurs during 2125*0b57cec5SDimitry Andric // partial ordering. 2126*0b57cec5SDimitry Andric if (Arg.isPackExpansion()) 2127*0b57cec5SDimitry Andric Arg = Arg.getPackExpansionPattern(); 2128*0b57cec5SDimitry Andric 2129*0b57cec5SDimitry Andric switch (Param.getKind()) { 2130*0b57cec5SDimitry Andric case TemplateArgument::Null: 2131*0b57cec5SDimitry Andric llvm_unreachable("Null template argument in parameter list"); 2132*0b57cec5SDimitry Andric 2133*0b57cec5SDimitry Andric case TemplateArgument::Type: 2134*0b57cec5SDimitry Andric if (Arg.getKind() == TemplateArgument::Type) 2135*0b57cec5SDimitry Andric return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, 2136*0b57cec5SDimitry Andric Param.getAsType(), 2137*0b57cec5SDimitry Andric Arg.getAsType(), 2138*0b57cec5SDimitry Andric Info, Deduced, 0); 2139*0b57cec5SDimitry Andric Info.FirstArg = Param; 2140*0b57cec5SDimitry Andric Info.SecondArg = Arg; 2141*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 2142*0b57cec5SDimitry Andric 2143*0b57cec5SDimitry Andric case TemplateArgument::Template: 2144*0b57cec5SDimitry Andric if (Arg.getKind() == TemplateArgument::Template) 2145*0b57cec5SDimitry Andric return DeduceTemplateArguments(S, TemplateParams, 2146*0b57cec5SDimitry Andric Param.getAsTemplate(), 2147*0b57cec5SDimitry Andric Arg.getAsTemplate(), Info, Deduced); 2148*0b57cec5SDimitry Andric Info.FirstArg = Param; 2149*0b57cec5SDimitry Andric Info.SecondArg = Arg; 2150*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 2151*0b57cec5SDimitry Andric 2152*0b57cec5SDimitry Andric case TemplateArgument::TemplateExpansion: 2153*0b57cec5SDimitry Andric llvm_unreachable("caller should handle pack expansions"); 2154*0b57cec5SDimitry Andric 2155*0b57cec5SDimitry Andric case TemplateArgument::Declaration: 2156*0b57cec5SDimitry Andric if (Arg.getKind() == TemplateArgument::Declaration && 2157*0b57cec5SDimitry Andric isSameDeclaration(Param.getAsDecl(), Arg.getAsDecl())) 2158*0b57cec5SDimitry Andric return Sema::TDK_Success; 2159*0b57cec5SDimitry Andric 2160*0b57cec5SDimitry Andric Info.FirstArg = Param; 2161*0b57cec5SDimitry Andric Info.SecondArg = Arg; 2162*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 2163*0b57cec5SDimitry Andric 2164*0b57cec5SDimitry Andric case TemplateArgument::NullPtr: 2165*0b57cec5SDimitry Andric if (Arg.getKind() == TemplateArgument::NullPtr && 2166*0b57cec5SDimitry Andric S.Context.hasSameType(Param.getNullPtrType(), Arg.getNullPtrType())) 2167*0b57cec5SDimitry Andric return Sema::TDK_Success; 2168*0b57cec5SDimitry Andric 2169*0b57cec5SDimitry Andric Info.FirstArg = Param; 2170*0b57cec5SDimitry Andric Info.SecondArg = Arg; 2171*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 2172*0b57cec5SDimitry Andric 2173*0b57cec5SDimitry Andric case TemplateArgument::Integral: 2174*0b57cec5SDimitry Andric if (Arg.getKind() == TemplateArgument::Integral) { 2175*0b57cec5SDimitry Andric if (hasSameExtendedValue(Param.getAsIntegral(), Arg.getAsIntegral())) 2176*0b57cec5SDimitry Andric return Sema::TDK_Success; 2177*0b57cec5SDimitry Andric 2178*0b57cec5SDimitry Andric Info.FirstArg = Param; 2179*0b57cec5SDimitry Andric Info.SecondArg = Arg; 2180*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 2181*0b57cec5SDimitry Andric } 2182*0b57cec5SDimitry Andric 2183*0b57cec5SDimitry Andric if (Arg.getKind() == TemplateArgument::Expression) { 2184*0b57cec5SDimitry Andric Info.FirstArg = Param; 2185*0b57cec5SDimitry Andric Info.SecondArg = Arg; 2186*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 2187*0b57cec5SDimitry Andric } 2188*0b57cec5SDimitry Andric 2189*0b57cec5SDimitry Andric Info.FirstArg = Param; 2190*0b57cec5SDimitry Andric Info.SecondArg = Arg; 2191*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 2192*0b57cec5SDimitry Andric 2193*0b57cec5SDimitry Andric case TemplateArgument::Expression: 2194*0b57cec5SDimitry Andric if (NonTypeTemplateParmDecl *NTTP 2195*0b57cec5SDimitry Andric = getDeducedParameterFromExpr(Info, Param.getAsExpr())) { 2196*0b57cec5SDimitry Andric if (Arg.getKind() == TemplateArgument::Integral) 2197*0b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, 2198*0b57cec5SDimitry Andric Arg.getAsIntegral(), 2199*0b57cec5SDimitry Andric Arg.getIntegralType(), 2200*0b57cec5SDimitry Andric /*ArrayBound=*/false, 2201*0b57cec5SDimitry Andric Info, Deduced); 2202*0b57cec5SDimitry Andric if (Arg.getKind() == TemplateArgument::NullPtr) 2203*0b57cec5SDimitry Andric return DeduceNullPtrTemplateArgument(S, TemplateParams, NTTP, 2204*0b57cec5SDimitry Andric Arg.getNullPtrType(), 2205*0b57cec5SDimitry Andric Info, Deduced); 2206*0b57cec5SDimitry Andric if (Arg.getKind() == TemplateArgument::Expression) 2207*0b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, 2208*0b57cec5SDimitry Andric Arg.getAsExpr(), Info, Deduced); 2209*0b57cec5SDimitry Andric if (Arg.getKind() == TemplateArgument::Declaration) 2210*0b57cec5SDimitry Andric return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP, 2211*0b57cec5SDimitry Andric Arg.getAsDecl(), 2212*0b57cec5SDimitry Andric Arg.getParamTypeForDecl(), 2213*0b57cec5SDimitry Andric Info, Deduced); 2214*0b57cec5SDimitry Andric 2215*0b57cec5SDimitry Andric Info.FirstArg = Param; 2216*0b57cec5SDimitry Andric Info.SecondArg = Arg; 2217*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 2218*0b57cec5SDimitry Andric } 2219*0b57cec5SDimitry Andric 2220*0b57cec5SDimitry Andric // Can't deduce anything, but that's okay. 2221*0b57cec5SDimitry Andric return Sema::TDK_Success; 2222*0b57cec5SDimitry Andric 2223*0b57cec5SDimitry Andric case TemplateArgument::Pack: 2224*0b57cec5SDimitry Andric llvm_unreachable("Argument packs should be expanded by the caller!"); 2225*0b57cec5SDimitry Andric } 2226*0b57cec5SDimitry Andric 2227*0b57cec5SDimitry Andric llvm_unreachable("Invalid TemplateArgument Kind!"); 2228*0b57cec5SDimitry Andric } 2229*0b57cec5SDimitry Andric 2230*0b57cec5SDimitry Andric /// Determine whether there is a template argument to be used for 2231*0b57cec5SDimitry Andric /// deduction. 2232*0b57cec5SDimitry Andric /// 2233*0b57cec5SDimitry Andric /// This routine "expands" argument packs in-place, overriding its input 2234*0b57cec5SDimitry Andric /// parameters so that \c Args[ArgIdx] will be the available template argument. 2235*0b57cec5SDimitry Andric /// 2236*0b57cec5SDimitry Andric /// \returns true if there is another template argument (which will be at 2237*0b57cec5SDimitry Andric /// \c Args[ArgIdx]), false otherwise. 2238*0b57cec5SDimitry Andric static bool hasTemplateArgumentForDeduction(ArrayRef<TemplateArgument> &Args, 2239*0b57cec5SDimitry Andric unsigned &ArgIdx) { 2240*0b57cec5SDimitry Andric if (ArgIdx == Args.size()) 2241*0b57cec5SDimitry Andric return false; 2242*0b57cec5SDimitry Andric 2243*0b57cec5SDimitry Andric const TemplateArgument &Arg = Args[ArgIdx]; 2244*0b57cec5SDimitry Andric if (Arg.getKind() != TemplateArgument::Pack) 2245*0b57cec5SDimitry Andric return true; 2246*0b57cec5SDimitry Andric 2247*0b57cec5SDimitry Andric assert(ArgIdx == Args.size() - 1 && "Pack not at the end of argument list?"); 2248*0b57cec5SDimitry Andric Args = Arg.pack_elements(); 2249*0b57cec5SDimitry Andric ArgIdx = 0; 2250*0b57cec5SDimitry Andric return ArgIdx < Args.size(); 2251*0b57cec5SDimitry Andric } 2252*0b57cec5SDimitry Andric 2253*0b57cec5SDimitry Andric /// Determine whether the given set of template arguments has a pack 2254*0b57cec5SDimitry Andric /// expansion that is not the last template argument. 2255*0b57cec5SDimitry Andric static bool hasPackExpansionBeforeEnd(ArrayRef<TemplateArgument> Args) { 2256*0b57cec5SDimitry Andric bool FoundPackExpansion = false; 2257*0b57cec5SDimitry Andric for (const auto &A : Args) { 2258*0b57cec5SDimitry Andric if (FoundPackExpansion) 2259*0b57cec5SDimitry Andric return true; 2260*0b57cec5SDimitry Andric 2261*0b57cec5SDimitry Andric if (A.getKind() == TemplateArgument::Pack) 2262*0b57cec5SDimitry Andric return hasPackExpansionBeforeEnd(A.pack_elements()); 2263*0b57cec5SDimitry Andric 2264*0b57cec5SDimitry Andric // FIXME: If this is a fixed-arity pack expansion from an outer level of 2265*0b57cec5SDimitry Andric // templates, it should not be treated as a pack expansion. 2266*0b57cec5SDimitry Andric if (A.isPackExpansion()) 2267*0b57cec5SDimitry Andric FoundPackExpansion = true; 2268*0b57cec5SDimitry Andric } 2269*0b57cec5SDimitry Andric 2270*0b57cec5SDimitry Andric return false; 2271*0b57cec5SDimitry Andric } 2272*0b57cec5SDimitry Andric 2273*0b57cec5SDimitry Andric static Sema::TemplateDeductionResult 2274*0b57cec5SDimitry Andric DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams, 2275*0b57cec5SDimitry Andric ArrayRef<TemplateArgument> Params, 2276*0b57cec5SDimitry Andric ArrayRef<TemplateArgument> Args, 2277*0b57cec5SDimitry Andric TemplateDeductionInfo &Info, 2278*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 2279*0b57cec5SDimitry Andric bool NumberOfArgumentsMustMatch) { 2280*0b57cec5SDimitry Andric // C++0x [temp.deduct.type]p9: 2281*0b57cec5SDimitry Andric // If the template argument list of P contains a pack expansion that is not 2282*0b57cec5SDimitry Andric // the last template argument, the entire template argument list is a 2283*0b57cec5SDimitry Andric // non-deduced context. 2284*0b57cec5SDimitry Andric if (hasPackExpansionBeforeEnd(Params)) 2285*0b57cec5SDimitry Andric return Sema::TDK_Success; 2286*0b57cec5SDimitry Andric 2287*0b57cec5SDimitry Andric // C++0x [temp.deduct.type]p9: 2288*0b57cec5SDimitry Andric // If P has a form that contains <T> or <i>, then each argument Pi of the 2289*0b57cec5SDimitry Andric // respective template argument list P is compared with the corresponding 2290*0b57cec5SDimitry Andric // argument Ai of the corresponding template argument list of A. 2291*0b57cec5SDimitry Andric unsigned ArgIdx = 0, ParamIdx = 0; 2292*0b57cec5SDimitry Andric for (; hasTemplateArgumentForDeduction(Params, ParamIdx); ++ParamIdx) { 2293*0b57cec5SDimitry Andric if (!Params[ParamIdx].isPackExpansion()) { 2294*0b57cec5SDimitry Andric // The simple case: deduce template arguments by matching Pi and Ai. 2295*0b57cec5SDimitry Andric 2296*0b57cec5SDimitry Andric // Check whether we have enough arguments. 2297*0b57cec5SDimitry Andric if (!hasTemplateArgumentForDeduction(Args, ArgIdx)) 2298*0b57cec5SDimitry Andric return NumberOfArgumentsMustMatch 2299*0b57cec5SDimitry Andric ? Sema::TDK_MiscellaneousDeductionFailure 2300*0b57cec5SDimitry Andric : Sema::TDK_Success; 2301*0b57cec5SDimitry Andric 2302*0b57cec5SDimitry Andric // C++1z [temp.deduct.type]p9: 2303*0b57cec5SDimitry Andric // During partial ordering, if Ai was originally a pack expansion [and] 2304*0b57cec5SDimitry Andric // Pi is not a pack expansion, template argument deduction fails. 2305*0b57cec5SDimitry Andric if (Args[ArgIdx].isPackExpansion()) 2306*0b57cec5SDimitry Andric return Sema::TDK_MiscellaneousDeductionFailure; 2307*0b57cec5SDimitry Andric 2308*0b57cec5SDimitry Andric // Perform deduction for this Pi/Ai pair. 2309*0b57cec5SDimitry Andric if (Sema::TemplateDeductionResult Result 2310*0b57cec5SDimitry Andric = DeduceTemplateArguments(S, TemplateParams, 2311*0b57cec5SDimitry Andric Params[ParamIdx], Args[ArgIdx], 2312*0b57cec5SDimitry Andric Info, Deduced)) 2313*0b57cec5SDimitry Andric return Result; 2314*0b57cec5SDimitry Andric 2315*0b57cec5SDimitry Andric // Move to the next argument. 2316*0b57cec5SDimitry Andric ++ArgIdx; 2317*0b57cec5SDimitry Andric continue; 2318*0b57cec5SDimitry Andric } 2319*0b57cec5SDimitry Andric 2320*0b57cec5SDimitry Andric // The parameter is a pack expansion. 2321*0b57cec5SDimitry Andric 2322*0b57cec5SDimitry Andric // C++0x [temp.deduct.type]p9: 2323*0b57cec5SDimitry Andric // If Pi is a pack expansion, then the pattern of Pi is compared with 2324*0b57cec5SDimitry Andric // each remaining argument in the template argument list of A. Each 2325*0b57cec5SDimitry Andric // comparison deduces template arguments for subsequent positions in the 2326*0b57cec5SDimitry Andric // template parameter packs expanded by Pi. 2327*0b57cec5SDimitry Andric TemplateArgument Pattern = Params[ParamIdx].getPackExpansionPattern(); 2328*0b57cec5SDimitry Andric 2329*0b57cec5SDimitry Andric // Prepare to deduce the packs within the pattern. 2330*0b57cec5SDimitry Andric PackDeductionScope PackScope(S, TemplateParams, Deduced, Info, Pattern); 2331*0b57cec5SDimitry Andric 2332*0b57cec5SDimitry Andric // Keep track of the deduced template arguments for each parameter pack 2333*0b57cec5SDimitry Andric // expanded by this pack expansion (the outer index) and for each 2334*0b57cec5SDimitry Andric // template argument (the inner SmallVectors). 2335*0b57cec5SDimitry Andric for (; hasTemplateArgumentForDeduction(Args, ArgIdx) && 2336*0b57cec5SDimitry Andric PackScope.hasNextElement(); 2337*0b57cec5SDimitry Andric ++ArgIdx) { 2338*0b57cec5SDimitry Andric // Deduce template arguments from the pattern. 2339*0b57cec5SDimitry Andric if (Sema::TemplateDeductionResult Result 2340*0b57cec5SDimitry Andric = DeduceTemplateArguments(S, TemplateParams, Pattern, Args[ArgIdx], 2341*0b57cec5SDimitry Andric Info, Deduced)) 2342*0b57cec5SDimitry Andric return Result; 2343*0b57cec5SDimitry Andric 2344*0b57cec5SDimitry Andric PackScope.nextPackElement(); 2345*0b57cec5SDimitry Andric } 2346*0b57cec5SDimitry Andric 2347*0b57cec5SDimitry Andric // Build argument packs for each of the parameter packs expanded by this 2348*0b57cec5SDimitry Andric // pack expansion. 2349*0b57cec5SDimitry Andric if (auto Result = PackScope.finish()) 2350*0b57cec5SDimitry Andric return Result; 2351*0b57cec5SDimitry Andric } 2352*0b57cec5SDimitry Andric 2353*0b57cec5SDimitry Andric return Sema::TDK_Success; 2354*0b57cec5SDimitry Andric } 2355*0b57cec5SDimitry Andric 2356*0b57cec5SDimitry Andric static Sema::TemplateDeductionResult 2357*0b57cec5SDimitry Andric DeduceTemplateArguments(Sema &S, 2358*0b57cec5SDimitry Andric TemplateParameterList *TemplateParams, 2359*0b57cec5SDimitry Andric const TemplateArgumentList &ParamList, 2360*0b57cec5SDimitry Andric const TemplateArgumentList &ArgList, 2361*0b57cec5SDimitry Andric TemplateDeductionInfo &Info, 2362*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced) { 2363*0b57cec5SDimitry Andric return DeduceTemplateArguments(S, TemplateParams, ParamList.asArray(), 2364*0b57cec5SDimitry Andric ArgList.asArray(), Info, Deduced, 2365*0b57cec5SDimitry Andric /*NumberOfArgumentsMustMatch*/false); 2366*0b57cec5SDimitry Andric } 2367*0b57cec5SDimitry Andric 2368*0b57cec5SDimitry Andric /// Determine whether two template arguments are the same. 2369*0b57cec5SDimitry Andric static bool isSameTemplateArg(ASTContext &Context, 2370*0b57cec5SDimitry Andric TemplateArgument X, 2371*0b57cec5SDimitry Andric const TemplateArgument &Y, 2372*0b57cec5SDimitry Andric bool PackExpansionMatchesPack = false) { 2373*0b57cec5SDimitry Andric // If we're checking deduced arguments (X) against original arguments (Y), 2374*0b57cec5SDimitry Andric // we will have flattened packs to non-expansions in X. 2375*0b57cec5SDimitry Andric if (PackExpansionMatchesPack && X.isPackExpansion() && !Y.isPackExpansion()) 2376*0b57cec5SDimitry Andric X = X.getPackExpansionPattern(); 2377*0b57cec5SDimitry Andric 2378*0b57cec5SDimitry Andric if (X.getKind() != Y.getKind()) 2379*0b57cec5SDimitry Andric return false; 2380*0b57cec5SDimitry Andric 2381*0b57cec5SDimitry Andric switch (X.getKind()) { 2382*0b57cec5SDimitry Andric case TemplateArgument::Null: 2383*0b57cec5SDimitry Andric llvm_unreachable("Comparing NULL template argument"); 2384*0b57cec5SDimitry Andric 2385*0b57cec5SDimitry Andric case TemplateArgument::Type: 2386*0b57cec5SDimitry Andric return Context.getCanonicalType(X.getAsType()) == 2387*0b57cec5SDimitry Andric Context.getCanonicalType(Y.getAsType()); 2388*0b57cec5SDimitry Andric 2389*0b57cec5SDimitry Andric case TemplateArgument::Declaration: 2390*0b57cec5SDimitry Andric return isSameDeclaration(X.getAsDecl(), Y.getAsDecl()); 2391*0b57cec5SDimitry Andric 2392*0b57cec5SDimitry Andric case TemplateArgument::NullPtr: 2393*0b57cec5SDimitry Andric return Context.hasSameType(X.getNullPtrType(), Y.getNullPtrType()); 2394*0b57cec5SDimitry Andric 2395*0b57cec5SDimitry Andric case TemplateArgument::Template: 2396*0b57cec5SDimitry Andric case TemplateArgument::TemplateExpansion: 2397*0b57cec5SDimitry Andric return Context.getCanonicalTemplateName( 2398*0b57cec5SDimitry Andric X.getAsTemplateOrTemplatePattern()).getAsVoidPointer() == 2399*0b57cec5SDimitry Andric Context.getCanonicalTemplateName( 2400*0b57cec5SDimitry Andric Y.getAsTemplateOrTemplatePattern()).getAsVoidPointer(); 2401*0b57cec5SDimitry Andric 2402*0b57cec5SDimitry Andric case TemplateArgument::Integral: 2403*0b57cec5SDimitry Andric return hasSameExtendedValue(X.getAsIntegral(), Y.getAsIntegral()); 2404*0b57cec5SDimitry Andric 2405*0b57cec5SDimitry Andric case TemplateArgument::Expression: { 2406*0b57cec5SDimitry Andric llvm::FoldingSetNodeID XID, YID; 2407*0b57cec5SDimitry Andric X.getAsExpr()->Profile(XID, Context, true); 2408*0b57cec5SDimitry Andric Y.getAsExpr()->Profile(YID, Context, true); 2409*0b57cec5SDimitry Andric return XID == YID; 2410*0b57cec5SDimitry Andric } 2411*0b57cec5SDimitry Andric 2412*0b57cec5SDimitry Andric case TemplateArgument::Pack: 2413*0b57cec5SDimitry Andric if (X.pack_size() != Y.pack_size()) 2414*0b57cec5SDimitry Andric return false; 2415*0b57cec5SDimitry Andric 2416*0b57cec5SDimitry Andric for (TemplateArgument::pack_iterator XP = X.pack_begin(), 2417*0b57cec5SDimitry Andric XPEnd = X.pack_end(), 2418*0b57cec5SDimitry Andric YP = Y.pack_begin(); 2419*0b57cec5SDimitry Andric XP != XPEnd; ++XP, ++YP) 2420*0b57cec5SDimitry Andric if (!isSameTemplateArg(Context, *XP, *YP, PackExpansionMatchesPack)) 2421*0b57cec5SDimitry Andric return false; 2422*0b57cec5SDimitry Andric 2423*0b57cec5SDimitry Andric return true; 2424*0b57cec5SDimitry Andric } 2425*0b57cec5SDimitry Andric 2426*0b57cec5SDimitry Andric llvm_unreachable("Invalid TemplateArgument Kind!"); 2427*0b57cec5SDimitry Andric } 2428*0b57cec5SDimitry Andric 2429*0b57cec5SDimitry Andric /// Allocate a TemplateArgumentLoc where all locations have 2430*0b57cec5SDimitry Andric /// been initialized to the given location. 2431*0b57cec5SDimitry Andric /// 2432*0b57cec5SDimitry Andric /// \param Arg The template argument we are producing template argument 2433*0b57cec5SDimitry Andric /// location information for. 2434*0b57cec5SDimitry Andric /// 2435*0b57cec5SDimitry Andric /// \param NTTPType For a declaration template argument, the type of 2436*0b57cec5SDimitry Andric /// the non-type template parameter that corresponds to this template 2437*0b57cec5SDimitry Andric /// argument. Can be null if no type sugar is available to add to the 2438*0b57cec5SDimitry Andric /// type from the template argument. 2439*0b57cec5SDimitry Andric /// 2440*0b57cec5SDimitry Andric /// \param Loc The source location to use for the resulting template 2441*0b57cec5SDimitry Andric /// argument. 2442*0b57cec5SDimitry Andric TemplateArgumentLoc 2443*0b57cec5SDimitry Andric Sema::getTrivialTemplateArgumentLoc(const TemplateArgument &Arg, 2444*0b57cec5SDimitry Andric QualType NTTPType, SourceLocation Loc) { 2445*0b57cec5SDimitry Andric switch (Arg.getKind()) { 2446*0b57cec5SDimitry Andric case TemplateArgument::Null: 2447*0b57cec5SDimitry Andric llvm_unreachable("Can't get a NULL template argument here"); 2448*0b57cec5SDimitry Andric 2449*0b57cec5SDimitry Andric case TemplateArgument::Type: 2450*0b57cec5SDimitry Andric return TemplateArgumentLoc( 2451*0b57cec5SDimitry Andric Arg, Context.getTrivialTypeSourceInfo(Arg.getAsType(), Loc)); 2452*0b57cec5SDimitry Andric 2453*0b57cec5SDimitry Andric case TemplateArgument::Declaration: { 2454*0b57cec5SDimitry Andric if (NTTPType.isNull()) 2455*0b57cec5SDimitry Andric NTTPType = Arg.getParamTypeForDecl(); 2456*0b57cec5SDimitry Andric Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) 2457*0b57cec5SDimitry Andric .getAs<Expr>(); 2458*0b57cec5SDimitry Andric return TemplateArgumentLoc(TemplateArgument(E), E); 2459*0b57cec5SDimitry Andric } 2460*0b57cec5SDimitry Andric 2461*0b57cec5SDimitry Andric case TemplateArgument::NullPtr: { 2462*0b57cec5SDimitry Andric if (NTTPType.isNull()) 2463*0b57cec5SDimitry Andric NTTPType = Arg.getNullPtrType(); 2464*0b57cec5SDimitry Andric Expr *E = BuildExpressionFromDeclTemplateArgument(Arg, NTTPType, Loc) 2465*0b57cec5SDimitry Andric .getAs<Expr>(); 2466*0b57cec5SDimitry Andric return TemplateArgumentLoc(TemplateArgument(NTTPType, /*isNullPtr*/true), 2467*0b57cec5SDimitry Andric E); 2468*0b57cec5SDimitry Andric } 2469*0b57cec5SDimitry Andric 2470*0b57cec5SDimitry Andric case TemplateArgument::Integral: { 2471*0b57cec5SDimitry Andric Expr *E = 2472*0b57cec5SDimitry Andric BuildExpressionFromIntegralTemplateArgument(Arg, Loc).getAs<Expr>(); 2473*0b57cec5SDimitry Andric return TemplateArgumentLoc(TemplateArgument(E), E); 2474*0b57cec5SDimitry Andric } 2475*0b57cec5SDimitry Andric 2476*0b57cec5SDimitry Andric case TemplateArgument::Template: 2477*0b57cec5SDimitry Andric case TemplateArgument::TemplateExpansion: { 2478*0b57cec5SDimitry Andric NestedNameSpecifierLocBuilder Builder; 2479*0b57cec5SDimitry Andric TemplateName Template = Arg.getAsTemplate(); 2480*0b57cec5SDimitry Andric if (DependentTemplateName *DTN = Template.getAsDependentTemplateName()) 2481*0b57cec5SDimitry Andric Builder.MakeTrivial(Context, DTN->getQualifier(), Loc); 2482*0b57cec5SDimitry Andric else if (QualifiedTemplateName *QTN = 2483*0b57cec5SDimitry Andric Template.getAsQualifiedTemplateName()) 2484*0b57cec5SDimitry Andric Builder.MakeTrivial(Context, QTN->getQualifier(), Loc); 2485*0b57cec5SDimitry Andric 2486*0b57cec5SDimitry Andric if (Arg.getKind() == TemplateArgument::Template) 2487*0b57cec5SDimitry Andric return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(Context), 2488*0b57cec5SDimitry Andric Loc); 2489*0b57cec5SDimitry Andric 2490*0b57cec5SDimitry Andric return TemplateArgumentLoc(Arg, Builder.getWithLocInContext(Context), 2491*0b57cec5SDimitry Andric Loc, Loc); 2492*0b57cec5SDimitry Andric } 2493*0b57cec5SDimitry Andric 2494*0b57cec5SDimitry Andric case TemplateArgument::Expression: 2495*0b57cec5SDimitry Andric return TemplateArgumentLoc(Arg, Arg.getAsExpr()); 2496*0b57cec5SDimitry Andric 2497*0b57cec5SDimitry Andric case TemplateArgument::Pack: 2498*0b57cec5SDimitry Andric return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo()); 2499*0b57cec5SDimitry Andric } 2500*0b57cec5SDimitry Andric 2501*0b57cec5SDimitry Andric llvm_unreachable("Invalid TemplateArgument Kind!"); 2502*0b57cec5SDimitry Andric } 2503*0b57cec5SDimitry Andric 2504*0b57cec5SDimitry Andric /// Convert the given deduced template argument and add it to the set of 2505*0b57cec5SDimitry Andric /// fully-converted template arguments. 2506*0b57cec5SDimitry Andric static bool 2507*0b57cec5SDimitry Andric ConvertDeducedTemplateArgument(Sema &S, NamedDecl *Param, 2508*0b57cec5SDimitry Andric DeducedTemplateArgument Arg, 2509*0b57cec5SDimitry Andric NamedDecl *Template, 2510*0b57cec5SDimitry Andric TemplateDeductionInfo &Info, 2511*0b57cec5SDimitry Andric bool IsDeduced, 2512*0b57cec5SDimitry Andric SmallVectorImpl<TemplateArgument> &Output) { 2513*0b57cec5SDimitry Andric auto ConvertArg = [&](DeducedTemplateArgument Arg, 2514*0b57cec5SDimitry Andric unsigned ArgumentPackIndex) { 2515*0b57cec5SDimitry Andric // Convert the deduced template argument into a template 2516*0b57cec5SDimitry Andric // argument that we can check, almost as if the user had written 2517*0b57cec5SDimitry Andric // the template argument explicitly. 2518*0b57cec5SDimitry Andric TemplateArgumentLoc ArgLoc = 2519*0b57cec5SDimitry Andric S.getTrivialTemplateArgumentLoc(Arg, QualType(), Info.getLocation()); 2520*0b57cec5SDimitry Andric 2521*0b57cec5SDimitry Andric // Check the template argument, converting it as necessary. 2522*0b57cec5SDimitry Andric return S.CheckTemplateArgument( 2523*0b57cec5SDimitry Andric Param, ArgLoc, Template, Template->getLocation(), 2524*0b57cec5SDimitry Andric Template->getSourceRange().getEnd(), ArgumentPackIndex, Output, 2525*0b57cec5SDimitry Andric IsDeduced 2526*0b57cec5SDimitry Andric ? (Arg.wasDeducedFromArrayBound() ? Sema::CTAK_DeducedFromArrayBound 2527*0b57cec5SDimitry Andric : Sema::CTAK_Deduced) 2528*0b57cec5SDimitry Andric : Sema::CTAK_Specified); 2529*0b57cec5SDimitry Andric }; 2530*0b57cec5SDimitry Andric 2531*0b57cec5SDimitry Andric if (Arg.getKind() == TemplateArgument::Pack) { 2532*0b57cec5SDimitry Andric // This is a template argument pack, so check each of its arguments against 2533*0b57cec5SDimitry Andric // the template parameter. 2534*0b57cec5SDimitry Andric SmallVector<TemplateArgument, 2> PackedArgsBuilder; 2535*0b57cec5SDimitry Andric for (const auto &P : Arg.pack_elements()) { 2536*0b57cec5SDimitry Andric // When converting the deduced template argument, append it to the 2537*0b57cec5SDimitry Andric // general output list. We need to do this so that the template argument 2538*0b57cec5SDimitry Andric // checking logic has all of the prior template arguments available. 2539*0b57cec5SDimitry Andric DeducedTemplateArgument InnerArg(P); 2540*0b57cec5SDimitry Andric InnerArg.setDeducedFromArrayBound(Arg.wasDeducedFromArrayBound()); 2541*0b57cec5SDimitry Andric assert(InnerArg.getKind() != TemplateArgument::Pack && 2542*0b57cec5SDimitry Andric "deduced nested pack"); 2543*0b57cec5SDimitry Andric if (P.isNull()) { 2544*0b57cec5SDimitry Andric // We deduced arguments for some elements of this pack, but not for 2545*0b57cec5SDimitry Andric // all of them. This happens if we get a conditionally-non-deduced 2546*0b57cec5SDimitry Andric // context in a pack expansion (such as an overload set in one of the 2547*0b57cec5SDimitry Andric // arguments). 2548*0b57cec5SDimitry Andric S.Diag(Param->getLocation(), 2549*0b57cec5SDimitry Andric diag::err_template_arg_deduced_incomplete_pack) 2550*0b57cec5SDimitry Andric << Arg << Param; 2551*0b57cec5SDimitry Andric return true; 2552*0b57cec5SDimitry Andric } 2553*0b57cec5SDimitry Andric if (ConvertArg(InnerArg, PackedArgsBuilder.size())) 2554*0b57cec5SDimitry Andric return true; 2555*0b57cec5SDimitry Andric 2556*0b57cec5SDimitry Andric // Move the converted template argument into our argument pack. 2557*0b57cec5SDimitry Andric PackedArgsBuilder.push_back(Output.pop_back_val()); 2558*0b57cec5SDimitry Andric } 2559*0b57cec5SDimitry Andric 2560*0b57cec5SDimitry Andric // If the pack is empty, we still need to substitute into the parameter 2561*0b57cec5SDimitry Andric // itself, in case that substitution fails. 2562*0b57cec5SDimitry Andric if (PackedArgsBuilder.empty()) { 2563*0b57cec5SDimitry Andric LocalInstantiationScope Scope(S); 2564*0b57cec5SDimitry Andric TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Output); 2565*0b57cec5SDimitry Andric MultiLevelTemplateArgumentList Args(TemplateArgs); 2566*0b57cec5SDimitry Andric 2567*0b57cec5SDimitry Andric if (auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Param)) { 2568*0b57cec5SDimitry Andric Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, 2569*0b57cec5SDimitry Andric NTTP, Output, 2570*0b57cec5SDimitry Andric Template->getSourceRange()); 2571*0b57cec5SDimitry Andric if (Inst.isInvalid() || 2572*0b57cec5SDimitry Andric S.SubstType(NTTP->getType(), Args, NTTP->getLocation(), 2573*0b57cec5SDimitry Andric NTTP->getDeclName()).isNull()) 2574*0b57cec5SDimitry Andric return true; 2575*0b57cec5SDimitry Andric } else if (auto *TTP = dyn_cast<TemplateTemplateParmDecl>(Param)) { 2576*0b57cec5SDimitry Andric Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, 2577*0b57cec5SDimitry Andric TTP, Output, 2578*0b57cec5SDimitry Andric Template->getSourceRange()); 2579*0b57cec5SDimitry Andric if (Inst.isInvalid() || !S.SubstDecl(TTP, S.CurContext, Args)) 2580*0b57cec5SDimitry Andric return true; 2581*0b57cec5SDimitry Andric } 2582*0b57cec5SDimitry Andric // For type parameters, no substitution is ever required. 2583*0b57cec5SDimitry Andric } 2584*0b57cec5SDimitry Andric 2585*0b57cec5SDimitry Andric // Create the resulting argument pack. 2586*0b57cec5SDimitry Andric Output.push_back( 2587*0b57cec5SDimitry Andric TemplateArgument::CreatePackCopy(S.Context, PackedArgsBuilder)); 2588*0b57cec5SDimitry Andric return false; 2589*0b57cec5SDimitry Andric } 2590*0b57cec5SDimitry Andric 2591*0b57cec5SDimitry Andric return ConvertArg(Arg, 0); 2592*0b57cec5SDimitry Andric } 2593*0b57cec5SDimitry Andric 2594*0b57cec5SDimitry Andric // FIXME: This should not be a template, but 2595*0b57cec5SDimitry Andric // ClassTemplatePartialSpecializationDecl sadly does not derive from 2596*0b57cec5SDimitry Andric // TemplateDecl. 2597*0b57cec5SDimitry Andric template<typename TemplateDeclT> 2598*0b57cec5SDimitry Andric static Sema::TemplateDeductionResult ConvertDeducedTemplateArguments( 2599*0b57cec5SDimitry Andric Sema &S, TemplateDeclT *Template, bool IsDeduced, 2600*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 2601*0b57cec5SDimitry Andric TemplateDeductionInfo &Info, SmallVectorImpl<TemplateArgument> &Builder, 2602*0b57cec5SDimitry Andric LocalInstantiationScope *CurrentInstantiationScope = nullptr, 2603*0b57cec5SDimitry Andric unsigned NumAlreadyConverted = 0, bool PartialOverloading = false) { 2604*0b57cec5SDimitry Andric TemplateParameterList *TemplateParams = Template->getTemplateParameters(); 2605*0b57cec5SDimitry Andric 2606*0b57cec5SDimitry Andric for (unsigned I = 0, N = TemplateParams->size(); I != N; ++I) { 2607*0b57cec5SDimitry Andric NamedDecl *Param = TemplateParams->getParam(I); 2608*0b57cec5SDimitry Andric 2609*0b57cec5SDimitry Andric // C++0x [temp.arg.explicit]p3: 2610*0b57cec5SDimitry Andric // A trailing template parameter pack (14.5.3) not otherwise deduced will 2611*0b57cec5SDimitry Andric // be deduced to an empty sequence of template arguments. 2612*0b57cec5SDimitry Andric // FIXME: Where did the word "trailing" come from? 2613*0b57cec5SDimitry Andric if (Deduced[I].isNull() && Param->isTemplateParameterPack()) { 2614*0b57cec5SDimitry Andric if (auto Result = PackDeductionScope(S, TemplateParams, Deduced, Info, I) 2615*0b57cec5SDimitry Andric .finish(/*TreatNoDeductionsAsNonDeduced*/false)) 2616*0b57cec5SDimitry Andric return Result; 2617*0b57cec5SDimitry Andric } 2618*0b57cec5SDimitry Andric 2619*0b57cec5SDimitry Andric if (!Deduced[I].isNull()) { 2620*0b57cec5SDimitry Andric if (I < NumAlreadyConverted) { 2621*0b57cec5SDimitry Andric // We may have had explicitly-specified template arguments for a 2622*0b57cec5SDimitry Andric // template parameter pack (that may or may not have been extended 2623*0b57cec5SDimitry Andric // via additional deduced arguments). 2624*0b57cec5SDimitry Andric if (Param->isParameterPack() && CurrentInstantiationScope && 2625*0b57cec5SDimitry Andric CurrentInstantiationScope->getPartiallySubstitutedPack() == Param) { 2626*0b57cec5SDimitry Andric // Forget the partially-substituted pack; its substitution is now 2627*0b57cec5SDimitry Andric // complete. 2628*0b57cec5SDimitry Andric CurrentInstantiationScope->ResetPartiallySubstitutedPack(); 2629*0b57cec5SDimitry Andric // We still need to check the argument in case it was extended by 2630*0b57cec5SDimitry Andric // deduction. 2631*0b57cec5SDimitry Andric } else { 2632*0b57cec5SDimitry Andric // We have already fully type-checked and converted this 2633*0b57cec5SDimitry Andric // argument, because it was explicitly-specified. Just record the 2634*0b57cec5SDimitry Andric // presence of this argument. 2635*0b57cec5SDimitry Andric Builder.push_back(Deduced[I]); 2636*0b57cec5SDimitry Andric continue; 2637*0b57cec5SDimitry Andric } 2638*0b57cec5SDimitry Andric } 2639*0b57cec5SDimitry Andric 2640*0b57cec5SDimitry Andric // We may have deduced this argument, so it still needs to be 2641*0b57cec5SDimitry Andric // checked and converted. 2642*0b57cec5SDimitry Andric if (ConvertDeducedTemplateArgument(S, Param, Deduced[I], Template, Info, 2643*0b57cec5SDimitry Andric IsDeduced, Builder)) { 2644*0b57cec5SDimitry Andric Info.Param = makeTemplateParameter(Param); 2645*0b57cec5SDimitry Andric // FIXME: These template arguments are temporary. Free them! 2646*0b57cec5SDimitry Andric Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); 2647*0b57cec5SDimitry Andric return Sema::TDK_SubstitutionFailure; 2648*0b57cec5SDimitry Andric } 2649*0b57cec5SDimitry Andric 2650*0b57cec5SDimitry Andric continue; 2651*0b57cec5SDimitry Andric } 2652*0b57cec5SDimitry Andric 2653*0b57cec5SDimitry Andric // Substitute into the default template argument, if available. 2654*0b57cec5SDimitry Andric bool HasDefaultArg = false; 2655*0b57cec5SDimitry Andric TemplateDecl *TD = dyn_cast<TemplateDecl>(Template); 2656*0b57cec5SDimitry Andric if (!TD) { 2657*0b57cec5SDimitry Andric assert(isa<ClassTemplatePartialSpecializationDecl>(Template) || 2658*0b57cec5SDimitry Andric isa<VarTemplatePartialSpecializationDecl>(Template)); 2659*0b57cec5SDimitry Andric return Sema::TDK_Incomplete; 2660*0b57cec5SDimitry Andric } 2661*0b57cec5SDimitry Andric 2662*0b57cec5SDimitry Andric TemplateArgumentLoc DefArg = S.SubstDefaultTemplateArgumentIfAvailable( 2663*0b57cec5SDimitry Andric TD, TD->getLocation(), TD->getSourceRange().getEnd(), Param, Builder, 2664*0b57cec5SDimitry Andric HasDefaultArg); 2665*0b57cec5SDimitry Andric 2666*0b57cec5SDimitry Andric // If there was no default argument, deduction is incomplete. 2667*0b57cec5SDimitry Andric if (DefArg.getArgument().isNull()) { 2668*0b57cec5SDimitry Andric Info.Param = makeTemplateParameter( 2669*0b57cec5SDimitry Andric const_cast<NamedDecl *>(TemplateParams->getParam(I))); 2670*0b57cec5SDimitry Andric Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); 2671*0b57cec5SDimitry Andric if (PartialOverloading) break; 2672*0b57cec5SDimitry Andric 2673*0b57cec5SDimitry Andric return HasDefaultArg ? Sema::TDK_SubstitutionFailure 2674*0b57cec5SDimitry Andric : Sema::TDK_Incomplete; 2675*0b57cec5SDimitry Andric } 2676*0b57cec5SDimitry Andric 2677*0b57cec5SDimitry Andric // Check whether we can actually use the default argument. 2678*0b57cec5SDimitry Andric if (S.CheckTemplateArgument(Param, DefArg, TD, TD->getLocation(), 2679*0b57cec5SDimitry Andric TD->getSourceRange().getEnd(), 0, Builder, 2680*0b57cec5SDimitry Andric Sema::CTAK_Specified)) { 2681*0b57cec5SDimitry Andric Info.Param = makeTemplateParameter( 2682*0b57cec5SDimitry Andric const_cast<NamedDecl *>(TemplateParams->getParam(I))); 2683*0b57cec5SDimitry Andric // FIXME: These template arguments are temporary. Free them! 2684*0b57cec5SDimitry Andric Info.reset(TemplateArgumentList::CreateCopy(S.Context, Builder)); 2685*0b57cec5SDimitry Andric return Sema::TDK_SubstitutionFailure; 2686*0b57cec5SDimitry Andric } 2687*0b57cec5SDimitry Andric 2688*0b57cec5SDimitry Andric // If we get here, we successfully used the default template argument. 2689*0b57cec5SDimitry Andric } 2690*0b57cec5SDimitry Andric 2691*0b57cec5SDimitry Andric return Sema::TDK_Success; 2692*0b57cec5SDimitry Andric } 2693*0b57cec5SDimitry Andric 2694*0b57cec5SDimitry Andric static DeclContext *getAsDeclContextOrEnclosing(Decl *D) { 2695*0b57cec5SDimitry Andric if (auto *DC = dyn_cast<DeclContext>(D)) 2696*0b57cec5SDimitry Andric return DC; 2697*0b57cec5SDimitry Andric return D->getDeclContext(); 2698*0b57cec5SDimitry Andric } 2699*0b57cec5SDimitry Andric 2700*0b57cec5SDimitry Andric template<typename T> struct IsPartialSpecialization { 2701*0b57cec5SDimitry Andric static constexpr bool value = false; 2702*0b57cec5SDimitry Andric }; 2703*0b57cec5SDimitry Andric template<> 2704*0b57cec5SDimitry Andric struct IsPartialSpecialization<ClassTemplatePartialSpecializationDecl> { 2705*0b57cec5SDimitry Andric static constexpr bool value = true; 2706*0b57cec5SDimitry Andric }; 2707*0b57cec5SDimitry Andric template<> 2708*0b57cec5SDimitry Andric struct IsPartialSpecialization<VarTemplatePartialSpecializationDecl> { 2709*0b57cec5SDimitry Andric static constexpr bool value = true; 2710*0b57cec5SDimitry Andric }; 2711*0b57cec5SDimitry Andric 2712*0b57cec5SDimitry Andric /// Complete template argument deduction for a partial specialization. 2713*0b57cec5SDimitry Andric template <typename T> 2714*0b57cec5SDimitry Andric static typename std::enable_if<IsPartialSpecialization<T>::value, 2715*0b57cec5SDimitry Andric Sema::TemplateDeductionResult>::type 2716*0b57cec5SDimitry Andric FinishTemplateArgumentDeduction( 2717*0b57cec5SDimitry Andric Sema &S, T *Partial, bool IsPartialOrdering, 2718*0b57cec5SDimitry Andric const TemplateArgumentList &TemplateArgs, 2719*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 2720*0b57cec5SDimitry Andric TemplateDeductionInfo &Info) { 2721*0b57cec5SDimitry Andric // Unevaluated SFINAE context. 2722*0b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 2723*0b57cec5SDimitry Andric S, Sema::ExpressionEvaluationContext::Unevaluated); 2724*0b57cec5SDimitry Andric Sema::SFINAETrap Trap(S); 2725*0b57cec5SDimitry Andric 2726*0b57cec5SDimitry Andric Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Partial)); 2727*0b57cec5SDimitry Andric 2728*0b57cec5SDimitry Andric // C++ [temp.deduct.type]p2: 2729*0b57cec5SDimitry Andric // [...] or if any template argument remains neither deduced nor 2730*0b57cec5SDimitry Andric // explicitly specified, template argument deduction fails. 2731*0b57cec5SDimitry Andric SmallVector<TemplateArgument, 4> Builder; 2732*0b57cec5SDimitry Andric if (auto Result = ConvertDeducedTemplateArguments( 2733*0b57cec5SDimitry Andric S, Partial, IsPartialOrdering, Deduced, Info, Builder)) 2734*0b57cec5SDimitry Andric return Result; 2735*0b57cec5SDimitry Andric 2736*0b57cec5SDimitry Andric // Form the template argument list from the deduced template arguments. 2737*0b57cec5SDimitry Andric TemplateArgumentList *DeducedArgumentList 2738*0b57cec5SDimitry Andric = TemplateArgumentList::CreateCopy(S.Context, Builder); 2739*0b57cec5SDimitry Andric 2740*0b57cec5SDimitry Andric Info.reset(DeducedArgumentList); 2741*0b57cec5SDimitry Andric 2742*0b57cec5SDimitry Andric // Substitute the deduced template arguments into the template 2743*0b57cec5SDimitry Andric // arguments of the class template partial specialization, and 2744*0b57cec5SDimitry Andric // verify that the instantiated template arguments are both valid 2745*0b57cec5SDimitry Andric // and are equivalent to the template arguments originally provided 2746*0b57cec5SDimitry Andric // to the class template. 2747*0b57cec5SDimitry Andric LocalInstantiationScope InstScope(S); 2748*0b57cec5SDimitry Andric auto *Template = Partial->getSpecializedTemplate(); 2749*0b57cec5SDimitry Andric const ASTTemplateArgumentListInfo *PartialTemplArgInfo = 2750*0b57cec5SDimitry Andric Partial->getTemplateArgsAsWritten(); 2751*0b57cec5SDimitry Andric const TemplateArgumentLoc *PartialTemplateArgs = 2752*0b57cec5SDimitry Andric PartialTemplArgInfo->getTemplateArgs(); 2753*0b57cec5SDimitry Andric 2754*0b57cec5SDimitry Andric TemplateArgumentListInfo InstArgs(PartialTemplArgInfo->LAngleLoc, 2755*0b57cec5SDimitry Andric PartialTemplArgInfo->RAngleLoc); 2756*0b57cec5SDimitry Andric 2757*0b57cec5SDimitry Andric if (S.Subst(PartialTemplateArgs, PartialTemplArgInfo->NumTemplateArgs, 2758*0b57cec5SDimitry Andric InstArgs, MultiLevelTemplateArgumentList(*DeducedArgumentList))) { 2759*0b57cec5SDimitry Andric unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; 2760*0b57cec5SDimitry Andric if (ParamIdx >= Partial->getTemplateParameters()->size()) 2761*0b57cec5SDimitry Andric ParamIdx = Partial->getTemplateParameters()->size() - 1; 2762*0b57cec5SDimitry Andric 2763*0b57cec5SDimitry Andric Decl *Param = const_cast<NamedDecl *>( 2764*0b57cec5SDimitry Andric Partial->getTemplateParameters()->getParam(ParamIdx)); 2765*0b57cec5SDimitry Andric Info.Param = makeTemplateParameter(Param); 2766*0b57cec5SDimitry Andric Info.FirstArg = PartialTemplateArgs[ArgIdx].getArgument(); 2767*0b57cec5SDimitry Andric return Sema::TDK_SubstitutionFailure; 2768*0b57cec5SDimitry Andric } 2769*0b57cec5SDimitry Andric 2770*0b57cec5SDimitry Andric SmallVector<TemplateArgument, 4> ConvertedInstArgs; 2771*0b57cec5SDimitry Andric if (S.CheckTemplateArgumentList(Template, Partial->getLocation(), InstArgs, 2772*0b57cec5SDimitry Andric false, ConvertedInstArgs)) 2773*0b57cec5SDimitry Andric return Sema::TDK_SubstitutionFailure; 2774*0b57cec5SDimitry Andric 2775*0b57cec5SDimitry Andric TemplateParameterList *TemplateParams = Template->getTemplateParameters(); 2776*0b57cec5SDimitry Andric for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { 2777*0b57cec5SDimitry Andric TemplateArgument InstArg = ConvertedInstArgs.data()[I]; 2778*0b57cec5SDimitry Andric if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg)) { 2779*0b57cec5SDimitry Andric Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); 2780*0b57cec5SDimitry Andric Info.FirstArg = TemplateArgs[I]; 2781*0b57cec5SDimitry Andric Info.SecondArg = InstArg; 2782*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 2783*0b57cec5SDimitry Andric } 2784*0b57cec5SDimitry Andric } 2785*0b57cec5SDimitry Andric 2786*0b57cec5SDimitry Andric if (Trap.hasErrorOccurred()) 2787*0b57cec5SDimitry Andric return Sema::TDK_SubstitutionFailure; 2788*0b57cec5SDimitry Andric 2789*0b57cec5SDimitry Andric return Sema::TDK_Success; 2790*0b57cec5SDimitry Andric } 2791*0b57cec5SDimitry Andric 2792*0b57cec5SDimitry Andric /// Complete template argument deduction for a class or variable template, 2793*0b57cec5SDimitry Andric /// when partial ordering against a partial specialization. 2794*0b57cec5SDimitry Andric // FIXME: Factor out duplication with partial specialization version above. 2795*0b57cec5SDimitry Andric static Sema::TemplateDeductionResult FinishTemplateArgumentDeduction( 2796*0b57cec5SDimitry Andric Sema &S, TemplateDecl *Template, bool PartialOrdering, 2797*0b57cec5SDimitry Andric const TemplateArgumentList &TemplateArgs, 2798*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 2799*0b57cec5SDimitry Andric TemplateDeductionInfo &Info) { 2800*0b57cec5SDimitry Andric // Unevaluated SFINAE context. 2801*0b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 2802*0b57cec5SDimitry Andric S, Sema::ExpressionEvaluationContext::Unevaluated); 2803*0b57cec5SDimitry Andric Sema::SFINAETrap Trap(S); 2804*0b57cec5SDimitry Andric 2805*0b57cec5SDimitry Andric Sema::ContextRAII SavedContext(S, getAsDeclContextOrEnclosing(Template)); 2806*0b57cec5SDimitry Andric 2807*0b57cec5SDimitry Andric // C++ [temp.deduct.type]p2: 2808*0b57cec5SDimitry Andric // [...] or if any template argument remains neither deduced nor 2809*0b57cec5SDimitry Andric // explicitly specified, template argument deduction fails. 2810*0b57cec5SDimitry Andric SmallVector<TemplateArgument, 4> Builder; 2811*0b57cec5SDimitry Andric if (auto Result = ConvertDeducedTemplateArguments( 2812*0b57cec5SDimitry Andric S, Template, /*IsDeduced*/PartialOrdering, Deduced, Info, Builder)) 2813*0b57cec5SDimitry Andric return Result; 2814*0b57cec5SDimitry Andric 2815*0b57cec5SDimitry Andric // Check that we produced the correct argument list. 2816*0b57cec5SDimitry Andric TemplateParameterList *TemplateParams = Template->getTemplateParameters(); 2817*0b57cec5SDimitry Andric for (unsigned I = 0, E = TemplateParams->size(); I != E; ++I) { 2818*0b57cec5SDimitry Andric TemplateArgument InstArg = Builder[I]; 2819*0b57cec5SDimitry Andric if (!isSameTemplateArg(S.Context, TemplateArgs[I], InstArg, 2820*0b57cec5SDimitry Andric /*PackExpansionMatchesPack*/true)) { 2821*0b57cec5SDimitry Andric Info.Param = makeTemplateParameter(TemplateParams->getParam(I)); 2822*0b57cec5SDimitry Andric Info.FirstArg = TemplateArgs[I]; 2823*0b57cec5SDimitry Andric Info.SecondArg = InstArg; 2824*0b57cec5SDimitry Andric return Sema::TDK_NonDeducedMismatch; 2825*0b57cec5SDimitry Andric } 2826*0b57cec5SDimitry Andric } 2827*0b57cec5SDimitry Andric 2828*0b57cec5SDimitry Andric if (Trap.hasErrorOccurred()) 2829*0b57cec5SDimitry Andric return Sema::TDK_SubstitutionFailure; 2830*0b57cec5SDimitry Andric 2831*0b57cec5SDimitry Andric return Sema::TDK_Success; 2832*0b57cec5SDimitry Andric } 2833*0b57cec5SDimitry Andric 2834*0b57cec5SDimitry Andric 2835*0b57cec5SDimitry Andric /// Perform template argument deduction to determine whether 2836*0b57cec5SDimitry Andric /// the given template arguments match the given class template 2837*0b57cec5SDimitry Andric /// partial specialization per C++ [temp.class.spec.match]. 2838*0b57cec5SDimitry Andric Sema::TemplateDeductionResult 2839*0b57cec5SDimitry Andric Sema::DeduceTemplateArguments(ClassTemplatePartialSpecializationDecl *Partial, 2840*0b57cec5SDimitry Andric const TemplateArgumentList &TemplateArgs, 2841*0b57cec5SDimitry Andric TemplateDeductionInfo &Info) { 2842*0b57cec5SDimitry Andric if (Partial->isInvalidDecl()) 2843*0b57cec5SDimitry Andric return TDK_Invalid; 2844*0b57cec5SDimitry Andric 2845*0b57cec5SDimitry Andric // C++ [temp.class.spec.match]p2: 2846*0b57cec5SDimitry Andric // A partial specialization matches a given actual template 2847*0b57cec5SDimitry Andric // argument list if the template arguments of the partial 2848*0b57cec5SDimitry Andric // specialization can be deduced from the actual template argument 2849*0b57cec5SDimitry Andric // list (14.8.2). 2850*0b57cec5SDimitry Andric 2851*0b57cec5SDimitry Andric // Unevaluated SFINAE context. 2852*0b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 2853*0b57cec5SDimitry Andric *this, Sema::ExpressionEvaluationContext::Unevaluated); 2854*0b57cec5SDimitry Andric SFINAETrap Trap(*this); 2855*0b57cec5SDimitry Andric 2856*0b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 4> Deduced; 2857*0b57cec5SDimitry Andric Deduced.resize(Partial->getTemplateParameters()->size()); 2858*0b57cec5SDimitry Andric if (TemplateDeductionResult Result 2859*0b57cec5SDimitry Andric = ::DeduceTemplateArguments(*this, 2860*0b57cec5SDimitry Andric Partial->getTemplateParameters(), 2861*0b57cec5SDimitry Andric Partial->getTemplateArgs(), 2862*0b57cec5SDimitry Andric TemplateArgs, Info, Deduced)) 2863*0b57cec5SDimitry Andric return Result; 2864*0b57cec5SDimitry Andric 2865*0b57cec5SDimitry Andric SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); 2866*0b57cec5SDimitry Andric InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, 2867*0b57cec5SDimitry Andric Info); 2868*0b57cec5SDimitry Andric if (Inst.isInvalid()) 2869*0b57cec5SDimitry Andric return TDK_InstantiationDepth; 2870*0b57cec5SDimitry Andric 2871*0b57cec5SDimitry Andric if (Trap.hasErrorOccurred()) 2872*0b57cec5SDimitry Andric return Sema::TDK_SubstitutionFailure; 2873*0b57cec5SDimitry Andric 2874*0b57cec5SDimitry Andric return ::FinishTemplateArgumentDeduction( 2875*0b57cec5SDimitry Andric *this, Partial, /*IsPartialOrdering=*/false, TemplateArgs, Deduced, Info); 2876*0b57cec5SDimitry Andric } 2877*0b57cec5SDimitry Andric 2878*0b57cec5SDimitry Andric /// Perform template argument deduction to determine whether 2879*0b57cec5SDimitry Andric /// the given template arguments match the given variable template 2880*0b57cec5SDimitry Andric /// partial specialization per C++ [temp.class.spec.match]. 2881*0b57cec5SDimitry Andric Sema::TemplateDeductionResult 2882*0b57cec5SDimitry Andric Sema::DeduceTemplateArguments(VarTemplatePartialSpecializationDecl *Partial, 2883*0b57cec5SDimitry Andric const TemplateArgumentList &TemplateArgs, 2884*0b57cec5SDimitry Andric TemplateDeductionInfo &Info) { 2885*0b57cec5SDimitry Andric if (Partial->isInvalidDecl()) 2886*0b57cec5SDimitry Andric return TDK_Invalid; 2887*0b57cec5SDimitry Andric 2888*0b57cec5SDimitry Andric // C++ [temp.class.spec.match]p2: 2889*0b57cec5SDimitry Andric // A partial specialization matches a given actual template 2890*0b57cec5SDimitry Andric // argument list if the template arguments of the partial 2891*0b57cec5SDimitry Andric // specialization can be deduced from the actual template argument 2892*0b57cec5SDimitry Andric // list (14.8.2). 2893*0b57cec5SDimitry Andric 2894*0b57cec5SDimitry Andric // Unevaluated SFINAE context. 2895*0b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 2896*0b57cec5SDimitry Andric *this, Sema::ExpressionEvaluationContext::Unevaluated); 2897*0b57cec5SDimitry Andric SFINAETrap Trap(*this); 2898*0b57cec5SDimitry Andric 2899*0b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 4> Deduced; 2900*0b57cec5SDimitry Andric Deduced.resize(Partial->getTemplateParameters()->size()); 2901*0b57cec5SDimitry Andric if (TemplateDeductionResult Result = ::DeduceTemplateArguments( 2902*0b57cec5SDimitry Andric *this, Partial->getTemplateParameters(), Partial->getTemplateArgs(), 2903*0b57cec5SDimitry Andric TemplateArgs, Info, Deduced)) 2904*0b57cec5SDimitry Andric return Result; 2905*0b57cec5SDimitry Andric 2906*0b57cec5SDimitry Andric SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); 2907*0b57cec5SDimitry Andric InstantiatingTemplate Inst(*this, Info.getLocation(), Partial, DeducedArgs, 2908*0b57cec5SDimitry Andric Info); 2909*0b57cec5SDimitry Andric if (Inst.isInvalid()) 2910*0b57cec5SDimitry Andric return TDK_InstantiationDepth; 2911*0b57cec5SDimitry Andric 2912*0b57cec5SDimitry Andric if (Trap.hasErrorOccurred()) 2913*0b57cec5SDimitry Andric return Sema::TDK_SubstitutionFailure; 2914*0b57cec5SDimitry Andric 2915*0b57cec5SDimitry Andric return ::FinishTemplateArgumentDeduction( 2916*0b57cec5SDimitry Andric *this, Partial, /*IsPartialOrdering=*/false, TemplateArgs, Deduced, Info); 2917*0b57cec5SDimitry Andric } 2918*0b57cec5SDimitry Andric 2919*0b57cec5SDimitry Andric /// Determine whether the given type T is a simple-template-id type. 2920*0b57cec5SDimitry Andric static bool isSimpleTemplateIdType(QualType T) { 2921*0b57cec5SDimitry Andric if (const TemplateSpecializationType *Spec 2922*0b57cec5SDimitry Andric = T->getAs<TemplateSpecializationType>()) 2923*0b57cec5SDimitry Andric return Spec->getTemplateName().getAsTemplateDecl() != nullptr; 2924*0b57cec5SDimitry Andric 2925*0b57cec5SDimitry Andric // C++17 [temp.local]p2: 2926*0b57cec5SDimitry Andric // the injected-class-name [...] is equivalent to the template-name followed 2927*0b57cec5SDimitry Andric // by the template-arguments of the class template specialization or partial 2928*0b57cec5SDimitry Andric // specialization enclosed in <> 2929*0b57cec5SDimitry Andric // ... which means it's equivalent to a simple-template-id. 2930*0b57cec5SDimitry Andric // 2931*0b57cec5SDimitry Andric // This only arises during class template argument deduction for a copy 2932*0b57cec5SDimitry Andric // deduction candidate, where it permits slicing. 2933*0b57cec5SDimitry Andric if (T->getAs<InjectedClassNameType>()) 2934*0b57cec5SDimitry Andric return true; 2935*0b57cec5SDimitry Andric 2936*0b57cec5SDimitry Andric return false; 2937*0b57cec5SDimitry Andric } 2938*0b57cec5SDimitry Andric 2939*0b57cec5SDimitry Andric /// Substitute the explicitly-provided template arguments into the 2940*0b57cec5SDimitry Andric /// given function template according to C++ [temp.arg.explicit]. 2941*0b57cec5SDimitry Andric /// 2942*0b57cec5SDimitry Andric /// \param FunctionTemplate the function template into which the explicit 2943*0b57cec5SDimitry Andric /// template arguments will be substituted. 2944*0b57cec5SDimitry Andric /// 2945*0b57cec5SDimitry Andric /// \param ExplicitTemplateArgs the explicitly-specified template 2946*0b57cec5SDimitry Andric /// arguments. 2947*0b57cec5SDimitry Andric /// 2948*0b57cec5SDimitry Andric /// \param Deduced the deduced template arguments, which will be populated 2949*0b57cec5SDimitry Andric /// with the converted and checked explicit template arguments. 2950*0b57cec5SDimitry Andric /// 2951*0b57cec5SDimitry Andric /// \param ParamTypes will be populated with the instantiated function 2952*0b57cec5SDimitry Andric /// parameters. 2953*0b57cec5SDimitry Andric /// 2954*0b57cec5SDimitry Andric /// \param FunctionType if non-NULL, the result type of the function template 2955*0b57cec5SDimitry Andric /// will also be instantiated and the pointed-to value will be updated with 2956*0b57cec5SDimitry Andric /// the instantiated function type. 2957*0b57cec5SDimitry Andric /// 2958*0b57cec5SDimitry Andric /// \param Info if substitution fails for any reason, this object will be 2959*0b57cec5SDimitry Andric /// populated with more information about the failure. 2960*0b57cec5SDimitry Andric /// 2961*0b57cec5SDimitry Andric /// \returns TDK_Success if substitution was successful, or some failure 2962*0b57cec5SDimitry Andric /// condition. 2963*0b57cec5SDimitry Andric Sema::TemplateDeductionResult 2964*0b57cec5SDimitry Andric Sema::SubstituteExplicitTemplateArguments( 2965*0b57cec5SDimitry Andric FunctionTemplateDecl *FunctionTemplate, 2966*0b57cec5SDimitry Andric TemplateArgumentListInfo &ExplicitTemplateArgs, 2967*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 2968*0b57cec5SDimitry Andric SmallVectorImpl<QualType> &ParamTypes, 2969*0b57cec5SDimitry Andric QualType *FunctionType, 2970*0b57cec5SDimitry Andric TemplateDeductionInfo &Info) { 2971*0b57cec5SDimitry Andric FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 2972*0b57cec5SDimitry Andric TemplateParameterList *TemplateParams 2973*0b57cec5SDimitry Andric = FunctionTemplate->getTemplateParameters(); 2974*0b57cec5SDimitry Andric 2975*0b57cec5SDimitry Andric if (ExplicitTemplateArgs.size() == 0) { 2976*0b57cec5SDimitry Andric // No arguments to substitute; just copy over the parameter types and 2977*0b57cec5SDimitry Andric // fill in the function type. 2978*0b57cec5SDimitry Andric for (auto P : Function->parameters()) 2979*0b57cec5SDimitry Andric ParamTypes.push_back(P->getType()); 2980*0b57cec5SDimitry Andric 2981*0b57cec5SDimitry Andric if (FunctionType) 2982*0b57cec5SDimitry Andric *FunctionType = Function->getType(); 2983*0b57cec5SDimitry Andric return TDK_Success; 2984*0b57cec5SDimitry Andric } 2985*0b57cec5SDimitry Andric 2986*0b57cec5SDimitry Andric // Unevaluated SFINAE context. 2987*0b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 2988*0b57cec5SDimitry Andric *this, Sema::ExpressionEvaluationContext::Unevaluated); 2989*0b57cec5SDimitry Andric SFINAETrap Trap(*this); 2990*0b57cec5SDimitry Andric 2991*0b57cec5SDimitry Andric // C++ [temp.arg.explicit]p3: 2992*0b57cec5SDimitry Andric // Template arguments that are present shall be specified in the 2993*0b57cec5SDimitry Andric // declaration order of their corresponding template-parameters. The 2994*0b57cec5SDimitry Andric // template argument list shall not specify more template-arguments than 2995*0b57cec5SDimitry Andric // there are corresponding template-parameters. 2996*0b57cec5SDimitry Andric SmallVector<TemplateArgument, 4> Builder; 2997*0b57cec5SDimitry Andric 2998*0b57cec5SDimitry Andric // Enter a new template instantiation context where we check the 2999*0b57cec5SDimitry Andric // explicitly-specified template arguments against this function template, 3000*0b57cec5SDimitry Andric // and then substitute them into the function parameter types. 3001*0b57cec5SDimitry Andric SmallVector<TemplateArgument, 4> DeducedArgs; 3002*0b57cec5SDimitry Andric InstantiatingTemplate Inst( 3003*0b57cec5SDimitry Andric *this, Info.getLocation(), FunctionTemplate, DeducedArgs, 3004*0b57cec5SDimitry Andric CodeSynthesisContext::ExplicitTemplateArgumentSubstitution, Info); 3005*0b57cec5SDimitry Andric if (Inst.isInvalid()) 3006*0b57cec5SDimitry Andric return TDK_InstantiationDepth; 3007*0b57cec5SDimitry Andric 3008*0b57cec5SDimitry Andric if (CheckTemplateArgumentList(FunctionTemplate, SourceLocation(), 3009*0b57cec5SDimitry Andric ExplicitTemplateArgs, true, Builder, false) || 3010*0b57cec5SDimitry Andric Trap.hasErrorOccurred()) { 3011*0b57cec5SDimitry Andric unsigned Index = Builder.size(); 3012*0b57cec5SDimitry Andric if (Index >= TemplateParams->size()) 3013*0b57cec5SDimitry Andric return TDK_SubstitutionFailure; 3014*0b57cec5SDimitry Andric Info.Param = makeTemplateParameter(TemplateParams->getParam(Index)); 3015*0b57cec5SDimitry Andric return TDK_InvalidExplicitArguments; 3016*0b57cec5SDimitry Andric } 3017*0b57cec5SDimitry Andric 3018*0b57cec5SDimitry Andric // Form the template argument list from the explicitly-specified 3019*0b57cec5SDimitry Andric // template arguments. 3020*0b57cec5SDimitry Andric TemplateArgumentList *ExplicitArgumentList 3021*0b57cec5SDimitry Andric = TemplateArgumentList::CreateCopy(Context, Builder); 3022*0b57cec5SDimitry Andric Info.setExplicitArgs(ExplicitArgumentList); 3023*0b57cec5SDimitry Andric 3024*0b57cec5SDimitry Andric // Template argument deduction and the final substitution should be 3025*0b57cec5SDimitry Andric // done in the context of the templated declaration. Explicit 3026*0b57cec5SDimitry Andric // argument substitution, on the other hand, needs to happen in the 3027*0b57cec5SDimitry Andric // calling context. 3028*0b57cec5SDimitry Andric ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); 3029*0b57cec5SDimitry Andric 3030*0b57cec5SDimitry Andric // If we deduced template arguments for a template parameter pack, 3031*0b57cec5SDimitry Andric // note that the template argument pack is partially substituted and record 3032*0b57cec5SDimitry Andric // the explicit template arguments. They'll be used as part of deduction 3033*0b57cec5SDimitry Andric // for this template parameter pack. 3034*0b57cec5SDimitry Andric unsigned PartiallySubstitutedPackIndex = -1u; 3035*0b57cec5SDimitry Andric if (!Builder.empty()) { 3036*0b57cec5SDimitry Andric const TemplateArgument &Arg = Builder.back(); 3037*0b57cec5SDimitry Andric if (Arg.getKind() == TemplateArgument::Pack) { 3038*0b57cec5SDimitry Andric auto *Param = TemplateParams->getParam(Builder.size() - 1); 3039*0b57cec5SDimitry Andric // If this is a fully-saturated fixed-size pack, it should be 3040*0b57cec5SDimitry Andric // fully-substituted, not partially-substituted. 3041*0b57cec5SDimitry Andric Optional<unsigned> Expansions = getExpandedPackSize(Param); 3042*0b57cec5SDimitry Andric if (!Expansions || Arg.pack_size() < *Expansions) { 3043*0b57cec5SDimitry Andric PartiallySubstitutedPackIndex = Builder.size() - 1; 3044*0b57cec5SDimitry Andric CurrentInstantiationScope->SetPartiallySubstitutedPack( 3045*0b57cec5SDimitry Andric Param, Arg.pack_begin(), Arg.pack_size()); 3046*0b57cec5SDimitry Andric } 3047*0b57cec5SDimitry Andric } 3048*0b57cec5SDimitry Andric } 3049*0b57cec5SDimitry Andric 3050*0b57cec5SDimitry Andric const FunctionProtoType *Proto 3051*0b57cec5SDimitry Andric = Function->getType()->getAs<FunctionProtoType>(); 3052*0b57cec5SDimitry Andric assert(Proto && "Function template does not have a prototype?"); 3053*0b57cec5SDimitry Andric 3054*0b57cec5SDimitry Andric // Isolate our substituted parameters from our caller. 3055*0b57cec5SDimitry Andric LocalInstantiationScope InstScope(*this, /*MergeWithOuterScope*/true); 3056*0b57cec5SDimitry Andric 3057*0b57cec5SDimitry Andric ExtParameterInfoBuilder ExtParamInfos; 3058*0b57cec5SDimitry Andric 3059*0b57cec5SDimitry Andric // Instantiate the types of each of the function parameters given the 3060*0b57cec5SDimitry Andric // explicitly-specified template arguments. If the function has a trailing 3061*0b57cec5SDimitry Andric // return type, substitute it after the arguments to ensure we substitute 3062*0b57cec5SDimitry Andric // in lexical order. 3063*0b57cec5SDimitry Andric if (Proto->hasTrailingReturn()) { 3064*0b57cec5SDimitry Andric if (SubstParmTypes(Function->getLocation(), Function->parameters(), 3065*0b57cec5SDimitry Andric Proto->getExtParameterInfosOrNull(), 3066*0b57cec5SDimitry Andric MultiLevelTemplateArgumentList(*ExplicitArgumentList), 3067*0b57cec5SDimitry Andric ParamTypes, /*params*/ nullptr, ExtParamInfos)) 3068*0b57cec5SDimitry Andric return TDK_SubstitutionFailure; 3069*0b57cec5SDimitry Andric } 3070*0b57cec5SDimitry Andric 3071*0b57cec5SDimitry Andric // Instantiate the return type. 3072*0b57cec5SDimitry Andric QualType ResultType; 3073*0b57cec5SDimitry Andric { 3074*0b57cec5SDimitry Andric // C++11 [expr.prim.general]p3: 3075*0b57cec5SDimitry Andric // If a declaration declares a member function or member function 3076*0b57cec5SDimitry Andric // template of a class X, the expression this is a prvalue of type 3077*0b57cec5SDimitry Andric // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq 3078*0b57cec5SDimitry Andric // and the end of the function-definition, member-declarator, or 3079*0b57cec5SDimitry Andric // declarator. 3080*0b57cec5SDimitry Andric Qualifiers ThisTypeQuals; 3081*0b57cec5SDimitry Andric CXXRecordDecl *ThisContext = nullptr; 3082*0b57cec5SDimitry Andric if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Function)) { 3083*0b57cec5SDimitry Andric ThisContext = Method->getParent(); 3084*0b57cec5SDimitry Andric ThisTypeQuals = Method->getMethodQualifiers(); 3085*0b57cec5SDimitry Andric } 3086*0b57cec5SDimitry Andric 3087*0b57cec5SDimitry Andric CXXThisScopeRAII ThisScope(*this, ThisContext, ThisTypeQuals, 3088*0b57cec5SDimitry Andric getLangOpts().CPlusPlus11); 3089*0b57cec5SDimitry Andric 3090*0b57cec5SDimitry Andric ResultType = 3091*0b57cec5SDimitry Andric SubstType(Proto->getReturnType(), 3092*0b57cec5SDimitry Andric MultiLevelTemplateArgumentList(*ExplicitArgumentList), 3093*0b57cec5SDimitry Andric Function->getTypeSpecStartLoc(), Function->getDeclName()); 3094*0b57cec5SDimitry Andric if (ResultType.isNull() || Trap.hasErrorOccurred()) 3095*0b57cec5SDimitry Andric return TDK_SubstitutionFailure; 3096*0b57cec5SDimitry Andric } 3097*0b57cec5SDimitry Andric 3098*0b57cec5SDimitry Andric // Instantiate the types of each of the function parameters given the 3099*0b57cec5SDimitry Andric // explicitly-specified template arguments if we didn't do so earlier. 3100*0b57cec5SDimitry Andric if (!Proto->hasTrailingReturn() && 3101*0b57cec5SDimitry Andric SubstParmTypes(Function->getLocation(), Function->parameters(), 3102*0b57cec5SDimitry Andric Proto->getExtParameterInfosOrNull(), 3103*0b57cec5SDimitry Andric MultiLevelTemplateArgumentList(*ExplicitArgumentList), 3104*0b57cec5SDimitry Andric ParamTypes, /*params*/ nullptr, ExtParamInfos)) 3105*0b57cec5SDimitry Andric return TDK_SubstitutionFailure; 3106*0b57cec5SDimitry Andric 3107*0b57cec5SDimitry Andric if (FunctionType) { 3108*0b57cec5SDimitry Andric auto EPI = Proto->getExtProtoInfo(); 3109*0b57cec5SDimitry Andric EPI.ExtParameterInfos = ExtParamInfos.getPointerOrNull(ParamTypes.size()); 3110*0b57cec5SDimitry Andric 3111*0b57cec5SDimitry Andric // In C++1z onwards, exception specifications are part of the function type, 3112*0b57cec5SDimitry Andric // so substitution into the type must also substitute into the exception 3113*0b57cec5SDimitry Andric // specification. 3114*0b57cec5SDimitry Andric SmallVector<QualType, 4> ExceptionStorage; 3115*0b57cec5SDimitry Andric if (getLangOpts().CPlusPlus17 && 3116*0b57cec5SDimitry Andric SubstExceptionSpec( 3117*0b57cec5SDimitry Andric Function->getLocation(), EPI.ExceptionSpec, ExceptionStorage, 3118*0b57cec5SDimitry Andric MultiLevelTemplateArgumentList(*ExplicitArgumentList))) 3119*0b57cec5SDimitry Andric return TDK_SubstitutionFailure; 3120*0b57cec5SDimitry Andric 3121*0b57cec5SDimitry Andric *FunctionType = BuildFunctionType(ResultType, ParamTypes, 3122*0b57cec5SDimitry Andric Function->getLocation(), 3123*0b57cec5SDimitry Andric Function->getDeclName(), 3124*0b57cec5SDimitry Andric EPI); 3125*0b57cec5SDimitry Andric if (FunctionType->isNull() || Trap.hasErrorOccurred()) 3126*0b57cec5SDimitry Andric return TDK_SubstitutionFailure; 3127*0b57cec5SDimitry Andric } 3128*0b57cec5SDimitry Andric 3129*0b57cec5SDimitry Andric // C++ [temp.arg.explicit]p2: 3130*0b57cec5SDimitry Andric // Trailing template arguments that can be deduced (14.8.2) may be 3131*0b57cec5SDimitry Andric // omitted from the list of explicit template-arguments. If all of the 3132*0b57cec5SDimitry Andric // template arguments can be deduced, they may all be omitted; in this 3133*0b57cec5SDimitry Andric // case, the empty template argument list <> itself may also be omitted. 3134*0b57cec5SDimitry Andric // 3135*0b57cec5SDimitry Andric // Take all of the explicitly-specified arguments and put them into 3136*0b57cec5SDimitry Andric // the set of deduced template arguments. The partially-substituted 3137*0b57cec5SDimitry Andric // parameter pack, however, will be set to NULL since the deduction 3138*0b57cec5SDimitry Andric // mechanism handles the partially-substituted argument pack directly. 3139*0b57cec5SDimitry Andric Deduced.reserve(TemplateParams->size()); 3140*0b57cec5SDimitry Andric for (unsigned I = 0, N = ExplicitArgumentList->size(); I != N; ++I) { 3141*0b57cec5SDimitry Andric const TemplateArgument &Arg = ExplicitArgumentList->get(I); 3142*0b57cec5SDimitry Andric if (I == PartiallySubstitutedPackIndex) 3143*0b57cec5SDimitry Andric Deduced.push_back(DeducedTemplateArgument()); 3144*0b57cec5SDimitry Andric else 3145*0b57cec5SDimitry Andric Deduced.push_back(Arg); 3146*0b57cec5SDimitry Andric } 3147*0b57cec5SDimitry Andric 3148*0b57cec5SDimitry Andric return TDK_Success; 3149*0b57cec5SDimitry Andric } 3150*0b57cec5SDimitry Andric 3151*0b57cec5SDimitry Andric /// Check whether the deduced argument type for a call to a function 3152*0b57cec5SDimitry Andric /// template matches the actual argument type per C++ [temp.deduct.call]p4. 3153*0b57cec5SDimitry Andric static Sema::TemplateDeductionResult 3154*0b57cec5SDimitry Andric CheckOriginalCallArgDeduction(Sema &S, TemplateDeductionInfo &Info, 3155*0b57cec5SDimitry Andric Sema::OriginalCallArg OriginalArg, 3156*0b57cec5SDimitry Andric QualType DeducedA) { 3157*0b57cec5SDimitry Andric ASTContext &Context = S.Context; 3158*0b57cec5SDimitry Andric 3159*0b57cec5SDimitry Andric auto Failed = [&]() -> Sema::TemplateDeductionResult { 3160*0b57cec5SDimitry Andric Info.FirstArg = TemplateArgument(DeducedA); 3161*0b57cec5SDimitry Andric Info.SecondArg = TemplateArgument(OriginalArg.OriginalArgType); 3162*0b57cec5SDimitry Andric Info.CallArgIndex = OriginalArg.ArgIdx; 3163*0b57cec5SDimitry Andric return OriginalArg.DecomposedParam ? Sema::TDK_DeducedMismatchNested 3164*0b57cec5SDimitry Andric : Sema::TDK_DeducedMismatch; 3165*0b57cec5SDimitry Andric }; 3166*0b57cec5SDimitry Andric 3167*0b57cec5SDimitry Andric QualType A = OriginalArg.OriginalArgType; 3168*0b57cec5SDimitry Andric QualType OriginalParamType = OriginalArg.OriginalParamType; 3169*0b57cec5SDimitry Andric 3170*0b57cec5SDimitry Andric // Check for type equality (top-level cv-qualifiers are ignored). 3171*0b57cec5SDimitry Andric if (Context.hasSameUnqualifiedType(A, DeducedA)) 3172*0b57cec5SDimitry Andric return Sema::TDK_Success; 3173*0b57cec5SDimitry Andric 3174*0b57cec5SDimitry Andric // Strip off references on the argument types; they aren't needed for 3175*0b57cec5SDimitry Andric // the following checks. 3176*0b57cec5SDimitry Andric if (const ReferenceType *DeducedARef = DeducedA->getAs<ReferenceType>()) 3177*0b57cec5SDimitry Andric DeducedA = DeducedARef->getPointeeType(); 3178*0b57cec5SDimitry Andric if (const ReferenceType *ARef = A->getAs<ReferenceType>()) 3179*0b57cec5SDimitry Andric A = ARef->getPointeeType(); 3180*0b57cec5SDimitry Andric 3181*0b57cec5SDimitry Andric // C++ [temp.deduct.call]p4: 3182*0b57cec5SDimitry Andric // [...] However, there are three cases that allow a difference: 3183*0b57cec5SDimitry Andric // - If the original P is a reference type, the deduced A (i.e., the 3184*0b57cec5SDimitry Andric // type referred to by the reference) can be more cv-qualified than 3185*0b57cec5SDimitry Andric // the transformed A. 3186*0b57cec5SDimitry Andric if (const ReferenceType *OriginalParamRef 3187*0b57cec5SDimitry Andric = OriginalParamType->getAs<ReferenceType>()) { 3188*0b57cec5SDimitry Andric // We don't want to keep the reference around any more. 3189*0b57cec5SDimitry Andric OriginalParamType = OriginalParamRef->getPointeeType(); 3190*0b57cec5SDimitry Andric 3191*0b57cec5SDimitry Andric // FIXME: Resolve core issue (no number yet): if the original P is a 3192*0b57cec5SDimitry Andric // reference type and the transformed A is function type "noexcept F", 3193*0b57cec5SDimitry Andric // the deduced A can be F. 3194*0b57cec5SDimitry Andric QualType Tmp; 3195*0b57cec5SDimitry Andric if (A->isFunctionType() && S.IsFunctionConversion(A, DeducedA, Tmp)) 3196*0b57cec5SDimitry Andric return Sema::TDK_Success; 3197*0b57cec5SDimitry Andric 3198*0b57cec5SDimitry Andric Qualifiers AQuals = A.getQualifiers(); 3199*0b57cec5SDimitry Andric Qualifiers DeducedAQuals = DeducedA.getQualifiers(); 3200*0b57cec5SDimitry Andric 3201*0b57cec5SDimitry Andric // Under Objective-C++ ARC, the deduced type may have implicitly 3202*0b57cec5SDimitry Andric // been given strong or (when dealing with a const reference) 3203*0b57cec5SDimitry Andric // unsafe_unretained lifetime. If so, update the original 3204*0b57cec5SDimitry Andric // qualifiers to include this lifetime. 3205*0b57cec5SDimitry Andric if (S.getLangOpts().ObjCAutoRefCount && 3206*0b57cec5SDimitry Andric ((DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_Strong && 3207*0b57cec5SDimitry Andric AQuals.getObjCLifetime() == Qualifiers::OCL_None) || 3208*0b57cec5SDimitry Andric (DeducedAQuals.hasConst() && 3209*0b57cec5SDimitry Andric DeducedAQuals.getObjCLifetime() == Qualifiers::OCL_ExplicitNone))) { 3210*0b57cec5SDimitry Andric AQuals.setObjCLifetime(DeducedAQuals.getObjCLifetime()); 3211*0b57cec5SDimitry Andric } 3212*0b57cec5SDimitry Andric 3213*0b57cec5SDimitry Andric if (AQuals == DeducedAQuals) { 3214*0b57cec5SDimitry Andric // Qualifiers match; there's nothing to do. 3215*0b57cec5SDimitry Andric } else if (!DeducedAQuals.compatiblyIncludes(AQuals)) { 3216*0b57cec5SDimitry Andric return Failed(); 3217*0b57cec5SDimitry Andric } else { 3218*0b57cec5SDimitry Andric // Qualifiers are compatible, so have the argument type adopt the 3219*0b57cec5SDimitry Andric // deduced argument type's qualifiers as if we had performed the 3220*0b57cec5SDimitry Andric // qualification conversion. 3221*0b57cec5SDimitry Andric A = Context.getQualifiedType(A.getUnqualifiedType(), DeducedAQuals); 3222*0b57cec5SDimitry Andric } 3223*0b57cec5SDimitry Andric } 3224*0b57cec5SDimitry Andric 3225*0b57cec5SDimitry Andric // - The transformed A can be another pointer or pointer to member 3226*0b57cec5SDimitry Andric // type that can be converted to the deduced A via a function pointer 3227*0b57cec5SDimitry Andric // conversion and/or a qualification conversion. 3228*0b57cec5SDimitry Andric // 3229*0b57cec5SDimitry Andric // Also allow conversions which merely strip __attribute__((noreturn)) from 3230*0b57cec5SDimitry Andric // function types (recursively). 3231*0b57cec5SDimitry Andric bool ObjCLifetimeConversion = false; 3232*0b57cec5SDimitry Andric QualType ResultTy; 3233*0b57cec5SDimitry Andric if ((A->isAnyPointerType() || A->isMemberPointerType()) && 3234*0b57cec5SDimitry Andric (S.IsQualificationConversion(A, DeducedA, false, 3235*0b57cec5SDimitry Andric ObjCLifetimeConversion) || 3236*0b57cec5SDimitry Andric S.IsFunctionConversion(A, DeducedA, ResultTy))) 3237*0b57cec5SDimitry Andric return Sema::TDK_Success; 3238*0b57cec5SDimitry Andric 3239*0b57cec5SDimitry Andric // - If P is a class and P has the form simple-template-id, then the 3240*0b57cec5SDimitry Andric // transformed A can be a derived class of the deduced A. [...] 3241*0b57cec5SDimitry Andric // [...] Likewise, if P is a pointer to a class of the form 3242*0b57cec5SDimitry Andric // simple-template-id, the transformed A can be a pointer to a 3243*0b57cec5SDimitry Andric // derived class pointed to by the deduced A. 3244*0b57cec5SDimitry Andric if (const PointerType *OriginalParamPtr 3245*0b57cec5SDimitry Andric = OriginalParamType->getAs<PointerType>()) { 3246*0b57cec5SDimitry Andric if (const PointerType *DeducedAPtr = DeducedA->getAs<PointerType>()) { 3247*0b57cec5SDimitry Andric if (const PointerType *APtr = A->getAs<PointerType>()) { 3248*0b57cec5SDimitry Andric if (A->getPointeeType()->isRecordType()) { 3249*0b57cec5SDimitry Andric OriginalParamType = OriginalParamPtr->getPointeeType(); 3250*0b57cec5SDimitry Andric DeducedA = DeducedAPtr->getPointeeType(); 3251*0b57cec5SDimitry Andric A = APtr->getPointeeType(); 3252*0b57cec5SDimitry Andric } 3253*0b57cec5SDimitry Andric } 3254*0b57cec5SDimitry Andric } 3255*0b57cec5SDimitry Andric } 3256*0b57cec5SDimitry Andric 3257*0b57cec5SDimitry Andric if (Context.hasSameUnqualifiedType(A, DeducedA)) 3258*0b57cec5SDimitry Andric return Sema::TDK_Success; 3259*0b57cec5SDimitry Andric 3260*0b57cec5SDimitry Andric if (A->isRecordType() && isSimpleTemplateIdType(OriginalParamType) && 3261*0b57cec5SDimitry Andric S.IsDerivedFrom(Info.getLocation(), A, DeducedA)) 3262*0b57cec5SDimitry Andric return Sema::TDK_Success; 3263*0b57cec5SDimitry Andric 3264*0b57cec5SDimitry Andric return Failed(); 3265*0b57cec5SDimitry Andric } 3266*0b57cec5SDimitry Andric 3267*0b57cec5SDimitry Andric /// Find the pack index for a particular parameter index in an instantiation of 3268*0b57cec5SDimitry Andric /// a function template with specific arguments. 3269*0b57cec5SDimitry Andric /// 3270*0b57cec5SDimitry Andric /// \return The pack index for whichever pack produced this parameter, or -1 3271*0b57cec5SDimitry Andric /// if this was not produced by a parameter. Intended to be used as the 3272*0b57cec5SDimitry Andric /// ArgumentPackSubstitutionIndex for further substitutions. 3273*0b57cec5SDimitry Andric // FIXME: We should track this in OriginalCallArgs so we don't need to 3274*0b57cec5SDimitry Andric // reconstruct it here. 3275*0b57cec5SDimitry Andric static unsigned getPackIndexForParam(Sema &S, 3276*0b57cec5SDimitry Andric FunctionTemplateDecl *FunctionTemplate, 3277*0b57cec5SDimitry Andric const MultiLevelTemplateArgumentList &Args, 3278*0b57cec5SDimitry Andric unsigned ParamIdx) { 3279*0b57cec5SDimitry Andric unsigned Idx = 0; 3280*0b57cec5SDimitry Andric for (auto *PD : FunctionTemplate->getTemplatedDecl()->parameters()) { 3281*0b57cec5SDimitry Andric if (PD->isParameterPack()) { 3282*0b57cec5SDimitry Andric unsigned NumExpansions = 3283*0b57cec5SDimitry Andric S.getNumArgumentsInExpansion(PD->getType(), Args).getValueOr(1); 3284*0b57cec5SDimitry Andric if (Idx + NumExpansions > ParamIdx) 3285*0b57cec5SDimitry Andric return ParamIdx - Idx; 3286*0b57cec5SDimitry Andric Idx += NumExpansions; 3287*0b57cec5SDimitry Andric } else { 3288*0b57cec5SDimitry Andric if (Idx == ParamIdx) 3289*0b57cec5SDimitry Andric return -1; // Not a pack expansion 3290*0b57cec5SDimitry Andric ++Idx; 3291*0b57cec5SDimitry Andric } 3292*0b57cec5SDimitry Andric } 3293*0b57cec5SDimitry Andric 3294*0b57cec5SDimitry Andric llvm_unreachable("parameter index would not be produced from template"); 3295*0b57cec5SDimitry Andric } 3296*0b57cec5SDimitry Andric 3297*0b57cec5SDimitry Andric /// Finish template argument deduction for a function template, 3298*0b57cec5SDimitry Andric /// checking the deduced template arguments for completeness and forming 3299*0b57cec5SDimitry Andric /// the function template specialization. 3300*0b57cec5SDimitry Andric /// 3301*0b57cec5SDimitry Andric /// \param OriginalCallArgs If non-NULL, the original call arguments against 3302*0b57cec5SDimitry Andric /// which the deduced argument types should be compared. 3303*0b57cec5SDimitry Andric Sema::TemplateDeductionResult Sema::FinishTemplateArgumentDeduction( 3304*0b57cec5SDimitry Andric FunctionTemplateDecl *FunctionTemplate, 3305*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 3306*0b57cec5SDimitry Andric unsigned NumExplicitlySpecified, FunctionDecl *&Specialization, 3307*0b57cec5SDimitry Andric TemplateDeductionInfo &Info, 3308*0b57cec5SDimitry Andric SmallVectorImpl<OriginalCallArg> const *OriginalCallArgs, 3309*0b57cec5SDimitry Andric bool PartialOverloading, llvm::function_ref<bool()> CheckNonDependent) { 3310*0b57cec5SDimitry Andric // Unevaluated SFINAE context. 3311*0b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 3312*0b57cec5SDimitry Andric *this, Sema::ExpressionEvaluationContext::Unevaluated); 3313*0b57cec5SDimitry Andric SFINAETrap Trap(*this); 3314*0b57cec5SDimitry Andric 3315*0b57cec5SDimitry Andric // Enter a new template instantiation context while we instantiate the 3316*0b57cec5SDimitry Andric // actual function declaration. 3317*0b57cec5SDimitry Andric SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), Deduced.end()); 3318*0b57cec5SDimitry Andric InstantiatingTemplate Inst( 3319*0b57cec5SDimitry Andric *this, Info.getLocation(), FunctionTemplate, DeducedArgs, 3320*0b57cec5SDimitry Andric CodeSynthesisContext::DeducedTemplateArgumentSubstitution, Info); 3321*0b57cec5SDimitry Andric if (Inst.isInvalid()) 3322*0b57cec5SDimitry Andric return TDK_InstantiationDepth; 3323*0b57cec5SDimitry Andric 3324*0b57cec5SDimitry Andric ContextRAII SavedContext(*this, FunctionTemplate->getTemplatedDecl()); 3325*0b57cec5SDimitry Andric 3326*0b57cec5SDimitry Andric // C++ [temp.deduct.type]p2: 3327*0b57cec5SDimitry Andric // [...] or if any template argument remains neither deduced nor 3328*0b57cec5SDimitry Andric // explicitly specified, template argument deduction fails. 3329*0b57cec5SDimitry Andric SmallVector<TemplateArgument, 4> Builder; 3330*0b57cec5SDimitry Andric if (auto Result = ConvertDeducedTemplateArguments( 3331*0b57cec5SDimitry Andric *this, FunctionTemplate, /*IsDeduced*/true, Deduced, Info, Builder, 3332*0b57cec5SDimitry Andric CurrentInstantiationScope, NumExplicitlySpecified, 3333*0b57cec5SDimitry Andric PartialOverloading)) 3334*0b57cec5SDimitry Andric return Result; 3335*0b57cec5SDimitry Andric 3336*0b57cec5SDimitry Andric // C++ [temp.deduct.call]p10: [DR1391] 3337*0b57cec5SDimitry Andric // If deduction succeeds for all parameters that contain 3338*0b57cec5SDimitry Andric // template-parameters that participate in template argument deduction, 3339*0b57cec5SDimitry Andric // and all template arguments are explicitly specified, deduced, or 3340*0b57cec5SDimitry Andric // obtained from default template arguments, remaining parameters are then 3341*0b57cec5SDimitry Andric // compared with the corresponding arguments. For each remaining parameter 3342*0b57cec5SDimitry Andric // P with a type that was non-dependent before substitution of any 3343*0b57cec5SDimitry Andric // explicitly-specified template arguments, if the corresponding argument 3344*0b57cec5SDimitry Andric // A cannot be implicitly converted to P, deduction fails. 3345*0b57cec5SDimitry Andric if (CheckNonDependent()) 3346*0b57cec5SDimitry Andric return TDK_NonDependentConversionFailure; 3347*0b57cec5SDimitry Andric 3348*0b57cec5SDimitry Andric // Form the template argument list from the deduced template arguments. 3349*0b57cec5SDimitry Andric TemplateArgumentList *DeducedArgumentList 3350*0b57cec5SDimitry Andric = TemplateArgumentList::CreateCopy(Context, Builder); 3351*0b57cec5SDimitry Andric Info.reset(DeducedArgumentList); 3352*0b57cec5SDimitry Andric 3353*0b57cec5SDimitry Andric // Substitute the deduced template arguments into the function template 3354*0b57cec5SDimitry Andric // declaration to produce the function template specialization. 3355*0b57cec5SDimitry Andric DeclContext *Owner = FunctionTemplate->getDeclContext(); 3356*0b57cec5SDimitry Andric if (FunctionTemplate->getFriendObjectKind()) 3357*0b57cec5SDimitry Andric Owner = FunctionTemplate->getLexicalDeclContext(); 3358*0b57cec5SDimitry Andric MultiLevelTemplateArgumentList SubstArgs(*DeducedArgumentList); 3359*0b57cec5SDimitry Andric Specialization = cast_or_null<FunctionDecl>( 3360*0b57cec5SDimitry Andric SubstDecl(FunctionTemplate->getTemplatedDecl(), Owner, SubstArgs)); 3361*0b57cec5SDimitry Andric if (!Specialization || Specialization->isInvalidDecl()) 3362*0b57cec5SDimitry Andric return TDK_SubstitutionFailure; 3363*0b57cec5SDimitry Andric 3364*0b57cec5SDimitry Andric assert(Specialization->getPrimaryTemplate()->getCanonicalDecl() == 3365*0b57cec5SDimitry Andric FunctionTemplate->getCanonicalDecl()); 3366*0b57cec5SDimitry Andric 3367*0b57cec5SDimitry Andric // If the template argument list is owned by the function template 3368*0b57cec5SDimitry Andric // specialization, release it. 3369*0b57cec5SDimitry Andric if (Specialization->getTemplateSpecializationArgs() == DeducedArgumentList && 3370*0b57cec5SDimitry Andric !Trap.hasErrorOccurred()) 3371*0b57cec5SDimitry Andric Info.take(); 3372*0b57cec5SDimitry Andric 3373*0b57cec5SDimitry Andric // There may have been an error that did not prevent us from constructing a 3374*0b57cec5SDimitry Andric // declaration. Mark the declaration invalid and return with a substitution 3375*0b57cec5SDimitry Andric // failure. 3376*0b57cec5SDimitry Andric if (Trap.hasErrorOccurred()) { 3377*0b57cec5SDimitry Andric Specialization->setInvalidDecl(true); 3378*0b57cec5SDimitry Andric return TDK_SubstitutionFailure; 3379*0b57cec5SDimitry Andric } 3380*0b57cec5SDimitry Andric 3381*0b57cec5SDimitry Andric if (OriginalCallArgs) { 3382*0b57cec5SDimitry Andric // C++ [temp.deduct.call]p4: 3383*0b57cec5SDimitry Andric // In general, the deduction process attempts to find template argument 3384*0b57cec5SDimitry Andric // values that will make the deduced A identical to A (after the type A 3385*0b57cec5SDimitry Andric // is transformed as described above). [...] 3386*0b57cec5SDimitry Andric llvm::SmallDenseMap<std::pair<unsigned, QualType>, QualType> DeducedATypes; 3387*0b57cec5SDimitry Andric for (unsigned I = 0, N = OriginalCallArgs->size(); I != N; ++I) { 3388*0b57cec5SDimitry Andric OriginalCallArg OriginalArg = (*OriginalCallArgs)[I]; 3389*0b57cec5SDimitry Andric 3390*0b57cec5SDimitry Andric auto ParamIdx = OriginalArg.ArgIdx; 3391*0b57cec5SDimitry Andric if (ParamIdx >= Specialization->getNumParams()) 3392*0b57cec5SDimitry Andric // FIXME: This presumably means a pack ended up smaller than we 3393*0b57cec5SDimitry Andric // expected while deducing. Should this not result in deduction 3394*0b57cec5SDimitry Andric // failure? Can it even happen? 3395*0b57cec5SDimitry Andric continue; 3396*0b57cec5SDimitry Andric 3397*0b57cec5SDimitry Andric QualType DeducedA; 3398*0b57cec5SDimitry Andric if (!OriginalArg.DecomposedParam) { 3399*0b57cec5SDimitry Andric // P is one of the function parameters, just look up its substituted 3400*0b57cec5SDimitry Andric // type. 3401*0b57cec5SDimitry Andric DeducedA = Specialization->getParamDecl(ParamIdx)->getType(); 3402*0b57cec5SDimitry Andric } else { 3403*0b57cec5SDimitry Andric // P is a decomposed element of a parameter corresponding to a 3404*0b57cec5SDimitry Andric // braced-init-list argument. Substitute back into P to find the 3405*0b57cec5SDimitry Andric // deduced A. 3406*0b57cec5SDimitry Andric QualType &CacheEntry = 3407*0b57cec5SDimitry Andric DeducedATypes[{ParamIdx, OriginalArg.OriginalParamType}]; 3408*0b57cec5SDimitry Andric if (CacheEntry.isNull()) { 3409*0b57cec5SDimitry Andric ArgumentPackSubstitutionIndexRAII PackIndex( 3410*0b57cec5SDimitry Andric *this, getPackIndexForParam(*this, FunctionTemplate, SubstArgs, 3411*0b57cec5SDimitry Andric ParamIdx)); 3412*0b57cec5SDimitry Andric CacheEntry = 3413*0b57cec5SDimitry Andric SubstType(OriginalArg.OriginalParamType, SubstArgs, 3414*0b57cec5SDimitry Andric Specialization->getTypeSpecStartLoc(), 3415*0b57cec5SDimitry Andric Specialization->getDeclName()); 3416*0b57cec5SDimitry Andric } 3417*0b57cec5SDimitry Andric DeducedA = CacheEntry; 3418*0b57cec5SDimitry Andric } 3419*0b57cec5SDimitry Andric 3420*0b57cec5SDimitry Andric if (auto TDK = 3421*0b57cec5SDimitry Andric CheckOriginalCallArgDeduction(*this, Info, OriginalArg, DeducedA)) 3422*0b57cec5SDimitry Andric return TDK; 3423*0b57cec5SDimitry Andric } 3424*0b57cec5SDimitry Andric } 3425*0b57cec5SDimitry Andric 3426*0b57cec5SDimitry Andric // If we suppressed any diagnostics while performing template argument 3427*0b57cec5SDimitry Andric // deduction, and if we haven't already instantiated this declaration, 3428*0b57cec5SDimitry Andric // keep track of these diagnostics. They'll be emitted if this specialization 3429*0b57cec5SDimitry Andric // is actually used. 3430*0b57cec5SDimitry Andric if (Info.diag_begin() != Info.diag_end()) { 3431*0b57cec5SDimitry Andric SuppressedDiagnosticsMap::iterator 3432*0b57cec5SDimitry Andric Pos = SuppressedDiagnostics.find(Specialization->getCanonicalDecl()); 3433*0b57cec5SDimitry Andric if (Pos == SuppressedDiagnostics.end()) 3434*0b57cec5SDimitry Andric SuppressedDiagnostics[Specialization->getCanonicalDecl()] 3435*0b57cec5SDimitry Andric .append(Info.diag_begin(), Info.diag_end()); 3436*0b57cec5SDimitry Andric } 3437*0b57cec5SDimitry Andric 3438*0b57cec5SDimitry Andric return TDK_Success; 3439*0b57cec5SDimitry Andric } 3440*0b57cec5SDimitry Andric 3441*0b57cec5SDimitry Andric /// Gets the type of a function for template-argument-deducton 3442*0b57cec5SDimitry Andric /// purposes when it's considered as part of an overload set. 3443*0b57cec5SDimitry Andric static QualType GetTypeOfFunction(Sema &S, const OverloadExpr::FindResult &R, 3444*0b57cec5SDimitry Andric FunctionDecl *Fn) { 3445*0b57cec5SDimitry Andric // We may need to deduce the return type of the function now. 3446*0b57cec5SDimitry Andric if (S.getLangOpts().CPlusPlus14 && Fn->getReturnType()->isUndeducedType() && 3447*0b57cec5SDimitry Andric S.DeduceReturnType(Fn, R.Expression->getExprLoc(), /*Diagnose*/ false)) 3448*0b57cec5SDimitry Andric return {}; 3449*0b57cec5SDimitry Andric 3450*0b57cec5SDimitry Andric if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Fn)) 3451*0b57cec5SDimitry Andric if (Method->isInstance()) { 3452*0b57cec5SDimitry Andric // An instance method that's referenced in a form that doesn't 3453*0b57cec5SDimitry Andric // look like a member pointer is just invalid. 3454*0b57cec5SDimitry Andric if (!R.HasFormOfMemberPointer) 3455*0b57cec5SDimitry Andric return {}; 3456*0b57cec5SDimitry Andric 3457*0b57cec5SDimitry Andric return S.Context.getMemberPointerType(Fn->getType(), 3458*0b57cec5SDimitry Andric S.Context.getTypeDeclType(Method->getParent()).getTypePtr()); 3459*0b57cec5SDimitry Andric } 3460*0b57cec5SDimitry Andric 3461*0b57cec5SDimitry Andric if (!R.IsAddressOfOperand) return Fn->getType(); 3462*0b57cec5SDimitry Andric return S.Context.getPointerType(Fn->getType()); 3463*0b57cec5SDimitry Andric } 3464*0b57cec5SDimitry Andric 3465*0b57cec5SDimitry Andric /// Apply the deduction rules for overload sets. 3466*0b57cec5SDimitry Andric /// 3467*0b57cec5SDimitry Andric /// \return the null type if this argument should be treated as an 3468*0b57cec5SDimitry Andric /// undeduced context 3469*0b57cec5SDimitry Andric static QualType 3470*0b57cec5SDimitry Andric ResolveOverloadForDeduction(Sema &S, TemplateParameterList *TemplateParams, 3471*0b57cec5SDimitry Andric Expr *Arg, QualType ParamType, 3472*0b57cec5SDimitry Andric bool ParamWasReference) { 3473*0b57cec5SDimitry Andric 3474*0b57cec5SDimitry Andric OverloadExpr::FindResult R = OverloadExpr::find(Arg); 3475*0b57cec5SDimitry Andric 3476*0b57cec5SDimitry Andric OverloadExpr *Ovl = R.Expression; 3477*0b57cec5SDimitry Andric 3478*0b57cec5SDimitry Andric // C++0x [temp.deduct.call]p4 3479*0b57cec5SDimitry Andric unsigned TDF = 0; 3480*0b57cec5SDimitry Andric if (ParamWasReference) 3481*0b57cec5SDimitry Andric TDF |= TDF_ParamWithReferenceType; 3482*0b57cec5SDimitry Andric if (R.IsAddressOfOperand) 3483*0b57cec5SDimitry Andric TDF |= TDF_IgnoreQualifiers; 3484*0b57cec5SDimitry Andric 3485*0b57cec5SDimitry Andric // C++0x [temp.deduct.call]p6: 3486*0b57cec5SDimitry Andric // When P is a function type, pointer to function type, or pointer 3487*0b57cec5SDimitry Andric // to member function type: 3488*0b57cec5SDimitry Andric 3489*0b57cec5SDimitry Andric if (!ParamType->isFunctionType() && 3490*0b57cec5SDimitry Andric !ParamType->isFunctionPointerType() && 3491*0b57cec5SDimitry Andric !ParamType->isMemberFunctionPointerType()) { 3492*0b57cec5SDimitry Andric if (Ovl->hasExplicitTemplateArgs()) { 3493*0b57cec5SDimitry Andric // But we can still look for an explicit specialization. 3494*0b57cec5SDimitry Andric if (FunctionDecl *ExplicitSpec 3495*0b57cec5SDimitry Andric = S.ResolveSingleFunctionTemplateSpecialization(Ovl)) 3496*0b57cec5SDimitry Andric return GetTypeOfFunction(S, R, ExplicitSpec); 3497*0b57cec5SDimitry Andric } 3498*0b57cec5SDimitry Andric 3499*0b57cec5SDimitry Andric DeclAccessPair DAP; 3500*0b57cec5SDimitry Andric if (FunctionDecl *Viable = 3501*0b57cec5SDimitry Andric S.resolveAddressOfOnlyViableOverloadCandidate(Arg, DAP)) 3502*0b57cec5SDimitry Andric return GetTypeOfFunction(S, R, Viable); 3503*0b57cec5SDimitry Andric 3504*0b57cec5SDimitry Andric return {}; 3505*0b57cec5SDimitry Andric } 3506*0b57cec5SDimitry Andric 3507*0b57cec5SDimitry Andric // Gather the explicit template arguments, if any. 3508*0b57cec5SDimitry Andric TemplateArgumentListInfo ExplicitTemplateArgs; 3509*0b57cec5SDimitry Andric if (Ovl->hasExplicitTemplateArgs()) 3510*0b57cec5SDimitry Andric Ovl->copyTemplateArgumentsInto(ExplicitTemplateArgs); 3511*0b57cec5SDimitry Andric QualType Match; 3512*0b57cec5SDimitry Andric for (UnresolvedSetIterator I = Ovl->decls_begin(), 3513*0b57cec5SDimitry Andric E = Ovl->decls_end(); I != E; ++I) { 3514*0b57cec5SDimitry Andric NamedDecl *D = (*I)->getUnderlyingDecl(); 3515*0b57cec5SDimitry Andric 3516*0b57cec5SDimitry Andric if (FunctionTemplateDecl *FunTmpl = dyn_cast<FunctionTemplateDecl>(D)) { 3517*0b57cec5SDimitry Andric // - If the argument is an overload set containing one or more 3518*0b57cec5SDimitry Andric // function templates, the parameter is treated as a 3519*0b57cec5SDimitry Andric // non-deduced context. 3520*0b57cec5SDimitry Andric if (!Ovl->hasExplicitTemplateArgs()) 3521*0b57cec5SDimitry Andric return {}; 3522*0b57cec5SDimitry Andric 3523*0b57cec5SDimitry Andric // Otherwise, see if we can resolve a function type 3524*0b57cec5SDimitry Andric FunctionDecl *Specialization = nullptr; 3525*0b57cec5SDimitry Andric TemplateDeductionInfo Info(Ovl->getNameLoc()); 3526*0b57cec5SDimitry Andric if (S.DeduceTemplateArguments(FunTmpl, &ExplicitTemplateArgs, 3527*0b57cec5SDimitry Andric Specialization, Info)) 3528*0b57cec5SDimitry Andric continue; 3529*0b57cec5SDimitry Andric 3530*0b57cec5SDimitry Andric D = Specialization; 3531*0b57cec5SDimitry Andric } 3532*0b57cec5SDimitry Andric 3533*0b57cec5SDimitry Andric FunctionDecl *Fn = cast<FunctionDecl>(D); 3534*0b57cec5SDimitry Andric QualType ArgType = GetTypeOfFunction(S, R, Fn); 3535*0b57cec5SDimitry Andric if (ArgType.isNull()) continue; 3536*0b57cec5SDimitry Andric 3537*0b57cec5SDimitry Andric // Function-to-pointer conversion. 3538*0b57cec5SDimitry Andric if (!ParamWasReference && ParamType->isPointerType() && 3539*0b57cec5SDimitry Andric ArgType->isFunctionType()) 3540*0b57cec5SDimitry Andric ArgType = S.Context.getPointerType(ArgType); 3541*0b57cec5SDimitry Andric 3542*0b57cec5SDimitry Andric // - If the argument is an overload set (not containing function 3543*0b57cec5SDimitry Andric // templates), trial argument deduction is attempted using each 3544*0b57cec5SDimitry Andric // of the members of the set. If deduction succeeds for only one 3545*0b57cec5SDimitry Andric // of the overload set members, that member is used as the 3546*0b57cec5SDimitry Andric // argument value for the deduction. If deduction succeeds for 3547*0b57cec5SDimitry Andric // more than one member of the overload set the parameter is 3548*0b57cec5SDimitry Andric // treated as a non-deduced context. 3549*0b57cec5SDimitry Andric 3550*0b57cec5SDimitry Andric // We do all of this in a fresh context per C++0x [temp.deduct.type]p2: 3551*0b57cec5SDimitry Andric // Type deduction is done independently for each P/A pair, and 3552*0b57cec5SDimitry Andric // the deduced template argument values are then combined. 3553*0b57cec5SDimitry Andric // So we do not reject deductions which were made elsewhere. 3554*0b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 8> 3555*0b57cec5SDimitry Andric Deduced(TemplateParams->size()); 3556*0b57cec5SDimitry Andric TemplateDeductionInfo Info(Ovl->getNameLoc()); 3557*0b57cec5SDimitry Andric Sema::TemplateDeductionResult Result 3558*0b57cec5SDimitry Andric = DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, 3559*0b57cec5SDimitry Andric ArgType, Info, Deduced, TDF); 3560*0b57cec5SDimitry Andric if (Result) continue; 3561*0b57cec5SDimitry Andric if (!Match.isNull()) 3562*0b57cec5SDimitry Andric return {}; 3563*0b57cec5SDimitry Andric Match = ArgType; 3564*0b57cec5SDimitry Andric } 3565*0b57cec5SDimitry Andric 3566*0b57cec5SDimitry Andric return Match; 3567*0b57cec5SDimitry Andric } 3568*0b57cec5SDimitry Andric 3569*0b57cec5SDimitry Andric /// Perform the adjustments to the parameter and argument types 3570*0b57cec5SDimitry Andric /// described in C++ [temp.deduct.call]. 3571*0b57cec5SDimitry Andric /// 3572*0b57cec5SDimitry Andric /// \returns true if the caller should not attempt to perform any template 3573*0b57cec5SDimitry Andric /// argument deduction based on this P/A pair because the argument is an 3574*0b57cec5SDimitry Andric /// overloaded function set that could not be resolved. 3575*0b57cec5SDimitry Andric static bool AdjustFunctionParmAndArgTypesForDeduction( 3576*0b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, 3577*0b57cec5SDimitry Andric QualType &ParamType, QualType &ArgType, Expr *Arg, unsigned &TDF) { 3578*0b57cec5SDimitry Andric // C++0x [temp.deduct.call]p3: 3579*0b57cec5SDimitry Andric // If P is a cv-qualified type, the top level cv-qualifiers of P's type 3580*0b57cec5SDimitry Andric // are ignored for type deduction. 3581*0b57cec5SDimitry Andric if (ParamType.hasQualifiers()) 3582*0b57cec5SDimitry Andric ParamType = ParamType.getUnqualifiedType(); 3583*0b57cec5SDimitry Andric 3584*0b57cec5SDimitry Andric // [...] If P is a reference type, the type referred to by P is 3585*0b57cec5SDimitry Andric // used for type deduction. 3586*0b57cec5SDimitry Andric const ReferenceType *ParamRefType = ParamType->getAs<ReferenceType>(); 3587*0b57cec5SDimitry Andric if (ParamRefType) 3588*0b57cec5SDimitry Andric ParamType = ParamRefType->getPointeeType(); 3589*0b57cec5SDimitry Andric 3590*0b57cec5SDimitry Andric // Overload sets usually make this parameter an undeduced context, 3591*0b57cec5SDimitry Andric // but there are sometimes special circumstances. Typically 3592*0b57cec5SDimitry Andric // involving a template-id-expr. 3593*0b57cec5SDimitry Andric if (ArgType == S.Context.OverloadTy) { 3594*0b57cec5SDimitry Andric ArgType = ResolveOverloadForDeduction(S, TemplateParams, 3595*0b57cec5SDimitry Andric Arg, ParamType, 3596*0b57cec5SDimitry Andric ParamRefType != nullptr); 3597*0b57cec5SDimitry Andric if (ArgType.isNull()) 3598*0b57cec5SDimitry Andric return true; 3599*0b57cec5SDimitry Andric } 3600*0b57cec5SDimitry Andric 3601*0b57cec5SDimitry Andric if (ParamRefType) { 3602*0b57cec5SDimitry Andric // If the argument has incomplete array type, try to complete its type. 3603*0b57cec5SDimitry Andric if (ArgType->isIncompleteArrayType()) { 3604*0b57cec5SDimitry Andric S.completeExprArrayBound(Arg); 3605*0b57cec5SDimitry Andric ArgType = Arg->getType(); 3606*0b57cec5SDimitry Andric } 3607*0b57cec5SDimitry Andric 3608*0b57cec5SDimitry Andric // C++1z [temp.deduct.call]p3: 3609*0b57cec5SDimitry Andric // If P is a forwarding reference and the argument is an lvalue, the type 3610*0b57cec5SDimitry Andric // "lvalue reference to A" is used in place of A for type deduction. 3611*0b57cec5SDimitry Andric if (isForwardingReference(QualType(ParamRefType, 0), FirstInnerIndex) && 3612*0b57cec5SDimitry Andric Arg->isLValue()) 3613*0b57cec5SDimitry Andric ArgType = S.Context.getLValueReferenceType(ArgType); 3614*0b57cec5SDimitry Andric } else { 3615*0b57cec5SDimitry Andric // C++ [temp.deduct.call]p2: 3616*0b57cec5SDimitry Andric // If P is not a reference type: 3617*0b57cec5SDimitry Andric // - If A is an array type, the pointer type produced by the 3618*0b57cec5SDimitry Andric // array-to-pointer standard conversion (4.2) is used in place of 3619*0b57cec5SDimitry Andric // A for type deduction; otherwise, 3620*0b57cec5SDimitry Andric if (ArgType->isArrayType()) 3621*0b57cec5SDimitry Andric ArgType = S.Context.getArrayDecayedType(ArgType); 3622*0b57cec5SDimitry Andric // - If A is a function type, the pointer type produced by the 3623*0b57cec5SDimitry Andric // function-to-pointer standard conversion (4.3) is used in place 3624*0b57cec5SDimitry Andric // of A for type deduction; otherwise, 3625*0b57cec5SDimitry Andric else if (ArgType->isFunctionType()) 3626*0b57cec5SDimitry Andric ArgType = S.Context.getPointerType(ArgType); 3627*0b57cec5SDimitry Andric else { 3628*0b57cec5SDimitry Andric // - If A is a cv-qualified type, the top level cv-qualifiers of A's 3629*0b57cec5SDimitry Andric // type are ignored for type deduction. 3630*0b57cec5SDimitry Andric ArgType = ArgType.getUnqualifiedType(); 3631*0b57cec5SDimitry Andric } 3632*0b57cec5SDimitry Andric } 3633*0b57cec5SDimitry Andric 3634*0b57cec5SDimitry Andric // C++0x [temp.deduct.call]p4: 3635*0b57cec5SDimitry Andric // In general, the deduction process attempts to find template argument 3636*0b57cec5SDimitry Andric // values that will make the deduced A identical to A (after the type A 3637*0b57cec5SDimitry Andric // is transformed as described above). [...] 3638*0b57cec5SDimitry Andric TDF = TDF_SkipNonDependent; 3639*0b57cec5SDimitry Andric 3640*0b57cec5SDimitry Andric // - If the original P is a reference type, the deduced A (i.e., the 3641*0b57cec5SDimitry Andric // type referred to by the reference) can be more cv-qualified than 3642*0b57cec5SDimitry Andric // the transformed A. 3643*0b57cec5SDimitry Andric if (ParamRefType) 3644*0b57cec5SDimitry Andric TDF |= TDF_ParamWithReferenceType; 3645*0b57cec5SDimitry Andric // - The transformed A can be another pointer or pointer to member 3646*0b57cec5SDimitry Andric // type that can be converted to the deduced A via a qualification 3647*0b57cec5SDimitry Andric // conversion (4.4). 3648*0b57cec5SDimitry Andric if (ArgType->isPointerType() || ArgType->isMemberPointerType() || 3649*0b57cec5SDimitry Andric ArgType->isObjCObjectPointerType()) 3650*0b57cec5SDimitry Andric TDF |= TDF_IgnoreQualifiers; 3651*0b57cec5SDimitry Andric // - If P is a class and P has the form simple-template-id, then the 3652*0b57cec5SDimitry Andric // transformed A can be a derived class of the deduced A. Likewise, 3653*0b57cec5SDimitry Andric // if P is a pointer to a class of the form simple-template-id, the 3654*0b57cec5SDimitry Andric // transformed A can be a pointer to a derived class pointed to by 3655*0b57cec5SDimitry Andric // the deduced A. 3656*0b57cec5SDimitry Andric if (isSimpleTemplateIdType(ParamType) || 3657*0b57cec5SDimitry Andric (isa<PointerType>(ParamType) && 3658*0b57cec5SDimitry Andric isSimpleTemplateIdType( 3659*0b57cec5SDimitry Andric ParamType->getAs<PointerType>()->getPointeeType()))) 3660*0b57cec5SDimitry Andric TDF |= TDF_DerivedClass; 3661*0b57cec5SDimitry Andric 3662*0b57cec5SDimitry Andric return false; 3663*0b57cec5SDimitry Andric } 3664*0b57cec5SDimitry Andric 3665*0b57cec5SDimitry Andric static bool 3666*0b57cec5SDimitry Andric hasDeducibleTemplateParameters(Sema &S, FunctionTemplateDecl *FunctionTemplate, 3667*0b57cec5SDimitry Andric QualType T); 3668*0b57cec5SDimitry Andric 3669*0b57cec5SDimitry Andric static Sema::TemplateDeductionResult DeduceTemplateArgumentsFromCallArgument( 3670*0b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, 3671*0b57cec5SDimitry Andric QualType ParamType, Expr *Arg, TemplateDeductionInfo &Info, 3672*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 3673*0b57cec5SDimitry Andric SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, 3674*0b57cec5SDimitry Andric bool DecomposedParam, unsigned ArgIdx, unsigned TDF); 3675*0b57cec5SDimitry Andric 3676*0b57cec5SDimitry Andric /// Attempt template argument deduction from an initializer list 3677*0b57cec5SDimitry Andric /// deemed to be an argument in a function call. 3678*0b57cec5SDimitry Andric static Sema::TemplateDeductionResult DeduceFromInitializerList( 3679*0b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, QualType AdjustedParamType, 3680*0b57cec5SDimitry Andric InitListExpr *ILE, TemplateDeductionInfo &Info, 3681*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 3682*0b57cec5SDimitry Andric SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, unsigned ArgIdx, 3683*0b57cec5SDimitry Andric unsigned TDF) { 3684*0b57cec5SDimitry Andric // C++ [temp.deduct.call]p1: (CWG 1591) 3685*0b57cec5SDimitry Andric // If removing references and cv-qualifiers from P gives 3686*0b57cec5SDimitry Andric // std::initializer_list<P0> or P0[N] for some P0 and N and the argument is 3687*0b57cec5SDimitry Andric // a non-empty initializer list, then deduction is performed instead for 3688*0b57cec5SDimitry Andric // each element of the initializer list, taking P0 as a function template 3689*0b57cec5SDimitry Andric // parameter type and the initializer element as its argument 3690*0b57cec5SDimitry Andric // 3691*0b57cec5SDimitry Andric // We've already removed references and cv-qualifiers here. 3692*0b57cec5SDimitry Andric if (!ILE->getNumInits()) 3693*0b57cec5SDimitry Andric return Sema::TDK_Success; 3694*0b57cec5SDimitry Andric 3695*0b57cec5SDimitry Andric QualType ElTy; 3696*0b57cec5SDimitry Andric auto *ArrTy = S.Context.getAsArrayType(AdjustedParamType); 3697*0b57cec5SDimitry Andric if (ArrTy) 3698*0b57cec5SDimitry Andric ElTy = ArrTy->getElementType(); 3699*0b57cec5SDimitry Andric else if (!S.isStdInitializerList(AdjustedParamType, &ElTy)) { 3700*0b57cec5SDimitry Andric // Otherwise, an initializer list argument causes the parameter to be 3701*0b57cec5SDimitry Andric // considered a non-deduced context 3702*0b57cec5SDimitry Andric return Sema::TDK_Success; 3703*0b57cec5SDimitry Andric } 3704*0b57cec5SDimitry Andric 3705*0b57cec5SDimitry Andric // Deduction only needs to be done for dependent types. 3706*0b57cec5SDimitry Andric if (ElTy->isDependentType()) { 3707*0b57cec5SDimitry Andric for (Expr *E : ILE->inits()) { 3708*0b57cec5SDimitry Andric if (auto Result = DeduceTemplateArgumentsFromCallArgument( 3709*0b57cec5SDimitry Andric S, TemplateParams, 0, ElTy, E, Info, Deduced, OriginalCallArgs, true, 3710*0b57cec5SDimitry Andric ArgIdx, TDF)) 3711*0b57cec5SDimitry Andric return Result; 3712*0b57cec5SDimitry Andric } 3713*0b57cec5SDimitry Andric } 3714*0b57cec5SDimitry Andric 3715*0b57cec5SDimitry Andric // in the P0[N] case, if N is a non-type template parameter, N is deduced 3716*0b57cec5SDimitry Andric // from the length of the initializer list. 3717*0b57cec5SDimitry Andric if (auto *DependentArrTy = dyn_cast_or_null<DependentSizedArrayType>(ArrTy)) { 3718*0b57cec5SDimitry Andric // Determine the array bound is something we can deduce. 3719*0b57cec5SDimitry Andric if (NonTypeTemplateParmDecl *NTTP = 3720*0b57cec5SDimitry Andric getDeducedParameterFromExpr(Info, DependentArrTy->getSizeExpr())) { 3721*0b57cec5SDimitry Andric // We can perform template argument deduction for the given non-type 3722*0b57cec5SDimitry Andric // template parameter. 3723*0b57cec5SDimitry Andric // C++ [temp.deduct.type]p13: 3724*0b57cec5SDimitry Andric // The type of N in the type T[N] is std::size_t. 3725*0b57cec5SDimitry Andric QualType T = S.Context.getSizeType(); 3726*0b57cec5SDimitry Andric llvm::APInt Size(S.Context.getIntWidth(T), ILE->getNumInits()); 3727*0b57cec5SDimitry Andric if (auto Result = DeduceNonTypeTemplateArgument( 3728*0b57cec5SDimitry Andric S, TemplateParams, NTTP, llvm::APSInt(Size), T, 3729*0b57cec5SDimitry Andric /*ArrayBound=*/true, Info, Deduced)) 3730*0b57cec5SDimitry Andric return Result; 3731*0b57cec5SDimitry Andric } 3732*0b57cec5SDimitry Andric } 3733*0b57cec5SDimitry Andric 3734*0b57cec5SDimitry Andric return Sema::TDK_Success; 3735*0b57cec5SDimitry Andric } 3736*0b57cec5SDimitry Andric 3737*0b57cec5SDimitry Andric /// Perform template argument deduction per [temp.deduct.call] for a 3738*0b57cec5SDimitry Andric /// single parameter / argument pair. 3739*0b57cec5SDimitry Andric static Sema::TemplateDeductionResult DeduceTemplateArgumentsFromCallArgument( 3740*0b57cec5SDimitry Andric Sema &S, TemplateParameterList *TemplateParams, unsigned FirstInnerIndex, 3741*0b57cec5SDimitry Andric QualType ParamType, Expr *Arg, TemplateDeductionInfo &Info, 3742*0b57cec5SDimitry Andric SmallVectorImpl<DeducedTemplateArgument> &Deduced, 3743*0b57cec5SDimitry Andric SmallVectorImpl<Sema::OriginalCallArg> &OriginalCallArgs, 3744*0b57cec5SDimitry Andric bool DecomposedParam, unsigned ArgIdx, unsigned TDF) { 3745*0b57cec5SDimitry Andric QualType ArgType = Arg->getType(); 3746*0b57cec5SDimitry Andric QualType OrigParamType = ParamType; 3747*0b57cec5SDimitry Andric 3748*0b57cec5SDimitry Andric // If P is a reference type [...] 3749*0b57cec5SDimitry Andric // If P is a cv-qualified type [...] 3750*0b57cec5SDimitry Andric if (AdjustFunctionParmAndArgTypesForDeduction( 3751*0b57cec5SDimitry Andric S, TemplateParams, FirstInnerIndex, ParamType, ArgType, Arg, TDF)) 3752*0b57cec5SDimitry Andric return Sema::TDK_Success; 3753*0b57cec5SDimitry Andric 3754*0b57cec5SDimitry Andric // If [...] the argument is a non-empty initializer list [...] 3755*0b57cec5SDimitry Andric if (InitListExpr *ILE = dyn_cast<InitListExpr>(Arg)) 3756*0b57cec5SDimitry Andric return DeduceFromInitializerList(S, TemplateParams, ParamType, ILE, Info, 3757*0b57cec5SDimitry Andric Deduced, OriginalCallArgs, ArgIdx, TDF); 3758*0b57cec5SDimitry Andric 3759*0b57cec5SDimitry Andric // [...] the deduction process attempts to find template argument values 3760*0b57cec5SDimitry Andric // that will make the deduced A identical to A 3761*0b57cec5SDimitry Andric // 3762*0b57cec5SDimitry Andric // Keep track of the argument type and corresponding parameter index, 3763*0b57cec5SDimitry Andric // so we can check for compatibility between the deduced A and A. 3764*0b57cec5SDimitry Andric OriginalCallArgs.push_back( 3765*0b57cec5SDimitry Andric Sema::OriginalCallArg(OrigParamType, DecomposedParam, ArgIdx, ArgType)); 3766*0b57cec5SDimitry Andric return DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, ParamType, 3767*0b57cec5SDimitry Andric ArgType, Info, Deduced, TDF); 3768*0b57cec5SDimitry Andric } 3769*0b57cec5SDimitry Andric 3770*0b57cec5SDimitry Andric /// Perform template argument deduction from a function call 3771*0b57cec5SDimitry Andric /// (C++ [temp.deduct.call]). 3772*0b57cec5SDimitry Andric /// 3773*0b57cec5SDimitry Andric /// \param FunctionTemplate the function template for which we are performing 3774*0b57cec5SDimitry Andric /// template argument deduction. 3775*0b57cec5SDimitry Andric /// 3776*0b57cec5SDimitry Andric /// \param ExplicitTemplateArgs the explicit template arguments provided 3777*0b57cec5SDimitry Andric /// for this call. 3778*0b57cec5SDimitry Andric /// 3779*0b57cec5SDimitry Andric /// \param Args the function call arguments 3780*0b57cec5SDimitry Andric /// 3781*0b57cec5SDimitry Andric /// \param Specialization if template argument deduction was successful, 3782*0b57cec5SDimitry Andric /// this will be set to the function template specialization produced by 3783*0b57cec5SDimitry Andric /// template argument deduction. 3784*0b57cec5SDimitry Andric /// 3785*0b57cec5SDimitry Andric /// \param Info the argument will be updated to provide additional information 3786*0b57cec5SDimitry Andric /// about template argument deduction. 3787*0b57cec5SDimitry Andric /// 3788*0b57cec5SDimitry Andric /// \param CheckNonDependent A callback to invoke to check conversions for 3789*0b57cec5SDimitry Andric /// non-dependent parameters, between deduction and substitution, per DR1391. 3790*0b57cec5SDimitry Andric /// If this returns true, substitution will be skipped and we return 3791*0b57cec5SDimitry Andric /// TDK_NonDependentConversionFailure. The callback is passed the parameter 3792*0b57cec5SDimitry Andric /// types (after substituting explicit template arguments). 3793*0b57cec5SDimitry Andric /// 3794*0b57cec5SDimitry Andric /// \returns the result of template argument deduction. 3795*0b57cec5SDimitry Andric Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( 3796*0b57cec5SDimitry Andric FunctionTemplateDecl *FunctionTemplate, 3797*0b57cec5SDimitry Andric TemplateArgumentListInfo *ExplicitTemplateArgs, ArrayRef<Expr *> Args, 3798*0b57cec5SDimitry Andric FunctionDecl *&Specialization, TemplateDeductionInfo &Info, 3799*0b57cec5SDimitry Andric bool PartialOverloading, 3800*0b57cec5SDimitry Andric llvm::function_ref<bool(ArrayRef<QualType>)> CheckNonDependent) { 3801*0b57cec5SDimitry Andric if (FunctionTemplate->isInvalidDecl()) 3802*0b57cec5SDimitry Andric return TDK_Invalid; 3803*0b57cec5SDimitry Andric 3804*0b57cec5SDimitry Andric FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 3805*0b57cec5SDimitry Andric unsigned NumParams = Function->getNumParams(); 3806*0b57cec5SDimitry Andric 3807*0b57cec5SDimitry Andric unsigned FirstInnerIndex = getFirstInnerIndex(FunctionTemplate); 3808*0b57cec5SDimitry Andric 3809*0b57cec5SDimitry Andric // C++ [temp.deduct.call]p1: 3810*0b57cec5SDimitry Andric // Template argument deduction is done by comparing each function template 3811*0b57cec5SDimitry Andric // parameter type (call it P) with the type of the corresponding argument 3812*0b57cec5SDimitry Andric // of the call (call it A) as described below. 3813*0b57cec5SDimitry Andric if (Args.size() < Function->getMinRequiredArguments() && !PartialOverloading) 3814*0b57cec5SDimitry Andric return TDK_TooFewArguments; 3815*0b57cec5SDimitry Andric else if (TooManyArguments(NumParams, Args.size(), PartialOverloading)) { 3816*0b57cec5SDimitry Andric const FunctionProtoType *Proto 3817*0b57cec5SDimitry Andric = Function->getType()->getAs<FunctionProtoType>(); 3818*0b57cec5SDimitry Andric if (Proto->isTemplateVariadic()) 3819*0b57cec5SDimitry Andric /* Do nothing */; 3820*0b57cec5SDimitry Andric else if (!Proto->isVariadic()) 3821*0b57cec5SDimitry Andric return TDK_TooManyArguments; 3822*0b57cec5SDimitry Andric } 3823*0b57cec5SDimitry Andric 3824*0b57cec5SDimitry Andric // The types of the parameters from which we will perform template argument 3825*0b57cec5SDimitry Andric // deduction. 3826*0b57cec5SDimitry Andric LocalInstantiationScope InstScope(*this); 3827*0b57cec5SDimitry Andric TemplateParameterList *TemplateParams 3828*0b57cec5SDimitry Andric = FunctionTemplate->getTemplateParameters(); 3829*0b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 4> Deduced; 3830*0b57cec5SDimitry Andric SmallVector<QualType, 8> ParamTypes; 3831*0b57cec5SDimitry Andric unsigned NumExplicitlySpecified = 0; 3832*0b57cec5SDimitry Andric if (ExplicitTemplateArgs) { 3833*0b57cec5SDimitry Andric TemplateDeductionResult Result = 3834*0b57cec5SDimitry Andric SubstituteExplicitTemplateArguments(FunctionTemplate, 3835*0b57cec5SDimitry Andric *ExplicitTemplateArgs, 3836*0b57cec5SDimitry Andric Deduced, 3837*0b57cec5SDimitry Andric ParamTypes, 3838*0b57cec5SDimitry Andric nullptr, 3839*0b57cec5SDimitry Andric Info); 3840*0b57cec5SDimitry Andric if (Result) 3841*0b57cec5SDimitry Andric return Result; 3842*0b57cec5SDimitry Andric 3843*0b57cec5SDimitry Andric NumExplicitlySpecified = Deduced.size(); 3844*0b57cec5SDimitry Andric } else { 3845*0b57cec5SDimitry Andric // Just fill in the parameter types from the function declaration. 3846*0b57cec5SDimitry Andric for (unsigned I = 0; I != NumParams; ++I) 3847*0b57cec5SDimitry Andric ParamTypes.push_back(Function->getParamDecl(I)->getType()); 3848*0b57cec5SDimitry Andric } 3849*0b57cec5SDimitry Andric 3850*0b57cec5SDimitry Andric SmallVector<OriginalCallArg, 8> OriginalCallArgs; 3851*0b57cec5SDimitry Andric 3852*0b57cec5SDimitry Andric // Deduce an argument of type ParamType from an expression with index ArgIdx. 3853*0b57cec5SDimitry Andric auto DeduceCallArgument = [&](QualType ParamType, unsigned ArgIdx) { 3854*0b57cec5SDimitry Andric // C++ [demp.deduct.call]p1: (DR1391) 3855*0b57cec5SDimitry Andric // Template argument deduction is done by comparing each function template 3856*0b57cec5SDimitry Andric // parameter that contains template-parameters that participate in 3857*0b57cec5SDimitry Andric // template argument deduction ... 3858*0b57cec5SDimitry Andric if (!hasDeducibleTemplateParameters(*this, FunctionTemplate, ParamType)) 3859*0b57cec5SDimitry Andric return Sema::TDK_Success; 3860*0b57cec5SDimitry Andric 3861*0b57cec5SDimitry Andric // ... with the type of the corresponding argument 3862*0b57cec5SDimitry Andric return DeduceTemplateArgumentsFromCallArgument( 3863*0b57cec5SDimitry Andric *this, TemplateParams, FirstInnerIndex, ParamType, Args[ArgIdx], Info, Deduced, 3864*0b57cec5SDimitry Andric OriginalCallArgs, /*Decomposed*/false, ArgIdx, /*TDF*/ 0); 3865*0b57cec5SDimitry Andric }; 3866*0b57cec5SDimitry Andric 3867*0b57cec5SDimitry Andric // Deduce template arguments from the function parameters. 3868*0b57cec5SDimitry Andric Deduced.resize(TemplateParams->size()); 3869*0b57cec5SDimitry Andric SmallVector<QualType, 8> ParamTypesForArgChecking; 3870*0b57cec5SDimitry Andric for (unsigned ParamIdx = 0, NumParamTypes = ParamTypes.size(), ArgIdx = 0; 3871*0b57cec5SDimitry Andric ParamIdx != NumParamTypes; ++ParamIdx) { 3872*0b57cec5SDimitry Andric QualType ParamType = ParamTypes[ParamIdx]; 3873*0b57cec5SDimitry Andric 3874*0b57cec5SDimitry Andric const PackExpansionType *ParamExpansion = 3875*0b57cec5SDimitry Andric dyn_cast<PackExpansionType>(ParamType); 3876*0b57cec5SDimitry Andric if (!ParamExpansion) { 3877*0b57cec5SDimitry Andric // Simple case: matching a function parameter to a function argument. 3878*0b57cec5SDimitry Andric if (ArgIdx >= Args.size()) 3879*0b57cec5SDimitry Andric break; 3880*0b57cec5SDimitry Andric 3881*0b57cec5SDimitry Andric ParamTypesForArgChecking.push_back(ParamType); 3882*0b57cec5SDimitry Andric if (auto Result = DeduceCallArgument(ParamType, ArgIdx++)) 3883*0b57cec5SDimitry Andric return Result; 3884*0b57cec5SDimitry Andric 3885*0b57cec5SDimitry Andric continue; 3886*0b57cec5SDimitry Andric } 3887*0b57cec5SDimitry Andric 3888*0b57cec5SDimitry Andric QualType ParamPattern = ParamExpansion->getPattern(); 3889*0b57cec5SDimitry Andric PackDeductionScope PackScope(*this, TemplateParams, Deduced, Info, 3890*0b57cec5SDimitry Andric ParamPattern); 3891*0b57cec5SDimitry Andric 3892*0b57cec5SDimitry Andric // C++0x [temp.deduct.call]p1: 3893*0b57cec5SDimitry Andric // For a function parameter pack that occurs at the end of the 3894*0b57cec5SDimitry Andric // parameter-declaration-list, the type A of each remaining argument of 3895*0b57cec5SDimitry Andric // the call is compared with the type P of the declarator-id of the 3896*0b57cec5SDimitry Andric // function parameter pack. Each comparison deduces template arguments 3897*0b57cec5SDimitry Andric // for subsequent positions in the template parameter packs expanded by 3898*0b57cec5SDimitry Andric // the function parameter pack. When a function parameter pack appears 3899*0b57cec5SDimitry Andric // in a non-deduced context [not at the end of the list], the type of 3900*0b57cec5SDimitry Andric // that parameter pack is never deduced. 3901*0b57cec5SDimitry Andric // 3902*0b57cec5SDimitry Andric // FIXME: The above rule allows the size of the parameter pack to change 3903*0b57cec5SDimitry Andric // after we skip it (in the non-deduced case). That makes no sense, so 3904*0b57cec5SDimitry Andric // we instead notionally deduce the pack against N arguments, where N is 3905*0b57cec5SDimitry Andric // the length of the explicitly-specified pack if it's expanded by the 3906*0b57cec5SDimitry Andric // parameter pack and 0 otherwise, and we treat each deduction as a 3907*0b57cec5SDimitry Andric // non-deduced context. 3908*0b57cec5SDimitry Andric if (ParamIdx + 1 == NumParamTypes || PackScope.hasFixedArity()) { 3909*0b57cec5SDimitry Andric for (; ArgIdx < Args.size() && PackScope.hasNextElement(); 3910*0b57cec5SDimitry Andric PackScope.nextPackElement(), ++ArgIdx) { 3911*0b57cec5SDimitry Andric ParamTypesForArgChecking.push_back(ParamPattern); 3912*0b57cec5SDimitry Andric if (auto Result = DeduceCallArgument(ParamPattern, ArgIdx)) 3913*0b57cec5SDimitry Andric return Result; 3914*0b57cec5SDimitry Andric } 3915*0b57cec5SDimitry Andric } else { 3916*0b57cec5SDimitry Andric // If the parameter type contains an explicitly-specified pack that we 3917*0b57cec5SDimitry Andric // could not expand, skip the number of parameters notionally created 3918*0b57cec5SDimitry Andric // by the expansion. 3919*0b57cec5SDimitry Andric Optional<unsigned> NumExpansions = ParamExpansion->getNumExpansions(); 3920*0b57cec5SDimitry Andric if (NumExpansions && !PackScope.isPartiallyExpanded()) { 3921*0b57cec5SDimitry Andric for (unsigned I = 0; I != *NumExpansions && ArgIdx < Args.size(); 3922*0b57cec5SDimitry Andric ++I, ++ArgIdx) { 3923*0b57cec5SDimitry Andric ParamTypesForArgChecking.push_back(ParamPattern); 3924*0b57cec5SDimitry Andric // FIXME: Should we add OriginalCallArgs for these? What if the 3925*0b57cec5SDimitry Andric // corresponding argument is a list? 3926*0b57cec5SDimitry Andric PackScope.nextPackElement(); 3927*0b57cec5SDimitry Andric } 3928*0b57cec5SDimitry Andric } 3929*0b57cec5SDimitry Andric } 3930*0b57cec5SDimitry Andric 3931*0b57cec5SDimitry Andric // Build argument packs for each of the parameter packs expanded by this 3932*0b57cec5SDimitry Andric // pack expansion. 3933*0b57cec5SDimitry Andric if (auto Result = PackScope.finish()) 3934*0b57cec5SDimitry Andric return Result; 3935*0b57cec5SDimitry Andric } 3936*0b57cec5SDimitry Andric 3937*0b57cec5SDimitry Andric // Capture the context in which the function call is made. This is the context 3938*0b57cec5SDimitry Andric // that is needed when the accessibility of template arguments is checked. 3939*0b57cec5SDimitry Andric DeclContext *CallingCtx = CurContext; 3940*0b57cec5SDimitry Andric 3941*0b57cec5SDimitry Andric return FinishTemplateArgumentDeduction( 3942*0b57cec5SDimitry Andric FunctionTemplate, Deduced, NumExplicitlySpecified, Specialization, Info, 3943*0b57cec5SDimitry Andric &OriginalCallArgs, PartialOverloading, [&, CallingCtx]() { 3944*0b57cec5SDimitry Andric ContextRAII SavedContext(*this, CallingCtx); 3945*0b57cec5SDimitry Andric return CheckNonDependent(ParamTypesForArgChecking); 3946*0b57cec5SDimitry Andric }); 3947*0b57cec5SDimitry Andric } 3948*0b57cec5SDimitry Andric 3949*0b57cec5SDimitry Andric QualType Sema::adjustCCAndNoReturn(QualType ArgFunctionType, 3950*0b57cec5SDimitry Andric QualType FunctionType, 3951*0b57cec5SDimitry Andric bool AdjustExceptionSpec) { 3952*0b57cec5SDimitry Andric if (ArgFunctionType.isNull()) 3953*0b57cec5SDimitry Andric return ArgFunctionType; 3954*0b57cec5SDimitry Andric 3955*0b57cec5SDimitry Andric const FunctionProtoType *FunctionTypeP = 3956*0b57cec5SDimitry Andric FunctionType->castAs<FunctionProtoType>(); 3957*0b57cec5SDimitry Andric const FunctionProtoType *ArgFunctionTypeP = 3958*0b57cec5SDimitry Andric ArgFunctionType->getAs<FunctionProtoType>(); 3959*0b57cec5SDimitry Andric 3960*0b57cec5SDimitry Andric FunctionProtoType::ExtProtoInfo EPI = ArgFunctionTypeP->getExtProtoInfo(); 3961*0b57cec5SDimitry Andric bool Rebuild = false; 3962*0b57cec5SDimitry Andric 3963*0b57cec5SDimitry Andric CallingConv CC = FunctionTypeP->getCallConv(); 3964*0b57cec5SDimitry Andric if (EPI.ExtInfo.getCC() != CC) { 3965*0b57cec5SDimitry Andric EPI.ExtInfo = EPI.ExtInfo.withCallingConv(CC); 3966*0b57cec5SDimitry Andric Rebuild = true; 3967*0b57cec5SDimitry Andric } 3968*0b57cec5SDimitry Andric 3969*0b57cec5SDimitry Andric bool NoReturn = FunctionTypeP->getNoReturnAttr(); 3970*0b57cec5SDimitry Andric if (EPI.ExtInfo.getNoReturn() != NoReturn) { 3971*0b57cec5SDimitry Andric EPI.ExtInfo = EPI.ExtInfo.withNoReturn(NoReturn); 3972*0b57cec5SDimitry Andric Rebuild = true; 3973*0b57cec5SDimitry Andric } 3974*0b57cec5SDimitry Andric 3975*0b57cec5SDimitry Andric if (AdjustExceptionSpec && (FunctionTypeP->hasExceptionSpec() || 3976*0b57cec5SDimitry Andric ArgFunctionTypeP->hasExceptionSpec())) { 3977*0b57cec5SDimitry Andric EPI.ExceptionSpec = FunctionTypeP->getExtProtoInfo().ExceptionSpec; 3978*0b57cec5SDimitry Andric Rebuild = true; 3979*0b57cec5SDimitry Andric } 3980*0b57cec5SDimitry Andric 3981*0b57cec5SDimitry Andric if (!Rebuild) 3982*0b57cec5SDimitry Andric return ArgFunctionType; 3983*0b57cec5SDimitry Andric 3984*0b57cec5SDimitry Andric return Context.getFunctionType(ArgFunctionTypeP->getReturnType(), 3985*0b57cec5SDimitry Andric ArgFunctionTypeP->getParamTypes(), EPI); 3986*0b57cec5SDimitry Andric } 3987*0b57cec5SDimitry Andric 3988*0b57cec5SDimitry Andric /// Deduce template arguments when taking the address of a function 3989*0b57cec5SDimitry Andric /// template (C++ [temp.deduct.funcaddr]) or matching a specialization to 3990*0b57cec5SDimitry Andric /// a template. 3991*0b57cec5SDimitry Andric /// 3992*0b57cec5SDimitry Andric /// \param FunctionTemplate the function template for which we are performing 3993*0b57cec5SDimitry Andric /// template argument deduction. 3994*0b57cec5SDimitry Andric /// 3995*0b57cec5SDimitry Andric /// \param ExplicitTemplateArgs the explicitly-specified template 3996*0b57cec5SDimitry Andric /// arguments. 3997*0b57cec5SDimitry Andric /// 3998*0b57cec5SDimitry Andric /// \param ArgFunctionType the function type that will be used as the 3999*0b57cec5SDimitry Andric /// "argument" type (A) when performing template argument deduction from the 4000*0b57cec5SDimitry Andric /// function template's function type. This type may be NULL, if there is no 4001*0b57cec5SDimitry Andric /// argument type to compare against, in C++0x [temp.arg.explicit]p3. 4002*0b57cec5SDimitry Andric /// 4003*0b57cec5SDimitry Andric /// \param Specialization if template argument deduction was successful, 4004*0b57cec5SDimitry Andric /// this will be set to the function template specialization produced by 4005*0b57cec5SDimitry Andric /// template argument deduction. 4006*0b57cec5SDimitry Andric /// 4007*0b57cec5SDimitry Andric /// \param Info the argument will be updated to provide additional information 4008*0b57cec5SDimitry Andric /// about template argument deduction. 4009*0b57cec5SDimitry Andric /// 4010*0b57cec5SDimitry Andric /// \param IsAddressOfFunction If \c true, we are deducing as part of taking 4011*0b57cec5SDimitry Andric /// the address of a function template per [temp.deduct.funcaddr] and 4012*0b57cec5SDimitry Andric /// [over.over]. If \c false, we are looking up a function template 4013*0b57cec5SDimitry Andric /// specialization based on its signature, per [temp.deduct.decl]. 4014*0b57cec5SDimitry Andric /// 4015*0b57cec5SDimitry Andric /// \returns the result of template argument deduction. 4016*0b57cec5SDimitry Andric Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( 4017*0b57cec5SDimitry Andric FunctionTemplateDecl *FunctionTemplate, 4018*0b57cec5SDimitry Andric TemplateArgumentListInfo *ExplicitTemplateArgs, QualType ArgFunctionType, 4019*0b57cec5SDimitry Andric FunctionDecl *&Specialization, TemplateDeductionInfo &Info, 4020*0b57cec5SDimitry Andric bool IsAddressOfFunction) { 4021*0b57cec5SDimitry Andric if (FunctionTemplate->isInvalidDecl()) 4022*0b57cec5SDimitry Andric return TDK_Invalid; 4023*0b57cec5SDimitry Andric 4024*0b57cec5SDimitry Andric FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 4025*0b57cec5SDimitry Andric TemplateParameterList *TemplateParams 4026*0b57cec5SDimitry Andric = FunctionTemplate->getTemplateParameters(); 4027*0b57cec5SDimitry Andric QualType FunctionType = Function->getType(); 4028*0b57cec5SDimitry Andric 4029*0b57cec5SDimitry Andric // Substitute any explicit template arguments. 4030*0b57cec5SDimitry Andric LocalInstantiationScope InstScope(*this); 4031*0b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 4> Deduced; 4032*0b57cec5SDimitry Andric unsigned NumExplicitlySpecified = 0; 4033*0b57cec5SDimitry Andric SmallVector<QualType, 4> ParamTypes; 4034*0b57cec5SDimitry Andric if (ExplicitTemplateArgs) { 4035*0b57cec5SDimitry Andric if (TemplateDeductionResult Result 4036*0b57cec5SDimitry Andric = SubstituteExplicitTemplateArguments(FunctionTemplate, 4037*0b57cec5SDimitry Andric *ExplicitTemplateArgs, 4038*0b57cec5SDimitry Andric Deduced, ParamTypes, 4039*0b57cec5SDimitry Andric &FunctionType, Info)) 4040*0b57cec5SDimitry Andric return Result; 4041*0b57cec5SDimitry Andric 4042*0b57cec5SDimitry Andric NumExplicitlySpecified = Deduced.size(); 4043*0b57cec5SDimitry Andric } 4044*0b57cec5SDimitry Andric 4045*0b57cec5SDimitry Andric // When taking the address of a function, we require convertibility of 4046*0b57cec5SDimitry Andric // the resulting function type. Otherwise, we allow arbitrary mismatches 4047*0b57cec5SDimitry Andric // of calling convention and noreturn. 4048*0b57cec5SDimitry Andric if (!IsAddressOfFunction) 4049*0b57cec5SDimitry Andric ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, FunctionType, 4050*0b57cec5SDimitry Andric /*AdjustExceptionSpec*/false); 4051*0b57cec5SDimitry Andric 4052*0b57cec5SDimitry Andric // Unevaluated SFINAE context. 4053*0b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 4054*0b57cec5SDimitry Andric *this, Sema::ExpressionEvaluationContext::Unevaluated); 4055*0b57cec5SDimitry Andric SFINAETrap Trap(*this); 4056*0b57cec5SDimitry Andric 4057*0b57cec5SDimitry Andric Deduced.resize(TemplateParams->size()); 4058*0b57cec5SDimitry Andric 4059*0b57cec5SDimitry Andric // If the function has a deduced return type, substitute it for a dependent 4060*0b57cec5SDimitry Andric // type so that we treat it as a non-deduced context in what follows. If we 4061*0b57cec5SDimitry Andric // are looking up by signature, the signature type should also have a deduced 4062*0b57cec5SDimitry Andric // return type, which we instead expect to exactly match. 4063*0b57cec5SDimitry Andric bool HasDeducedReturnType = false; 4064*0b57cec5SDimitry Andric if (getLangOpts().CPlusPlus14 && IsAddressOfFunction && 4065*0b57cec5SDimitry Andric Function->getReturnType()->getContainedAutoType()) { 4066*0b57cec5SDimitry Andric FunctionType = SubstAutoType(FunctionType, Context.DependentTy); 4067*0b57cec5SDimitry Andric HasDeducedReturnType = true; 4068*0b57cec5SDimitry Andric } 4069*0b57cec5SDimitry Andric 4070*0b57cec5SDimitry Andric if (!ArgFunctionType.isNull()) { 4071*0b57cec5SDimitry Andric unsigned TDF = 4072*0b57cec5SDimitry Andric TDF_TopLevelParameterTypeList | TDF_AllowCompatibleFunctionType; 4073*0b57cec5SDimitry Andric // Deduce template arguments from the function type. 4074*0b57cec5SDimitry Andric if (TemplateDeductionResult Result 4075*0b57cec5SDimitry Andric = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, 4076*0b57cec5SDimitry Andric FunctionType, ArgFunctionType, 4077*0b57cec5SDimitry Andric Info, Deduced, TDF)) 4078*0b57cec5SDimitry Andric return Result; 4079*0b57cec5SDimitry Andric } 4080*0b57cec5SDimitry Andric 4081*0b57cec5SDimitry Andric if (TemplateDeductionResult Result 4082*0b57cec5SDimitry Andric = FinishTemplateArgumentDeduction(FunctionTemplate, Deduced, 4083*0b57cec5SDimitry Andric NumExplicitlySpecified, 4084*0b57cec5SDimitry Andric Specialization, Info)) 4085*0b57cec5SDimitry Andric return Result; 4086*0b57cec5SDimitry Andric 4087*0b57cec5SDimitry Andric // If the function has a deduced return type, deduce it now, so we can check 4088*0b57cec5SDimitry Andric // that the deduced function type matches the requested type. 4089*0b57cec5SDimitry Andric if (HasDeducedReturnType && 4090*0b57cec5SDimitry Andric Specialization->getReturnType()->isUndeducedType() && 4091*0b57cec5SDimitry Andric DeduceReturnType(Specialization, Info.getLocation(), false)) 4092*0b57cec5SDimitry Andric return TDK_MiscellaneousDeductionFailure; 4093*0b57cec5SDimitry Andric 4094*0b57cec5SDimitry Andric // If the function has a dependent exception specification, resolve it now, 4095*0b57cec5SDimitry Andric // so we can check that the exception specification matches. 4096*0b57cec5SDimitry Andric auto *SpecializationFPT = 4097*0b57cec5SDimitry Andric Specialization->getType()->castAs<FunctionProtoType>(); 4098*0b57cec5SDimitry Andric if (getLangOpts().CPlusPlus17 && 4099*0b57cec5SDimitry Andric isUnresolvedExceptionSpec(SpecializationFPT->getExceptionSpecType()) && 4100*0b57cec5SDimitry Andric !ResolveExceptionSpec(Info.getLocation(), SpecializationFPT)) 4101*0b57cec5SDimitry Andric return TDK_MiscellaneousDeductionFailure; 4102*0b57cec5SDimitry Andric 4103*0b57cec5SDimitry Andric // Adjust the exception specification of the argument to match the 4104*0b57cec5SDimitry Andric // substituted and resolved type we just formed. (Calling convention and 4105*0b57cec5SDimitry Andric // noreturn can't be dependent, so we don't actually need this for them 4106*0b57cec5SDimitry Andric // right now.) 4107*0b57cec5SDimitry Andric QualType SpecializationType = Specialization->getType(); 4108*0b57cec5SDimitry Andric if (!IsAddressOfFunction) 4109*0b57cec5SDimitry Andric ArgFunctionType = adjustCCAndNoReturn(ArgFunctionType, SpecializationType, 4110*0b57cec5SDimitry Andric /*AdjustExceptionSpec*/true); 4111*0b57cec5SDimitry Andric 4112*0b57cec5SDimitry Andric // If the requested function type does not match the actual type of the 4113*0b57cec5SDimitry Andric // specialization with respect to arguments of compatible pointer to function 4114*0b57cec5SDimitry Andric // types, template argument deduction fails. 4115*0b57cec5SDimitry Andric if (!ArgFunctionType.isNull()) { 4116*0b57cec5SDimitry Andric if (IsAddressOfFunction && 4117*0b57cec5SDimitry Andric !isSameOrCompatibleFunctionType( 4118*0b57cec5SDimitry Andric Context.getCanonicalType(SpecializationType), 4119*0b57cec5SDimitry Andric Context.getCanonicalType(ArgFunctionType))) 4120*0b57cec5SDimitry Andric return TDK_MiscellaneousDeductionFailure; 4121*0b57cec5SDimitry Andric 4122*0b57cec5SDimitry Andric if (!IsAddressOfFunction && 4123*0b57cec5SDimitry Andric !Context.hasSameType(SpecializationType, ArgFunctionType)) 4124*0b57cec5SDimitry Andric return TDK_MiscellaneousDeductionFailure; 4125*0b57cec5SDimitry Andric } 4126*0b57cec5SDimitry Andric 4127*0b57cec5SDimitry Andric return TDK_Success; 4128*0b57cec5SDimitry Andric } 4129*0b57cec5SDimitry Andric 4130*0b57cec5SDimitry Andric /// Deduce template arguments for a templated conversion 4131*0b57cec5SDimitry Andric /// function (C++ [temp.deduct.conv]) and, if successful, produce a 4132*0b57cec5SDimitry Andric /// conversion function template specialization. 4133*0b57cec5SDimitry Andric Sema::TemplateDeductionResult 4134*0b57cec5SDimitry Andric Sema::DeduceTemplateArguments(FunctionTemplateDecl *ConversionTemplate, 4135*0b57cec5SDimitry Andric QualType ToType, 4136*0b57cec5SDimitry Andric CXXConversionDecl *&Specialization, 4137*0b57cec5SDimitry Andric TemplateDeductionInfo &Info) { 4138*0b57cec5SDimitry Andric if (ConversionTemplate->isInvalidDecl()) 4139*0b57cec5SDimitry Andric return TDK_Invalid; 4140*0b57cec5SDimitry Andric 4141*0b57cec5SDimitry Andric CXXConversionDecl *ConversionGeneric 4142*0b57cec5SDimitry Andric = cast<CXXConversionDecl>(ConversionTemplate->getTemplatedDecl()); 4143*0b57cec5SDimitry Andric 4144*0b57cec5SDimitry Andric QualType FromType = ConversionGeneric->getConversionType(); 4145*0b57cec5SDimitry Andric 4146*0b57cec5SDimitry Andric // Canonicalize the types for deduction. 4147*0b57cec5SDimitry Andric QualType P = Context.getCanonicalType(FromType); 4148*0b57cec5SDimitry Andric QualType A = Context.getCanonicalType(ToType); 4149*0b57cec5SDimitry Andric 4150*0b57cec5SDimitry Andric // C++0x [temp.deduct.conv]p2: 4151*0b57cec5SDimitry Andric // If P is a reference type, the type referred to by P is used for 4152*0b57cec5SDimitry Andric // type deduction. 4153*0b57cec5SDimitry Andric if (const ReferenceType *PRef = P->getAs<ReferenceType>()) 4154*0b57cec5SDimitry Andric P = PRef->getPointeeType(); 4155*0b57cec5SDimitry Andric 4156*0b57cec5SDimitry Andric // C++0x [temp.deduct.conv]p4: 4157*0b57cec5SDimitry Andric // [...] If A is a reference type, the type referred to by A is used 4158*0b57cec5SDimitry Andric // for type deduction. 4159*0b57cec5SDimitry Andric if (const ReferenceType *ARef = A->getAs<ReferenceType>()) { 4160*0b57cec5SDimitry Andric A = ARef->getPointeeType(); 4161*0b57cec5SDimitry Andric // We work around a defect in the standard here: cv-qualifiers are also 4162*0b57cec5SDimitry Andric // removed from P and A in this case, unless P was a reference type. This 4163*0b57cec5SDimitry Andric // seems to mostly match what other compilers are doing. 4164*0b57cec5SDimitry Andric if (!FromType->getAs<ReferenceType>()) { 4165*0b57cec5SDimitry Andric A = A.getUnqualifiedType(); 4166*0b57cec5SDimitry Andric P = P.getUnqualifiedType(); 4167*0b57cec5SDimitry Andric } 4168*0b57cec5SDimitry Andric 4169*0b57cec5SDimitry Andric // C++ [temp.deduct.conv]p3: 4170*0b57cec5SDimitry Andric // 4171*0b57cec5SDimitry Andric // If A is not a reference type: 4172*0b57cec5SDimitry Andric } else { 4173*0b57cec5SDimitry Andric assert(!A->isReferenceType() && "Reference types were handled above"); 4174*0b57cec5SDimitry Andric 4175*0b57cec5SDimitry Andric // - If P is an array type, the pointer type produced by the 4176*0b57cec5SDimitry Andric // array-to-pointer standard conversion (4.2) is used in place 4177*0b57cec5SDimitry Andric // of P for type deduction; otherwise, 4178*0b57cec5SDimitry Andric if (P->isArrayType()) 4179*0b57cec5SDimitry Andric P = Context.getArrayDecayedType(P); 4180*0b57cec5SDimitry Andric // - If P is a function type, the pointer type produced by the 4181*0b57cec5SDimitry Andric // function-to-pointer standard conversion (4.3) is used in 4182*0b57cec5SDimitry Andric // place of P for type deduction; otherwise, 4183*0b57cec5SDimitry Andric else if (P->isFunctionType()) 4184*0b57cec5SDimitry Andric P = Context.getPointerType(P); 4185*0b57cec5SDimitry Andric // - If P is a cv-qualified type, the top level cv-qualifiers of 4186*0b57cec5SDimitry Andric // P's type are ignored for type deduction. 4187*0b57cec5SDimitry Andric else 4188*0b57cec5SDimitry Andric P = P.getUnqualifiedType(); 4189*0b57cec5SDimitry Andric 4190*0b57cec5SDimitry Andric // C++0x [temp.deduct.conv]p4: 4191*0b57cec5SDimitry Andric // If A is a cv-qualified type, the top level cv-qualifiers of A's 4192*0b57cec5SDimitry Andric // type are ignored for type deduction. If A is a reference type, the type 4193*0b57cec5SDimitry Andric // referred to by A is used for type deduction. 4194*0b57cec5SDimitry Andric A = A.getUnqualifiedType(); 4195*0b57cec5SDimitry Andric } 4196*0b57cec5SDimitry Andric 4197*0b57cec5SDimitry Andric // Unevaluated SFINAE context. 4198*0b57cec5SDimitry Andric EnterExpressionEvaluationContext Unevaluated( 4199*0b57cec5SDimitry Andric *this, Sema::ExpressionEvaluationContext::Unevaluated); 4200*0b57cec5SDimitry Andric SFINAETrap Trap(*this); 4201*0b57cec5SDimitry Andric 4202*0b57cec5SDimitry Andric // C++ [temp.deduct.conv]p1: 4203*0b57cec5SDimitry Andric // Template argument deduction is done by comparing the return 4204*0b57cec5SDimitry Andric // type of the template conversion function (call it P) with the 4205*0b57cec5SDimitry Andric // type that is required as the result of the conversion (call it 4206*0b57cec5SDimitry Andric // A) as described in 14.8.2.4. 4207*0b57cec5SDimitry Andric TemplateParameterList *TemplateParams 4208*0b57cec5SDimitry Andric = ConversionTemplate->getTemplateParameters(); 4209*0b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 4> Deduced; 4210*0b57cec5SDimitry Andric Deduced.resize(TemplateParams->size()); 4211*0b57cec5SDimitry Andric 4212*0b57cec5SDimitry Andric // C++0x [temp.deduct.conv]p4: 4213*0b57cec5SDimitry Andric // In general, the deduction process attempts to find template 4214*0b57cec5SDimitry Andric // argument values that will make the deduced A identical to 4215*0b57cec5SDimitry Andric // A. However, there are two cases that allow a difference: 4216*0b57cec5SDimitry Andric unsigned TDF = 0; 4217*0b57cec5SDimitry Andric // - If the original A is a reference type, A can be more 4218*0b57cec5SDimitry Andric // cv-qualified than the deduced A (i.e., the type referred to 4219*0b57cec5SDimitry Andric // by the reference) 4220*0b57cec5SDimitry Andric if (ToType->isReferenceType()) 4221*0b57cec5SDimitry Andric TDF |= TDF_ArgWithReferenceType; 4222*0b57cec5SDimitry Andric // - The deduced A can be another pointer or pointer to member 4223*0b57cec5SDimitry Andric // type that can be converted to A via a qualification 4224*0b57cec5SDimitry Andric // conversion. 4225*0b57cec5SDimitry Andric // 4226*0b57cec5SDimitry Andric // (C++0x [temp.deduct.conv]p6 clarifies that this only happens when 4227*0b57cec5SDimitry Andric // both P and A are pointers or member pointers. In this case, we 4228*0b57cec5SDimitry Andric // just ignore cv-qualifiers completely). 4229*0b57cec5SDimitry Andric if ((P->isPointerType() && A->isPointerType()) || 4230*0b57cec5SDimitry Andric (P->isMemberPointerType() && A->isMemberPointerType())) 4231*0b57cec5SDimitry Andric TDF |= TDF_IgnoreQualifiers; 4232*0b57cec5SDimitry Andric if (TemplateDeductionResult Result 4233*0b57cec5SDimitry Andric = DeduceTemplateArgumentsByTypeMatch(*this, TemplateParams, 4234*0b57cec5SDimitry Andric P, A, Info, Deduced, TDF)) 4235*0b57cec5SDimitry Andric return Result; 4236*0b57cec5SDimitry Andric 4237*0b57cec5SDimitry Andric // Create an Instantiation Scope for finalizing the operator. 4238*0b57cec5SDimitry Andric LocalInstantiationScope InstScope(*this); 4239*0b57cec5SDimitry Andric // Finish template argument deduction. 4240*0b57cec5SDimitry Andric FunctionDecl *ConversionSpecialized = nullptr; 4241*0b57cec5SDimitry Andric TemplateDeductionResult Result 4242*0b57cec5SDimitry Andric = FinishTemplateArgumentDeduction(ConversionTemplate, Deduced, 0, 4243*0b57cec5SDimitry Andric ConversionSpecialized, Info); 4244*0b57cec5SDimitry Andric Specialization = cast_or_null<CXXConversionDecl>(ConversionSpecialized); 4245*0b57cec5SDimitry Andric return Result; 4246*0b57cec5SDimitry Andric } 4247*0b57cec5SDimitry Andric 4248*0b57cec5SDimitry Andric /// Deduce template arguments for a function template when there is 4249*0b57cec5SDimitry Andric /// nothing to deduce against (C++0x [temp.arg.explicit]p3). 4250*0b57cec5SDimitry Andric /// 4251*0b57cec5SDimitry Andric /// \param FunctionTemplate the function template for which we are performing 4252*0b57cec5SDimitry Andric /// template argument deduction. 4253*0b57cec5SDimitry Andric /// 4254*0b57cec5SDimitry Andric /// \param ExplicitTemplateArgs the explicitly-specified template 4255*0b57cec5SDimitry Andric /// arguments. 4256*0b57cec5SDimitry Andric /// 4257*0b57cec5SDimitry Andric /// \param Specialization if template argument deduction was successful, 4258*0b57cec5SDimitry Andric /// this will be set to the function template specialization produced by 4259*0b57cec5SDimitry Andric /// template argument deduction. 4260*0b57cec5SDimitry Andric /// 4261*0b57cec5SDimitry Andric /// \param Info the argument will be updated to provide additional information 4262*0b57cec5SDimitry Andric /// about template argument deduction. 4263*0b57cec5SDimitry Andric /// 4264*0b57cec5SDimitry Andric /// \param IsAddressOfFunction If \c true, we are deducing as part of taking 4265*0b57cec5SDimitry Andric /// the address of a function template in a context where we do not have a 4266*0b57cec5SDimitry Andric /// target type, per [over.over]. If \c false, we are looking up a function 4267*0b57cec5SDimitry Andric /// template specialization based on its signature, which only happens when 4268*0b57cec5SDimitry Andric /// deducing a function parameter type from an argument that is a template-id 4269*0b57cec5SDimitry Andric /// naming a function template specialization. 4270*0b57cec5SDimitry Andric /// 4271*0b57cec5SDimitry Andric /// \returns the result of template argument deduction. 4272*0b57cec5SDimitry Andric Sema::TemplateDeductionResult Sema::DeduceTemplateArguments( 4273*0b57cec5SDimitry Andric FunctionTemplateDecl *FunctionTemplate, 4274*0b57cec5SDimitry Andric TemplateArgumentListInfo *ExplicitTemplateArgs, 4275*0b57cec5SDimitry Andric FunctionDecl *&Specialization, TemplateDeductionInfo &Info, 4276*0b57cec5SDimitry Andric bool IsAddressOfFunction) { 4277*0b57cec5SDimitry Andric return DeduceTemplateArguments(FunctionTemplate, ExplicitTemplateArgs, 4278*0b57cec5SDimitry Andric QualType(), Specialization, Info, 4279*0b57cec5SDimitry Andric IsAddressOfFunction); 4280*0b57cec5SDimitry Andric } 4281*0b57cec5SDimitry Andric 4282*0b57cec5SDimitry Andric namespace { 4283*0b57cec5SDimitry Andric struct DependentAuto { bool IsPack; }; 4284*0b57cec5SDimitry Andric 4285*0b57cec5SDimitry Andric /// Substitute the 'auto' specifier or deduced template specialization type 4286*0b57cec5SDimitry Andric /// specifier within a type for a given replacement type. 4287*0b57cec5SDimitry Andric class SubstituteDeducedTypeTransform : 4288*0b57cec5SDimitry Andric public TreeTransform<SubstituteDeducedTypeTransform> { 4289*0b57cec5SDimitry Andric QualType Replacement; 4290*0b57cec5SDimitry Andric bool ReplacementIsPack; 4291*0b57cec5SDimitry Andric bool UseTypeSugar; 4292*0b57cec5SDimitry Andric 4293*0b57cec5SDimitry Andric public: 4294*0b57cec5SDimitry Andric SubstituteDeducedTypeTransform(Sema &SemaRef, DependentAuto DA) 4295*0b57cec5SDimitry Andric : TreeTransform<SubstituteDeducedTypeTransform>(SemaRef), Replacement(), 4296*0b57cec5SDimitry Andric ReplacementIsPack(DA.IsPack), UseTypeSugar(true) {} 4297*0b57cec5SDimitry Andric 4298*0b57cec5SDimitry Andric SubstituteDeducedTypeTransform(Sema &SemaRef, QualType Replacement, 4299*0b57cec5SDimitry Andric bool UseTypeSugar = true) 4300*0b57cec5SDimitry Andric : TreeTransform<SubstituteDeducedTypeTransform>(SemaRef), 4301*0b57cec5SDimitry Andric Replacement(Replacement), ReplacementIsPack(false), 4302*0b57cec5SDimitry Andric UseTypeSugar(UseTypeSugar) {} 4303*0b57cec5SDimitry Andric 4304*0b57cec5SDimitry Andric QualType TransformDesugared(TypeLocBuilder &TLB, DeducedTypeLoc TL) { 4305*0b57cec5SDimitry Andric assert(isa<TemplateTypeParmType>(Replacement) && 4306*0b57cec5SDimitry Andric "unexpected unsugared replacement kind"); 4307*0b57cec5SDimitry Andric QualType Result = Replacement; 4308*0b57cec5SDimitry Andric TemplateTypeParmTypeLoc NewTL = TLB.push<TemplateTypeParmTypeLoc>(Result); 4309*0b57cec5SDimitry Andric NewTL.setNameLoc(TL.getNameLoc()); 4310*0b57cec5SDimitry Andric return Result; 4311*0b57cec5SDimitry Andric } 4312*0b57cec5SDimitry Andric 4313*0b57cec5SDimitry Andric QualType TransformAutoType(TypeLocBuilder &TLB, AutoTypeLoc TL) { 4314*0b57cec5SDimitry Andric // If we're building the type pattern to deduce against, don't wrap the 4315*0b57cec5SDimitry Andric // substituted type in an AutoType. Certain template deduction rules 4316*0b57cec5SDimitry Andric // apply only when a template type parameter appears directly (and not if 4317*0b57cec5SDimitry Andric // the parameter is found through desugaring). For instance: 4318*0b57cec5SDimitry Andric // auto &&lref = lvalue; 4319*0b57cec5SDimitry Andric // must transform into "rvalue reference to T" not "rvalue reference to 4320*0b57cec5SDimitry Andric // auto type deduced as T" in order for [temp.deduct.call]p3 to apply. 4321*0b57cec5SDimitry Andric // 4322*0b57cec5SDimitry Andric // FIXME: Is this still necessary? 4323*0b57cec5SDimitry Andric if (!UseTypeSugar) 4324*0b57cec5SDimitry Andric return TransformDesugared(TLB, TL); 4325*0b57cec5SDimitry Andric 4326*0b57cec5SDimitry Andric QualType Result = SemaRef.Context.getAutoType( 4327*0b57cec5SDimitry Andric Replacement, TL.getTypePtr()->getKeyword(), Replacement.isNull(), 4328*0b57cec5SDimitry Andric ReplacementIsPack); 4329*0b57cec5SDimitry Andric auto NewTL = TLB.push<AutoTypeLoc>(Result); 4330*0b57cec5SDimitry Andric NewTL.setNameLoc(TL.getNameLoc()); 4331*0b57cec5SDimitry Andric return Result; 4332*0b57cec5SDimitry Andric } 4333*0b57cec5SDimitry Andric 4334*0b57cec5SDimitry Andric QualType TransformDeducedTemplateSpecializationType( 4335*0b57cec5SDimitry Andric TypeLocBuilder &TLB, DeducedTemplateSpecializationTypeLoc TL) { 4336*0b57cec5SDimitry Andric if (!UseTypeSugar) 4337*0b57cec5SDimitry Andric return TransformDesugared(TLB, TL); 4338*0b57cec5SDimitry Andric 4339*0b57cec5SDimitry Andric QualType Result = SemaRef.Context.getDeducedTemplateSpecializationType( 4340*0b57cec5SDimitry Andric TL.getTypePtr()->getTemplateName(), 4341*0b57cec5SDimitry Andric Replacement, Replacement.isNull()); 4342*0b57cec5SDimitry Andric auto NewTL = TLB.push<DeducedTemplateSpecializationTypeLoc>(Result); 4343*0b57cec5SDimitry Andric NewTL.setNameLoc(TL.getNameLoc()); 4344*0b57cec5SDimitry Andric return Result; 4345*0b57cec5SDimitry Andric } 4346*0b57cec5SDimitry Andric 4347*0b57cec5SDimitry Andric ExprResult TransformLambdaExpr(LambdaExpr *E) { 4348*0b57cec5SDimitry Andric // Lambdas never need to be transformed. 4349*0b57cec5SDimitry Andric return E; 4350*0b57cec5SDimitry Andric } 4351*0b57cec5SDimitry Andric 4352*0b57cec5SDimitry Andric QualType Apply(TypeLoc TL) { 4353*0b57cec5SDimitry Andric // Create some scratch storage for the transformed type locations. 4354*0b57cec5SDimitry Andric // FIXME: We're just going to throw this information away. Don't build it. 4355*0b57cec5SDimitry Andric TypeLocBuilder TLB; 4356*0b57cec5SDimitry Andric TLB.reserve(TL.getFullDataSize()); 4357*0b57cec5SDimitry Andric return TransformType(TLB, TL); 4358*0b57cec5SDimitry Andric } 4359*0b57cec5SDimitry Andric }; 4360*0b57cec5SDimitry Andric 4361*0b57cec5SDimitry Andric } // namespace 4362*0b57cec5SDimitry Andric 4363*0b57cec5SDimitry Andric Sema::DeduceAutoResult 4364*0b57cec5SDimitry Andric Sema::DeduceAutoType(TypeSourceInfo *Type, Expr *&Init, QualType &Result, 4365*0b57cec5SDimitry Andric Optional<unsigned> DependentDeductionDepth) { 4366*0b57cec5SDimitry Andric return DeduceAutoType(Type->getTypeLoc(), Init, Result, 4367*0b57cec5SDimitry Andric DependentDeductionDepth); 4368*0b57cec5SDimitry Andric } 4369*0b57cec5SDimitry Andric 4370*0b57cec5SDimitry Andric /// Attempt to produce an informative diagostic explaining why auto deduction 4371*0b57cec5SDimitry Andric /// failed. 4372*0b57cec5SDimitry Andric /// \return \c true if diagnosed, \c false if not. 4373*0b57cec5SDimitry Andric static bool diagnoseAutoDeductionFailure(Sema &S, 4374*0b57cec5SDimitry Andric Sema::TemplateDeductionResult TDK, 4375*0b57cec5SDimitry Andric TemplateDeductionInfo &Info, 4376*0b57cec5SDimitry Andric ArrayRef<SourceRange> Ranges) { 4377*0b57cec5SDimitry Andric switch (TDK) { 4378*0b57cec5SDimitry Andric case Sema::TDK_Inconsistent: { 4379*0b57cec5SDimitry Andric // Inconsistent deduction means we were deducing from an initializer list. 4380*0b57cec5SDimitry Andric auto D = S.Diag(Info.getLocation(), diag::err_auto_inconsistent_deduction); 4381*0b57cec5SDimitry Andric D << Info.FirstArg << Info.SecondArg; 4382*0b57cec5SDimitry Andric for (auto R : Ranges) 4383*0b57cec5SDimitry Andric D << R; 4384*0b57cec5SDimitry Andric return true; 4385*0b57cec5SDimitry Andric } 4386*0b57cec5SDimitry Andric 4387*0b57cec5SDimitry Andric // FIXME: Are there other cases for which a custom diagnostic is more useful 4388*0b57cec5SDimitry Andric // than the basic "types don't match" diagnostic? 4389*0b57cec5SDimitry Andric 4390*0b57cec5SDimitry Andric default: 4391*0b57cec5SDimitry Andric return false; 4392*0b57cec5SDimitry Andric } 4393*0b57cec5SDimitry Andric } 4394*0b57cec5SDimitry Andric 4395*0b57cec5SDimitry Andric /// Deduce the type for an auto type-specifier (C++11 [dcl.spec.auto]p6) 4396*0b57cec5SDimitry Andric /// 4397*0b57cec5SDimitry Andric /// Note that this is done even if the initializer is dependent. (This is 4398*0b57cec5SDimitry Andric /// necessary to support partial ordering of templates using 'auto'.) 4399*0b57cec5SDimitry Andric /// A dependent type will be produced when deducing from a dependent type. 4400*0b57cec5SDimitry Andric /// 4401*0b57cec5SDimitry Andric /// \param Type the type pattern using the auto type-specifier. 4402*0b57cec5SDimitry Andric /// \param Init the initializer for the variable whose type is to be deduced. 4403*0b57cec5SDimitry Andric /// \param Result if type deduction was successful, this will be set to the 4404*0b57cec5SDimitry Andric /// deduced type. 4405*0b57cec5SDimitry Andric /// \param DependentDeductionDepth Set if we should permit deduction in 4406*0b57cec5SDimitry Andric /// dependent cases. This is necessary for template partial ordering with 4407*0b57cec5SDimitry Andric /// 'auto' template parameters. The value specified is the template 4408*0b57cec5SDimitry Andric /// parameter depth at which we should perform 'auto' deduction. 4409*0b57cec5SDimitry Andric Sema::DeduceAutoResult 4410*0b57cec5SDimitry Andric Sema::DeduceAutoType(TypeLoc Type, Expr *&Init, QualType &Result, 4411*0b57cec5SDimitry Andric Optional<unsigned> DependentDeductionDepth) { 4412*0b57cec5SDimitry Andric if (Init->getType()->isNonOverloadPlaceholderType()) { 4413*0b57cec5SDimitry Andric ExprResult NonPlaceholder = CheckPlaceholderExpr(Init); 4414*0b57cec5SDimitry Andric if (NonPlaceholder.isInvalid()) 4415*0b57cec5SDimitry Andric return DAR_FailedAlreadyDiagnosed; 4416*0b57cec5SDimitry Andric Init = NonPlaceholder.get(); 4417*0b57cec5SDimitry Andric } 4418*0b57cec5SDimitry Andric 4419*0b57cec5SDimitry Andric DependentAuto DependentResult = { 4420*0b57cec5SDimitry Andric /*.IsPack = */ (bool)Type.getAs<PackExpansionTypeLoc>()}; 4421*0b57cec5SDimitry Andric 4422*0b57cec5SDimitry Andric if (!DependentDeductionDepth && 4423*0b57cec5SDimitry Andric (Type.getType()->isDependentType() || Init->isTypeDependent())) { 4424*0b57cec5SDimitry Andric Result = SubstituteDeducedTypeTransform(*this, DependentResult).Apply(Type); 4425*0b57cec5SDimitry Andric assert(!Result.isNull() && "substituting DependentTy can't fail"); 4426*0b57cec5SDimitry Andric return DAR_Succeeded; 4427*0b57cec5SDimitry Andric } 4428*0b57cec5SDimitry Andric 4429*0b57cec5SDimitry Andric // Find the depth of template parameter to synthesize. 4430*0b57cec5SDimitry Andric unsigned Depth = DependentDeductionDepth.getValueOr(0); 4431*0b57cec5SDimitry Andric 4432*0b57cec5SDimitry Andric // If this is a 'decltype(auto)' specifier, do the decltype dance. 4433*0b57cec5SDimitry Andric // Since 'decltype(auto)' can only occur at the top of the type, we 4434*0b57cec5SDimitry Andric // don't need to go digging for it. 4435*0b57cec5SDimitry Andric if (const AutoType *AT = Type.getType()->getAs<AutoType>()) { 4436*0b57cec5SDimitry Andric if (AT->isDecltypeAuto()) { 4437*0b57cec5SDimitry Andric if (isa<InitListExpr>(Init)) { 4438*0b57cec5SDimitry Andric Diag(Init->getBeginLoc(), diag::err_decltype_auto_initializer_list); 4439*0b57cec5SDimitry Andric return DAR_FailedAlreadyDiagnosed; 4440*0b57cec5SDimitry Andric } 4441*0b57cec5SDimitry Andric 4442*0b57cec5SDimitry Andric ExprResult ER = CheckPlaceholderExpr(Init); 4443*0b57cec5SDimitry Andric if (ER.isInvalid()) 4444*0b57cec5SDimitry Andric return DAR_FailedAlreadyDiagnosed; 4445*0b57cec5SDimitry Andric Init = ER.get(); 4446*0b57cec5SDimitry Andric QualType Deduced = BuildDecltypeType(Init, Init->getBeginLoc(), false); 4447*0b57cec5SDimitry Andric if (Deduced.isNull()) 4448*0b57cec5SDimitry Andric return DAR_FailedAlreadyDiagnosed; 4449*0b57cec5SDimitry Andric // FIXME: Support a non-canonical deduced type for 'auto'. 4450*0b57cec5SDimitry Andric Deduced = Context.getCanonicalType(Deduced); 4451*0b57cec5SDimitry Andric Result = SubstituteDeducedTypeTransform(*this, Deduced).Apply(Type); 4452*0b57cec5SDimitry Andric if (Result.isNull()) 4453*0b57cec5SDimitry Andric return DAR_FailedAlreadyDiagnosed; 4454*0b57cec5SDimitry Andric return DAR_Succeeded; 4455*0b57cec5SDimitry Andric } else if (!getLangOpts().CPlusPlus) { 4456*0b57cec5SDimitry Andric if (isa<InitListExpr>(Init)) { 4457*0b57cec5SDimitry Andric Diag(Init->getBeginLoc(), diag::err_auto_init_list_from_c); 4458*0b57cec5SDimitry Andric return DAR_FailedAlreadyDiagnosed; 4459*0b57cec5SDimitry Andric } 4460*0b57cec5SDimitry Andric } 4461*0b57cec5SDimitry Andric } 4462*0b57cec5SDimitry Andric 4463*0b57cec5SDimitry Andric SourceLocation Loc = Init->getExprLoc(); 4464*0b57cec5SDimitry Andric 4465*0b57cec5SDimitry Andric LocalInstantiationScope InstScope(*this); 4466*0b57cec5SDimitry Andric 4467*0b57cec5SDimitry Andric // Build template<class TemplParam> void Func(FuncParam); 4468*0b57cec5SDimitry Andric TemplateTypeParmDecl *TemplParam = TemplateTypeParmDecl::Create( 4469*0b57cec5SDimitry Andric Context, nullptr, SourceLocation(), Loc, Depth, 0, nullptr, false, false); 4470*0b57cec5SDimitry Andric QualType TemplArg = QualType(TemplParam->getTypeForDecl(), 0); 4471*0b57cec5SDimitry Andric NamedDecl *TemplParamPtr = TemplParam; 4472*0b57cec5SDimitry Andric FixedSizeTemplateParameterListStorage<1, false> TemplateParamsSt( 4473*0b57cec5SDimitry Andric Loc, Loc, TemplParamPtr, Loc, nullptr); 4474*0b57cec5SDimitry Andric 4475*0b57cec5SDimitry Andric QualType FuncParam = 4476*0b57cec5SDimitry Andric SubstituteDeducedTypeTransform(*this, TemplArg, /*UseTypeSugar*/false) 4477*0b57cec5SDimitry Andric .Apply(Type); 4478*0b57cec5SDimitry Andric assert(!FuncParam.isNull() && 4479*0b57cec5SDimitry Andric "substituting template parameter for 'auto' failed"); 4480*0b57cec5SDimitry Andric 4481*0b57cec5SDimitry Andric // Deduce type of TemplParam in Func(Init) 4482*0b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 1> Deduced; 4483*0b57cec5SDimitry Andric Deduced.resize(1); 4484*0b57cec5SDimitry Andric 4485*0b57cec5SDimitry Andric TemplateDeductionInfo Info(Loc, Depth); 4486*0b57cec5SDimitry Andric 4487*0b57cec5SDimitry Andric // If deduction failed, don't diagnose if the initializer is dependent; it 4488*0b57cec5SDimitry Andric // might acquire a matching type in the instantiation. 4489*0b57cec5SDimitry Andric auto DeductionFailed = [&](TemplateDeductionResult TDK, 4490*0b57cec5SDimitry Andric ArrayRef<SourceRange> Ranges) -> DeduceAutoResult { 4491*0b57cec5SDimitry Andric if (Init->isTypeDependent()) { 4492*0b57cec5SDimitry Andric Result = 4493*0b57cec5SDimitry Andric SubstituteDeducedTypeTransform(*this, DependentResult).Apply(Type); 4494*0b57cec5SDimitry Andric assert(!Result.isNull() && "substituting DependentTy can't fail"); 4495*0b57cec5SDimitry Andric return DAR_Succeeded; 4496*0b57cec5SDimitry Andric } 4497*0b57cec5SDimitry Andric if (diagnoseAutoDeductionFailure(*this, TDK, Info, Ranges)) 4498*0b57cec5SDimitry Andric return DAR_FailedAlreadyDiagnosed; 4499*0b57cec5SDimitry Andric return DAR_Failed; 4500*0b57cec5SDimitry Andric }; 4501*0b57cec5SDimitry Andric 4502*0b57cec5SDimitry Andric SmallVector<OriginalCallArg, 4> OriginalCallArgs; 4503*0b57cec5SDimitry Andric 4504*0b57cec5SDimitry Andric InitListExpr *InitList = dyn_cast<InitListExpr>(Init); 4505*0b57cec5SDimitry Andric if (InitList) { 4506*0b57cec5SDimitry Andric // Notionally, we substitute std::initializer_list<T> for 'auto' and deduce 4507*0b57cec5SDimitry Andric // against that. Such deduction only succeeds if removing cv-qualifiers and 4508*0b57cec5SDimitry Andric // references results in std::initializer_list<T>. 4509*0b57cec5SDimitry Andric if (!Type.getType().getNonReferenceType()->getAs<AutoType>()) 4510*0b57cec5SDimitry Andric return DAR_Failed; 4511*0b57cec5SDimitry Andric 4512*0b57cec5SDimitry Andric SourceRange DeducedFromInitRange; 4513*0b57cec5SDimitry Andric for (unsigned i = 0, e = InitList->getNumInits(); i < e; ++i) { 4514*0b57cec5SDimitry Andric Expr *Init = InitList->getInit(i); 4515*0b57cec5SDimitry Andric 4516*0b57cec5SDimitry Andric if (auto TDK = DeduceTemplateArgumentsFromCallArgument( 4517*0b57cec5SDimitry Andric *this, TemplateParamsSt.get(), 0, TemplArg, Init, 4518*0b57cec5SDimitry Andric Info, Deduced, OriginalCallArgs, /*Decomposed*/ true, 4519*0b57cec5SDimitry Andric /*ArgIdx*/ 0, /*TDF*/ 0)) 4520*0b57cec5SDimitry Andric return DeductionFailed(TDK, {DeducedFromInitRange, 4521*0b57cec5SDimitry Andric Init->getSourceRange()}); 4522*0b57cec5SDimitry Andric 4523*0b57cec5SDimitry Andric if (DeducedFromInitRange.isInvalid() && 4524*0b57cec5SDimitry Andric Deduced[0].getKind() != TemplateArgument::Null) 4525*0b57cec5SDimitry Andric DeducedFromInitRange = Init->getSourceRange(); 4526*0b57cec5SDimitry Andric } 4527*0b57cec5SDimitry Andric } else { 4528*0b57cec5SDimitry Andric if (!getLangOpts().CPlusPlus && Init->refersToBitField()) { 4529*0b57cec5SDimitry Andric Diag(Loc, diag::err_auto_bitfield); 4530*0b57cec5SDimitry Andric return DAR_FailedAlreadyDiagnosed; 4531*0b57cec5SDimitry Andric } 4532*0b57cec5SDimitry Andric 4533*0b57cec5SDimitry Andric if (auto TDK = DeduceTemplateArgumentsFromCallArgument( 4534*0b57cec5SDimitry Andric *this, TemplateParamsSt.get(), 0, FuncParam, Init, Info, Deduced, 4535*0b57cec5SDimitry Andric OriginalCallArgs, /*Decomposed*/ false, /*ArgIdx*/ 0, /*TDF*/ 0)) 4536*0b57cec5SDimitry Andric return DeductionFailed(TDK, {}); 4537*0b57cec5SDimitry Andric } 4538*0b57cec5SDimitry Andric 4539*0b57cec5SDimitry Andric // Could be null if somehow 'auto' appears in a non-deduced context. 4540*0b57cec5SDimitry Andric if (Deduced[0].getKind() != TemplateArgument::Type) 4541*0b57cec5SDimitry Andric return DeductionFailed(TDK_Incomplete, {}); 4542*0b57cec5SDimitry Andric 4543*0b57cec5SDimitry Andric QualType DeducedType = Deduced[0].getAsType(); 4544*0b57cec5SDimitry Andric 4545*0b57cec5SDimitry Andric if (InitList) { 4546*0b57cec5SDimitry Andric DeducedType = BuildStdInitializerList(DeducedType, Loc); 4547*0b57cec5SDimitry Andric if (DeducedType.isNull()) 4548*0b57cec5SDimitry Andric return DAR_FailedAlreadyDiagnosed; 4549*0b57cec5SDimitry Andric } 4550*0b57cec5SDimitry Andric 4551*0b57cec5SDimitry Andric Result = SubstituteDeducedTypeTransform(*this, DeducedType).Apply(Type); 4552*0b57cec5SDimitry Andric if (Result.isNull()) 4553*0b57cec5SDimitry Andric return DAR_FailedAlreadyDiagnosed; 4554*0b57cec5SDimitry Andric 4555*0b57cec5SDimitry Andric // Check that the deduced argument type is compatible with the original 4556*0b57cec5SDimitry Andric // argument type per C++ [temp.deduct.call]p4. 4557*0b57cec5SDimitry Andric QualType DeducedA = InitList ? Deduced[0].getAsType() : Result; 4558*0b57cec5SDimitry Andric for (const OriginalCallArg &OriginalArg : OriginalCallArgs) { 4559*0b57cec5SDimitry Andric assert((bool)InitList == OriginalArg.DecomposedParam && 4560*0b57cec5SDimitry Andric "decomposed non-init-list in auto deduction?"); 4561*0b57cec5SDimitry Andric if (auto TDK = 4562*0b57cec5SDimitry Andric CheckOriginalCallArgDeduction(*this, Info, OriginalArg, DeducedA)) { 4563*0b57cec5SDimitry Andric Result = QualType(); 4564*0b57cec5SDimitry Andric return DeductionFailed(TDK, {}); 4565*0b57cec5SDimitry Andric } 4566*0b57cec5SDimitry Andric } 4567*0b57cec5SDimitry Andric 4568*0b57cec5SDimitry Andric return DAR_Succeeded; 4569*0b57cec5SDimitry Andric } 4570*0b57cec5SDimitry Andric 4571*0b57cec5SDimitry Andric QualType Sema::SubstAutoType(QualType TypeWithAuto, 4572*0b57cec5SDimitry Andric QualType TypeToReplaceAuto) { 4573*0b57cec5SDimitry Andric if (TypeToReplaceAuto->isDependentType()) 4574*0b57cec5SDimitry Andric return SubstituteDeducedTypeTransform( 4575*0b57cec5SDimitry Andric *this, DependentAuto{ 4576*0b57cec5SDimitry Andric TypeToReplaceAuto->containsUnexpandedParameterPack()}) 4577*0b57cec5SDimitry Andric .TransformType(TypeWithAuto); 4578*0b57cec5SDimitry Andric return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto) 4579*0b57cec5SDimitry Andric .TransformType(TypeWithAuto); 4580*0b57cec5SDimitry Andric } 4581*0b57cec5SDimitry Andric 4582*0b57cec5SDimitry Andric TypeSourceInfo *Sema::SubstAutoTypeSourceInfo(TypeSourceInfo *TypeWithAuto, 4583*0b57cec5SDimitry Andric QualType TypeToReplaceAuto) { 4584*0b57cec5SDimitry Andric if (TypeToReplaceAuto->isDependentType()) 4585*0b57cec5SDimitry Andric return SubstituteDeducedTypeTransform( 4586*0b57cec5SDimitry Andric *this, 4587*0b57cec5SDimitry Andric DependentAuto{ 4588*0b57cec5SDimitry Andric TypeToReplaceAuto->containsUnexpandedParameterPack()}) 4589*0b57cec5SDimitry Andric .TransformType(TypeWithAuto); 4590*0b57cec5SDimitry Andric return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto) 4591*0b57cec5SDimitry Andric .TransformType(TypeWithAuto); 4592*0b57cec5SDimitry Andric } 4593*0b57cec5SDimitry Andric 4594*0b57cec5SDimitry Andric QualType Sema::ReplaceAutoType(QualType TypeWithAuto, 4595*0b57cec5SDimitry Andric QualType TypeToReplaceAuto) { 4596*0b57cec5SDimitry Andric return SubstituteDeducedTypeTransform(*this, TypeToReplaceAuto, 4597*0b57cec5SDimitry Andric /*UseTypeSugar*/ false) 4598*0b57cec5SDimitry Andric .TransformType(TypeWithAuto); 4599*0b57cec5SDimitry Andric } 4600*0b57cec5SDimitry Andric 4601*0b57cec5SDimitry Andric void Sema::DiagnoseAutoDeductionFailure(VarDecl *VDecl, Expr *Init) { 4602*0b57cec5SDimitry Andric if (isa<InitListExpr>(Init)) 4603*0b57cec5SDimitry Andric Diag(VDecl->getLocation(), 4604*0b57cec5SDimitry Andric VDecl->isInitCapture() 4605*0b57cec5SDimitry Andric ? diag::err_init_capture_deduction_failure_from_init_list 4606*0b57cec5SDimitry Andric : diag::err_auto_var_deduction_failure_from_init_list) 4607*0b57cec5SDimitry Andric << VDecl->getDeclName() << VDecl->getType() << Init->getSourceRange(); 4608*0b57cec5SDimitry Andric else 4609*0b57cec5SDimitry Andric Diag(VDecl->getLocation(), 4610*0b57cec5SDimitry Andric VDecl->isInitCapture() ? diag::err_init_capture_deduction_failure 4611*0b57cec5SDimitry Andric : diag::err_auto_var_deduction_failure) 4612*0b57cec5SDimitry Andric << VDecl->getDeclName() << VDecl->getType() << Init->getType() 4613*0b57cec5SDimitry Andric << Init->getSourceRange(); 4614*0b57cec5SDimitry Andric } 4615*0b57cec5SDimitry Andric 4616*0b57cec5SDimitry Andric bool Sema::DeduceReturnType(FunctionDecl *FD, SourceLocation Loc, 4617*0b57cec5SDimitry Andric bool Diagnose) { 4618*0b57cec5SDimitry Andric assert(FD->getReturnType()->isUndeducedType()); 4619*0b57cec5SDimitry Andric 4620*0b57cec5SDimitry Andric // For a lambda's conversion operator, deduce any 'auto' or 'decltype(auto)' 4621*0b57cec5SDimitry Andric // within the return type from the call operator's type. 4622*0b57cec5SDimitry Andric if (isLambdaConversionOperator(FD)) { 4623*0b57cec5SDimitry Andric CXXRecordDecl *Lambda = cast<CXXMethodDecl>(FD)->getParent(); 4624*0b57cec5SDimitry Andric FunctionDecl *CallOp = Lambda->getLambdaCallOperator(); 4625*0b57cec5SDimitry Andric 4626*0b57cec5SDimitry Andric // For a generic lambda, instantiate the call operator if needed. 4627*0b57cec5SDimitry Andric if (auto *Args = FD->getTemplateSpecializationArgs()) { 4628*0b57cec5SDimitry Andric CallOp = InstantiateFunctionDeclaration( 4629*0b57cec5SDimitry Andric CallOp->getDescribedFunctionTemplate(), Args, Loc); 4630*0b57cec5SDimitry Andric if (!CallOp || CallOp->isInvalidDecl()) 4631*0b57cec5SDimitry Andric return true; 4632*0b57cec5SDimitry Andric 4633*0b57cec5SDimitry Andric // We might need to deduce the return type by instantiating the definition 4634*0b57cec5SDimitry Andric // of the operator() function. 4635*0b57cec5SDimitry Andric if (CallOp->getReturnType()->isUndeducedType()) 4636*0b57cec5SDimitry Andric InstantiateFunctionDefinition(Loc, CallOp); 4637*0b57cec5SDimitry Andric } 4638*0b57cec5SDimitry Andric 4639*0b57cec5SDimitry Andric if (CallOp->isInvalidDecl()) 4640*0b57cec5SDimitry Andric return true; 4641*0b57cec5SDimitry Andric assert(!CallOp->getReturnType()->isUndeducedType() && 4642*0b57cec5SDimitry Andric "failed to deduce lambda return type"); 4643*0b57cec5SDimitry Andric 4644*0b57cec5SDimitry Andric // Build the new return type from scratch. 4645*0b57cec5SDimitry Andric QualType RetType = getLambdaConversionFunctionResultType( 4646*0b57cec5SDimitry Andric CallOp->getType()->castAs<FunctionProtoType>()); 4647*0b57cec5SDimitry Andric if (FD->getReturnType()->getAs<PointerType>()) 4648*0b57cec5SDimitry Andric RetType = Context.getPointerType(RetType); 4649*0b57cec5SDimitry Andric else { 4650*0b57cec5SDimitry Andric assert(FD->getReturnType()->getAs<BlockPointerType>()); 4651*0b57cec5SDimitry Andric RetType = Context.getBlockPointerType(RetType); 4652*0b57cec5SDimitry Andric } 4653*0b57cec5SDimitry Andric Context.adjustDeducedFunctionResultType(FD, RetType); 4654*0b57cec5SDimitry Andric return false; 4655*0b57cec5SDimitry Andric } 4656*0b57cec5SDimitry Andric 4657*0b57cec5SDimitry Andric if (FD->getTemplateInstantiationPattern()) 4658*0b57cec5SDimitry Andric InstantiateFunctionDefinition(Loc, FD); 4659*0b57cec5SDimitry Andric 4660*0b57cec5SDimitry Andric bool StillUndeduced = FD->getReturnType()->isUndeducedType(); 4661*0b57cec5SDimitry Andric if (StillUndeduced && Diagnose && !FD->isInvalidDecl()) { 4662*0b57cec5SDimitry Andric Diag(Loc, diag::err_auto_fn_used_before_defined) << FD; 4663*0b57cec5SDimitry Andric Diag(FD->getLocation(), diag::note_callee_decl) << FD; 4664*0b57cec5SDimitry Andric } 4665*0b57cec5SDimitry Andric 4666*0b57cec5SDimitry Andric return StillUndeduced; 4667*0b57cec5SDimitry Andric } 4668*0b57cec5SDimitry Andric 4669*0b57cec5SDimitry Andric /// If this is a non-static member function, 4670*0b57cec5SDimitry Andric static void 4671*0b57cec5SDimitry Andric AddImplicitObjectParameterType(ASTContext &Context, 4672*0b57cec5SDimitry Andric CXXMethodDecl *Method, 4673*0b57cec5SDimitry Andric SmallVectorImpl<QualType> &ArgTypes) { 4674*0b57cec5SDimitry Andric // C++11 [temp.func.order]p3: 4675*0b57cec5SDimitry Andric // [...] The new parameter is of type "reference to cv A," where cv are 4676*0b57cec5SDimitry Andric // the cv-qualifiers of the function template (if any) and A is 4677*0b57cec5SDimitry Andric // the class of which the function template is a member. 4678*0b57cec5SDimitry Andric // 4679*0b57cec5SDimitry Andric // The standard doesn't say explicitly, but we pick the appropriate kind of 4680*0b57cec5SDimitry Andric // reference type based on [over.match.funcs]p4. 4681*0b57cec5SDimitry Andric QualType ArgTy = Context.getTypeDeclType(Method->getParent()); 4682*0b57cec5SDimitry Andric ArgTy = Context.getQualifiedType(ArgTy, Method->getMethodQualifiers()); 4683*0b57cec5SDimitry Andric if (Method->getRefQualifier() == RQ_RValue) 4684*0b57cec5SDimitry Andric ArgTy = Context.getRValueReferenceType(ArgTy); 4685*0b57cec5SDimitry Andric else 4686*0b57cec5SDimitry Andric ArgTy = Context.getLValueReferenceType(ArgTy); 4687*0b57cec5SDimitry Andric ArgTypes.push_back(ArgTy); 4688*0b57cec5SDimitry Andric } 4689*0b57cec5SDimitry Andric 4690*0b57cec5SDimitry Andric /// Determine whether the function template \p FT1 is at least as 4691*0b57cec5SDimitry Andric /// specialized as \p FT2. 4692*0b57cec5SDimitry Andric static bool isAtLeastAsSpecializedAs(Sema &S, 4693*0b57cec5SDimitry Andric SourceLocation Loc, 4694*0b57cec5SDimitry Andric FunctionTemplateDecl *FT1, 4695*0b57cec5SDimitry Andric FunctionTemplateDecl *FT2, 4696*0b57cec5SDimitry Andric TemplatePartialOrderingContext TPOC, 4697*0b57cec5SDimitry Andric unsigned NumCallArguments1) { 4698*0b57cec5SDimitry Andric FunctionDecl *FD1 = FT1->getTemplatedDecl(); 4699*0b57cec5SDimitry Andric FunctionDecl *FD2 = FT2->getTemplatedDecl(); 4700*0b57cec5SDimitry Andric const FunctionProtoType *Proto1 = FD1->getType()->getAs<FunctionProtoType>(); 4701*0b57cec5SDimitry Andric const FunctionProtoType *Proto2 = FD2->getType()->getAs<FunctionProtoType>(); 4702*0b57cec5SDimitry Andric 4703*0b57cec5SDimitry Andric assert(Proto1 && Proto2 && "Function templates must have prototypes"); 4704*0b57cec5SDimitry Andric TemplateParameterList *TemplateParams = FT2->getTemplateParameters(); 4705*0b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 4> Deduced; 4706*0b57cec5SDimitry Andric Deduced.resize(TemplateParams->size()); 4707*0b57cec5SDimitry Andric 4708*0b57cec5SDimitry Andric // C++0x [temp.deduct.partial]p3: 4709*0b57cec5SDimitry Andric // The types used to determine the ordering depend on the context in which 4710*0b57cec5SDimitry Andric // the partial ordering is done: 4711*0b57cec5SDimitry Andric TemplateDeductionInfo Info(Loc); 4712*0b57cec5SDimitry Andric SmallVector<QualType, 4> Args2; 4713*0b57cec5SDimitry Andric switch (TPOC) { 4714*0b57cec5SDimitry Andric case TPOC_Call: { 4715*0b57cec5SDimitry Andric // - In the context of a function call, the function parameter types are 4716*0b57cec5SDimitry Andric // used. 4717*0b57cec5SDimitry Andric CXXMethodDecl *Method1 = dyn_cast<CXXMethodDecl>(FD1); 4718*0b57cec5SDimitry Andric CXXMethodDecl *Method2 = dyn_cast<CXXMethodDecl>(FD2); 4719*0b57cec5SDimitry Andric 4720*0b57cec5SDimitry Andric // C++11 [temp.func.order]p3: 4721*0b57cec5SDimitry Andric // [...] If only one of the function templates is a non-static 4722*0b57cec5SDimitry Andric // member, that function template is considered to have a new 4723*0b57cec5SDimitry Andric // first parameter inserted in its function parameter list. The 4724*0b57cec5SDimitry Andric // new parameter is of type "reference to cv A," where cv are 4725*0b57cec5SDimitry Andric // the cv-qualifiers of the function template (if any) and A is 4726*0b57cec5SDimitry Andric // the class of which the function template is a member. 4727*0b57cec5SDimitry Andric // 4728*0b57cec5SDimitry Andric // Note that we interpret this to mean "if one of the function 4729*0b57cec5SDimitry Andric // templates is a non-static member and the other is a non-member"; 4730*0b57cec5SDimitry Andric // otherwise, the ordering rules for static functions against non-static 4731*0b57cec5SDimitry Andric // functions don't make any sense. 4732*0b57cec5SDimitry Andric // 4733*0b57cec5SDimitry Andric // C++98/03 doesn't have this provision but we've extended DR532 to cover 4734*0b57cec5SDimitry Andric // it as wording was broken prior to it. 4735*0b57cec5SDimitry Andric SmallVector<QualType, 4> Args1; 4736*0b57cec5SDimitry Andric 4737*0b57cec5SDimitry Andric unsigned NumComparedArguments = NumCallArguments1; 4738*0b57cec5SDimitry Andric 4739*0b57cec5SDimitry Andric if (!Method2 && Method1 && !Method1->isStatic()) { 4740*0b57cec5SDimitry Andric // Compare 'this' from Method1 against first parameter from Method2. 4741*0b57cec5SDimitry Andric AddImplicitObjectParameterType(S.Context, Method1, Args1); 4742*0b57cec5SDimitry Andric ++NumComparedArguments; 4743*0b57cec5SDimitry Andric } else if (!Method1 && Method2 && !Method2->isStatic()) { 4744*0b57cec5SDimitry Andric // Compare 'this' from Method2 against first parameter from Method1. 4745*0b57cec5SDimitry Andric AddImplicitObjectParameterType(S.Context, Method2, Args2); 4746*0b57cec5SDimitry Andric } 4747*0b57cec5SDimitry Andric 4748*0b57cec5SDimitry Andric Args1.insert(Args1.end(), Proto1->param_type_begin(), 4749*0b57cec5SDimitry Andric Proto1->param_type_end()); 4750*0b57cec5SDimitry Andric Args2.insert(Args2.end(), Proto2->param_type_begin(), 4751*0b57cec5SDimitry Andric Proto2->param_type_end()); 4752*0b57cec5SDimitry Andric 4753*0b57cec5SDimitry Andric // C++ [temp.func.order]p5: 4754*0b57cec5SDimitry Andric // The presence of unused ellipsis and default arguments has no effect on 4755*0b57cec5SDimitry Andric // the partial ordering of function templates. 4756*0b57cec5SDimitry Andric if (Args1.size() > NumComparedArguments) 4757*0b57cec5SDimitry Andric Args1.resize(NumComparedArguments); 4758*0b57cec5SDimitry Andric if (Args2.size() > NumComparedArguments) 4759*0b57cec5SDimitry Andric Args2.resize(NumComparedArguments); 4760*0b57cec5SDimitry Andric if (DeduceTemplateArguments(S, TemplateParams, Args2.data(), Args2.size(), 4761*0b57cec5SDimitry Andric Args1.data(), Args1.size(), Info, Deduced, 4762*0b57cec5SDimitry Andric TDF_None, /*PartialOrdering=*/true)) 4763*0b57cec5SDimitry Andric return false; 4764*0b57cec5SDimitry Andric 4765*0b57cec5SDimitry Andric break; 4766*0b57cec5SDimitry Andric } 4767*0b57cec5SDimitry Andric 4768*0b57cec5SDimitry Andric case TPOC_Conversion: 4769*0b57cec5SDimitry Andric // - In the context of a call to a conversion operator, the return types 4770*0b57cec5SDimitry Andric // of the conversion function templates are used. 4771*0b57cec5SDimitry Andric if (DeduceTemplateArgumentsByTypeMatch( 4772*0b57cec5SDimitry Andric S, TemplateParams, Proto2->getReturnType(), Proto1->getReturnType(), 4773*0b57cec5SDimitry Andric Info, Deduced, TDF_None, 4774*0b57cec5SDimitry Andric /*PartialOrdering=*/true)) 4775*0b57cec5SDimitry Andric return false; 4776*0b57cec5SDimitry Andric break; 4777*0b57cec5SDimitry Andric 4778*0b57cec5SDimitry Andric case TPOC_Other: 4779*0b57cec5SDimitry Andric // - In other contexts (14.6.6.2) the function template's function type 4780*0b57cec5SDimitry Andric // is used. 4781*0b57cec5SDimitry Andric if (DeduceTemplateArgumentsByTypeMatch(S, TemplateParams, 4782*0b57cec5SDimitry Andric FD2->getType(), FD1->getType(), 4783*0b57cec5SDimitry Andric Info, Deduced, TDF_None, 4784*0b57cec5SDimitry Andric /*PartialOrdering=*/true)) 4785*0b57cec5SDimitry Andric return false; 4786*0b57cec5SDimitry Andric break; 4787*0b57cec5SDimitry Andric } 4788*0b57cec5SDimitry Andric 4789*0b57cec5SDimitry Andric // C++0x [temp.deduct.partial]p11: 4790*0b57cec5SDimitry Andric // In most cases, all template parameters must have values in order for 4791*0b57cec5SDimitry Andric // deduction to succeed, but for partial ordering purposes a template 4792*0b57cec5SDimitry Andric // parameter may remain without a value provided it is not used in the 4793*0b57cec5SDimitry Andric // types being used for partial ordering. [ Note: a template parameter used 4794*0b57cec5SDimitry Andric // in a non-deduced context is considered used. -end note] 4795*0b57cec5SDimitry Andric unsigned ArgIdx = 0, NumArgs = Deduced.size(); 4796*0b57cec5SDimitry Andric for (; ArgIdx != NumArgs; ++ArgIdx) 4797*0b57cec5SDimitry Andric if (Deduced[ArgIdx].isNull()) 4798*0b57cec5SDimitry Andric break; 4799*0b57cec5SDimitry Andric 4800*0b57cec5SDimitry Andric // FIXME: We fail to implement [temp.deduct.type]p1 along this path. We need 4801*0b57cec5SDimitry Andric // to substitute the deduced arguments back into the template and check that 4802*0b57cec5SDimitry Andric // we get the right type. 4803*0b57cec5SDimitry Andric 4804*0b57cec5SDimitry Andric if (ArgIdx == NumArgs) { 4805*0b57cec5SDimitry Andric // All template arguments were deduced. FT1 is at least as specialized 4806*0b57cec5SDimitry Andric // as FT2. 4807*0b57cec5SDimitry Andric return true; 4808*0b57cec5SDimitry Andric } 4809*0b57cec5SDimitry Andric 4810*0b57cec5SDimitry Andric // Figure out which template parameters were used. 4811*0b57cec5SDimitry Andric llvm::SmallBitVector UsedParameters(TemplateParams->size()); 4812*0b57cec5SDimitry Andric switch (TPOC) { 4813*0b57cec5SDimitry Andric case TPOC_Call: 4814*0b57cec5SDimitry Andric for (unsigned I = 0, N = Args2.size(); I != N; ++I) 4815*0b57cec5SDimitry Andric ::MarkUsedTemplateParameters(S.Context, Args2[I], false, 4816*0b57cec5SDimitry Andric TemplateParams->getDepth(), 4817*0b57cec5SDimitry Andric UsedParameters); 4818*0b57cec5SDimitry Andric break; 4819*0b57cec5SDimitry Andric 4820*0b57cec5SDimitry Andric case TPOC_Conversion: 4821*0b57cec5SDimitry Andric ::MarkUsedTemplateParameters(S.Context, Proto2->getReturnType(), false, 4822*0b57cec5SDimitry Andric TemplateParams->getDepth(), UsedParameters); 4823*0b57cec5SDimitry Andric break; 4824*0b57cec5SDimitry Andric 4825*0b57cec5SDimitry Andric case TPOC_Other: 4826*0b57cec5SDimitry Andric ::MarkUsedTemplateParameters(S.Context, FD2->getType(), false, 4827*0b57cec5SDimitry Andric TemplateParams->getDepth(), 4828*0b57cec5SDimitry Andric UsedParameters); 4829*0b57cec5SDimitry Andric break; 4830*0b57cec5SDimitry Andric } 4831*0b57cec5SDimitry Andric 4832*0b57cec5SDimitry Andric for (; ArgIdx != NumArgs; ++ArgIdx) 4833*0b57cec5SDimitry Andric // If this argument had no value deduced but was used in one of the types 4834*0b57cec5SDimitry Andric // used for partial ordering, then deduction fails. 4835*0b57cec5SDimitry Andric if (Deduced[ArgIdx].isNull() && UsedParameters[ArgIdx]) 4836*0b57cec5SDimitry Andric return false; 4837*0b57cec5SDimitry Andric 4838*0b57cec5SDimitry Andric return true; 4839*0b57cec5SDimitry Andric } 4840*0b57cec5SDimitry Andric 4841*0b57cec5SDimitry Andric /// Determine whether this a function template whose parameter-type-list 4842*0b57cec5SDimitry Andric /// ends with a function parameter pack. 4843*0b57cec5SDimitry Andric static bool isVariadicFunctionTemplate(FunctionTemplateDecl *FunTmpl) { 4844*0b57cec5SDimitry Andric FunctionDecl *Function = FunTmpl->getTemplatedDecl(); 4845*0b57cec5SDimitry Andric unsigned NumParams = Function->getNumParams(); 4846*0b57cec5SDimitry Andric if (NumParams == 0) 4847*0b57cec5SDimitry Andric return false; 4848*0b57cec5SDimitry Andric 4849*0b57cec5SDimitry Andric ParmVarDecl *Last = Function->getParamDecl(NumParams - 1); 4850*0b57cec5SDimitry Andric if (!Last->isParameterPack()) 4851*0b57cec5SDimitry Andric return false; 4852*0b57cec5SDimitry Andric 4853*0b57cec5SDimitry Andric // Make sure that no previous parameter is a parameter pack. 4854*0b57cec5SDimitry Andric while (--NumParams > 0) { 4855*0b57cec5SDimitry Andric if (Function->getParamDecl(NumParams - 1)->isParameterPack()) 4856*0b57cec5SDimitry Andric return false; 4857*0b57cec5SDimitry Andric } 4858*0b57cec5SDimitry Andric 4859*0b57cec5SDimitry Andric return true; 4860*0b57cec5SDimitry Andric } 4861*0b57cec5SDimitry Andric 4862*0b57cec5SDimitry Andric /// Returns the more specialized function template according 4863*0b57cec5SDimitry Andric /// to the rules of function template partial ordering (C++ [temp.func.order]). 4864*0b57cec5SDimitry Andric /// 4865*0b57cec5SDimitry Andric /// \param FT1 the first function template 4866*0b57cec5SDimitry Andric /// 4867*0b57cec5SDimitry Andric /// \param FT2 the second function template 4868*0b57cec5SDimitry Andric /// 4869*0b57cec5SDimitry Andric /// \param TPOC the context in which we are performing partial ordering of 4870*0b57cec5SDimitry Andric /// function templates. 4871*0b57cec5SDimitry Andric /// 4872*0b57cec5SDimitry Andric /// \param NumCallArguments1 The number of arguments in the call to FT1, used 4873*0b57cec5SDimitry Andric /// only when \c TPOC is \c TPOC_Call. 4874*0b57cec5SDimitry Andric /// 4875*0b57cec5SDimitry Andric /// \param NumCallArguments2 The number of arguments in the call to FT2, used 4876*0b57cec5SDimitry Andric /// only when \c TPOC is \c TPOC_Call. 4877*0b57cec5SDimitry Andric /// 4878*0b57cec5SDimitry Andric /// \returns the more specialized function template. If neither 4879*0b57cec5SDimitry Andric /// template is more specialized, returns NULL. 4880*0b57cec5SDimitry Andric FunctionTemplateDecl * 4881*0b57cec5SDimitry Andric Sema::getMoreSpecializedTemplate(FunctionTemplateDecl *FT1, 4882*0b57cec5SDimitry Andric FunctionTemplateDecl *FT2, 4883*0b57cec5SDimitry Andric SourceLocation Loc, 4884*0b57cec5SDimitry Andric TemplatePartialOrderingContext TPOC, 4885*0b57cec5SDimitry Andric unsigned NumCallArguments1, 4886*0b57cec5SDimitry Andric unsigned NumCallArguments2) { 4887*0b57cec5SDimitry Andric bool Better1 = isAtLeastAsSpecializedAs(*this, Loc, FT1, FT2, TPOC, 4888*0b57cec5SDimitry Andric NumCallArguments1); 4889*0b57cec5SDimitry Andric bool Better2 = isAtLeastAsSpecializedAs(*this, Loc, FT2, FT1, TPOC, 4890*0b57cec5SDimitry Andric NumCallArguments2); 4891*0b57cec5SDimitry Andric 4892*0b57cec5SDimitry Andric if (Better1 != Better2) // We have a clear winner 4893*0b57cec5SDimitry Andric return Better1 ? FT1 : FT2; 4894*0b57cec5SDimitry Andric 4895*0b57cec5SDimitry Andric if (!Better1 && !Better2) // Neither is better than the other 4896*0b57cec5SDimitry Andric return nullptr; 4897*0b57cec5SDimitry Andric 4898*0b57cec5SDimitry Andric // FIXME: This mimics what GCC implements, but doesn't match up with the 4899*0b57cec5SDimitry Andric // proposed resolution for core issue 692. This area needs to be sorted out, 4900*0b57cec5SDimitry Andric // but for now we attempt to maintain compatibility. 4901*0b57cec5SDimitry Andric bool Variadic1 = isVariadicFunctionTemplate(FT1); 4902*0b57cec5SDimitry Andric bool Variadic2 = isVariadicFunctionTemplate(FT2); 4903*0b57cec5SDimitry Andric if (Variadic1 != Variadic2) 4904*0b57cec5SDimitry Andric return Variadic1? FT2 : FT1; 4905*0b57cec5SDimitry Andric 4906*0b57cec5SDimitry Andric return nullptr; 4907*0b57cec5SDimitry Andric } 4908*0b57cec5SDimitry Andric 4909*0b57cec5SDimitry Andric /// Determine if the two templates are equivalent. 4910*0b57cec5SDimitry Andric static bool isSameTemplate(TemplateDecl *T1, TemplateDecl *T2) { 4911*0b57cec5SDimitry Andric if (T1 == T2) 4912*0b57cec5SDimitry Andric return true; 4913*0b57cec5SDimitry Andric 4914*0b57cec5SDimitry Andric if (!T1 || !T2) 4915*0b57cec5SDimitry Andric return false; 4916*0b57cec5SDimitry Andric 4917*0b57cec5SDimitry Andric return T1->getCanonicalDecl() == T2->getCanonicalDecl(); 4918*0b57cec5SDimitry Andric } 4919*0b57cec5SDimitry Andric 4920*0b57cec5SDimitry Andric /// Retrieve the most specialized of the given function template 4921*0b57cec5SDimitry Andric /// specializations. 4922*0b57cec5SDimitry Andric /// 4923*0b57cec5SDimitry Andric /// \param SpecBegin the start iterator of the function template 4924*0b57cec5SDimitry Andric /// specializations that we will be comparing. 4925*0b57cec5SDimitry Andric /// 4926*0b57cec5SDimitry Andric /// \param SpecEnd the end iterator of the function template 4927*0b57cec5SDimitry Andric /// specializations, paired with \p SpecBegin. 4928*0b57cec5SDimitry Andric /// 4929*0b57cec5SDimitry Andric /// \param Loc the location where the ambiguity or no-specializations 4930*0b57cec5SDimitry Andric /// diagnostic should occur. 4931*0b57cec5SDimitry Andric /// 4932*0b57cec5SDimitry Andric /// \param NoneDiag partial diagnostic used to diagnose cases where there are 4933*0b57cec5SDimitry Andric /// no matching candidates. 4934*0b57cec5SDimitry Andric /// 4935*0b57cec5SDimitry Andric /// \param AmbigDiag partial diagnostic used to diagnose an ambiguity, if one 4936*0b57cec5SDimitry Andric /// occurs. 4937*0b57cec5SDimitry Andric /// 4938*0b57cec5SDimitry Andric /// \param CandidateDiag partial diagnostic used for each function template 4939*0b57cec5SDimitry Andric /// specialization that is a candidate in the ambiguous ordering. One parameter 4940*0b57cec5SDimitry Andric /// in this diagnostic should be unbound, which will correspond to the string 4941*0b57cec5SDimitry Andric /// describing the template arguments for the function template specialization. 4942*0b57cec5SDimitry Andric /// 4943*0b57cec5SDimitry Andric /// \returns the most specialized function template specialization, if 4944*0b57cec5SDimitry Andric /// found. Otherwise, returns SpecEnd. 4945*0b57cec5SDimitry Andric UnresolvedSetIterator Sema::getMostSpecialized( 4946*0b57cec5SDimitry Andric UnresolvedSetIterator SpecBegin, UnresolvedSetIterator SpecEnd, 4947*0b57cec5SDimitry Andric TemplateSpecCandidateSet &FailedCandidates, 4948*0b57cec5SDimitry Andric SourceLocation Loc, const PartialDiagnostic &NoneDiag, 4949*0b57cec5SDimitry Andric const PartialDiagnostic &AmbigDiag, const PartialDiagnostic &CandidateDiag, 4950*0b57cec5SDimitry Andric bool Complain, QualType TargetType) { 4951*0b57cec5SDimitry Andric if (SpecBegin == SpecEnd) { 4952*0b57cec5SDimitry Andric if (Complain) { 4953*0b57cec5SDimitry Andric Diag(Loc, NoneDiag); 4954*0b57cec5SDimitry Andric FailedCandidates.NoteCandidates(*this, Loc); 4955*0b57cec5SDimitry Andric } 4956*0b57cec5SDimitry Andric return SpecEnd; 4957*0b57cec5SDimitry Andric } 4958*0b57cec5SDimitry Andric 4959*0b57cec5SDimitry Andric if (SpecBegin + 1 == SpecEnd) 4960*0b57cec5SDimitry Andric return SpecBegin; 4961*0b57cec5SDimitry Andric 4962*0b57cec5SDimitry Andric // Find the function template that is better than all of the templates it 4963*0b57cec5SDimitry Andric // has been compared to. 4964*0b57cec5SDimitry Andric UnresolvedSetIterator Best = SpecBegin; 4965*0b57cec5SDimitry Andric FunctionTemplateDecl *BestTemplate 4966*0b57cec5SDimitry Andric = cast<FunctionDecl>(*Best)->getPrimaryTemplate(); 4967*0b57cec5SDimitry Andric assert(BestTemplate && "Not a function template specialization?"); 4968*0b57cec5SDimitry Andric for (UnresolvedSetIterator I = SpecBegin + 1; I != SpecEnd; ++I) { 4969*0b57cec5SDimitry Andric FunctionTemplateDecl *Challenger 4970*0b57cec5SDimitry Andric = cast<FunctionDecl>(*I)->getPrimaryTemplate(); 4971*0b57cec5SDimitry Andric assert(Challenger && "Not a function template specialization?"); 4972*0b57cec5SDimitry Andric if (isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, 4973*0b57cec5SDimitry Andric Loc, TPOC_Other, 0, 0), 4974*0b57cec5SDimitry Andric Challenger)) { 4975*0b57cec5SDimitry Andric Best = I; 4976*0b57cec5SDimitry Andric BestTemplate = Challenger; 4977*0b57cec5SDimitry Andric } 4978*0b57cec5SDimitry Andric } 4979*0b57cec5SDimitry Andric 4980*0b57cec5SDimitry Andric // Make sure that the "best" function template is more specialized than all 4981*0b57cec5SDimitry Andric // of the others. 4982*0b57cec5SDimitry Andric bool Ambiguous = false; 4983*0b57cec5SDimitry Andric for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { 4984*0b57cec5SDimitry Andric FunctionTemplateDecl *Challenger 4985*0b57cec5SDimitry Andric = cast<FunctionDecl>(*I)->getPrimaryTemplate(); 4986*0b57cec5SDimitry Andric if (I != Best && 4987*0b57cec5SDimitry Andric !isSameTemplate(getMoreSpecializedTemplate(BestTemplate, Challenger, 4988*0b57cec5SDimitry Andric Loc, TPOC_Other, 0, 0), 4989*0b57cec5SDimitry Andric BestTemplate)) { 4990*0b57cec5SDimitry Andric Ambiguous = true; 4991*0b57cec5SDimitry Andric break; 4992*0b57cec5SDimitry Andric } 4993*0b57cec5SDimitry Andric } 4994*0b57cec5SDimitry Andric 4995*0b57cec5SDimitry Andric if (!Ambiguous) { 4996*0b57cec5SDimitry Andric // We found an answer. Return it. 4997*0b57cec5SDimitry Andric return Best; 4998*0b57cec5SDimitry Andric } 4999*0b57cec5SDimitry Andric 5000*0b57cec5SDimitry Andric // Diagnose the ambiguity. 5001*0b57cec5SDimitry Andric if (Complain) { 5002*0b57cec5SDimitry Andric Diag(Loc, AmbigDiag); 5003*0b57cec5SDimitry Andric 5004*0b57cec5SDimitry Andric // FIXME: Can we order the candidates in some sane way? 5005*0b57cec5SDimitry Andric for (UnresolvedSetIterator I = SpecBegin; I != SpecEnd; ++I) { 5006*0b57cec5SDimitry Andric PartialDiagnostic PD = CandidateDiag; 5007*0b57cec5SDimitry Andric const auto *FD = cast<FunctionDecl>(*I); 5008*0b57cec5SDimitry Andric PD << FD << getTemplateArgumentBindingsText( 5009*0b57cec5SDimitry Andric FD->getPrimaryTemplate()->getTemplateParameters(), 5010*0b57cec5SDimitry Andric *FD->getTemplateSpecializationArgs()); 5011*0b57cec5SDimitry Andric if (!TargetType.isNull()) 5012*0b57cec5SDimitry Andric HandleFunctionTypeMismatch(PD, FD->getType(), TargetType); 5013*0b57cec5SDimitry Andric Diag((*I)->getLocation(), PD); 5014*0b57cec5SDimitry Andric } 5015*0b57cec5SDimitry Andric } 5016*0b57cec5SDimitry Andric 5017*0b57cec5SDimitry Andric return SpecEnd; 5018*0b57cec5SDimitry Andric } 5019*0b57cec5SDimitry Andric 5020*0b57cec5SDimitry Andric /// Determine whether one partial specialization, P1, is at least as 5021*0b57cec5SDimitry Andric /// specialized than another, P2. 5022*0b57cec5SDimitry Andric /// 5023*0b57cec5SDimitry Andric /// \tparam TemplateLikeDecl The kind of P2, which must be a 5024*0b57cec5SDimitry Andric /// TemplateDecl or {Class,Var}TemplatePartialSpecializationDecl. 5025*0b57cec5SDimitry Andric /// \param T1 The injected-class-name of P1 (faked for a variable template). 5026*0b57cec5SDimitry Andric /// \param T2 The injected-class-name of P2 (faked for a variable template). 5027*0b57cec5SDimitry Andric template<typename TemplateLikeDecl> 5028*0b57cec5SDimitry Andric static bool isAtLeastAsSpecializedAs(Sema &S, QualType T1, QualType T2, 5029*0b57cec5SDimitry Andric TemplateLikeDecl *P2, 5030*0b57cec5SDimitry Andric TemplateDeductionInfo &Info) { 5031*0b57cec5SDimitry Andric // C++ [temp.class.order]p1: 5032*0b57cec5SDimitry Andric // For two class template partial specializations, the first is at least as 5033*0b57cec5SDimitry Andric // specialized as the second if, given the following rewrite to two 5034*0b57cec5SDimitry Andric // function templates, the first function template is at least as 5035*0b57cec5SDimitry Andric // specialized as the second according to the ordering rules for function 5036*0b57cec5SDimitry Andric // templates (14.6.6.2): 5037*0b57cec5SDimitry Andric // - the first function template has the same template parameters as the 5038*0b57cec5SDimitry Andric // first partial specialization and has a single function parameter 5039*0b57cec5SDimitry Andric // whose type is a class template specialization with the template 5040*0b57cec5SDimitry Andric // arguments of the first partial specialization, and 5041*0b57cec5SDimitry Andric // - the second function template has the same template parameters as the 5042*0b57cec5SDimitry Andric // second partial specialization and has a single function parameter 5043*0b57cec5SDimitry Andric // whose type is a class template specialization with the template 5044*0b57cec5SDimitry Andric // arguments of the second partial specialization. 5045*0b57cec5SDimitry Andric // 5046*0b57cec5SDimitry Andric // Rather than synthesize function templates, we merely perform the 5047*0b57cec5SDimitry Andric // equivalent partial ordering by performing deduction directly on 5048*0b57cec5SDimitry Andric // the template arguments of the class template partial 5049*0b57cec5SDimitry Andric // specializations. This computation is slightly simpler than the 5050*0b57cec5SDimitry Andric // general problem of function template partial ordering, because 5051*0b57cec5SDimitry Andric // class template partial specializations are more constrained. We 5052*0b57cec5SDimitry Andric // know that every template parameter is deducible from the class 5053*0b57cec5SDimitry Andric // template partial specialization's template arguments, for 5054*0b57cec5SDimitry Andric // example. 5055*0b57cec5SDimitry Andric SmallVector<DeducedTemplateArgument, 4> Deduced; 5056*0b57cec5SDimitry Andric 5057*0b57cec5SDimitry Andric // Determine whether P1 is at least as specialized as P2. 5058*0b57cec5SDimitry Andric Deduced.resize(P2->getTemplateParameters()->size()); 5059*0b57cec5SDimitry Andric if (DeduceTemplateArgumentsByTypeMatch(S, P2->getTemplateParameters(), 5060*0b57cec5SDimitry Andric T2, T1, Info, Deduced, TDF_None, 5061*0b57cec5SDimitry Andric /*PartialOrdering=*/true)) 5062*0b57cec5SDimitry Andric return false; 5063*0b57cec5SDimitry Andric 5064*0b57cec5SDimitry Andric SmallVector<TemplateArgument, 4> DeducedArgs(Deduced.begin(), 5065*0b57cec5SDimitry Andric Deduced.end()); 5066*0b57cec5SDimitry Andric Sema::InstantiatingTemplate Inst(S, Info.getLocation(), P2, DeducedArgs, 5067*0b57cec5SDimitry Andric Info); 5068*0b57cec5SDimitry Andric auto *TST1 = T1->castAs<TemplateSpecializationType>(); 5069*0b57cec5SDimitry Andric if (FinishTemplateArgumentDeduction( 5070*0b57cec5SDimitry Andric S, P2, /*IsPartialOrdering=*/true, 5071*0b57cec5SDimitry Andric TemplateArgumentList(TemplateArgumentList::OnStack, 5072*0b57cec5SDimitry Andric TST1->template_arguments()), 5073*0b57cec5SDimitry Andric Deduced, Info)) 5074*0b57cec5SDimitry Andric return false; 5075*0b57cec5SDimitry Andric 5076*0b57cec5SDimitry Andric return true; 5077*0b57cec5SDimitry Andric } 5078*0b57cec5SDimitry Andric 5079*0b57cec5SDimitry Andric /// Returns the more specialized class template partial specialization 5080*0b57cec5SDimitry Andric /// according to the rules of partial ordering of class template partial 5081*0b57cec5SDimitry Andric /// specializations (C++ [temp.class.order]). 5082*0b57cec5SDimitry Andric /// 5083*0b57cec5SDimitry Andric /// \param PS1 the first class template partial specialization 5084*0b57cec5SDimitry Andric /// 5085*0b57cec5SDimitry Andric /// \param PS2 the second class template partial specialization 5086*0b57cec5SDimitry Andric /// 5087*0b57cec5SDimitry Andric /// \returns the more specialized class template partial specialization. If 5088*0b57cec5SDimitry Andric /// neither partial specialization is more specialized, returns NULL. 5089*0b57cec5SDimitry Andric ClassTemplatePartialSpecializationDecl * 5090*0b57cec5SDimitry Andric Sema::getMoreSpecializedPartialSpecialization( 5091*0b57cec5SDimitry Andric ClassTemplatePartialSpecializationDecl *PS1, 5092*0b57cec5SDimitry Andric ClassTemplatePartialSpecializationDecl *PS2, 5093*0b57cec5SDimitry Andric SourceLocation Loc) { 5094*0b57cec5SDimitry Andric QualType PT1 = PS1->getInjectedSpecializationType(); 5095*0b57cec5SDimitry Andric QualType PT2 = PS2->getInjectedSpecializationType(); 5096*0b57cec5SDimitry Andric 5097*0b57cec5SDimitry Andric TemplateDeductionInfo Info(Loc); 5098*0b57cec5SDimitry Andric bool Better1 = isAtLeastAsSpecializedAs(*this, PT1, PT2, PS2, Info); 5099*0b57cec5SDimitry Andric bool Better2 = isAtLeastAsSpecializedAs(*this, PT2, PT1, PS1, Info); 5100*0b57cec5SDimitry Andric 5101*0b57cec5SDimitry Andric if (Better1 == Better2) 5102*0b57cec5SDimitry Andric return nullptr; 5103*0b57cec5SDimitry Andric 5104*0b57cec5SDimitry Andric return Better1 ? PS1 : PS2; 5105*0b57cec5SDimitry Andric } 5106*0b57cec5SDimitry Andric 5107*0b57cec5SDimitry Andric bool Sema::isMoreSpecializedThanPrimary( 5108*0b57cec5SDimitry Andric ClassTemplatePartialSpecializationDecl *Spec, TemplateDeductionInfo &Info) { 5109*0b57cec5SDimitry Andric ClassTemplateDecl *Primary = Spec->getSpecializedTemplate(); 5110*0b57cec5SDimitry Andric QualType PrimaryT = Primary->getInjectedClassNameSpecialization(); 5111*0b57cec5SDimitry Andric QualType PartialT = Spec->getInjectedSpecializationType(); 5112*0b57cec5SDimitry Andric if (!isAtLeastAsSpecializedAs(*this, PartialT, PrimaryT, Primary, Info)) 5113*0b57cec5SDimitry Andric return false; 5114*0b57cec5SDimitry Andric if (isAtLeastAsSpecializedAs(*this, PrimaryT, PartialT, Spec, Info)) { 5115*0b57cec5SDimitry Andric Info.clearSFINAEDiagnostic(); 5116*0b57cec5SDimitry Andric return false; 5117*0b57cec5SDimitry Andric } 5118*0b57cec5SDimitry Andric return true; 5119*0b57cec5SDimitry Andric } 5120*0b57cec5SDimitry Andric 5121*0b57cec5SDimitry Andric VarTemplatePartialSpecializationDecl * 5122*0b57cec5SDimitry Andric Sema::getMoreSpecializedPartialSpecialization( 5123*0b57cec5SDimitry Andric VarTemplatePartialSpecializationDecl *PS1, 5124*0b57cec5SDimitry Andric VarTemplatePartialSpecializationDecl *PS2, SourceLocation Loc) { 5125*0b57cec5SDimitry Andric // Pretend the variable template specializations are class template 5126*0b57cec5SDimitry Andric // specializations and form a fake injected class name type for comparison. 5127*0b57cec5SDimitry Andric assert(PS1->getSpecializedTemplate() == PS2->getSpecializedTemplate() && 5128*0b57cec5SDimitry Andric "the partial specializations being compared should specialize" 5129*0b57cec5SDimitry Andric " the same template."); 5130*0b57cec5SDimitry Andric TemplateName Name(PS1->getSpecializedTemplate()); 5131*0b57cec5SDimitry Andric TemplateName CanonTemplate = Context.getCanonicalTemplateName(Name); 5132*0b57cec5SDimitry Andric QualType PT1 = Context.getTemplateSpecializationType( 5133*0b57cec5SDimitry Andric CanonTemplate, PS1->getTemplateArgs().asArray()); 5134*0b57cec5SDimitry Andric QualType PT2 = Context.getTemplateSpecializationType( 5135*0b57cec5SDimitry Andric CanonTemplate, PS2->getTemplateArgs().asArray()); 5136*0b57cec5SDimitry Andric 5137*0b57cec5SDimitry Andric TemplateDeductionInfo Info(Loc); 5138*0b57cec5SDimitry Andric bool Better1 = isAtLeastAsSpecializedAs(*this, PT1, PT2, PS2, Info); 5139*0b57cec5SDimitry Andric bool Better2 = isAtLeastAsSpecializedAs(*this, PT2, PT1, PS1, Info); 5140*0b57cec5SDimitry Andric 5141*0b57cec5SDimitry Andric if (Better1 == Better2) 5142*0b57cec5SDimitry Andric return nullptr; 5143*0b57cec5SDimitry Andric 5144*0b57cec5SDimitry Andric return Better1 ? PS1 : PS2; 5145*0b57cec5SDimitry Andric } 5146*0b57cec5SDimitry Andric 5147*0b57cec5SDimitry Andric bool Sema::isMoreSpecializedThanPrimary( 5148*0b57cec5SDimitry Andric VarTemplatePartialSpecializationDecl *Spec, TemplateDeductionInfo &Info) { 5149*0b57cec5SDimitry Andric TemplateDecl *Primary = Spec->getSpecializedTemplate(); 5150*0b57cec5SDimitry Andric // FIXME: Cache the injected template arguments rather than recomputing 5151*0b57cec5SDimitry Andric // them for each partial specialization. 5152*0b57cec5SDimitry Andric SmallVector<TemplateArgument, 8> PrimaryArgs; 5153*0b57cec5SDimitry Andric Context.getInjectedTemplateArgs(Primary->getTemplateParameters(), 5154*0b57cec5SDimitry Andric PrimaryArgs); 5155*0b57cec5SDimitry Andric 5156*0b57cec5SDimitry Andric TemplateName CanonTemplate = 5157*0b57cec5SDimitry Andric Context.getCanonicalTemplateName(TemplateName(Primary)); 5158*0b57cec5SDimitry Andric QualType PrimaryT = Context.getTemplateSpecializationType( 5159*0b57cec5SDimitry Andric CanonTemplate, PrimaryArgs); 5160*0b57cec5SDimitry Andric QualType PartialT = Context.getTemplateSpecializationType( 5161*0b57cec5SDimitry Andric CanonTemplate, Spec->getTemplateArgs().asArray()); 5162*0b57cec5SDimitry Andric if (!isAtLeastAsSpecializedAs(*this, PartialT, PrimaryT, Primary, Info)) 5163*0b57cec5SDimitry Andric return false; 5164*0b57cec5SDimitry Andric if (isAtLeastAsSpecializedAs(*this, PrimaryT, PartialT, Spec, Info)) { 5165*0b57cec5SDimitry Andric Info.clearSFINAEDiagnostic(); 5166*0b57cec5SDimitry Andric return false; 5167*0b57cec5SDimitry Andric } 5168*0b57cec5SDimitry Andric return true; 5169*0b57cec5SDimitry Andric } 5170*0b57cec5SDimitry Andric 5171*0b57cec5SDimitry Andric bool Sema::isTemplateTemplateParameterAtLeastAsSpecializedAs( 5172*0b57cec5SDimitry Andric TemplateParameterList *P, TemplateDecl *AArg, SourceLocation Loc) { 5173*0b57cec5SDimitry Andric // C++1z [temp.arg.template]p4: (DR 150) 5174*0b57cec5SDimitry Andric // A template template-parameter P is at least as specialized as a 5175*0b57cec5SDimitry Andric // template template-argument A if, given the following rewrite to two 5176*0b57cec5SDimitry Andric // function templates... 5177*0b57cec5SDimitry Andric 5178*0b57cec5SDimitry Andric // Rather than synthesize function templates, we merely perform the 5179*0b57cec5SDimitry Andric // equivalent partial ordering by performing deduction directly on 5180*0b57cec5SDimitry Andric // the template parameter lists of the template template parameters. 5181*0b57cec5SDimitry Andric // 5182*0b57cec5SDimitry Andric // Given an invented class template X with the template parameter list of 5183*0b57cec5SDimitry Andric // A (including default arguments): 5184*0b57cec5SDimitry Andric TemplateName X = Context.getCanonicalTemplateName(TemplateName(AArg)); 5185*0b57cec5SDimitry Andric TemplateParameterList *A = AArg->getTemplateParameters(); 5186*0b57cec5SDimitry Andric 5187*0b57cec5SDimitry Andric // - Each function template has a single function parameter whose type is 5188*0b57cec5SDimitry Andric // a specialization of X with template arguments corresponding to the 5189*0b57cec5SDimitry Andric // template parameters from the respective function template 5190*0b57cec5SDimitry Andric SmallVector<TemplateArgument, 8> AArgs; 5191*0b57cec5SDimitry Andric Context.getInjectedTemplateArgs(A, AArgs); 5192*0b57cec5SDimitry Andric 5193*0b57cec5SDimitry Andric // Check P's arguments against A's parameter list. This will fill in default 5194*0b57cec5SDimitry Andric // template arguments as needed. AArgs are already correct by construction. 5195*0b57cec5SDimitry Andric // We can't just use CheckTemplateIdType because that will expand alias 5196*0b57cec5SDimitry Andric // templates. 5197*0b57cec5SDimitry Andric SmallVector<TemplateArgument, 4> PArgs; 5198*0b57cec5SDimitry Andric { 5199*0b57cec5SDimitry Andric SFINAETrap Trap(*this); 5200*0b57cec5SDimitry Andric 5201*0b57cec5SDimitry Andric Context.getInjectedTemplateArgs(P, PArgs); 5202*0b57cec5SDimitry Andric TemplateArgumentListInfo PArgList(P->getLAngleLoc(), P->getRAngleLoc()); 5203*0b57cec5SDimitry Andric for (unsigned I = 0, N = P->size(); I != N; ++I) { 5204*0b57cec5SDimitry Andric // Unwrap packs that getInjectedTemplateArgs wrapped around pack 5205*0b57cec5SDimitry Andric // expansions, to form an "as written" argument list. 5206*0b57cec5SDimitry Andric TemplateArgument Arg = PArgs[I]; 5207*0b57cec5SDimitry Andric if (Arg.getKind() == TemplateArgument::Pack) { 5208*0b57cec5SDimitry Andric assert(Arg.pack_size() == 1 && Arg.pack_begin()->isPackExpansion()); 5209*0b57cec5SDimitry Andric Arg = *Arg.pack_begin(); 5210*0b57cec5SDimitry Andric } 5211*0b57cec5SDimitry Andric PArgList.addArgument(getTrivialTemplateArgumentLoc( 5212*0b57cec5SDimitry Andric Arg, QualType(), P->getParam(I)->getLocation())); 5213*0b57cec5SDimitry Andric } 5214*0b57cec5SDimitry Andric PArgs.clear(); 5215*0b57cec5SDimitry Andric 5216*0b57cec5SDimitry Andric // C++1z [temp.arg.template]p3: 5217*0b57cec5SDimitry Andric // If the rewrite produces an invalid type, then P is not at least as 5218*0b57cec5SDimitry Andric // specialized as A. 5219*0b57cec5SDimitry Andric if (CheckTemplateArgumentList(AArg, Loc, PArgList, false, PArgs) || 5220*0b57cec5SDimitry Andric Trap.hasErrorOccurred()) 5221*0b57cec5SDimitry Andric return false; 5222*0b57cec5SDimitry Andric } 5223*0b57cec5SDimitry Andric 5224*0b57cec5SDimitry Andric QualType AType = Context.getTemplateSpecializationType(X, AArgs); 5225*0b57cec5SDimitry Andric QualType PType = Context.getTemplateSpecializationType(X, PArgs); 5226*0b57cec5SDimitry Andric 5227*0b57cec5SDimitry Andric // ... the function template corresponding to P is at least as specialized 5228*0b57cec5SDimitry Andric // as the function template corresponding to A according to the partial 5229*0b57cec5SDimitry Andric // ordering rules for function templates. 5230*0b57cec5SDimitry Andric TemplateDeductionInfo Info(Loc, A->getDepth()); 5231*0b57cec5SDimitry Andric return isAtLeastAsSpecializedAs(*this, PType, AType, AArg, Info); 5232*0b57cec5SDimitry Andric } 5233*0b57cec5SDimitry Andric 5234*0b57cec5SDimitry Andric /// Mark the template parameters that are used by the given 5235*0b57cec5SDimitry Andric /// expression. 5236*0b57cec5SDimitry Andric static void 5237*0b57cec5SDimitry Andric MarkUsedTemplateParameters(ASTContext &Ctx, 5238*0b57cec5SDimitry Andric const Expr *E, 5239*0b57cec5SDimitry Andric bool OnlyDeduced, 5240*0b57cec5SDimitry Andric unsigned Depth, 5241*0b57cec5SDimitry Andric llvm::SmallBitVector &Used) { 5242*0b57cec5SDimitry Andric // We can deduce from a pack expansion. 5243*0b57cec5SDimitry Andric if (const PackExpansionExpr *Expansion = dyn_cast<PackExpansionExpr>(E)) 5244*0b57cec5SDimitry Andric E = Expansion->getPattern(); 5245*0b57cec5SDimitry Andric 5246*0b57cec5SDimitry Andric // Skip through any implicit casts we added while type-checking, and any 5247*0b57cec5SDimitry Andric // substitutions performed by template alias expansion. 5248*0b57cec5SDimitry Andric while (true) { 5249*0b57cec5SDimitry Andric if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) 5250*0b57cec5SDimitry Andric E = ICE->getSubExpr(); 5251*0b57cec5SDimitry Andric else if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(E)) 5252*0b57cec5SDimitry Andric E = CE->getSubExpr(); 5253*0b57cec5SDimitry Andric else if (const SubstNonTypeTemplateParmExpr *Subst = 5254*0b57cec5SDimitry Andric dyn_cast<SubstNonTypeTemplateParmExpr>(E)) 5255*0b57cec5SDimitry Andric E = Subst->getReplacement(); 5256*0b57cec5SDimitry Andric else 5257*0b57cec5SDimitry Andric break; 5258*0b57cec5SDimitry Andric } 5259*0b57cec5SDimitry Andric 5260*0b57cec5SDimitry Andric // FIXME: if !OnlyDeduced, we have to walk the whole subexpression to 5261*0b57cec5SDimitry Andric // find other occurrences of template parameters. 5262*0b57cec5SDimitry Andric const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); 5263*0b57cec5SDimitry Andric if (!DRE) 5264*0b57cec5SDimitry Andric return; 5265*0b57cec5SDimitry Andric 5266*0b57cec5SDimitry Andric const NonTypeTemplateParmDecl *NTTP 5267*0b57cec5SDimitry Andric = dyn_cast<NonTypeTemplateParmDecl>(DRE->getDecl()); 5268*0b57cec5SDimitry Andric if (!NTTP) 5269*0b57cec5SDimitry Andric return; 5270*0b57cec5SDimitry Andric 5271*0b57cec5SDimitry Andric if (NTTP->getDepth() == Depth) 5272*0b57cec5SDimitry Andric Used[NTTP->getIndex()] = true; 5273*0b57cec5SDimitry Andric 5274*0b57cec5SDimitry Andric // In C++17 mode, additional arguments may be deduced from the type of a 5275*0b57cec5SDimitry Andric // non-type argument. 5276*0b57cec5SDimitry Andric if (Ctx.getLangOpts().CPlusPlus17) 5277*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, NTTP->getType(), OnlyDeduced, Depth, Used); 5278*0b57cec5SDimitry Andric } 5279*0b57cec5SDimitry Andric 5280*0b57cec5SDimitry Andric /// Mark the template parameters that are used by the given 5281*0b57cec5SDimitry Andric /// nested name specifier. 5282*0b57cec5SDimitry Andric static void 5283*0b57cec5SDimitry Andric MarkUsedTemplateParameters(ASTContext &Ctx, 5284*0b57cec5SDimitry Andric NestedNameSpecifier *NNS, 5285*0b57cec5SDimitry Andric bool OnlyDeduced, 5286*0b57cec5SDimitry Andric unsigned Depth, 5287*0b57cec5SDimitry Andric llvm::SmallBitVector &Used) { 5288*0b57cec5SDimitry Andric if (!NNS) 5289*0b57cec5SDimitry Andric return; 5290*0b57cec5SDimitry Andric 5291*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, NNS->getPrefix(), OnlyDeduced, Depth, 5292*0b57cec5SDimitry Andric Used); 5293*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, QualType(NNS->getAsType(), 0), 5294*0b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 5295*0b57cec5SDimitry Andric } 5296*0b57cec5SDimitry Andric 5297*0b57cec5SDimitry Andric /// Mark the template parameters that are used by the given 5298*0b57cec5SDimitry Andric /// template name. 5299*0b57cec5SDimitry Andric static void 5300*0b57cec5SDimitry Andric MarkUsedTemplateParameters(ASTContext &Ctx, 5301*0b57cec5SDimitry Andric TemplateName Name, 5302*0b57cec5SDimitry Andric bool OnlyDeduced, 5303*0b57cec5SDimitry Andric unsigned Depth, 5304*0b57cec5SDimitry Andric llvm::SmallBitVector &Used) { 5305*0b57cec5SDimitry Andric if (TemplateDecl *Template = Name.getAsTemplateDecl()) { 5306*0b57cec5SDimitry Andric if (TemplateTemplateParmDecl *TTP 5307*0b57cec5SDimitry Andric = dyn_cast<TemplateTemplateParmDecl>(Template)) { 5308*0b57cec5SDimitry Andric if (TTP->getDepth() == Depth) 5309*0b57cec5SDimitry Andric Used[TTP->getIndex()] = true; 5310*0b57cec5SDimitry Andric } 5311*0b57cec5SDimitry Andric return; 5312*0b57cec5SDimitry Andric } 5313*0b57cec5SDimitry Andric 5314*0b57cec5SDimitry Andric if (QualifiedTemplateName *QTN = Name.getAsQualifiedTemplateName()) 5315*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, QTN->getQualifier(), OnlyDeduced, 5316*0b57cec5SDimitry Andric Depth, Used); 5317*0b57cec5SDimitry Andric if (DependentTemplateName *DTN = Name.getAsDependentTemplateName()) 5318*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, DTN->getQualifier(), OnlyDeduced, 5319*0b57cec5SDimitry Andric Depth, Used); 5320*0b57cec5SDimitry Andric } 5321*0b57cec5SDimitry Andric 5322*0b57cec5SDimitry Andric /// Mark the template parameters that are used by the given 5323*0b57cec5SDimitry Andric /// type. 5324*0b57cec5SDimitry Andric static void 5325*0b57cec5SDimitry Andric MarkUsedTemplateParameters(ASTContext &Ctx, QualType T, 5326*0b57cec5SDimitry Andric bool OnlyDeduced, 5327*0b57cec5SDimitry Andric unsigned Depth, 5328*0b57cec5SDimitry Andric llvm::SmallBitVector &Used) { 5329*0b57cec5SDimitry Andric if (T.isNull()) 5330*0b57cec5SDimitry Andric return; 5331*0b57cec5SDimitry Andric 5332*0b57cec5SDimitry Andric // Non-dependent types have nothing deducible 5333*0b57cec5SDimitry Andric if (!T->isDependentType()) 5334*0b57cec5SDimitry Andric return; 5335*0b57cec5SDimitry Andric 5336*0b57cec5SDimitry Andric T = Ctx.getCanonicalType(T); 5337*0b57cec5SDimitry Andric switch (T->getTypeClass()) { 5338*0b57cec5SDimitry Andric case Type::Pointer: 5339*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 5340*0b57cec5SDimitry Andric cast<PointerType>(T)->getPointeeType(), 5341*0b57cec5SDimitry Andric OnlyDeduced, 5342*0b57cec5SDimitry Andric Depth, 5343*0b57cec5SDimitry Andric Used); 5344*0b57cec5SDimitry Andric break; 5345*0b57cec5SDimitry Andric 5346*0b57cec5SDimitry Andric case Type::BlockPointer: 5347*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 5348*0b57cec5SDimitry Andric cast<BlockPointerType>(T)->getPointeeType(), 5349*0b57cec5SDimitry Andric OnlyDeduced, 5350*0b57cec5SDimitry Andric Depth, 5351*0b57cec5SDimitry Andric Used); 5352*0b57cec5SDimitry Andric break; 5353*0b57cec5SDimitry Andric 5354*0b57cec5SDimitry Andric case Type::LValueReference: 5355*0b57cec5SDimitry Andric case Type::RValueReference: 5356*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 5357*0b57cec5SDimitry Andric cast<ReferenceType>(T)->getPointeeType(), 5358*0b57cec5SDimitry Andric OnlyDeduced, 5359*0b57cec5SDimitry Andric Depth, 5360*0b57cec5SDimitry Andric Used); 5361*0b57cec5SDimitry Andric break; 5362*0b57cec5SDimitry Andric 5363*0b57cec5SDimitry Andric case Type::MemberPointer: { 5364*0b57cec5SDimitry Andric const MemberPointerType *MemPtr = cast<MemberPointerType>(T.getTypePtr()); 5365*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, MemPtr->getPointeeType(), OnlyDeduced, 5366*0b57cec5SDimitry Andric Depth, Used); 5367*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, QualType(MemPtr->getClass(), 0), 5368*0b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 5369*0b57cec5SDimitry Andric break; 5370*0b57cec5SDimitry Andric } 5371*0b57cec5SDimitry Andric 5372*0b57cec5SDimitry Andric case Type::DependentSizedArray: 5373*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 5374*0b57cec5SDimitry Andric cast<DependentSizedArrayType>(T)->getSizeExpr(), 5375*0b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 5376*0b57cec5SDimitry Andric // Fall through to check the element type 5377*0b57cec5SDimitry Andric LLVM_FALLTHROUGH; 5378*0b57cec5SDimitry Andric 5379*0b57cec5SDimitry Andric case Type::ConstantArray: 5380*0b57cec5SDimitry Andric case Type::IncompleteArray: 5381*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 5382*0b57cec5SDimitry Andric cast<ArrayType>(T)->getElementType(), 5383*0b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 5384*0b57cec5SDimitry Andric break; 5385*0b57cec5SDimitry Andric 5386*0b57cec5SDimitry Andric case Type::Vector: 5387*0b57cec5SDimitry Andric case Type::ExtVector: 5388*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 5389*0b57cec5SDimitry Andric cast<VectorType>(T)->getElementType(), 5390*0b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 5391*0b57cec5SDimitry Andric break; 5392*0b57cec5SDimitry Andric 5393*0b57cec5SDimitry Andric case Type::DependentVector: { 5394*0b57cec5SDimitry Andric const auto *VecType = cast<DependentVectorType>(T); 5395*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced, 5396*0b57cec5SDimitry Andric Depth, Used); 5397*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced, Depth, 5398*0b57cec5SDimitry Andric Used); 5399*0b57cec5SDimitry Andric break; 5400*0b57cec5SDimitry Andric } 5401*0b57cec5SDimitry Andric case Type::DependentSizedExtVector: { 5402*0b57cec5SDimitry Andric const DependentSizedExtVectorType *VecType 5403*0b57cec5SDimitry Andric = cast<DependentSizedExtVectorType>(T); 5404*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, VecType->getElementType(), OnlyDeduced, 5405*0b57cec5SDimitry Andric Depth, Used); 5406*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, VecType->getSizeExpr(), OnlyDeduced, 5407*0b57cec5SDimitry Andric Depth, Used); 5408*0b57cec5SDimitry Andric break; 5409*0b57cec5SDimitry Andric } 5410*0b57cec5SDimitry Andric 5411*0b57cec5SDimitry Andric case Type::DependentAddressSpace: { 5412*0b57cec5SDimitry Andric const DependentAddressSpaceType *DependentASType = 5413*0b57cec5SDimitry Andric cast<DependentAddressSpaceType>(T); 5414*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, DependentASType->getPointeeType(), 5415*0b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 5416*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 5417*0b57cec5SDimitry Andric DependentASType->getAddrSpaceExpr(), 5418*0b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 5419*0b57cec5SDimitry Andric break; 5420*0b57cec5SDimitry Andric } 5421*0b57cec5SDimitry Andric 5422*0b57cec5SDimitry Andric case Type::FunctionProto: { 5423*0b57cec5SDimitry Andric const FunctionProtoType *Proto = cast<FunctionProtoType>(T); 5424*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, Proto->getReturnType(), OnlyDeduced, Depth, 5425*0b57cec5SDimitry Andric Used); 5426*0b57cec5SDimitry Andric for (unsigned I = 0, N = Proto->getNumParams(); I != N; ++I) { 5427*0b57cec5SDimitry Andric // C++17 [temp.deduct.type]p5: 5428*0b57cec5SDimitry Andric // The non-deduced contexts are: [...] 5429*0b57cec5SDimitry Andric // -- A function parameter pack that does not occur at the end of the 5430*0b57cec5SDimitry Andric // parameter-declaration-list. 5431*0b57cec5SDimitry Andric if (!OnlyDeduced || I + 1 == N || 5432*0b57cec5SDimitry Andric !Proto->getParamType(I)->getAs<PackExpansionType>()) { 5433*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, Proto->getParamType(I), OnlyDeduced, 5434*0b57cec5SDimitry Andric Depth, Used); 5435*0b57cec5SDimitry Andric } else { 5436*0b57cec5SDimitry Andric // FIXME: C++17 [temp.deduct.call]p1: 5437*0b57cec5SDimitry Andric // When a function parameter pack appears in a non-deduced context, 5438*0b57cec5SDimitry Andric // the type of that pack is never deduced. 5439*0b57cec5SDimitry Andric // 5440*0b57cec5SDimitry Andric // We should also track a set of "never deduced" parameters, and 5441*0b57cec5SDimitry Andric // subtract that from the list of deduced parameters after marking. 5442*0b57cec5SDimitry Andric } 5443*0b57cec5SDimitry Andric } 5444*0b57cec5SDimitry Andric if (auto *E = Proto->getNoexceptExpr()) 5445*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, E, OnlyDeduced, Depth, Used); 5446*0b57cec5SDimitry Andric break; 5447*0b57cec5SDimitry Andric } 5448*0b57cec5SDimitry Andric 5449*0b57cec5SDimitry Andric case Type::TemplateTypeParm: { 5450*0b57cec5SDimitry Andric const TemplateTypeParmType *TTP = cast<TemplateTypeParmType>(T); 5451*0b57cec5SDimitry Andric if (TTP->getDepth() == Depth) 5452*0b57cec5SDimitry Andric Used[TTP->getIndex()] = true; 5453*0b57cec5SDimitry Andric break; 5454*0b57cec5SDimitry Andric } 5455*0b57cec5SDimitry Andric 5456*0b57cec5SDimitry Andric case Type::SubstTemplateTypeParmPack: { 5457*0b57cec5SDimitry Andric const SubstTemplateTypeParmPackType *Subst 5458*0b57cec5SDimitry Andric = cast<SubstTemplateTypeParmPackType>(T); 5459*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 5460*0b57cec5SDimitry Andric QualType(Subst->getReplacedParameter(), 0), 5461*0b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 5462*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, Subst->getArgumentPack(), 5463*0b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 5464*0b57cec5SDimitry Andric break; 5465*0b57cec5SDimitry Andric } 5466*0b57cec5SDimitry Andric 5467*0b57cec5SDimitry Andric case Type::InjectedClassName: 5468*0b57cec5SDimitry Andric T = cast<InjectedClassNameType>(T)->getInjectedSpecializationType(); 5469*0b57cec5SDimitry Andric LLVM_FALLTHROUGH; 5470*0b57cec5SDimitry Andric 5471*0b57cec5SDimitry Andric case Type::TemplateSpecialization: { 5472*0b57cec5SDimitry Andric const TemplateSpecializationType *Spec 5473*0b57cec5SDimitry Andric = cast<TemplateSpecializationType>(T); 5474*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, Spec->getTemplateName(), OnlyDeduced, 5475*0b57cec5SDimitry Andric Depth, Used); 5476*0b57cec5SDimitry Andric 5477*0b57cec5SDimitry Andric // C++0x [temp.deduct.type]p9: 5478*0b57cec5SDimitry Andric // If the template argument list of P contains a pack expansion that is 5479*0b57cec5SDimitry Andric // not the last template argument, the entire template argument list is a 5480*0b57cec5SDimitry Andric // non-deduced context. 5481*0b57cec5SDimitry Andric if (OnlyDeduced && 5482*0b57cec5SDimitry Andric hasPackExpansionBeforeEnd(Spec->template_arguments())) 5483*0b57cec5SDimitry Andric break; 5484*0b57cec5SDimitry Andric 5485*0b57cec5SDimitry Andric for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) 5486*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, 5487*0b57cec5SDimitry Andric Used); 5488*0b57cec5SDimitry Andric break; 5489*0b57cec5SDimitry Andric } 5490*0b57cec5SDimitry Andric 5491*0b57cec5SDimitry Andric case Type::Complex: 5492*0b57cec5SDimitry Andric if (!OnlyDeduced) 5493*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 5494*0b57cec5SDimitry Andric cast<ComplexType>(T)->getElementType(), 5495*0b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 5496*0b57cec5SDimitry Andric break; 5497*0b57cec5SDimitry Andric 5498*0b57cec5SDimitry Andric case Type::Atomic: 5499*0b57cec5SDimitry Andric if (!OnlyDeduced) 5500*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 5501*0b57cec5SDimitry Andric cast<AtomicType>(T)->getValueType(), 5502*0b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 5503*0b57cec5SDimitry Andric break; 5504*0b57cec5SDimitry Andric 5505*0b57cec5SDimitry Andric case Type::DependentName: 5506*0b57cec5SDimitry Andric if (!OnlyDeduced) 5507*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 5508*0b57cec5SDimitry Andric cast<DependentNameType>(T)->getQualifier(), 5509*0b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 5510*0b57cec5SDimitry Andric break; 5511*0b57cec5SDimitry Andric 5512*0b57cec5SDimitry Andric case Type::DependentTemplateSpecialization: { 5513*0b57cec5SDimitry Andric // C++14 [temp.deduct.type]p5: 5514*0b57cec5SDimitry Andric // The non-deduced contexts are: 5515*0b57cec5SDimitry Andric // -- The nested-name-specifier of a type that was specified using a 5516*0b57cec5SDimitry Andric // qualified-id 5517*0b57cec5SDimitry Andric // 5518*0b57cec5SDimitry Andric // C++14 [temp.deduct.type]p6: 5519*0b57cec5SDimitry Andric // When a type name is specified in a way that includes a non-deduced 5520*0b57cec5SDimitry Andric // context, all of the types that comprise that type name are also 5521*0b57cec5SDimitry Andric // non-deduced. 5522*0b57cec5SDimitry Andric if (OnlyDeduced) 5523*0b57cec5SDimitry Andric break; 5524*0b57cec5SDimitry Andric 5525*0b57cec5SDimitry Andric const DependentTemplateSpecializationType *Spec 5526*0b57cec5SDimitry Andric = cast<DependentTemplateSpecializationType>(T); 5527*0b57cec5SDimitry Andric 5528*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, Spec->getQualifier(), 5529*0b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 5530*0b57cec5SDimitry Andric 5531*0b57cec5SDimitry Andric for (unsigned I = 0, N = Spec->getNumArgs(); I != N; ++I) 5532*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, Spec->getArg(I), OnlyDeduced, Depth, 5533*0b57cec5SDimitry Andric Used); 5534*0b57cec5SDimitry Andric break; 5535*0b57cec5SDimitry Andric } 5536*0b57cec5SDimitry Andric 5537*0b57cec5SDimitry Andric case Type::TypeOf: 5538*0b57cec5SDimitry Andric if (!OnlyDeduced) 5539*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 5540*0b57cec5SDimitry Andric cast<TypeOfType>(T)->getUnderlyingType(), 5541*0b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 5542*0b57cec5SDimitry Andric break; 5543*0b57cec5SDimitry Andric 5544*0b57cec5SDimitry Andric case Type::TypeOfExpr: 5545*0b57cec5SDimitry Andric if (!OnlyDeduced) 5546*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 5547*0b57cec5SDimitry Andric cast<TypeOfExprType>(T)->getUnderlyingExpr(), 5548*0b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 5549*0b57cec5SDimitry Andric break; 5550*0b57cec5SDimitry Andric 5551*0b57cec5SDimitry Andric case Type::Decltype: 5552*0b57cec5SDimitry Andric if (!OnlyDeduced) 5553*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 5554*0b57cec5SDimitry Andric cast<DecltypeType>(T)->getUnderlyingExpr(), 5555*0b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 5556*0b57cec5SDimitry Andric break; 5557*0b57cec5SDimitry Andric 5558*0b57cec5SDimitry Andric case Type::UnaryTransform: 5559*0b57cec5SDimitry Andric if (!OnlyDeduced) 5560*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 5561*0b57cec5SDimitry Andric cast<UnaryTransformType>(T)->getUnderlyingType(), 5562*0b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 5563*0b57cec5SDimitry Andric break; 5564*0b57cec5SDimitry Andric 5565*0b57cec5SDimitry Andric case Type::PackExpansion: 5566*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 5567*0b57cec5SDimitry Andric cast<PackExpansionType>(T)->getPattern(), 5568*0b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 5569*0b57cec5SDimitry Andric break; 5570*0b57cec5SDimitry Andric 5571*0b57cec5SDimitry Andric case Type::Auto: 5572*0b57cec5SDimitry Andric case Type::DeducedTemplateSpecialization: 5573*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 5574*0b57cec5SDimitry Andric cast<DeducedType>(T)->getDeducedType(), 5575*0b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 5576*0b57cec5SDimitry Andric break; 5577*0b57cec5SDimitry Andric 5578*0b57cec5SDimitry Andric // None of these types have any template parameters in them. 5579*0b57cec5SDimitry Andric case Type::Builtin: 5580*0b57cec5SDimitry Andric case Type::VariableArray: 5581*0b57cec5SDimitry Andric case Type::FunctionNoProto: 5582*0b57cec5SDimitry Andric case Type::Record: 5583*0b57cec5SDimitry Andric case Type::Enum: 5584*0b57cec5SDimitry Andric case Type::ObjCInterface: 5585*0b57cec5SDimitry Andric case Type::ObjCObject: 5586*0b57cec5SDimitry Andric case Type::ObjCObjectPointer: 5587*0b57cec5SDimitry Andric case Type::UnresolvedUsing: 5588*0b57cec5SDimitry Andric case Type::Pipe: 5589*0b57cec5SDimitry Andric #define TYPE(Class, Base) 5590*0b57cec5SDimitry Andric #define ABSTRACT_TYPE(Class, Base) 5591*0b57cec5SDimitry Andric #define DEPENDENT_TYPE(Class, Base) 5592*0b57cec5SDimitry Andric #define NON_CANONICAL_TYPE(Class, Base) case Type::Class: 5593*0b57cec5SDimitry Andric #include "clang/AST/TypeNodes.def" 5594*0b57cec5SDimitry Andric break; 5595*0b57cec5SDimitry Andric } 5596*0b57cec5SDimitry Andric } 5597*0b57cec5SDimitry Andric 5598*0b57cec5SDimitry Andric /// Mark the template parameters that are used by this 5599*0b57cec5SDimitry Andric /// template argument. 5600*0b57cec5SDimitry Andric static void 5601*0b57cec5SDimitry Andric MarkUsedTemplateParameters(ASTContext &Ctx, 5602*0b57cec5SDimitry Andric const TemplateArgument &TemplateArg, 5603*0b57cec5SDimitry Andric bool OnlyDeduced, 5604*0b57cec5SDimitry Andric unsigned Depth, 5605*0b57cec5SDimitry Andric llvm::SmallBitVector &Used) { 5606*0b57cec5SDimitry Andric switch (TemplateArg.getKind()) { 5607*0b57cec5SDimitry Andric case TemplateArgument::Null: 5608*0b57cec5SDimitry Andric case TemplateArgument::Integral: 5609*0b57cec5SDimitry Andric case TemplateArgument::Declaration: 5610*0b57cec5SDimitry Andric break; 5611*0b57cec5SDimitry Andric 5612*0b57cec5SDimitry Andric case TemplateArgument::NullPtr: 5613*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, TemplateArg.getNullPtrType(), OnlyDeduced, 5614*0b57cec5SDimitry Andric Depth, Used); 5615*0b57cec5SDimitry Andric break; 5616*0b57cec5SDimitry Andric 5617*0b57cec5SDimitry Andric case TemplateArgument::Type: 5618*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, TemplateArg.getAsType(), OnlyDeduced, 5619*0b57cec5SDimitry Andric Depth, Used); 5620*0b57cec5SDimitry Andric break; 5621*0b57cec5SDimitry Andric 5622*0b57cec5SDimitry Andric case TemplateArgument::Template: 5623*0b57cec5SDimitry Andric case TemplateArgument::TemplateExpansion: 5624*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, 5625*0b57cec5SDimitry Andric TemplateArg.getAsTemplateOrTemplatePattern(), 5626*0b57cec5SDimitry Andric OnlyDeduced, Depth, Used); 5627*0b57cec5SDimitry Andric break; 5628*0b57cec5SDimitry Andric 5629*0b57cec5SDimitry Andric case TemplateArgument::Expression: 5630*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, TemplateArg.getAsExpr(), OnlyDeduced, 5631*0b57cec5SDimitry Andric Depth, Used); 5632*0b57cec5SDimitry Andric break; 5633*0b57cec5SDimitry Andric 5634*0b57cec5SDimitry Andric case TemplateArgument::Pack: 5635*0b57cec5SDimitry Andric for (const auto &P : TemplateArg.pack_elements()) 5636*0b57cec5SDimitry Andric MarkUsedTemplateParameters(Ctx, P, OnlyDeduced, Depth, Used); 5637*0b57cec5SDimitry Andric break; 5638*0b57cec5SDimitry Andric } 5639*0b57cec5SDimitry Andric } 5640*0b57cec5SDimitry Andric 5641*0b57cec5SDimitry Andric /// Mark which template parameters can be deduced from a given 5642*0b57cec5SDimitry Andric /// template argument list. 5643*0b57cec5SDimitry Andric /// 5644*0b57cec5SDimitry Andric /// \param TemplateArgs the template argument list from which template 5645*0b57cec5SDimitry Andric /// parameters will be deduced. 5646*0b57cec5SDimitry Andric /// 5647*0b57cec5SDimitry Andric /// \param Used a bit vector whose elements will be set to \c true 5648*0b57cec5SDimitry Andric /// to indicate when the corresponding template parameter will be 5649*0b57cec5SDimitry Andric /// deduced. 5650*0b57cec5SDimitry Andric void 5651*0b57cec5SDimitry Andric Sema::MarkUsedTemplateParameters(const TemplateArgumentList &TemplateArgs, 5652*0b57cec5SDimitry Andric bool OnlyDeduced, unsigned Depth, 5653*0b57cec5SDimitry Andric llvm::SmallBitVector &Used) { 5654*0b57cec5SDimitry Andric // C++0x [temp.deduct.type]p9: 5655*0b57cec5SDimitry Andric // If the template argument list of P contains a pack expansion that is not 5656*0b57cec5SDimitry Andric // the last template argument, the entire template argument list is a 5657*0b57cec5SDimitry Andric // non-deduced context. 5658*0b57cec5SDimitry Andric if (OnlyDeduced && 5659*0b57cec5SDimitry Andric hasPackExpansionBeforeEnd(TemplateArgs.asArray())) 5660*0b57cec5SDimitry Andric return; 5661*0b57cec5SDimitry Andric 5662*0b57cec5SDimitry Andric for (unsigned I = 0, N = TemplateArgs.size(); I != N; ++I) 5663*0b57cec5SDimitry Andric ::MarkUsedTemplateParameters(Context, TemplateArgs[I], OnlyDeduced, 5664*0b57cec5SDimitry Andric Depth, Used); 5665*0b57cec5SDimitry Andric } 5666*0b57cec5SDimitry Andric 5667*0b57cec5SDimitry Andric /// Marks all of the template parameters that will be deduced by a 5668*0b57cec5SDimitry Andric /// call to the given function template. 5669*0b57cec5SDimitry Andric void Sema::MarkDeducedTemplateParameters( 5670*0b57cec5SDimitry Andric ASTContext &Ctx, const FunctionTemplateDecl *FunctionTemplate, 5671*0b57cec5SDimitry Andric llvm::SmallBitVector &Deduced) { 5672*0b57cec5SDimitry Andric TemplateParameterList *TemplateParams 5673*0b57cec5SDimitry Andric = FunctionTemplate->getTemplateParameters(); 5674*0b57cec5SDimitry Andric Deduced.clear(); 5675*0b57cec5SDimitry Andric Deduced.resize(TemplateParams->size()); 5676*0b57cec5SDimitry Andric 5677*0b57cec5SDimitry Andric FunctionDecl *Function = FunctionTemplate->getTemplatedDecl(); 5678*0b57cec5SDimitry Andric for (unsigned I = 0, N = Function->getNumParams(); I != N; ++I) 5679*0b57cec5SDimitry Andric ::MarkUsedTemplateParameters(Ctx, Function->getParamDecl(I)->getType(), 5680*0b57cec5SDimitry Andric true, TemplateParams->getDepth(), Deduced); 5681*0b57cec5SDimitry Andric } 5682*0b57cec5SDimitry Andric 5683*0b57cec5SDimitry Andric bool hasDeducibleTemplateParameters(Sema &S, 5684*0b57cec5SDimitry Andric FunctionTemplateDecl *FunctionTemplate, 5685*0b57cec5SDimitry Andric QualType T) { 5686*0b57cec5SDimitry Andric if (!T->isDependentType()) 5687*0b57cec5SDimitry Andric return false; 5688*0b57cec5SDimitry Andric 5689*0b57cec5SDimitry Andric TemplateParameterList *TemplateParams 5690*0b57cec5SDimitry Andric = FunctionTemplate->getTemplateParameters(); 5691*0b57cec5SDimitry Andric llvm::SmallBitVector Deduced(TemplateParams->size()); 5692*0b57cec5SDimitry Andric ::MarkUsedTemplateParameters(S.Context, T, true, TemplateParams->getDepth(), 5693*0b57cec5SDimitry Andric Deduced); 5694*0b57cec5SDimitry Andric 5695*0b57cec5SDimitry Andric return Deduced.any(); 5696*0b57cec5SDimitry Andric } 5697